Comments (7)
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.
oh this is very neat, does this deal with run time type checking as well?
from fastify-autoroutes.
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.
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.
not sure what you mean, can you explain what you want to accomplish?
from fastify-autoroutes.
I mean, are we able to export routes as we do with fastify, exporting an AsyncPlugin
from fastify-autoroutes.
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)
- Stable for use? HOT 2
- [Bug report] dependancies HOT 2
- Bug Report: Windows fileystem issue HOT 12
- Don't working ES module type HOT 4
- Support for Fastify 4.0 HOT 3
- Is there a reason why @fastify/error is listed as DevDependency ? HOT 2
- Plugin won't take status.ts as a route HOT 8
- Route conversion HOT 1
- Add "Prefix" feature. HOT 2
- [QUESTION] How to import with ES modules? HOT 4
- subroute multiple keys HOT 1
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-autoroutes.