fastify 3.26.0 → 4.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -4
- package/build/build-error-serializer.js +27 -0
- package/build/build-validation.js +49 -35
- package/docs/Guides/Ecosystem.md +2 -1
- package/docs/Guides/Prototype-Poisoning.md +3 -3
- package/docs/Migration-Guide-V4.md +12 -0
- package/docs/Reference/ContentTypeParser.md +8 -1
- package/docs/Reference/Errors.md +51 -6
- package/docs/Reference/Hooks.md +4 -7
- package/docs/Reference/LTS.md +5 -4
- package/docs/Reference/Reply.md +23 -22
- package/docs/Reference/Request.md +1 -3
- package/docs/Reference/Routes.md +17 -10
- package/docs/Reference/Server.md +98 -63
- package/docs/Reference/TypeScript.md +11 -13
- package/docs/Reference/Validation-and-Serialization.md +32 -54
- package/docs/Type-Providers.md +257 -0
- package/examples/hooks.js +1 -1
- package/examples/simple-stream.js +18 -0
- package/fastify.d.ts +36 -22
- package/fastify.js +72 -53
- package/lib/configValidator.js +902 -1023
- package/lib/contentTypeParser.js +6 -16
- package/lib/context.js +36 -10
- package/lib/decorate.js +5 -3
- package/lib/error-handler.js +158 -0
- package/lib/error-serializer.js +257 -0
- package/lib/errors.js +49 -10
- package/lib/fourOhFour.js +31 -20
- package/lib/handleRequest.js +10 -13
- package/lib/hooks.js +14 -9
- package/lib/noop-set.js +10 -0
- package/lib/pluginOverride.js +0 -3
- package/lib/pluginUtils.js +3 -2
- package/lib/reply.js +44 -163
- package/lib/request.js +13 -10
- package/lib/route.js +158 -139
- package/lib/schema-controller.js +3 -3
- package/lib/schemas.js +27 -1
- package/lib/server.js +219 -116
- package/lib/symbols.js +6 -4
- package/lib/validation.js +2 -1
- package/lib/warnings.js +2 -12
- package/lib/wrapThenable.js +4 -11
- package/package.json +40 -45
- package/test/404s.test.js +265 -108
- package/test/500s.test.js +2 -2
- package/test/async-await.test.js +15 -71
- package/test/close.test.js +39 -1
- package/test/content-parser.test.js +32 -0
- package/test/context-config.test.js +56 -4
- package/test/custom-http-server.test.js +14 -7
- package/test/custom-parser-async.test.js +0 -65
- package/test/custom-parser.test.js +54 -121
- package/test/decorator.test.js +1 -3
- package/test/delete.test.js +5 -5
- package/test/encapsulated-error-handler.test.js +50 -0
- package/test/esm/index.test.js +0 -14
- package/test/fastify-instance.test.js +4 -4
- package/test/fluent-schema.test.js +4 -4
- package/test/get.test.js +3 -3
- package/test/helper.js +18 -3
- package/test/hooks-async.test.js +14 -47
- package/test/hooks.on-ready.test.js +9 -4
- package/test/hooks.test.js +58 -99
- package/test/http2/closing.test.js +5 -11
- package/test/http2/unknown-http-method.test.js +3 -9
- package/test/https/custom-https-server.test.js +12 -6
- package/test/inject.test.js +1 -1
- package/test/input-validation.js +2 -2
- package/test/internals/all.test.js +2 -2
- package/test/internals/contentTypeParser.test.js +4 -4
- package/test/internals/handleRequest.test.js +9 -46
- package/test/internals/initialConfig.test.js +33 -12
- package/test/internals/logger.test.js +1 -1
- package/test/internals/reply.test.js +245 -3
- package/test/internals/request.test.js +13 -7
- package/test/internals/server.test.js +88 -0
- package/test/listen.test.js +84 -1
- package/test/logger.test.js +98 -58
- package/test/maxRequestsPerSocket.test.js +8 -6
- package/test/middleware.test.js +2 -25
- package/test/noop-set.test.js +19 -0
- package/test/nullable-validation.test.js +51 -14
- package/test/plugin.test.js +31 -5
- package/test/pretty-print.test.js +22 -10
- package/test/reply-error.test.js +123 -12
- package/test/request-error.test.js +2 -5
- package/test/route-hooks.test.js +17 -17
- package/test/route-prefix.test.js +2 -1
- package/test/route.test.js +216 -20
- package/test/router-options.test.js +1 -1
- package/test/schema-examples.test.js +11 -5
- package/test/schema-feature.test.js +24 -19
- package/test/schema-serialization.test.js +50 -9
- package/test/schema-special-usage.test.js +14 -81
- package/test/schema-validation.test.js +9 -9
- package/test/skip-reply-send.test.js +8 -8
- package/test/stream.test.js +23 -12
- package/test/throw.test.js +8 -5
- package/test/trust-proxy.test.js +1 -1
- package/test/type-provider.test.js +20 -0
- package/test/types/fastify.test-d.ts +12 -18
- package/test/types/hooks.test-d.ts +7 -3
- package/test/types/import.js +2 -0
- package/test/types/import.ts +1 -0
- package/test/types/instance.test-d.ts +61 -15
- package/test/types/logger.test-d.ts +44 -15
- package/test/types/route.test-d.ts +8 -2
- package/test/types/schema.test-d.ts +2 -39
- package/test/types/type-provider.test-d.ts +417 -0
- package/test/validation-error-handling.test.js +9 -9
- package/test/versioned-routes.test.js +29 -17
- package/test/wrapThenable.test.js +7 -6
- package/types/.eslintrc.json +1 -1
- package/types/content-type-parser.d.ts +17 -8
- package/types/hooks.d.ts +107 -60
- package/types/instance.d.ts +137 -105
- package/types/logger.d.ts +18 -104
- package/types/plugin.d.ts +10 -4
- package/types/register.d.ts +1 -1
- package/types/reply.d.ts +16 -11
- package/types/request.d.ts +10 -5
- package/types/route.d.ts +42 -31
- package/types/schema.d.ts +15 -1
- package/types/type-provider.d.ts +99 -0
- package/types/utils.d.ts +1 -1
- package/lib/schema-compilers.js +0 -12
- package/test/emit-warning.test.js +0 -166
package/types/logger.d.ts
CHANGED
|
@@ -1,127 +1,42 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Rationale for not directly importing types from @types/pino for use in fastify interfaces:
|
|
3
|
-
* - pino does not itself provide types so the types from @types must be used.
|
|
4
|
-
* - the types from @types are unofficial and the preference is to avoid using them or requiring them as a dependency of fastify.
|
|
5
|
-
* - the goal is to provide the minimum viable type definitions necessary to use fastify's official logger, pino.
|
|
6
|
-
* - the types provided should cover the basic use cases for the majority of fastify users while also being easy to maintain.
|
|
7
|
-
* - for advanced use cases needing the full set of types, users should be directed to manually install the unofficial types with
|
|
8
|
-
* `npm i -D @types/pino` and to supply their own logger instance as described at https://www.fastify.io/docs/latest/Logging/.
|
|
9
|
-
* - some fastify contributors have volunteered to maintain official types within pino (https://github.com/pinojs/pino/issues/910)
|
|
10
|
-
* in which case if the proposal is followed through with then in the future fastify will be able to directly import the full
|
|
11
|
-
* set of types rather than only duplicating and maintaining the subset chosen for providing a minimum viable logger api.
|
|
12
|
-
*
|
|
13
|
-
* Relevant discussions:
|
|
14
|
-
*
|
|
15
|
-
* https://github.com/fastify/fastify/pull/2550
|
|
16
|
-
* https://github.com/pinojs/pino/issues/910
|
|
17
|
-
* https://github.com/fastify/fastify/pull/1532
|
|
18
|
-
* https://github.com/fastify/fastify/issues/649
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
1
|
import { FastifyError } from 'fastify-error'
|
|
22
|
-
import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression } from './utils'
|
|
23
2
|
import { RouteGenericInterface } from './route'
|
|
24
3
|
import { FastifyRequest } from './request'
|
|
25
4
|
import { FastifyReply } from './reply'
|
|
5
|
+
import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
|
|
6
|
+
import { FastifyTypeProvider, FastifyTypeProviderDefault } from './type-provider'
|
|
7
|
+
import { FastifySchema } from './schema'
|
|
8
|
+
|
|
9
|
+
import pino from 'pino'
|
|
26
10
|
|
|
27
11
|
/**
|
|
28
12
|
* Standard Fastify logging function
|
|
29
13
|
*/
|
|
30
|
-
export
|
|
31
|
-
(msg: string, ...args: unknown[]): void;
|
|
32
|
-
(obj: unknown, msg?: string, ...args: unknown[]): void;
|
|
33
|
-
}
|
|
14
|
+
export type FastifyLogFn = pino.LogFn
|
|
34
15
|
|
|
35
|
-
export type LogLevel =
|
|
16
|
+
export type LogLevel = pino.Level
|
|
36
17
|
|
|
37
|
-
export type
|
|
18
|
+
export type Bindings = pino.Bindings
|
|
38
19
|
|
|
39
|
-
export
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
export interface FastifyLoggerInstance {
|
|
46
|
-
info: FastifyLogFn;
|
|
47
|
-
warn: FastifyLogFn;
|
|
48
|
-
error: FastifyLogFn;
|
|
49
|
-
fatal: FastifyLogFn;
|
|
50
|
-
trace: FastifyLogFn;
|
|
51
|
-
debug: FastifyLogFn;
|
|
52
|
-
child(bindings: Bindings): FastifyLoggerInstance;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// This interface is accurate for pino 6.3 and was copied from the following permalink:
|
|
56
|
-
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/72c9bd83316bd31e93ab86d64ddf598d922f33cd/types/pino/index.d.ts#L514-L567
|
|
57
|
-
export interface PrettyOptions {
|
|
58
|
-
/**
|
|
59
|
-
* Translate the epoch time value into a human readable date and time string.
|
|
60
|
-
* This flag also can set the format string to apply when translating the date to human readable format.
|
|
61
|
-
* The default format is yyyy-mm-dd HH:MM:ss.l o in UTC.
|
|
62
|
-
* For a list of available pattern letters see the {@link https://www.npmjs.com/package/dateformat|dateformat documentation}.
|
|
63
|
-
*/
|
|
64
|
-
translateTime?: boolean | string;
|
|
65
|
-
/**
|
|
66
|
-
* If set to true, it will print the name of the log level as the first field in the log line. Default: `false`.
|
|
67
|
-
*/
|
|
68
|
-
levelFirst?: boolean;
|
|
69
|
-
/**
|
|
70
|
-
* The key in the JSON object to use as the highlighted message. Default: "msg".
|
|
71
|
-
*/
|
|
72
|
-
messageKey?: string;
|
|
73
|
-
/**
|
|
74
|
-
* The key in the JSON object to use for timestamp display. Default: "time".
|
|
75
|
-
*/
|
|
76
|
-
timestampKey?: string;
|
|
77
|
-
/**
|
|
78
|
-
* Format output of message, e.g. {level} - {pid} will output message: INFO - 1123 Default: `false`.
|
|
79
|
-
*/
|
|
80
|
-
messageFormat?: false | string;
|
|
81
|
-
/**
|
|
82
|
-
* If set to true, will add color information to the formatted output message. Default: `false`.
|
|
83
|
-
*/
|
|
84
|
-
colorize?: boolean;
|
|
85
|
-
/**
|
|
86
|
-
* Appends carriage return and line feed, instead of just a line feed, to the formatted log line.
|
|
87
|
-
*/
|
|
88
|
-
crlf?: boolean;
|
|
89
|
-
/**
|
|
90
|
-
* Define the log keys that are associated with error like objects. Default: ["err", "error"]
|
|
91
|
-
*/
|
|
92
|
-
errorLikeObjectKeys?: string[];
|
|
93
|
-
/**
|
|
94
|
-
* When formatting an error object, display this list of properties.
|
|
95
|
-
* The list should be a comma separated list of properties. Default: ''
|
|
96
|
-
*/
|
|
97
|
-
errorProps?: string;
|
|
98
|
-
/**
|
|
99
|
-
* Specify a search pattern according to {@link http://jmespath.org|jmespath}
|
|
100
|
-
*/
|
|
101
|
-
search?: string;
|
|
102
|
-
/**
|
|
103
|
-
* Ignore one or several keys. Example: "time,hostname"
|
|
104
|
-
*/
|
|
105
|
-
ignore?: string;
|
|
106
|
-
/**
|
|
107
|
-
* Suppress warning on first synchronous flushing.
|
|
108
|
-
*/
|
|
109
|
-
suppressFlushSyncWarning?: boolean;
|
|
20
|
+
export type FastifyLoggerInstance = pino.Logger
|
|
21
|
+
// TODO make pino export BaseLogger again
|
|
22
|
+
// export type FastifyBaseLogger = pino.BaseLogger & {
|
|
23
|
+
export type FastifyBaseLogger = pino.Logger & {
|
|
24
|
+
child(bindings: Bindings): FastifyBaseLogger
|
|
110
25
|
}
|
|
111
26
|
|
|
112
27
|
export interface FastifyLoggerStreamDestination {
|
|
113
28
|
write(msg: string): void;
|
|
114
29
|
}
|
|
115
30
|
|
|
31
|
+
export type PinoLoggerOptions = pino.LoggerOptions
|
|
32
|
+
|
|
116
33
|
/**
|
|
117
|
-
* Fastify Custom Logger options.
|
|
118
|
-
* refer to this example:
|
|
119
|
-
* https://github.com/fastify/fastify/blob/2f56e10a24ecb70c2c7950bfffd60eda8f7782a6/docs/TypeScript.md#example-5-specifying-logger-types
|
|
34
|
+
* Fastify Custom Logger options.
|
|
120
35
|
*/
|
|
121
36
|
export interface FastifyLoggerOptions<
|
|
122
37
|
RawServer extends RawServerBase = RawServerDefault,
|
|
123
|
-
RawRequest extends FastifyRequest<RouteGenericInterface, RawServer, RawRequestDefaultExpression<RawServer
|
|
124
|
-
RawReply extends FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer
|
|
38
|
+
RawRequest extends FastifyRequest<RouteGenericInterface, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, FastifyTypeProvider> = FastifyRequest<RouteGenericInterface, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, FastifyTypeProviderDefault>,
|
|
39
|
+
RawReply extends FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, RouteGenericInterface, ContextConfigDefault, FastifySchema, FastifyTypeProvider> = FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, RouteGenericInterface, ContextConfigDefault, FastifySchema, FastifyTypeProviderDefault>,
|
|
125
40
|
> {
|
|
126
41
|
serializers?: {
|
|
127
42
|
req?: (req: RawRequest) => {
|
|
@@ -147,6 +62,5 @@ export interface FastifyLoggerOptions<
|
|
|
147
62
|
level?: string;
|
|
148
63
|
file?: string;
|
|
149
64
|
genReqId?: (req: RawRequest) => string;
|
|
150
|
-
prettyPrint?: boolean | PrettyOptions;
|
|
151
65
|
stream?: FastifyLoggerStreamDestination;
|
|
152
66
|
}
|
package/types/plugin.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { FastifyInstance } from './instance'
|
|
2
2
|
import { RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault } from './utils'
|
|
3
|
+
import { FastifyTypeProvider, FastifyTypeProviderDefault } from './type-provider'
|
|
4
|
+
import { FastifyLoggerInstance } from './logger'
|
|
3
5
|
|
|
4
6
|
export type FastifyPluginOptions = Record<string, any>
|
|
5
7
|
|
|
@@ -8,8 +10,8 @@ export type FastifyPluginOptions = Record<string, any>
|
|
|
8
10
|
*
|
|
9
11
|
* Fastify allows the user to extend its functionalities with plugins. A plugin can be a set of routes, a server decorator or whatever. To activate plugins, use the `fastify.register()` method.
|
|
10
12
|
*/
|
|
11
|
-
export type FastifyPluginCallback<Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault> = (
|
|
12
|
-
instance: FastifyInstance<Server, RawRequestDefaultExpression<Server>, RawReplyDefaultExpression<Server
|
|
13
|
+
export type FastifyPluginCallback<Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault> = (
|
|
14
|
+
instance: FastifyInstance<Server, RawRequestDefaultExpression<Server>, RawReplyDefaultExpression<Server>, FastifyLoggerInstance, TypeProvider>,
|
|
13
15
|
opts: Options,
|
|
14
16
|
done: (err?: Error) => void
|
|
15
17
|
) => void
|
|
@@ -19,8 +21,12 @@ export type FastifyPluginCallback<Options extends FastifyPluginOptions = Record<
|
|
|
19
21
|
*
|
|
20
22
|
* Fastify allows the user to extend its functionalities with plugins. A plugin can be a set of routes, a server decorator or whatever. To activate plugins, use the `fastify.register()` method.
|
|
21
23
|
*/
|
|
22
|
-
export type FastifyPluginAsync<
|
|
23
|
-
|
|
24
|
+
export type FastifyPluginAsync<
|
|
25
|
+
Options extends FastifyPluginOptions = Record<never, never>,
|
|
26
|
+
Server extends RawServerBase = RawServerDefault,
|
|
27
|
+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
28
|
+
> = (
|
|
29
|
+
instance: FastifyInstance<Server, RawRequestDefaultExpression<Server>, RawReplyDefaultExpression<Server>, FastifyLoggerInstance, TypeProvider>,
|
|
24
30
|
opts: Options
|
|
25
31
|
) => Promise<void>;
|
|
26
32
|
|
package/types/register.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { FastifyPluginOptions, FastifyPluginCallback, FastifyPluginAsync } from
|
|
|
2
2
|
import { LogLevel } from './logger'
|
|
3
3
|
import { FastifyInstance } from './instance'
|
|
4
4
|
|
|
5
|
-
interface RegisterOptions {
|
|
5
|
+
export interface RegisterOptions {
|
|
6
6
|
prefix?: string;
|
|
7
7
|
logLevel?: LogLevel;
|
|
8
8
|
logSerializers?: Record<string, (value: any) => string>;
|
package/types/reply.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { RawReplyDefaultExpression, RawServerBase, RawServerDefault, ContextConfigDefault, RawRequestDefaultExpression, ReplyDefault } from './utils'
|
|
2
|
+
import { FastifyReplyType, ResolveFastifyReplyType, FastifyTypeProvider, FastifyTypeProviderDefault } from './type-provider'
|
|
2
3
|
import { FastifyContext } from './context'
|
|
3
4
|
import { FastifyLoggerInstance } from './logger'
|
|
4
5
|
import { FastifyRequest } from './request'
|
|
5
6
|
import { RouteGenericInterface } from './route'
|
|
6
7
|
import { FastifyInstance } from './instance'
|
|
8
|
+
import { FastifySchema } from './schema'
|
|
7
9
|
|
|
8
10
|
export interface ReplyGenericInterface {
|
|
9
11
|
Reply?: ReplyDefault;
|
|
@@ -19,19 +21,22 @@ export interface FastifyReply<
|
|
|
19
21
|
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
|
|
20
22
|
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
|
|
21
23
|
ContextConfig = ContextConfigDefault,
|
|
24
|
+
SchemaCompiler extends FastifySchema = FastifySchema,
|
|
25
|
+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
26
|
+
ReplyType extends FastifyReplyType = ResolveFastifyReplyType<TypeProvider, SchemaCompiler, RouteGeneric>
|
|
22
27
|
> {
|
|
23
28
|
raw: RawReply;
|
|
24
29
|
context: FastifyContext<ContextConfig>;
|
|
25
30
|
log: FastifyLoggerInstance;
|
|
26
|
-
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>;
|
|
31
|
+
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>;
|
|
27
32
|
server: FastifyInstance;
|
|
28
|
-
code(statusCode: number): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
|
|
29
|
-
status(statusCode: number): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
|
|
33
|
+
code(statusCode: number): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
|
34
|
+
status(statusCode: number): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
|
30
35
|
statusCode: number;
|
|
31
36
|
sent: boolean;
|
|
32
|
-
send(payload?:
|
|
33
|
-
header(key: string, value: any): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
|
|
34
|
-
headers(values: {[key: string]: any}): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
|
|
37
|
+
send(payload?: ReplyType): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
|
38
|
+
header(key: string, value: any): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
|
39
|
+
headers(values: {[key: string]: any}): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
|
35
40
|
getHeader(key: string): string | undefined;
|
|
36
41
|
getHeaders(): {
|
|
37
42
|
// Node's `getHeaders()` can return numbers and arrays, so they're included here as possible types.
|
|
@@ -40,13 +45,13 @@ export interface FastifyReply<
|
|
|
40
45
|
removeHeader(key: string): void;
|
|
41
46
|
hasHeader(key: string): boolean;
|
|
42
47
|
// Note: should consider refactoring the argument order for redirect. statusCode is optional so it should be after the required url param
|
|
43
|
-
redirect(statusCode: number, url: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
|
|
44
|
-
redirect(url: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
|
|
45
|
-
hijack(): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
|
|
48
|
+
redirect(statusCode: number, url: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
|
49
|
+
redirect(url: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
|
50
|
+
hijack(): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
|
46
51
|
callNotFound(): void;
|
|
47
52
|
getResponseTime(): number;
|
|
48
|
-
type(contentType: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
|
|
49
|
-
serializer(fn: (payload: any) => string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
|
|
53
|
+
type(contentType: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
|
54
|
+
serializer(fn: (payload: any) => string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
|
50
55
|
serialize(payload: any): string;
|
|
51
56
|
then(fulfilled: () => void, rejected: (err: Error) => void): void;
|
|
52
57
|
}
|
package/types/request.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { FastifyLoggerInstance } from './logger'
|
|
|
2
2
|
import { ContextConfigDefault, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault } from './utils'
|
|
3
3
|
import { RouteGenericInterface } from './route'
|
|
4
4
|
import { FastifyInstance } from './instance'
|
|
5
|
+
import { FastifyTypeProvider, FastifyTypeProviderDefault, FastifyRequestType, ResolveFastifyRequestType } from './type-provider'
|
|
6
|
+
import { FastifySchema } from './schema'
|
|
5
7
|
import { FastifyContext } from './context'
|
|
6
8
|
|
|
7
9
|
export interface RequestGenericInterface {
|
|
@@ -19,16 +21,19 @@ export interface FastifyRequest<
|
|
|
19
21
|
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
|
|
20
22
|
RawServer extends RawServerBase = RawServerDefault,
|
|
21
23
|
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
|
|
24
|
+
SchemaCompiler extends FastifySchema = FastifySchema,
|
|
25
|
+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
22
26
|
ContextConfig = ContextConfigDefault,
|
|
27
|
+
RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>
|
|
23
28
|
> {
|
|
24
29
|
id: any;
|
|
25
|
-
params:
|
|
30
|
+
params: RequestType['params'];
|
|
26
31
|
raw: RawRequest;
|
|
27
|
-
query:
|
|
28
|
-
headers: RawRequest['headers'] &
|
|
32
|
+
query: RequestType['query'];
|
|
33
|
+
headers: RawRequest['headers'] & RequestType['headers']; // this enables the developer to extend the existing http(s|2) headers list
|
|
29
34
|
log: FastifyLoggerInstance;
|
|
30
35
|
server: FastifyInstance;
|
|
31
|
-
body:
|
|
36
|
+
body: RequestType['body'];
|
|
32
37
|
context: FastifyContext<ContextConfig>;
|
|
33
38
|
|
|
34
39
|
/** in order for this to be used the user should ensure they have set the attachValidation option. */
|
|
@@ -37,7 +42,7 @@ export interface FastifyRequest<
|
|
|
37
42
|
/**
|
|
38
43
|
* @deprecated Use `raw` property
|
|
39
44
|
*/
|
|
40
|
-
readonly req: RawRequest;
|
|
45
|
+
readonly req: RawRequest & RouteGeneric['Headers']; // this enables the developer to extend the existing http(s|2) headers list
|
|
41
46
|
readonly ip: string;
|
|
42
47
|
readonly ips?: string[];
|
|
43
48
|
readonly hostname: string;
|
package/types/route.d.ts
CHANGED
|
@@ -3,10 +3,11 @@ import { FastifyRequest, RequestGenericInterface } from './request'
|
|
|
3
3
|
import { FastifyReply, ReplyGenericInterface } from './reply'
|
|
4
4
|
import { FastifySchema, FastifySchemaCompiler, FastifySchemaValidationError, FastifySerializerCompiler } from './schema'
|
|
5
5
|
import { HTTPMethods, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
|
|
6
|
-
import { LogLevel } from './logger'
|
|
7
6
|
import { preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onRequestHookHandler, preParsingHookHandler, onResponseHookHandler, onSendHookHandler, onErrorHookHandler, onTimeoutHookHandler } from './hooks'
|
|
8
7
|
import { FastifyError } from 'fastify-error'
|
|
9
8
|
import { FastifyContext } from './context'
|
|
9
|
+
import { FastifyTypeProvider, FastifyTypeProviderDefault, ResolveFastifyReplyReturnType } from './type-provider'
|
|
10
|
+
import { FastifyLoggerInstance, LogLevel } from './logger'
|
|
10
11
|
|
|
11
12
|
export interface RouteGenericInterface extends RequestGenericInterface, ReplyGenericInterface {}
|
|
12
13
|
|
|
@@ -20,10 +21,12 @@ export interface RouteShorthandOptions<
|
|
|
20
21
|
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
|
|
21
22
|
ContextConfig = ContextConfigDefault,
|
|
22
23
|
SchemaCompiler = FastifySchema,
|
|
24
|
+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
23
25
|
> {
|
|
24
|
-
schema?: FastifySchema
|
|
26
|
+
schema?: SchemaCompiler, // originally FastifySchema
|
|
25
27
|
attachValidation?: boolean;
|
|
26
28
|
exposeHeadRoute?: boolean;
|
|
29
|
+
|
|
27
30
|
validatorCompiler?: FastifySchemaCompiler<SchemaCompiler>;
|
|
28
31
|
serializerCompiler?: FastifySerializerCompiler<SchemaCompiler>;
|
|
29
32
|
bodyLimit?: number;
|
|
@@ -37,15 +40,15 @@ export interface RouteShorthandOptions<
|
|
|
37
40
|
schemaErrorFormatter?: (errors: FastifySchemaValidationError[], dataVar: string) => Error;
|
|
38
41
|
|
|
39
42
|
// hooks
|
|
40
|
-
onRequest?: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
|
|
41
|
-
preParsing?: preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
|
|
42
|
-
preValidation?: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
|
|
43
|
-
preHandler?: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
|
|
44
|
-
preSerialization?: preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
|
|
45
|
-
onSend?: onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
|
|
46
|
-
onResponse?: onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
|
|
47
|
-
onTimeout?: onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
|
|
48
|
-
onError?: onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
|
|
43
|
+
onRequest?: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> | onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>[];
|
|
44
|
+
preParsing?: preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> | preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>[];
|
|
45
|
+
preValidation?: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> | preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>[];
|
|
46
|
+
preHandler?: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> | preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>[];
|
|
47
|
+
preSerialization?: preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> | preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>[];
|
|
48
|
+
onSend?: onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> | onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>[];
|
|
49
|
+
onResponse?: onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> | onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>[];
|
|
50
|
+
onTimeout?: onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> | onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>[];
|
|
51
|
+
onError?: onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider> | onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider>[];
|
|
49
52
|
}
|
|
50
53
|
|
|
51
54
|
/**
|
|
@@ -56,12 +59,15 @@ export type RouteHandlerMethod<
|
|
|
56
59
|
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
|
|
57
60
|
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
|
|
58
61
|
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
|
|
59
|
-
ContextConfig = ContextConfigDefault
|
|
62
|
+
ContextConfig = ContextConfigDefault,
|
|
63
|
+
SchemaCompiler extends FastifySchema = FastifySchema,
|
|
64
|
+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
65
|
+
ReturnType = ResolveFastifyReplyReturnType<TypeProvider, SchemaCompiler, RouteGeneric>
|
|
60
66
|
> = (
|
|
61
|
-
this: FastifyInstance<RawServer, RawRequest, RawReply>,
|
|
62
|
-
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
|
|
63
|
-
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
|
|
64
|
-
) =>
|
|
67
|
+
this: FastifyInstance<RawServer, RawRequest, RawReply, FastifyLoggerInstance, TypeProvider>,
|
|
68
|
+
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
|
|
69
|
+
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
|
|
70
|
+
) => ReturnType
|
|
65
71
|
|
|
66
72
|
/**
|
|
67
73
|
* Shorthand options including the handler function property
|
|
@@ -73,8 +79,9 @@ export interface RouteShorthandOptionsWithHandler<
|
|
|
73
79
|
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
|
|
74
80
|
ContextConfig = ContextConfigDefault,
|
|
75
81
|
SchemaCompiler = FastifySchema,
|
|
76
|
-
|
|
77
|
-
|
|
82
|
+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
83
|
+
> extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> {
|
|
84
|
+
handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
|
78
85
|
}
|
|
79
86
|
|
|
80
87
|
/**
|
|
@@ -84,20 +91,21 @@ export interface RouteShorthandMethod<
|
|
|
84
91
|
RawServer extends RawServerBase = RawServerDefault,
|
|
85
92
|
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
|
|
86
93
|
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
|
|
94
|
+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
87
95
|
> {
|
|
88
96
|
<RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = FastifySchema>(
|
|
89
97
|
path: string,
|
|
90
|
-
opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler>,
|
|
91
|
-
handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
|
|
92
|
-
): FastifyInstance<RawServer, RawRequest, RawReply>;
|
|
98
|
+
opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
|
|
99
|
+
handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
|
|
100
|
+
): FastifyInstance<RawServer, RawRequest, RawReply, FastifyLoggerInstance, TypeProvider>;
|
|
93
101
|
<RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault>(
|
|
94
102
|
path: string,
|
|
95
|
-
handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
|
|
96
|
-
): FastifyInstance<RawServer, RawRequest, RawReply>;
|
|
103
|
+
handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifySchema, TypeProvider>
|
|
104
|
+
): FastifyInstance<RawServer, RawRequest, RawReply, FastifyLoggerInstance, TypeProvider>;
|
|
97
105
|
<RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = FastifySchema>(
|
|
98
106
|
path: string,
|
|
99
|
-
opts: RouteShorthandOptionsWithHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler>
|
|
100
|
-
): FastifyInstance<RawServer, RawRequest, RawReply>;
|
|
107
|
+
opts: RouteShorthandOptionsWithHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
|
|
108
|
+
): FastifyInstance<RawServer, RawRequest, RawReply, FastifyLoggerInstance, TypeProvider>;
|
|
101
109
|
}
|
|
102
110
|
|
|
103
111
|
/**
|
|
@@ -110,10 +118,11 @@ export interface RouteOptions<
|
|
|
110
118
|
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
|
|
111
119
|
ContextConfig = ContextConfigDefault,
|
|
112
120
|
SchemaCompiler = FastifySchema,
|
|
113
|
-
|
|
121
|
+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
122
|
+
> extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> {
|
|
114
123
|
method: HTTPMethods | HTTPMethods[];
|
|
115
124
|
url: string;
|
|
116
|
-
handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
|
|
125
|
+
handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
|
117
126
|
}
|
|
118
127
|
|
|
119
128
|
export type RouteHandler<
|
|
@@ -121,11 +130,13 @@ export type RouteHandler<
|
|
|
121
130
|
RawServer extends RawServerBase = RawServerDefault,
|
|
122
131
|
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
|
|
123
132
|
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
|
|
124
|
-
ContextConfig = ContextConfigDefault
|
|
133
|
+
ContextConfig = ContextConfigDefault,
|
|
134
|
+
SchemaCompiler extends FastifySchema = FastifySchema,
|
|
135
|
+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
125
136
|
> = (
|
|
126
|
-
this: FastifyInstance<RawServer, RawRequest, RawReply>,
|
|
127
|
-
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
|
|
128
|
-
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
|
|
137
|
+
this: FastifyInstance<RawServer, RawRequest, RawReply, FastifyLoggerInstance, TypeProvider>,
|
|
138
|
+
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
|
|
139
|
+
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
|
|
129
140
|
) => void | Promise<RouteGeneric['Reply'] | void>
|
|
130
141
|
|
|
131
142
|
export type DefaultRoute<Request, Reply> = (
|
package/types/schema.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ValidatorCompiler } from '@fastify/ajv-compiler'
|
|
2
|
+
import { FastifyInstance, FastifyServerOptions } from '../fastify'
|
|
1
3
|
/**
|
|
2
4
|
* Schemas in Fastify follow the JSON-Schema standard. For this reason
|
|
3
5
|
* we have opted to not ship strict schema based types. Instead we provide
|
|
@@ -22,7 +24,7 @@ export interface FastifyRouteSchemaDef<T> {
|
|
|
22
24
|
|
|
23
25
|
export interface FastifySchemaValidationError {
|
|
24
26
|
message?: string;
|
|
25
|
-
|
|
27
|
+
instancePath: string;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
export interface FastifyValidationResult {
|
|
@@ -36,3 +38,15 @@ export interface FastifyValidationResult {
|
|
|
36
38
|
export type FastifySchemaCompiler<T> = (routeSchema: FastifyRouteSchemaDef<T>) => FastifyValidationResult
|
|
37
39
|
|
|
38
40
|
export type FastifySerializerCompiler<T> = (routeSchema: FastifyRouteSchemaDef<T>) => (data: any) => string
|
|
41
|
+
|
|
42
|
+
export interface FastifySchemaControllerOptions{
|
|
43
|
+
bucket?: (parentSchemas?: unknown) => {
|
|
44
|
+
addSchema(schema: unknown): FastifyInstance;
|
|
45
|
+
getSchema(schemaId: string): unknown;
|
|
46
|
+
getSchemas(): Record<string, unknown>;
|
|
47
|
+
};
|
|
48
|
+
compilersFactory?: {
|
|
49
|
+
buildValidator?: ValidatorCompiler;
|
|
50
|
+
buildSerializer?: (externalSchemas: unknown, serializerOptsServerOption: FastifyServerOptions['serializerOpts']) => FastifySerializerCompiler<unknown>;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
|
|
2
|
+
import { RouteGenericInterface } from './route'
|
|
3
|
+
import { FastifySchema } from './schema'
|
|
4
|
+
|
|
5
|
+
// -----------------------------------------------------------------------------------------------
|
|
6
|
+
// TypeProvider
|
|
7
|
+
// -----------------------------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
export interface FastifyTypeProvider {
|
|
10
|
+
readonly input: unknown,
|
|
11
|
+
readonly output: unknown,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface FastifyTypeProviderDefault extends FastifyTypeProvider {
|
|
15
|
+
output: unknown
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type CallTypeProvider<F extends FastifyTypeProvider, I> = (F & { input: I })['output']
|
|
19
|
+
|
|
20
|
+
// -----------------------------------------------------------------------------------------------
|
|
21
|
+
// FastifyRequestType
|
|
22
|
+
// -----------------------------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
// Used to map undefined SchemaCompiler properties to unknown
|
|
25
|
+
type UndefinedToUnknown<T> = T extends undefined ? unknown : T
|
|
26
|
+
|
|
27
|
+
// Resolves Request types either from generic argument or Type Provider.
|
|
28
|
+
type ResolveRequestParams<TypeProvider extends FastifyTypeProvider, SchemaCompiler extends FastifySchema, RouteGeneric extends RouteGenericInterface> =
|
|
29
|
+
UndefinedToUnknown<keyof RouteGeneric['Params'] extends never ? CallTypeProvider<TypeProvider, SchemaCompiler['params']> : RouteGeneric['Params']>
|
|
30
|
+
type ResolveRequestQuerystring<TypeProvider extends FastifyTypeProvider, SchemaCompiler extends FastifySchema, RouteGeneric extends RouteGenericInterface> =
|
|
31
|
+
UndefinedToUnknown<keyof RouteGeneric['Querystring'] extends never ? CallTypeProvider<TypeProvider, SchemaCompiler['querystring']> : RouteGeneric['Querystring']>
|
|
32
|
+
type ResolveRequestHeaders<TypeProvider extends FastifyTypeProvider, SchemaCompiler extends FastifySchema, RouteGeneric extends RouteGenericInterface> =
|
|
33
|
+
UndefinedToUnknown<keyof RouteGeneric['Headers'] extends never ? CallTypeProvider<TypeProvider, SchemaCompiler['headers']> : RouteGeneric['Headers']>
|
|
34
|
+
type ResolveRequestBody<TypeProvider extends FastifyTypeProvider, SchemaCompiler extends FastifySchema, RouteGeneric extends RouteGenericInterface> =
|
|
35
|
+
UndefinedToUnknown<keyof RouteGeneric['Body'] extends never ? CallTypeProvider<TypeProvider, SchemaCompiler['body']> : RouteGeneric['Body']>
|
|
36
|
+
|
|
37
|
+
// The target request type. This type is inferenced on fastify 'requests' via generic argument assignment
|
|
38
|
+
export interface FastifyRequestType<Params = unknown, Querystring = unknown, Headers = unknown, Body = unknown> {
|
|
39
|
+
params: Params,
|
|
40
|
+
query: Querystring,
|
|
41
|
+
headers: Headers,
|
|
42
|
+
body: Body
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type ResolveFastifyRequestType<TypeProvider extends FastifyTypeProvider, SchemaCompiler extends FastifySchema, RouteGeneric extends RouteGenericInterface> = FastifyRequestType<
|
|
46
|
+
ResolveRequestParams<TypeProvider, SchemaCompiler, RouteGeneric>,
|
|
47
|
+
ResolveRequestQuerystring<TypeProvider, SchemaCompiler, RouteGeneric>,
|
|
48
|
+
ResolveRequestHeaders<TypeProvider, SchemaCompiler, RouteGeneric>,
|
|
49
|
+
ResolveRequestBody<TypeProvider, SchemaCompiler, RouteGeneric>
|
|
50
|
+
>
|
|
51
|
+
|
|
52
|
+
// -----------------------------------------------------------------------------------------------
|
|
53
|
+
// FastifyReplyType
|
|
54
|
+
// -----------------------------------------------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
// Tests if the user has specified a generic argument for Reply
|
|
57
|
+
type UseReplyFromRouteGeneric<RouteGeneric extends RouteGenericInterface> = keyof RouteGeneric['Reply'] extends never ? false : true
|
|
58
|
+
|
|
59
|
+
// Tests if the user has specified a response schema.
|
|
60
|
+
type UseReplyFromSchemaCompiler<SchemaCompiler extends FastifySchema> = keyof SchemaCompiler['response'] extends never ? false : true
|
|
61
|
+
|
|
62
|
+
// Resolves the Reply type from the generic argument
|
|
63
|
+
type ResolveReplyFromRouteGeneric<RouteGeneric extends RouteGenericInterface> = RouteGeneric['Reply']
|
|
64
|
+
|
|
65
|
+
// Resolves the Reply type by taking a union of response status codes
|
|
66
|
+
type ResolveReplyFromSchemaCompiler<TypeProvider extends FastifyTypeProvider, SchemaCompiler extends FastifySchema> = {
|
|
67
|
+
[K in keyof SchemaCompiler['response']]: CallTypeProvider<TypeProvider, SchemaCompiler['response'][K]>
|
|
68
|
+
} extends infer Result ? Result[keyof Result] : unknown
|
|
69
|
+
|
|
70
|
+
// The target reply type. This type is inferenced on fastify 'replies' via generic argument assignment
|
|
71
|
+
export type FastifyReplyType<Reply = unknown> = Reply
|
|
72
|
+
|
|
73
|
+
// Resolves the Reply type either via generic argument or from response schema. This type uses a different
|
|
74
|
+
// resolution strategy to Requests where the Reply will infer a union of each status code type specified
|
|
75
|
+
// by the user. The Reply can be explicitly overriden by users providing a generic Reply type on the route.
|
|
76
|
+
export type ResolveFastifyReplyType<TypeProvider extends FastifyTypeProvider, SchemaCompiler extends FastifySchema, RouteGeneric extends RouteGenericInterface> = FastifyReplyType<
|
|
77
|
+
UseReplyFromRouteGeneric<RouteGeneric> extends true ? ResolveReplyFromRouteGeneric<RouteGeneric> :
|
|
78
|
+
UseReplyFromSchemaCompiler<SchemaCompiler> extends true ? ResolveReplyFromSchemaCompiler<TypeProvider, SchemaCompiler> :
|
|
79
|
+
unknown
|
|
80
|
+
>
|
|
81
|
+
|
|
82
|
+
// -----------------------------------------------------------------------------------------------
|
|
83
|
+
// FastifyReplyReturnType
|
|
84
|
+
// -----------------------------------------------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
// The target reply return type. This type is inferenced on fastify 'routes' via generic argument assignment
|
|
87
|
+
export type ResolveFastifyReplyReturnType<
|
|
88
|
+
TypeProvider extends FastifyTypeProvider,
|
|
89
|
+
SchemaCompiler extends FastifySchema,
|
|
90
|
+
RouteGeneric extends RouteGenericInterface,
|
|
91
|
+
> = ResolveFastifyReplyType<
|
|
92
|
+
TypeProvider,
|
|
93
|
+
SchemaCompiler,
|
|
94
|
+
RouteGeneric
|
|
95
|
+
> extends infer Return ?
|
|
96
|
+
(void | Promise<Return | void>)
|
|
97
|
+
// review: support both async and sync return types
|
|
98
|
+
// (Promise<Return> | Return | Promise<void> | void)
|
|
99
|
+
: unknown
|
package/types/utils.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export type RawServerDefault = http.Server
|
|
|
21
21
|
* The default request type based on the server type. Utilizes generic constraining.
|
|
22
22
|
*/
|
|
23
23
|
export type RawRequestDefaultExpression<
|
|
24
|
-
RawServer extends RawServerBase = RawServerDefault
|
|
24
|
+
RawServer extends RawServerBase = RawServerDefault,
|
|
25
25
|
> = RawServer extends http.Server | https.Server ? http.IncomingMessage
|
|
26
26
|
: RawServer extends http2.Http2Server | http2.Http2SecureServer ? http2.Http2ServerRequest
|
|
27
27
|
: never
|
package/lib/schema-compilers.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const fastJsonStringify = require('fast-json-stringify')
|
|
4
|
-
|
|
5
|
-
function serializerFactory (externalSchemas, serializerOpts) {
|
|
6
|
-
const opts = Object.assign({}, serializerOpts, { schema: externalSchemas })
|
|
7
|
-
return function responseSchemaCompiler ({ schema /* method, url, httpStatus */ }) {
|
|
8
|
-
return fastJsonStringify(schema, opts)
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
module.exports.serializerCompiler = serializerFactory
|