fastify 3.27.4 → 4.0.0-alpha.3

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.
Files changed (168) hide show
  1. package/.taprc +3 -0
  2. package/README.md +7 -7
  3. package/build/build-error-serializer.js +27 -0
  4. package/build/build-validation.js +47 -35
  5. package/docs/Guides/Database.md +320 -0
  6. package/docs/Guides/Ecosystem.md +9 -0
  7. package/docs/Guides/Getting-Started.md +7 -7
  8. package/docs/Guides/Plugins-Guide.md +1 -1
  9. package/docs/Guides/Serverless.md +3 -3
  10. package/docs/Guides/Testing.md +2 -2
  11. package/docs/Migration-Guide-V4.md +12 -0
  12. package/docs/Reference/ContentTypeParser.md +4 -0
  13. package/docs/Reference/Decorators.md +2 -2
  14. package/docs/Reference/Encapsulation.md +2 -2
  15. package/docs/Reference/Errors.md +51 -6
  16. package/docs/Reference/HTTP2.md +3 -3
  17. package/docs/Reference/Hooks.md +4 -7
  18. package/docs/Reference/LTS.md +5 -4
  19. package/docs/Reference/Plugins.md +3 -3
  20. package/docs/Reference/Reply.md +73 -22
  21. package/docs/Reference/Request.md +1 -3
  22. package/docs/Reference/Routes.md +22 -15
  23. package/docs/Reference/Server.md +69 -119
  24. package/docs/Reference/TypeScript.md +20 -22
  25. package/docs/Reference/Validation-and-Serialization.md +30 -55
  26. package/docs/Type-Providers.md +257 -0
  27. package/examples/asyncawait.js +1 -1
  28. package/examples/benchmark/hooks-benchmark-async-await.js +1 -1
  29. package/examples/benchmark/hooks-benchmark.js +1 -1
  30. package/examples/benchmark/simple.js +1 -1
  31. package/examples/hooks.js +2 -2
  32. package/examples/http2.js +1 -1
  33. package/examples/https.js +1 -1
  34. package/examples/parser.js +13 -3
  35. package/examples/route-prefix.js +1 -1
  36. package/examples/shared-schema.js +1 -1
  37. package/examples/simple-stream.js +18 -0
  38. package/examples/simple.js +1 -1
  39. package/examples/simple.mjs +1 -1
  40. package/examples/typescript-server.ts +1 -1
  41. package/examples/use-plugin.js +1 -1
  42. package/fastify.d.ts +34 -22
  43. package/fastify.js +40 -36
  44. package/lib/configValidator.js +902 -1023
  45. package/lib/contentTypeParser.js +6 -16
  46. package/lib/context.js +36 -10
  47. package/lib/decorate.js +3 -1
  48. package/lib/error-handler.js +158 -0
  49. package/lib/error-serializer.js +257 -0
  50. package/lib/errors.js +51 -9
  51. package/lib/fourOhFour.js +31 -20
  52. package/lib/handleRequest.js +10 -13
  53. package/lib/hooks.js +14 -9
  54. package/lib/pluginOverride.js +0 -3
  55. package/lib/pluginUtils.js +3 -2
  56. package/lib/reply.js +121 -175
  57. package/lib/request.js +13 -10
  58. package/lib/route.js +131 -138
  59. package/lib/schema-controller.js +2 -2
  60. package/lib/schemas.js +27 -1
  61. package/lib/server.js +242 -116
  62. package/lib/symbols.js +5 -3
  63. package/lib/validation.js +11 -9
  64. package/lib/warnings.js +4 -12
  65. package/lib/wrapThenable.js +4 -11
  66. package/package.json +37 -39
  67. package/test/404s.test.js +258 -125
  68. package/test/500s.test.js +3 -3
  69. package/test/als.test.js +1 -1
  70. package/test/async-await.test.js +20 -76
  71. package/test/bodyLimit.test.js +1 -1
  72. package/test/build-certificate.js +6 -7
  73. package/test/case-insensitive.test.js +4 -4
  74. package/test/close-pipelining.test.js +2 -2
  75. package/test/close.test.js +11 -11
  76. package/test/content-parser.test.js +32 -0
  77. package/test/context-config.test.js +52 -0
  78. package/test/custom-http-server.test.js +14 -7
  79. package/test/custom-parser-async.test.js +1 -66
  80. package/test/custom-parser.test.js +92 -159
  81. package/test/custom-querystring-parser.test.js +3 -3
  82. package/test/decorator.test.js +11 -13
  83. package/test/delete.test.js +6 -6
  84. package/test/encapsulated-error-handler.test.js +50 -0
  85. package/test/esm/index.test.js +0 -14
  86. package/test/fastify-instance.test.js +4 -4
  87. package/test/fluent-schema.test.js +4 -4
  88. package/test/genReqId.test.js +1 -1
  89. package/test/get.test.js +4 -4
  90. package/test/handler-context.test.js +2 -2
  91. package/test/head.test.js +1 -1
  92. package/test/helper.js +19 -4
  93. package/test/hooks-async.test.js +15 -48
  94. package/test/hooks.on-ready.test.js +10 -5
  95. package/test/hooks.test.js +78 -119
  96. package/test/http2/closing.test.js +10 -16
  97. package/test/http2/constraint.test.js +1 -1
  98. package/test/http2/head.test.js +1 -1
  99. package/test/http2/plain.test.js +1 -1
  100. package/test/http2/secure-with-fallback.test.js +1 -1
  101. package/test/http2/secure.test.js +1 -1
  102. package/test/http2/unknown-http-method.test.js +4 -10
  103. package/test/https/custom-https-server.test.js +12 -6
  104. package/test/https/https.test.js +1 -1
  105. package/test/input-validation.js +3 -3
  106. package/test/internals/handleRequest.test.js +6 -43
  107. package/test/internals/initialConfig.test.js +41 -12
  108. package/test/internals/logger.test.js +2 -2
  109. package/test/internals/reply.test.js +317 -48
  110. package/test/internals/request.test.js +13 -7
  111. package/test/internals/server.test.js +88 -0
  112. package/test/listen.deprecated.test.js +202 -0
  113. package/test/listen.test.js +140 -145
  114. package/test/logger.test.js +82 -42
  115. package/test/maxRequestsPerSocket.test.js +8 -6
  116. package/test/middleware.test.js +2 -25
  117. package/test/nullable-validation.test.js +53 -16
  118. package/test/output-validation.test.js +1 -1
  119. package/test/plugin.test.js +47 -21
  120. package/test/pretty-print.test.js +22 -10
  121. package/test/promises.test.js +1 -1
  122. package/test/proto-poisoning.test.js +6 -6
  123. package/test/register.test.js +3 -3
  124. package/test/reply-error.test.js +126 -15
  125. package/test/reply-trailers.test.js +270 -0
  126. package/test/request-error.test.js +3 -6
  127. package/test/route-hooks.test.js +18 -18
  128. package/test/route-prefix.test.js +2 -1
  129. package/test/route.test.js +206 -22
  130. package/test/router-options.test.js +2 -2
  131. package/test/schema-examples.test.js +11 -5
  132. package/test/schema-feature.test.js +25 -20
  133. package/test/schema-serialization.test.js +9 -9
  134. package/test/schema-special-usage.test.js +5 -153
  135. package/test/schema-validation.test.js +9 -9
  136. package/test/skip-reply-send.test.js +2 -2
  137. package/test/stream.test.js +82 -23
  138. package/test/throw.test.js +8 -5
  139. package/test/trust-proxy.test.js +6 -6
  140. package/test/type-provider.test.js +20 -0
  141. package/test/types/fastify.test-d.ts +10 -18
  142. package/test/types/hooks.test-d.ts +61 -5
  143. package/test/types/import.js +2 -0
  144. package/test/types/import.ts +1 -0
  145. package/test/types/instance.test-d.ts +68 -17
  146. package/test/types/logger.test-d.ts +44 -15
  147. package/test/types/reply.test-d.ts +2 -1
  148. package/test/types/request.test-d.ts +71 -1
  149. package/test/types/route.test-d.ts +8 -2
  150. package/test/types/schema.test-d.ts +2 -39
  151. package/test/types/type-provider.test-d.ts +424 -0
  152. package/test/url-rewriting.test.js +3 -3
  153. package/test/validation-error-handling.test.js +8 -8
  154. package/test/versioned-routes.test.js +30 -18
  155. package/test/wrapThenable.test.js +7 -6
  156. package/types/content-type-parser.d.ts +17 -8
  157. package/types/hooks.d.ts +182 -85
  158. package/types/instance.d.ts +286 -118
  159. package/types/logger.d.ts +18 -104
  160. package/types/plugin.d.ts +10 -4
  161. package/types/reply.d.ts +18 -12
  162. package/types/request.d.ts +13 -8
  163. package/types/route.d.ts +62 -34
  164. package/types/schema.d.ts +1 -1
  165. package/types/type-provider.d.ts +99 -0
  166. package/types/utils.d.ts +1 -1
  167. package/lib/schema-compilers.js +0 -12
  168. 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 interface FastifyLogFn {
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 = 'info' | 'error' | 'debug' | 'fatal' | 'warn' | 'trace'
16
+ export type LogLevel = pino.Level
36
17
 
37
- export type SerializerFn = (value: unknown) => unknown;
18
+ export type Bindings = pino.Bindings
38
19
 
39
- export interface Bindings {
40
- level?: LogLevel | string;
41
- serializers?: { [key: string]: SerializerFn };
42
- [key: string]: unknown;
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. To enable configuration of all Pino 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>> = FastifyRequest<RouteGenericInterface, RawServer, RawRequestDefaultExpression<RawServer>>,
124
- RawReply extends FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>> = 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<Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault> = (
23
- instance: FastifyInstance<Server, RawRequestDefaultExpression<Server>, RawReplyDefaultExpression<Server>>,
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/reply.d.ts CHANGED
@@ -1,9 +1,12 @@
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'
9
+ import { Buffer } from 'buffer'
7
10
 
8
11
  export interface ReplyGenericInterface {
9
12
  Reply?: ReplyDefault;
@@ -19,19 +22,22 @@ export interface FastifyReply<
19
22
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
20
23
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
21
24
  ContextConfig = ContextConfigDefault,
25
+ SchemaCompiler extends FastifySchema = FastifySchema,
26
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
27
+ ReplyType extends FastifyReplyType = ResolveFastifyReplyType<TypeProvider, SchemaCompiler, RouteGeneric>
22
28
  > {
23
29
  raw: RawReply;
24
30
  context: FastifyContext<ContextConfig>;
25
31
  log: FastifyLoggerInstance;
26
- request: FastifyRequest<RouteGeneric, RawServer, RawRequest>;
32
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>;
27
33
  server: FastifyInstance;
28
- code(statusCode: number): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
29
- status(statusCode: number): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
34
+ code(statusCode: number): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
35
+ status(statusCode: number): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
30
36
  statusCode: number;
31
37
  sent: boolean;
32
- send(payload?: RouteGeneric['Reply']): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
33
- header(key: string, value: any): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
34
- headers(values: {[key: string]: any}): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
38
+ send(payload?: ReplyType): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
39
+ header(key: string, value: any): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
40
+ headers(values: {[key: string]: any}): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
35
41
  getHeader(key: string): string | undefined;
36
42
  getHeaders(): {
37
43
  // Node's `getHeaders()` can return numbers and arrays, so they're included here as possible types.
@@ -40,13 +46,13 @@ export interface FastifyReply<
40
46
  removeHeader(key: string): void;
41
47
  hasHeader(key: string): boolean;
42
48
  // 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>;
49
+ redirect(statusCode: number, url: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
50
+ redirect(url: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
51
+ hijack(): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
46
52
  callNotFound(): void;
47
53
  getResponseTime(): number;
48
- type(contentType: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
49
- serializer(fn: (payload: any) => string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
50
- serialize(payload: any): string;
54
+ type(contentType: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
55
+ serializer(fn: (payload: any) => string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
56
+ serialize(payload: any): string | ArrayBuffer | Buffer;
51
57
  then(fulfilled: () => void, rejected: (err: Error) => void): void;
52
58
  }
@@ -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 {
@@ -15,20 +17,23 @@ export interface RequestGenericInterface {
15
17
  * FastifyRequest is an instance of the standard http or http2 request objects.
16
18
  * It defaults to http.IncomingMessage, and it also extends the relative request object.
17
19
  */
18
- export interface FastifyRequest<
19
- RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
20
+ export interface FastifyRequest<RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
20
21
  RawServer extends RawServerBase = RawServerDefault,
21
22
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
23
+ SchemaCompiler extends FastifySchema = FastifySchema,
24
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
22
25
  ContextConfig = ContextConfigDefault,
26
+ RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>,
27
+ Logger extends FastifyLoggerInstance = FastifyLoggerInstance
23
28
  > {
24
29
  id: any;
25
- params: RouteGeneric['Params'];
30
+ params: RequestType['params'];
26
31
  raw: RawRequest;
27
- query: RouteGeneric['Querystring'];
28
- headers: RawRequest['headers'] & RouteGeneric['Headers']; // this enables the developer to extend the existing http(s|2) headers list
29
- log: FastifyLoggerInstance;
32
+ query: RequestType['query'];
33
+ headers: RawRequest['headers'] & RequestType['headers']; // this enables the developer to extend the existing http(s|2) headers list
34
+ log: Logger;
30
35
  server: FastifyInstance;
31
- body: RouteGeneric['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,16 @@ 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 {
10
+ FastifyRequestType,
11
+ FastifyTypeProvider,
12
+ FastifyTypeProviderDefault,
13
+ ResolveFastifyReplyReturnType, ResolveFastifyRequestType
14
+ } from './type-provider'
15
+ import { FastifyLoggerInstance, LogLevel } from './logger'
10
16
 
11
17
  export interface RouteGenericInterface extends RequestGenericInterface, ReplyGenericInterface {}
12
18
 
@@ -20,10 +26,14 @@ export interface RouteShorthandOptions<
20
26
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
21
27
  ContextConfig = ContextConfigDefault,
22
28
  SchemaCompiler = FastifySchema,
29
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
30
+ RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>,
31
+ Logger extends FastifyLoggerInstance = FastifyLoggerInstance
23
32
  > {
24
- schema?: FastifySchema;
33
+ schema?: SchemaCompiler, // originally FastifySchema
25
34
  attachValidation?: boolean;
26
35
  exposeHeadRoute?: boolean;
36
+
27
37
  validatorCompiler?: FastifySchemaCompiler<SchemaCompiler>;
28
38
  serializerCompiler?: FastifySerializerCompiler<SchemaCompiler>;
29
39
  bodyLimit?: number;
@@ -37,15 +47,15 @@ export interface RouteShorthandOptions<
37
47
  schemaErrorFormatter?: (errors: FastifySchemaValidationError[], dataVar: string) => Error;
38
48
 
39
49
  // 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>[];
50
+ onRequest?: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger> | onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger>[];
51
+ preParsing?: preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger> | preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger>[];
52
+ preValidation?: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger> | preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger>[];
53
+ preHandler?: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger> | preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger>[];
54
+ preSerialization?: preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger> | preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger>[];
55
+ onSend?: onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger> | onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger>[];
56
+ onResponse?: onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger> | onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger>[];
57
+ onTimeout?: onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger> | onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger>[];
58
+ onError?: onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, RequestType, Logger> | onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, RequestType, Logger>[];
49
59
  }
50
60
 
51
61
  /**
@@ -56,12 +66,17 @@ export type RouteHandlerMethod<
56
66
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
57
67
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
58
68
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
59
- ContextConfig = ContextConfigDefault
69
+ ContextConfig = ContextConfigDefault,
70
+ SchemaCompiler extends FastifySchema = FastifySchema,
71
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
72
+ ReturnType = ResolveFastifyReplyReturnType<TypeProvider, SchemaCompiler, RouteGeneric>,
73
+ RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>,
74
+ Logger extends FastifyLoggerInstance = FastifyLoggerInstance
60
75
  > = (
61
- this: FastifyInstance<RawServer, RawRequest, RawReply>,
62
- request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
63
- reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
64
- ) => void | Promise<RouteGeneric['Reply'] | void>
76
+ this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
77
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, RequestType, Logger>,
78
+ reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
79
+ ) => ReturnType
65
80
 
66
81
  /**
67
82
  * Shorthand options including the handler function property
@@ -73,8 +88,12 @@ export interface RouteShorthandOptionsWithHandler<
73
88
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
74
89
  ContextConfig = ContextConfigDefault,
75
90
  SchemaCompiler = FastifySchema,
76
- > extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler> {
77
- handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
91
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
92
+ ReturnType = ResolveFastifyReplyReturnType<TypeProvider, SchemaCompiler, RouteGeneric>,
93
+ RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>,
94
+ Logger extends FastifyLoggerInstance = FastifyLoggerInstance
95
+ > extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger> {
96
+ handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, ReturnType, RequestType, Logger>;
78
97
  }
79
98
 
80
99
  /**
@@ -84,20 +103,21 @@ export interface RouteShorthandMethod<
84
103
  RawServer extends RawServerBase = RawServerDefault,
85
104
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
86
105
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
106
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
87
107
  > {
88
- <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = FastifySchema>(
108
+ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = FastifySchema, ReturnType = ResolveFastifyReplyReturnType<TypeProvider, SchemaCompiler, RouteGeneric>, RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>, Logger extends FastifyLoggerInstance = FastifyLoggerInstance>(
89
109
  path: string,
90
- opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler>,
91
- handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
92
- ): FastifyInstance<RawServer, RawRequest, RawReply>;
93
- <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault>(
110
+ opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger>,
111
+ handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, ReturnType, RequestType, Logger>
112
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
113
+ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = FastifySchema, ReturnType = ResolveFastifyReplyReturnType<TypeProvider, SchemaCompiler, RouteGeneric>, RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>, Logger extends FastifyLoggerInstance = FastifyLoggerInstance>(
94
114
  path: string,
95
- handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
96
- ): FastifyInstance<RawServer, RawRequest, RawReply>;
97
- <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = FastifySchema>(
115
+ handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, ReturnType, RequestType, Logger>
116
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
117
+ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = FastifySchema, ReturnType = ResolveFastifyReplyReturnType<TypeProvider, SchemaCompiler, RouteGeneric>, RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>, Logger extends FastifyLoggerInstance = FastifyLoggerInstance>(
98
118
  path: string,
99
- opts: RouteShorthandOptionsWithHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler>
100
- ): FastifyInstance<RawServer, RawRequest, RawReply>;
119
+ opts: RouteShorthandOptionsWithHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, ReturnType, RequestType, Logger>
120
+ ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
101
121
  }
102
122
 
103
123
  /**
@@ -110,10 +130,14 @@ export interface RouteOptions<
110
130
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
111
131
  ContextConfig = ContextConfigDefault,
112
132
  SchemaCompiler = FastifySchema,
113
- > extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler> {
133
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
134
+ ReturnType = ResolveFastifyReplyReturnType<TypeProvider, SchemaCompiler, RouteGeneric>,
135
+ RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>,
136
+ Logger extends FastifyLoggerInstance = FastifyLoggerInstance
137
+ > extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, RequestType, Logger> {
114
138
  method: HTTPMethods | HTTPMethods[];
115
139
  url: string;
116
- handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
140
+ handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, ReturnType, RequestType, Logger>;
117
141
  }
118
142
 
119
143
  export type RouteHandler<
@@ -121,11 +145,15 @@ export type RouteHandler<
121
145
  RawServer extends RawServerBase = RawServerDefault,
122
146
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
123
147
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
124
- ContextConfig = ContextConfigDefault
148
+ ContextConfig = ContextConfigDefault,
149
+ SchemaCompiler extends FastifySchema = FastifySchema,
150
+ TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
151
+ RequestType extends FastifyRequestType = ResolveFastifyRequestType<TypeProvider, SchemaCompiler, RouteGeneric>,
152
+ Logger extends FastifyLoggerInstance = FastifyLoggerInstance
125
153
  > = (
126
- this: FastifyInstance<RawServer, RawRequest, RawReply>,
127
- request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
128
- reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
154
+ this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
155
+ request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, RequestType, Logger>,
156
+ reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
129
157
  ) => void | Promise<RouteGeneric['Reply'] | void>
130
158
 
131
159
  export type DefaultRoute<Request, Reply> = (
package/types/schema.d.ts CHANGED
@@ -24,7 +24,7 @@ export interface FastifyRouteSchemaDef<T> {
24
24
 
25
25
  export interface FastifySchemaValidationError {
26
26
  message?: string;
27
- dataPath: string;
27
+ instancePath: string;
28
28
  }
29
29
 
30
30
  export interface FastifyValidationResult {
@@ -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
@@ -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