Skip to content

Commit

Permalink
feat: bump fastify / ts types and better type preHandler (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coobaha authored Sep 12, 2022
1 parent 977697a commit d7c8dce
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 123 deletions.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@
"yargs": "^17.5.1"
},
"devDependencies": {
"@fastify/swagger": "^7.4.1",
"@fastify/swagger": "^7.5.1",
"@types/crypto-js": "^4.1.1",
"@types/glob": "^7.2.0",
"@types/glob": "^8.0.0",
"@types/json-schema-merge-allof": "^0.6.1",
"@types/node": "^18.6.2",
"@types/prettier": "^2.6.4",
"@types/node": "^18.7.16",
"@types/prettier": "^2.7.0",
"@types/split2": "^3.2.1",
"@types/tap": "^15.0.7",
"@types/yargs": "^17.0.10",
"fastify": "^4.3.0",
"@types/yargs": "^17.0.12",
"fastify": "^4.5.3",
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
"pino-pretty": "^8.1.0",
"pino-pretty": "^9.1.0",
"pinst": "^3.0.0",
"prettier": "^2.7.1",
"split2": "^4.1.0",
"syncpack": "^8.2.4",
"tap": "^16.3.0",
"ts-node-dev": "^2.0.0",
"tsd": "^0.22.0",
"type-fest": "^2.17.0",
"typescript": "^4.7.4"
"tsd": "^0.23.0",
"type-fest": "^2.19.0",
"typescript": "^4.8.3"
},
"directories": {
"test": "test/",
Expand Down
124 changes: 86 additions & 38 deletions src/typed-fastify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as F from 'fastify';
import { RouteGenericInterface } from 'fastify/types/route';
import {
FastifyRequestType,
FastifyTypeProvider,
Expand All @@ -12,7 +13,7 @@ const addSchema = <
RawServer extends F.RawServerBase = F.RawServerDefault,
RawRequest extends F.RawRequestDefaultExpression<RawServer> = F.RawRequestDefaultExpression<RawServer>,
RawReply extends F.RawReplyDefaultExpression<RawServer> = F.RawReplyDefaultExpression<RawServer>,
Logger extends F.FastifyLoggerInstance = F.FastifyLoggerInstance,
Logger extends F.FastifyBaseLogger = F.FastifyBaseLogger,
SchemaCompiler extends F.FastifySchema = F.FastifySchema,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
ContextConfig = F.ContextConfigDefault,
Expand Down Expand Up @@ -147,6 +148,8 @@ type ExtractParams<T extends string | number | symbol, Acc = {}> = T extends `${
? Id<Acc & { [_ in P]: string }>
: Acc;

type ArrayTOrT<T> = T | T[];

interface Reply<
Op extends Operation,
Status,
Expand Down Expand Up @@ -184,7 +187,18 @@ interface Reply<
this: any,
path: IsKnown extends true ? P : Path,
): this is IsKnown extends true
? Reply<NewOp, S, Content, Headers, P, ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig>
? Reply<
Extract<NewOp, Operation>,
S,
Content,
Headers,
P,
ServiceSchema,
RawServer,
RawRequest,
RawReply,
ContextConfig
>
: never;

send<
Expand Down Expand Up @@ -297,12 +311,12 @@ interface Request<
SchemaCompiler extends F.FastifySchema = F.FastifySchema,
TypeProvider extends F.FastifyTypeProvider = F.FastifyTypeProviderDefault,
ContextConfig = F.ContextConfigDefault,
Logger extends F.FastifyLoggerInstance = F.FastifyLoggerInstance,
Logger extends F.FastifyBaseLogger = F.FastifyBaseLogger,
OpRequest extends Router<Op> = Router<Op>,
PathParams = OpRequest['Params'] extends never
? ExtractParams<Path>
: Id<Omit<ExtractParams<Path>, keyof OpRequest['Params']>>,
RouteGeneric = OpRequest['Params'] extends [never]
RouteGeneric extends RouteGenericInterface = OpRequest['Params'] extends [never]
? Omit<Router<Op>, 'Params'> & { Params: PathParams }
: Omit<Router<Op>, 'Params'> & { Params: Id<PathParams & Router<Op>['Params']> },
RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>,
Expand Down Expand Up @@ -351,11 +365,11 @@ type Handler<
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,
ContextConfig extends F.ContextConfigDefault = F.ContextConfigDefault,
SchemaCompiler extends F.FastifySchema = F.FastifySchema,
TypeProvider extends F.FastifyTypeProvider = F.FastifyTypeProviderDefault,
RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, Router<Op>>,
Logger extends F.FastifyLoggerInstance = F.FastifyLoggerInstance,
Logger extends F.FastifyBaseLogger = F.FastifyBaseLogger,
InvalidParams = GetInvalidParamsValidation<Op, Path, ServiceSchema>,
ValidSchema = [Op['response'][keyof Op['response']]] extends [never]
? Invalid<`${Extract<Path, string>} - has no response, every path should have at least one response defined`>
Expand All @@ -372,9 +386,9 @@ type Handler<
Path,
RawServer,
RawRequest,
ContextConfig,
SchemaCompiler,
TypeProvider,
RequestType,
ContextConfig,
Logger
>,
reply: Reply<Op, never, never, never, Path, ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig> & {
Expand All @@ -391,37 +405,63 @@ type HandlerObj<
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,
ContextConfig extends F.ContextConfigDefault = F.ContextConfigDefault,
SchemaCompiler extends F.FastifySchema = F.FastifySchema,
TypeProvider extends F.FastifyTypeProvider = F.FastifyTypeProviderDefault,
RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, Router<Op>>,
Logger extends F.FastifyLoggerInstance = F.FastifyLoggerInstance,
> = F.RouteShorthandOptions<
RawServer,
RawRequest,
RawReply,
Router<Op>,
ContextConfig,
SchemaCompiler,
TypeProvider,
Logger
Logger extends F.FastifyBaseLogger = F.FastifyBaseLogger,
_Handler = Handler<
Op,
Path,
ServiceSchema,
RawServer,
RawRequest,
RawReply,
ContextConfig,
SchemaCompiler,
TypeProvider,
RequestType,
Logger
>,
_Request extends Request<any, any, any> = Parameters<Extract<_Handler, (...args: any) => any>>[0],
_Reply extends Reply<any, any, any, any, any, any> = Parameters<Extract<_Handler, (...args: any) => any>>[1],
> = Omit<
F.RouteShorthandOptions<
RawServer,
RawRequest,
RawReply,
Router<Op>,
ContextConfig,
SchemaCompiler,
TypeProvider,
Logger
>,
'preHandler'
> & {
handler: Handler<Op, Path, ServiceSchema, RawServer, RawRequest, RawReply, ContextConfig, Logger>;
handler: _Handler;
preHandler?: ArrayTOrT<
(
this: F.FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
request: _Request,
reply: _Reply,
done: F.HookHandlerDoneFunction,
) => void
>;
};

export type Service<
S extends Schema,
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,
ContextConfig extends F.ContextConfigDefault = F.ContextConfigDefault,
SchemaCompiler extends F.FastifySchema = F.FastifySchema,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
Logger extends F.FastifyLoggerInstance = F.FastifyLoggerInstance,
Logger extends F.FastifyBaseLogger = F.FastifyBaseLogger,
> = {
[P in keyof S['paths']]:
| Handler<
S['paths'][P],
Extract<S['paths'][P], Operation>,
P,
S,
RawServer,
Expand All @@ -430,11 +470,11 @@ export type Service<
ContextConfig,
SchemaCompiler,
TypeProvider,
ResolveFastifyRequestType<TypeProvider, SchemaCompiler, Router<S['paths'][P]>>,
ResolveFastifyRequestType<TypeProvider, SchemaCompiler, Router<Extract<S['paths'][P], Operation>>>,
Logger
>
| HandlerObj<
S['paths'][P],
Extract<S['paths'][P], Operation>,
P,
S,
RawServer,
Expand All @@ -443,7 +483,7 @@ export type Service<
ContextConfig,
SchemaCompiler,
TypeProvider,
ResolveFastifyRequestType<TypeProvider, SchemaCompiler, Router<S['paths'][P]>>,
ResolveFastifyRequestType<TypeProvider, SchemaCompiler, Router<Extract<S['paths'][P], Operation>>>,
Logger
>;
};
Expand All @@ -454,15 +494,15 @@ export type RequestHandler<
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,
ContextConfig extends F.ContextConfigDefault = F.ContextConfigDefault,
SchemaCompiler extends F.FastifySchema = F.FastifySchema,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
Logger extends F.FastifyLoggerInstance = F.FastifyLoggerInstance,
Logger extends F.FastifyBaseLogger = F.FastifyBaseLogger,
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 extends keyof Paths ? Extract<Paths[Path], Operation> : never,
Path,
ServiceSchema,
RawServer,
Expand All @@ -471,13 +511,17 @@ export type RequestHandler<
ContextConfig,
SchemaCompiler,
TypeProvider,
ResolveFastifyRequestType<TypeProvider, SchemaCompiler, Router<Path extends keyof Paths ? Paths[Path] : never>>,
ResolveFastifyRequestType<
TypeProvider,
SchemaCompiler,
Router<Path extends keyof Paths ? Extract<Paths[Path], Operation> : never>
>,
Logger
>;
}[HandlerPaths],
OpHandlerObj = {
[Path in HandlerPaths]: HandlerObj<
Path extends keyof Paths ? Paths[Path] : never,
Path extends keyof Paths ? Extract<Paths[Path], Operation> : never,
Path,
ServiceSchema,
RawServer,
Expand All @@ -486,7 +530,11 @@ export type RequestHandler<
ContextConfig,
SchemaCompiler,
TypeProvider,
ResolveFastifyRequestType<TypeProvider, SchemaCompiler, Router<Path extends keyof Paths ? Paths[Path] : never>>,
ResolveFastifyRequestType<
TypeProvider,
SchemaCompiler,
Router<Path extends keyof Paths ? Extract<Paths[Path], Operation> : never>
>,
Logger
>;
}[HandlerPaths],
Expand All @@ -495,14 +543,14 @@ export type RequestHandler<
Request: Parameters<OpHandler>[0];
AsFastifyRequest: Parameters<OpHandler>[0] extends F.FastifyRequest<any, any, any>
? F.FastifyRequest<
Router<Paths[keyof Paths]>,
Router<Extract<Paths[keyof Paths], Operation>>,
RawServer,
RawRequest,
SchemaCompiler,
TypeProvider,
ContextConfig,
Logger,
ResolveFastifyRequestType<TypeProvider, SchemaCompiler, Router<Paths[keyof Paths]>>
ResolveFastifyRequestType<TypeProvider, SchemaCompiler, Router<Extract<Paths[keyof Paths], Operation>>>
>
: never;
Reply: Parameters<OpHandler>[1];
Expand Down
2 changes: 1 addition & 1 deletion test/test_schema.gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -359,5 +359,5 @@
}
}
},
"$hash": "c138d591328ddd9cc1b794e5e35447467e7c1f8d4129b8fc727c11d7e9b0ef02__v1.0.0"
"$hash": "c138d591328ddd9cc1b794e5e35447467e7c1f8d4129b8fc727c11d7e9b0ef02__v1.0.1"
}
Loading

0 comments on commit d7c8dce

Please sign in to comment.