Skip to content

Commit

Permalink
feat/top-level-preprocess (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlalmes authored Aug 25, 2022
1 parent 150c9f9 commit 1403dac
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/generator/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const getParameterObjects = (
});
}

const schema = unwrapZodType(_schema);
const schema = unwrapZodType(_schema, true);

if (pathParameters.length === 0 && instanceofZodTypeLikeVoid(schema)) {
return undefined;
Expand Down Expand Up @@ -108,7 +108,7 @@ export const getRequestBodyObject = (
});
}

const schema = unwrapZodType(_schema);
const schema = unwrapZodType(_schema, true);

if (pathParameters.length === 0 && instanceofZodTypeLikeVoid(schema)) {
return undefined;
Expand Down
17 changes: 10 additions & 7 deletions src/utils/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,25 @@ export const instanceofZodTypeLikeVoid = (type: z.ZodTypeAny): type is ZodTypeLi
);
};

export const unwrapZodType = (type: z.ZodTypeAny): z.ZodTypeAny => {
export const unwrapZodType = (type: z.ZodTypeAny, unwrapPreprocess: boolean): z.ZodTypeAny => {
if (instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodOptional)) {
return unwrapZodType(type.unwrap());
return unwrapZodType(type.unwrap(), unwrapPreprocess);
}
if (instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodDefault)) {
return unwrapZodType(type.removeDefault());
return unwrapZodType(type.removeDefault(), unwrapPreprocess);
}
if (instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodLazy)) {
return unwrapZodType(type._def.getter());
return unwrapZodType(type._def.getter(), unwrapPreprocess);
}
if (instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodEffects)) {
if (type._def.effect.type === 'refinement') {
return unwrapZodType(type._def.schema);
return unwrapZodType(type._def.schema, unwrapPreprocess);
}
if (type._def.effect.type === 'transform') {
return unwrapZodType(type._def.schema);
return unwrapZodType(type._def.schema, unwrapPreprocess);
}
if (unwrapPreprocess && type._def.effect.type === 'preprocess') {
return unwrapZodType(type._def.schema, unwrapPreprocess);
}
}
return type;
Expand All @@ -70,7 +73,7 @@ export type ZodTypeLikeString =
| z.ZodNativeEnum<NativeEnumType>;

export const instanceofZodTypeLikeString = (_type: z.ZodTypeAny): _type is ZodTypeLikeString => {
const type = unwrapZodType(_type);
const type = unwrapZodType(_type, false);

if (instanceofZodTypeKind(type, z.ZodFirstPartyTypeKind.ZodEffects)) {
if (type._def.effect.type === 'preprocess') {
Expand Down
60 changes: 60 additions & 0 deletions test/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2187,4 +2187,64 @@ describe('generator', () => {
]
`);
});

test('with top-level preprocess', () => {
const appRouter = trpc
.router<any, OpenApiMeta>()
.query('topLevelPreprocess', {
meta: { openapi: { enabled: true, path: '/top-level-preprocess', method: 'GET' } },
input: z.preprocess((arg) => arg, z.object({ id: z.string() })),
output: z.preprocess((arg) => arg, z.object({ id: z.string() })),
resolve: ({ input }) => ({ id: input.id }),
})
.mutation('topLevelPreprocess', {
meta: { openapi: { enabled: true, path: '/top-level-preprocess', method: 'POST' } },
input: z.preprocess((arg) => arg, z.object({ id: z.string() })),
output: z.preprocess((arg) => arg, z.object({ id: z.string() })),
resolve: ({ input }) => ({ id: input.id }),
});

const openApiDocument = generateOpenApiDocument(appRouter, {
title: 'tRPC OpenAPI',
version: '1.0.0',
baseUrl: 'http://localhost:3000/api',
});

expect(openApiSchemaValidator.validate(openApiDocument).errors).toEqual([]);
expect(openApiDocument.paths['/top-level-preprocess']!.get!.parameters).toMatchInlineSnapshot(`
Array [
Object {
"description": undefined,
"in": "query",
"name": "id",
"required": true,
"schema": Object {
"type": "string",
},
},
]
`);
expect(openApiDocument.paths['/top-level-preprocess']!.post!.requestBody)
.toMatchInlineSnapshot(`
Object {
"content": Object {
"application/json": Object {
"schema": Object {
"additionalProperties": false,
"properties": Object {
"id": Object {
"type": "string",
},
},
"required": Array [
"id",
],
"type": "object",
},
},
},
"required": true,
}
`);
});
});

0 comments on commit 1403dac

Please sign in to comment.