Issues with Fastify + TypeBox #5302
-
Hello! I'm trying to use Fastify with TypeBox and currently running into 2 issues:
Consider the following setup: // api.ts
import awsLambdaFastify from '@fastify/aws-lambda'
import fastify from 'fastify'
import fastifyCookie from '@fastify/cookie'
import routeSignup from '~/server/routes/signup.ts'
import type {TypeBoxTypeProvider} from '@fastify/type-provider-typebox'
const api = fastify().withTypeProvider<TypeBoxTypeProvider>()
api.register(fastifyCookie)
api.register((app, _, done) => {
routeSignup(app)
done()
}, {
prefix: '/api/'
})
export const handler = awsLambdaFastify(api) // routes/signup.ts
import {type FastifyTypebox, RouteSignupReqBody, RouteSignupRes, type TRouteSignupReqBody, type TRouteSignupRes} from '~/types/global.ts'
export default function (api : FastifyTypebox) {
api.post<{
Body : TRouteSignupReqBody
Reply : TRouteSignupRes
}>('/signup', {
schema: {
body: RouteSignupReqBody,
response: {
200: RouteSignupRes
400: {
msg: 'string'
}
}
}
}, (req, res) => {
if (req.body.email === 'already in use') {
res.status(400).send({
msg: 'email already in use'
})
} else {
res.send({
email: 'foo@bar.com',
id: '123'
name: 'foo bar'
})
}
}
} // types.ts
export type FastifyTypebox = FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, FastifyBaseLogger, TypeBoxTypeProvider>
export const RouteSignupReqBody = Type.Object({
email: Type.String({
format: 'email'
}),
name: Type.String()
})
export const RouteSignupRes = Type.Object({
email: Type.String({
format: 'email'
}),
id: Type.String(),
name: Type.String()
})
export type TRouteSignupRes = Static<typeof RouteSignupRes>
export type TRouteSignupReqBody = Static<typeof RouteSignupReqBody> This gives me an error saying:
If the schema allows setting a different schema for each status code, I thought the type provider would also allow the same, but that doesn't seem possible.
In the above example, I am trying to use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
Could you put assemble a repository for ease of reproduction? @sinclairzx81 could you take a look? |
Beta Was this translation helpful? Give feedback.
@hrishikesh-k Hi. I'm not familiar with @fastify/cookie plugin, however I did find a few issues with the
signup.ts
module. I've updated your repro script pasted these updates below.The main two issues I saw were:
api
{ msg: ... }
on either generics or schema)I've added comments where applicable.