Comments (6)
you need to do
const typeboxServer = server.withTypeProvider<TypeBoxTypeProvider>();
or add the withTypeProvider
call to your initial Fastify()
.
withTypeProvider
doesn't "mutate" the server it's called on, it returns a "new instance" (not really, but TS thinks so)
from fastify-type-provider-typebox.
cc @sinclairzx81 @Eomm @RafaelGSS
from fastify-type-provider-typebox.
That's correct. We can't mutate the server instance without returning a new instance with the typebox definitions.
A good strategy always calls it in the server creation:
const fastify = Fastify().withTypeProvider<TypeBoxTypeProvider>()
from fastify-type-provider-typebox.
@mcollina Hi!
The TypeProvider setup does require functions to chain in this way. Each call to withTypeProvider()
will remap the current instance generics and return the mapped instance to the caller for left-side assignment or additional chaining.
Type Mapping Statements (An Experiment)
Interestingly, it is actually possible to use TypeScript assertion predicates to map generic arguments with line by line statements (so non-fluent - mutable generics). As a proof of concept, the following implements statement assertions to map Type Providers.
TypeScript Link Here (Note: Type inference may take a little while to load)
const server = Fastify()
// -----------------------------------------------------------------------
Fastify.withTypeProvider<TypeBoxTypeProvider>(server)
server.post('/', {
schema: {
body: Type.Object({
name: Type.String()
})
}
}, async (request, reply) => {
const { name } = request.body // ok
return { hello: name }
})
// -----------------------------------------------------------------------
Fastify.withTypeProvider<JsonSchemaToTsProvider>(server)
server.post('/', {
schema: {
body: {
type: 'object',
required: ['name'],
properties: { name: { type: 'string' } }
} as const
}
}, async (request, reply) => {
const { name } = request.body // ok
return { hello: name }
})
// -----------------------------------------------------------------------
Fastify.withTypeProvider<ZodTypeProvider>(server)
server.post('/', {
schema: {
body: z.object({
name: z.string()
})
}
}, async (request, reply) => {
const { name } = request.body // ok
return { hello: name }
})
// -----------------------------------------------------------------------
Fastify.withTypeProvider<YupTypeProvider>(server)
server.post('/', {
schema: {
body: yup.object({
name: yup.string().required()
})
}
}, async (request, reply) => {
const { name } = request.body // ok
return { hello: name }
})
from fastify-type-provider-typebox.
Thanks! I will send a PR to include some of this in the README.
from fastify-type-provider-typebox.
I'm still getting untyped responses
from fastify-type-provider-typebox.
Related Issues (20)
- Error `forgotten to call 'done'` since version 3.4 HOT 9
- `Type.Void` / `Type.Undefined` throws `FST_ERR_SCH_SERIALIZATION_BUILD` since typebox 0.30.0 HOT 3
- Type error: TypeBoxTypeProvider does not satisfy the constraint FastifyTypeProvider HOT 1
- Reply type check improvement HOT 3
- Support for transform types HOT 2
- Doesn't support auto-coercion for response bodies HOT 6
- FST_ERR_SCH_VALIDATION_BUILD error when using 'header' validation
- Add Support Typebox 0.25.x HOT 6
- Expose all of typebox HOT 7
- Support for responses HOT 2
- Widen typebox peerDependency to allow 0.26 HOT 7
- Support typebox 0.27.8 HOT 2
- Release 3.1.0 broke type inference on fastify route handler request parameter HOT 18
- string auto convert to number HOT 2
- Mention in Typebox Readme.md under "Ecosystem" HOT 1
- Type.String({ format: "date" }) doesn't work in body validator HOT 7
- Allow providing `references: Types.TSchema[]` to TypeboxCompiler
- incorrect (and potentially broken) dependency resolution error when installing latest via NPM HOT 2
- Is there way to create wrapper function for route handler inferring schema type? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify-type-provider-typebox.