Skip to content

Instantly share code, notes, and snippets.

@Zeko369
Last active December 29, 2022 19:46
Show Gist options
  • Save Zeko369/88e101648c5d689c447a30cd887289eb to your computer and use it in GitHub Desktop.
Save Zeko369/88e101648c5d689c447a30cd887289eb to your computer and use it in GitHub Desktop.
const fastify = require("fastify");
const server =
/** @type {import('fastify').FastifyInstance<any, IncomingMessage | Http2ServerRequest, ServerResponse<IncomingMessage> | Http2ServerResponse, FastifyBaseLogger, import('@fastify/type-provider-typebox').TypeBoxTypeProvider> } */ (
fastify().withTypeProvider()
);
let ReturnSchema = {
type: "object",
properties: {
id: { type: "number" },
title: { type: "string" },
},
};
if (process.env.TYPEBOX === "true") {
const { Type } = require("@sinclair/typebox");
ReturnSchema = Type.Object({
id: Type.Number(),
title: Type.String(),
});
}
/** @returns {import('@sinclair/typebox').Static<ReturnSchema>} */
const fakeMongoGet = () => {
return {
id: 1,
title: "asd",
};
};
server.get(
"/route",
{
schema: {
response: { 200: ReturnSchema },
},
},
(request, reply) => {
// type Query = { foo: number, bar: string }
const obj = fakeMongoGet();
// ^?
}
);
server.listen({ port: 3000 }).then(() => {
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment