Git Product home page Git Product logo

Comments (7)

GiovanniCardamone avatar GiovanniCardamone commented on June 6, 2024

you can already do this, here an example:

import { enums, use } from 'class-schema'
import {
  FastifyInstance,
  FastifyRequest,
  RequestGenericInterface,
} from 'fastify'
import { StrictResource } from 'fastify-autoroutes'

interface RequestIndex extends RequestGenericInterface {}

interface GetRequestIndex extends RequestIndex {}

@schema()
export class ServerStatus {
  @enums(['running', 'manteniance'])
  status!: 'running' | 'manteniance'
}


export default (fastify: FastifyInstance): StrictResource => ({
  get: {
    schema: {
      summary: 'get server status',
      description: 'get informations about server',
      tags: ['Server'],
      consumes: ['application/json'],
      produces: ['application/json'],
      response: {
        200: use(ServerStatus),
      },
    },
    handler: async (
      request: FastifyRequest<GetRequestIndex>
    ): Promise<ServerStatus> => ({
      status: 'running',
    }),
  },
})

GetRequestIndex can be typed overriding RequestGenericInterface props (es, Querystring, Body, etc)

from fastify-autoroutes.

karmabadger avatar karmabadger commented on June 6, 2024

oh this is very neat, does this deal with run time type checking as well?

from fastify-autoroutes.

GiovanniCardamone avatar GiovanniCardamone commented on June 6, 2024

No, types annotation does not deal with types at runtime. For that there is fastify that does that (ajv usually for request types)

As you can see, there is types declaration in "schema" that deals with that, and types annotation (wich reflect types annotation) in handler

from fastify-autoroutes.

irg1008 avatar irg1008 commented on June 6, 2024

I think this brings as much boilerplate to the route declaration as actually registering routes. Can we have another solution or maybe allow plugin-based route declaration?

from fastify-autoroutes.

GiovanniCardamone avatar GiovanniCardamone commented on June 6, 2024

not sure what you mean, can you explain what you want to accomplish?

from fastify-autoroutes.

irg1008 avatar irg1008 commented on June 6, 2024

I mean, are we able to export routes as we do with fastify, exporting an AsyncPlugin

from fastify-autoroutes.

GiovanniCardamone avatar GiovanniCardamone commented on June 6, 2024

I think the original question made from @karmabadger was to enable the StrictResource type to allow Generics to define each route.

something like this actually

export default (server): StrictResource<GetReuqest, PostRequest, ...> => ({
  get: ... // here types are inferred from GetRequest
  post: ... // here types are inferred from PostReqiest
})

you can use the route without using the typescript as well, like:

export default (server) => ({
  get: ... // here you deal with the types
  post: ... // here you deal with the types
})

it's not related to how you do import/export.

I could create a builder that just annotate the thing is a strict resource and gives you fastify, like:

export default wrapper<GenericThatDescribeGetPostAndEtc>((server /* server and expected return are inferred */) => ({
    get: ... // here types are inferred from GenericThatDescribeGetPostAndEtc['get'],
    post: ...// here types are inferred from GenericThatDescribeGetPostAndEtc['post']
}))

but this won't solve boilerplate code. Not sure if the solution above it's really performant but it's doable

from fastify-autoroutes.

Related Issues (12)

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.