Giter Site home page Giter Site logo

Comments (6)

SimenB avatar SimenB commented on May 20, 2024 2

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.

mcollina avatar mcollina commented on May 20, 2024

cc @sinclairzx81 @Eomm @RafaelGSS

from fastify-type-provider-typebox.

RafaelGSS avatar RafaelGSS commented on May 20, 2024

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.

sinclairzx81 avatar sinclairzx81 commented on May 20, 2024

@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.

mcollina avatar mcollina commented on May 20, 2024

Thanks! I will send a PR to include some of this in the README.

from fastify-type-provider-typebox.

thelinuxlich avatar thelinuxlich commented on May 20, 2024

I'm still getting untyped responses
image

from fastify-type-provider-typebox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.