Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compile issue, pass error shape, and unwrap more zod types #2

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/adapters/node-http/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export const createOpenApiNodeHttpHandler = <
const statusCode = meta?.status ?? TRPC_ERROR_CODE_HTTP_STATUS[error.code] ?? 500;
const headers = meta?.headers ?? {};
const body: OpenApiErrorResponse = {
...errorShape, // Pass the error through
message: isInputValidationError
? 'Input validation failed'
: errorShape?.message ?? error.message ?? 'An error occurred',
Expand Down
75 changes: 43 additions & 32 deletions src/generator/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,43 +140,54 @@ export const getRequestBodyObject = (
export const hasInputs = (schema: unknown) =>
instanceofZodType(schema) && !instanceofZodTypeLikeVoid(unwrapZodType(schema, true));

const errorResponseObjectByCode: Record<string, ZodOpenApiResponseObject> = {};

export const errorResponseObject = (
code?: string,
code = 'INTERNAL_SERVER_ERROR',
message?: string,
issues?: { message: string }[],
): ZodOpenApiResponseObject => ({
description: message ?? 'An error response',
content: {
'application/json': {
schema: z
.object({
message: z.string().openapi({
description: 'The error message',
example: message ?? 'Internal server error',
}),
code: z
.string()
.openapi({ description: 'The error code', example: code ?? 'INTERNAL_SERVER_ERROR' }),
issues: z
.array(z.object({ message: z.string() }))
.optional()
): ZodOpenApiResponseObject => {
if (!errorResponseObjectByCode[code]) {
errorResponseObjectByCode[code] = {
description: message ?? 'An error response',
content: {
'application/json': {
schema: z
.object({
message: z.string().openapi({
description: 'The error message',
example: message ?? 'Internal server error',
}),
code: z
.string()
.openapi({
description: 'The error code',
example: code ?? 'INTERNAL_SERVER_ERROR',
}),
issues: z
.array(z.object({ message: z.string() }))
.optional()
.openapi({
description: 'An array of issues that were responsible for the error',
example: issues ?? [],
}),
})
.openapi({
description: 'An array of issues that were responsible for the error',
example: issues ?? [],
title: 'Error',
description: 'The error information',
example: {
code: code ?? 'INTERNAL_SERVER_ERROR',
message: message ?? 'Internal server error',
issues: issues ?? [],
},
ref: `error.${code}`,
}),
})
.openapi({
title: 'Error',
description: 'The error information',
example: {
code: code ?? 'INTERNAL_SERVER_ERROR',
message: message ?? 'Internal server error',
issues: issues ?? [],
},
}),
},
},
});
},
},
};
}
return errorResponseObjectByCode[code]!;
};

export const getResponsesObject = (
schema: ZodTypeAny,
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Procedure, ProcedureParams, Router } from '@trpc/server';
import type { RootConfig } from '@trpc/server/dist/core/internals/config';
import { TRPC_ERROR_CODE_KEY } from '@trpc/server/rpc';
import type { RouterDef } from '@trpc/server/src/core/router';
import type { RouterDef } from '@trpc/server/dist/core/router';
import { TRPC_ERROR_CODE_KEY } from '@trpc/server/dist/rpc';
import {
AnyZodObject,
ZodBigInt,
Expand Down
13 changes: 13 additions & 0 deletions src/utils/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ export const instanceofZodTypeLikeVoid = (type: z.ZodTypeAny): type is ZodTypeLi
};

export const unwrapZodType = (type: z.ZodTypeAny, unwrapPreprocess: boolean): z.ZodTypeAny => {
// TODO: Allow parsing array query params
// if (instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodArray)) {
// return unwrapZodType(type.element, unwrapPreprocess);
// }
if (instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodEnum)) {
return unwrapZodType(z.string(), unwrapPreprocess);
}
if (instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodNullable)) {
return unwrapZodType(type.unwrap(), unwrapPreprocess);
}
if (instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodBranded)) {
return unwrapZodType(type.unwrap(), unwrapPreprocess);
}
if (instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodOptional)) {
return unwrapZodType(type.unwrap(), unwrapPreprocess);
}
Expand Down