Skip to content

Commit

Permalink
feat(core): automatic 422 response when using validator (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsafalpiya authored Nov 4, 2024
1 parent b515810 commit 7678e36
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/light-balloons-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@tuyau/core': patch
'@tuyau/utils': patch
---

Automatic 422 response if the request input is validated using `validateUsing`.
2 changes: 1 addition & 1 deletion packages/core/src/codegen/api_types_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ export class ApiTypesGenerator {
const typeName = this.#generateTypeName(route)
typesByPattern[typeName] = {
request: schemaImport ? `MakeTuyauRequest<${schemaImport}>` : 'unknown',
response: `MakeTuyauResponse<import('${relativePath}').default['${routeHandler.method}']>`,
response: `MakeTuyauResponse<import('${relativePath}').default['${routeHandler.method}'], ${!!schemaImport}>`,
}

currentLevel.$url = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { InferInput } from '@vinejs/vine/types'
type UsersGet = {
request: unknown
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index'], false>
}
export interface ApiDefinition {
'users': {
Expand All @@ -25,7 +25,7 @@ import type { InferInput } from '@vinejs/vine/types'
type UsersGet = {
request: MakeTuyauRequest<InferInput<typeof import('../app/validators/get_users_validator.ts')['getUsersValidator']>>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index'], true>
}
export interface ApiDefinition {
'users': {
Expand Down Expand Up @@ -67,7 +67,7 @@ import type { InferInput } from '@vinejs/vine/types'
type UsersGet = {
request: unknown
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index'], false>
}
export interface ApiDefinition {
'users': {
Expand All @@ -89,7 +89,7 @@ import type { InferInput } from '@vinejs/vine/types'
type PostsGet = {
request: unknown
response: MakeTuyauResponse<import('../app/controllers/posts_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/posts_controller.ts').default['index'], false>
}
export interface ApiDefinition {
'posts': {
Expand All @@ -111,7 +111,7 @@ import type { InferInput } from '@vinejs/vine/types'
type UsersGet = {
request: unknown
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index'], false>
}
export interface ApiDefinition {
'users': {
Expand All @@ -133,7 +133,7 @@ import type { InferInput } from '@vinejs/vine/types'
type PostsGet = {
request: unknown
response: MakeTuyauResponse<import('../app/controllers/posts_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/posts_controller.ts').default['index'], false>
}
export interface ApiDefinition {
'posts': {
Expand All @@ -155,7 +155,7 @@ import type { InferInput } from '@vinejs/vine/types'
type UsersGet = {
request: MakeTuyauRequest<InferInput<typeof import('../app/validators/get_users_validator.ts')['default']>>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/users_controller.ts').default['index'], true>
}
export interface ApiDefinition {
'users': {
Expand All @@ -177,7 +177,7 @@ import type { InferInput } from '@vinejs/vine/types'
type GetUsersGet = {
request: MakeTuyauRequest<InferInput<typeof import('../app/controllers/get_users_controller.ts')['getUsersValidator']>>
response: MakeTuyauResponse<import('../app/controllers/get_users_controller.ts').default['index']>
response: MakeTuyauResponse<import('../app/controllers/get_users_controller.ts').default['index'], true>
}
export interface ApiDefinition {
'get_users': {
Expand Down
10 changes: 7 additions & 3 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,13 @@ export type MakeOptional<T extends object> = UndefinedProps<T> & Omit<T, keyof U
/**
* Shortcut for computing the Tuyau response type
*/
export type MakeTuyauResponse<T extends (...args: any) => any> = Simplify<
Serialize<ConvertReturnTypeToRecordStatusResponse<Awaited<ReturnType<T>>>>
>
export type MakeTuyauResponse<
T extends (...args: any) => any,
HasSchema extends boolean = false,
> = Simplify<Serialize<ConvertReturnTypeToRecordStatusResponse<Awaited<ReturnType<T>>>>> &
(HasSchema extends true
? { 422: { errors: { message: string; rule: string; field: string }[] } }
: {})

/**
* Shortcut for computing the Tuyau request type
Expand Down

0 comments on commit 7678e36

Please sign in to comment.