Last active
December 29, 2022 19:46
-
-
Save Zeko369/88e101648c5d689c447a30cd887289eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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