Skip to content

Commit

Permalink
feat: allow jsonified variants in response
Browse files Browse the repository at this point in the history
  • Loading branch information
qwelias committed Oct 26, 2023
1 parent 93a8b8c commit e15f273
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/typed-fastify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as F from 'fastify';
import type { Jsonify } from 'type-fest';
import { RouteGenericInterface } from 'fastify/types/route';
import { RequestRouteOptions } from 'fastify/types/request';

Expand Down Expand Up @@ -224,7 +225,7 @@ interface Reply<
]
: [Get2<Op['response'], Status, 'content'>] extends [never]
? []
: [Get2<Op['response'], Status, 'content'>]
: [Get2<Op['response'], Status, 'content'> | Jsonify<Get2<Op['response'], Status, 'content'>>]
): AsReply;

readonly request: Request<ServiceSchema, Op, Path, RawServer, RawRequest>;
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export const defaultService: Service<TestSchema> = {
'GET /user_and_obj': (req, reply) => {
return reply.status(200).send([{ name: 'user1' }, { id: '1', type: 'TEST' }, { any: 'thing' }]);
},
'POST /jsonify': (req, reply) => {
const { date } = req.body;
return reply.status(200).send({ date: date.toJSON() });
},
'POST /': (req, reply) => {
if (req.operationPath !== 'POST /') {
throw new Error('Should never happen');
Expand Down
12 changes: 12 additions & 0 deletions test/test_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ export interface TestSchema extends Schema {
};
};
};
'POST /jsonify': {
request: {
body: {
date: Date;
};
};
response: {
200: {
content: { date: Date };
};
};
};
'GET /empty': {
response: {
204: {};
Expand Down

0 comments on commit e15f273

Please sign in to comment.