Skip to content

Commit eaddb94

Browse files
committed
feat: support use null as typebox route response type
1 parent eea3463 commit eaddb94

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

.changeset/strange-tools-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ddadaal/next-typed-api-routes-runtime": minor
3+
---
4+
5+
support use Type.Null() as typebox route response type

example/src/pages/api/typeboxRoute/[test].ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ export const TypeboxRouteSchema = typeboxRouteSchema({
77
responses: {
88
200: Type.Object({ hello: Type.String() }),
99
404: Type.Object({ error: Type.String() }),
10+
500: Type.Null(),
1011
},
1112
});
1213

1314
export default typeboxRoute(TypeboxRouteSchema, async (req) => {
14-
15+
1516
if (req.body.error) {
1617
return { 404: { error: "123" } };
1718
} else {

example/src/pages/api/zodRoute/[test].ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const ZodRouteSchema = zodRouteSchema({
77
responses: {
88
200: z.object({ hello: z.string() }),
99
404: z.object({ error: z.string() }),
10+
500: z.null(),
1011
},
1112
});
1213

packages/runtime/src/route/typeboxRoute.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { Static, TObject } from "@sinclair/typebox";
1+
import { Kind, Static, TNull, TObject } from "@sinclair/typebox";
22
import fastJson from "fast-json-stringify";
33
import { NextApiRequest, NextApiResponse } from "next";
44

55
import { ajvOptions, createAjv } from "./ajv";
66
import { OrPromise, returnError, Serializer, ValueOf } from "./utils";
77

8+
export type TypeboxReturnType = TObject | TNull;
9+
810
export interface TypeboxRouteSchema<
911
TQuery extends TObject = TObject,
1012
TBody extends TObject = TObject,
11-
TResponses extends Record<number, TObject> = Record<number, TObject>,
13+
TResponses extends Record<number, TypeboxReturnType> = Record<number, TypeboxReturnType>,
1214
> {
1315
method: string;
1416
query?: TQuery;
@@ -18,7 +20,7 @@ export interface TypeboxRouteSchema<
1820

1921
export type TypeboxRawType<T> =
2022
T extends undefined ? undefined
21-
: T extends TObject ? Static<T>
23+
: T extends TypeboxReturnType ? Static<T>
2224
: T;
2325

2426

@@ -56,8 +58,10 @@ export function typeboxRoute<TSchema extends TypeboxRouteSchema>(
5658
const serializers = new Map<string, Serializer>();
5759

5860
for (const [code, schema] of Object.entries(responses)) {
59-
const serializer = fastJson(schema, { ajv: ajvOptions });
60-
serializers.set(code, serializer);
61+
if (schema[Kind] === "Object") {
62+
const serializer = fastJson(schema, { ajv: ajvOptions });
63+
serializers.set(code, serializer);
64+
}
6165
}
6266
return serializers;
6367
})(schema.responses);

0 commit comments

Comments
 (0)