@coobaha/typed-fastify
Advanced tools
Comparing version
# Changelog | ||
## [1.0.1](https://github.com/Coobaha/typed-fastify/compare/v1.0.0...v1.0.1) (2022-08-05) | ||
### Bug Fixes | ||
* fix type missmatch ([#57](https://github.com/Coobaha/typed-fastify/issues/57)) ([72e8d9b](https://github.com/Coobaha/typed-fastify/commit/72e8d9b61cad07ff3e39ced44b8b952ce6e5260c)) | ||
## [1.0.0](https://github.com/Coobaha/typed-fastify/compare/v0.8.0...v1.0.0) (2022-07-31) | ||
@@ -4,0 +11,0 @@ |
import type * as F from 'fastify'; | ||
import { FastifyRequestType, FastifyTypeProvider, FastifyTypeProviderDefault, ResolveFastifyRequestType } from 'fastify/types/type-provider'; | ||
import { Operation, Schema } from './schema'; | ||
declare const addSchema: <ServiceSchema extends Schema<string>, RawServer extends F.RawServerBase = import("http").Server, RawRequest extends F.RawRequestDefaultExpression<RawServer> = F.RawRequestDefaultExpression<RawServer>, RawReply extends F.RawReplyDefaultExpression<RawServer> = F.RawReplyDefaultExpression<RawServer>, Logger extends F.FastifyLoggerInstance = F.FastifyLoggerInstance, S = Service<ServiceSchema, RawServer, RawRequest, RawReply, Logger, F.FastifySchema, F.FastifyTypeProviderDefault, F.FastifyLoggerInstance>>(fastify: F.FastifyInstance<RawServer, RawRequest, RawReply, Logger, F.FastifyTypeProviderDefault>, opts: { | ||
declare const addSchema: <ServiceSchema extends Schema<string>, RawServer extends F.RawServerBase = import("http").Server, RawRequest extends F.RawRequestDefaultExpression<RawServer> = F.RawRequestDefaultExpression<RawServer>, RawReply extends F.RawReplyDefaultExpression<RawServer> = F.RawReplyDefaultExpression<RawServer>, Logger extends F.FastifyLoggerInstance = F.FastifyLoggerInstance, SchemaCompiler extends F.FastifySchema = F.FastifySchema, TypeProvider extends F.FastifyTypeProvider = F.FastifyTypeProviderDefault, ContextConfig = unknown, S = Service<ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig, SchemaCompiler, TypeProvider, Logger>>(fastify: F.FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>, opts: { | ||
jsonSchema: { | ||
@@ -89,3 +89,3 @@ schema: Record<string, any>; | ||
}; | ||
export declare type RequestHandler<ServiceSchema extends Schema, HandlerPaths extends keyof ServiceSchema['paths'], RawServer extends F.RawServerBase = F.RawServerDefault, RawRequest extends F.RawRequestDefaultExpression<RawServer> = F.RawRequestDefaultExpression<RawServer>, RawReply extends F.RawReplyDefaultExpression<RawServer> = F.RawReplyDefaultExpression<RawServer>, ContextConfig = F.ContextConfigDefault, SchemaCompiler = F.FastifySchema, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, Logger extends F.FastifyLoggerInstance = F.FastifyLoggerInstance, S = Service<ServiceSchema, RawServer, RawRequest, RawReply, Logger>, Paths = ServiceSchema['paths'], OpHandler = { | ||
export declare type RequestHandler<ServiceSchema extends Schema, HandlerPaths extends keyof ServiceSchema['paths'], RawServer extends F.RawServerBase = F.RawServerDefault, RawRequest extends F.RawRequestDefaultExpression<RawServer> = F.RawRequestDefaultExpression<RawServer>, RawReply extends F.RawReplyDefaultExpression<RawServer> = F.RawReplyDefaultExpression<RawServer>, ContextConfig = F.ContextConfigDefault, SchemaCompiler = F.FastifySchema, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, Logger extends F.FastifyLoggerInstance = F.FastifyLoggerInstance, S = Service<ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig, SchemaCompiler, TypeProvider, Logger>, Paths = ServiceSchema['paths'], OpHandler = { | ||
[Path in HandlerPaths]: Handler<Path extends keyof Paths ? Paths[Path] : never, Path, ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig, SchemaCompiler, TypeProvider, ResolveFastifyRequestType<TypeProvider, SchemaCompiler, Router<Path extends keyof Paths ? Paths[Path] : never>>, Logger>; | ||
@@ -92,0 +92,0 @@ }[HandlerPaths], OpHandlerObj = { |
@@ -51,3 +51,11 @@ "use strict"; | ||
}); | ||
const httpMethods = new Set(['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']); | ||
const httpMethods = new Set([ | ||
'DELETE', | ||
'GET', | ||
'HEAD', | ||
'PATCH', | ||
'POST', | ||
'PUT', | ||
'OPTIONS', | ||
]); | ||
for (const path in opts.service) { | ||
@@ -54,0 +62,0 @@ if (!Object.hasOwnProperty.call(opts.service, path)) |
{ | ||
"name": "@coobaha/typed-fastify", | ||
"description": "opinionated types for fastify", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"bin": { | ||
@@ -6,0 +6,0 @@ "typed-fastify-schema": "bin/gen.bin.js", |
@@ -17,4 +17,4 @@ # Typed Fastify | ||
transforms, all `@tjs` annotations can be used to fine-tune output) | ||
- since we use `typejescript-json-schema`: all known limitations of lib are inhereted: | ||
- Records are not transforrmed correctly, use `{ [k: string]: string }` instead or hint with `@tjs` | ||
- since we use `typejescript-json-schema`: all known limitations of lib are inherited: | ||
- Records are not transformed correctly, use `{ [k: string]: string }` instead or hint with `@tjs` | ||
- Runtime validation using generated JSON schema (optional but strongly recommended as it brings extra safety to runtime and ensures that code assumptions about data are correct) | ||
@@ -91,3 +91,3 @@ | ||
Complex examples can be found [typescript tests](./test/better-fastify.test-d.ts) and | ||
Complex examples can be found [typescript tests](./test/typed-fastify.test-d.ts) and | ||
in [integration.test.ts](./test/integration.test.ts). | ||
@@ -137,10 +137,6 @@ | ||
1. Handlers in one object | ||
Type inference will work nicely in this case, you just make TS happy and things are working 🥳 | ||
Type inference will work nicely in this case, you just make TS happy and things are working 🥳 | ||
2. Handlers in a different file or separate functions - you will need to hint TS with exact type of handler. | ||
2. Handlers in a different file or separate functions - you will need to hint TS with exact type of | ||
handler | ||
The Easiest way to do it is | ||
```typescript | ||
@@ -165,4 +161,3 @@ import { RequestHandler, Schema } from '@coobaha/typed-fastify'; | ||
4. Sometimes properties won't be the same (for instance GET never has body and POST will). In this | ||
case you will probably be asked to add types to function params | ||
4. Sometimes properties won't be the same (for instance GET never has body and POST will). In this case you will probably be asked to add types to function params | ||
@@ -201,1 +196,10 @@ ```typescript | ||
``` | ||
### Annotating types | ||
This library is using [typescript-json-schema](https://github.com/YousefED/typescript-json-schema) with custom | ||
transforms for schema generation. All `@tjs` [annotations](https://github.com/YousefED/typescript-json-schema#annotations) can be used to fine-tune schema output | ||
- `@type` can be used to specify end type after using `toJSON, toString` methods of objects like `ObjectID` from MogoDB | ||
- since we use `typejescript-json-schema`: all known limitations are also inherited: - Records are not transformed correctly, use `{ [k: string]: string }` instead or hint with `@tjs` |
@@ -16,5 +16,8 @@ import type * as F from 'fastify'; | ||
Logger extends F.FastifyLoggerInstance = F.FastifyLoggerInstance, | ||
S = Service<ServiceSchema, RawServer, RawRequest, RawReply, Logger>, | ||
SchemaCompiler extends F.FastifySchema = F.FastifySchema, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, | ||
ContextConfig = F.ContextConfigDefault, | ||
S = Service<ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig, SchemaCompiler, TypeProvider, Logger>, | ||
>( | ||
fastify: F.FastifyInstance<RawServer, RawRequest, RawReply, Logger>, | ||
fastify: F.FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>, | ||
opts: { | ||
@@ -76,3 +79,11 @@ jsonSchema: { | ||
}); | ||
const httpMethods: Set<F.HTTPMethods> = new Set(['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']); | ||
const httpMethods: Set<F.HTTPMethods> = new Set([ | ||
'DELETE', | ||
'GET', | ||
'HEAD', | ||
'PATCH', | ||
'POST', | ||
'PUT', | ||
'OPTIONS', | ||
] as const); | ||
@@ -452,3 +463,3 @@ for (const path in opts.service) { | ||
Logger extends F.FastifyLoggerInstance = F.FastifyLoggerInstance, | ||
S = Service<ServiceSchema, RawServer, RawRequest, RawReply, Logger>, | ||
S = Service<ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig, SchemaCompiler, TypeProvider, Logger>, | ||
Paths = ServiceSchema['paths'], | ||
@@ -455,0 +466,0 @@ OpHandler = { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
94761
1.54%1089
1.78%201
2.03%