fastify 5.8.4 → 5.9.0

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 (105) hide show
  1. package/SECURITY.md +1 -1
  2. package/SPONSORS.md +6 -4
  3. package/docs/Guides/Database.md +0 -28
  4. package/docs/Guides/Ecosystem.md +17 -2
  5. package/docs/Guides/Serverless.md +2 -2
  6. package/docs/Guides/Write-Plugin.md +1 -1
  7. package/docs/Reference/Encapsulation.md +27 -26
  8. package/docs/Reference/Errors.md +10 -4
  9. package/docs/Reference/HTTP2.md +10 -10
  10. package/docs/Reference/Hooks.md +4 -4
  11. package/docs/Reference/Index.md +14 -16
  12. package/docs/Reference/LTS.md +12 -13
  13. package/docs/Reference/Lifecycle.md +9 -8
  14. package/docs/Reference/Logging.md +44 -39
  15. package/docs/Reference/Middleware.md +21 -25
  16. package/docs/Reference/Principles.md +2 -2
  17. package/docs/Reference/Reply.md +6 -1
  18. package/docs/Reference/Request.md +27 -16
  19. package/docs/Reference/Routes.md +5 -2
  20. package/docs/Reference/Server.md +31 -3
  21. package/docs/Reference/Type-Providers.md +29 -5
  22. package/docs/Reference/Validation-and-Serialization.md +15 -2
  23. package/docs/Reference/Warnings.md +7 -6
  24. package/eslint.config.js +7 -2
  25. package/fastify.d.ts +8 -3
  26. package/fastify.js +43 -14
  27. package/lib/content-type-parser.js +13 -1
  28. package/lib/decorate.js +11 -3
  29. package/lib/error-handler.js +4 -3
  30. package/lib/error-serializer.js +59 -59
  31. package/lib/errors.js +16 -1
  32. package/lib/four-oh-four.js +14 -9
  33. package/lib/handle-request.js +11 -5
  34. package/lib/plugin-override.js +2 -1
  35. package/lib/plugin-utils.js +5 -5
  36. package/lib/reply.js +63 -8
  37. package/lib/request.js +23 -19
  38. package/lib/route.js +20 -6
  39. package/lib/schema-controller.js +1 -1
  40. package/lib/schemas.js +37 -30
  41. package/lib/symbols.js +3 -1
  42. package/lib/validation.js +1 -13
  43. package/lib/warnings.js +3 -3
  44. package/package.json +16 -18
  45. package/scripts/validate-ecosystem-links.js +1 -0
  46. package/test/bundler/esbuild/package.json +1 -1
  47. package/test/close-pipelining.test.js +1 -2
  48. package/test/custom-http-server.test.js +38 -0
  49. package/test/decorator-instance-properties.test.js +63 -0
  50. package/test/diagnostics-channel/async-error-handler.test.js +74 -0
  51. package/test/hooks.test.js +23 -0
  52. package/test/http-methods/get.test.js +1 -1
  53. package/test/http2/plain.test.js +135 -0
  54. package/test/http2/secure-with-fallback.test.js +1 -1
  55. package/test/https/https.test.js +1 -2
  56. package/test/internals/errors.test.js +31 -1
  57. package/test/internals/plugin.test.js +3 -1
  58. package/test/internals/request.test.js +27 -3
  59. package/test/internals/schema-controller-perf.test.js +33 -0
  60. package/test/logger/logging.test.js +18 -1
  61. package/test/logger/options.test.js +38 -1
  62. package/test/reply-error.test.js +1 -1
  63. package/test/reply-trailers.test.js +70 -0
  64. package/test/request-media-type.test.js +105 -0
  65. package/test/request-port.test.js +72 -0
  66. package/test/route-prefix.test.js +34 -0
  67. package/test/router-options.test.js +222 -11
  68. package/test/schema-serialization.test.js +108 -0
  69. package/test/schema-validation.test.js +45 -0
  70. package/test/scripts/validate-ecosystem-links.test.js +40 -57
  71. package/test/throw.test.js +14 -0
  72. package/test/trust-proxy.test.js +74 -0
  73. package/test/types/content-type-parser.tst.ts +70 -0
  74. package/test/types/{decorate-request-reply.test-d.ts → decorate-request-reply.tst.ts} +4 -4
  75. package/test/types/{dummy-plugin.ts → dummy-plugin.mts} +1 -1
  76. package/test/types/errors.tst.ts +91 -0
  77. package/test/types/fastify.tst.ts +351 -0
  78. package/test/types/hooks.tst.ts +578 -0
  79. package/test/types/instance.tst.ts +597 -0
  80. package/test/types/{logger.test-d.ts → logger.tst.ts} +61 -62
  81. package/test/types/plugin.tst.ts +96 -0
  82. package/test/types/register.tst.ts +245 -0
  83. package/test/types/reply.tst.ts +297 -0
  84. package/test/types/request.tst.ts +199 -0
  85. package/test/types/route.tst.ts +576 -0
  86. package/test/types/{schema.test-d.ts → schema.tst.ts} +22 -22
  87. package/test/types/{serverFactory.test-d.ts → serverFactory.tst.ts} +4 -4
  88. package/test/types/tsconfig.json +9 -0
  89. package/test/types/{type-provider.test-d.ts → type-provider.tst.ts} +256 -250
  90. package/test/types/using.tst.ts +14 -0
  91. package/types/errors.d.ts +3 -0
  92. package/types/request.d.ts +23 -2
  93. package/AGENTS.md +0 -290
  94. package/test/types/content-type-parser.test-d.ts +0 -72
  95. package/test/types/errors.test-d.ts +0 -90
  96. package/test/types/fastify.test-d.ts +0 -352
  97. package/test/types/hooks.test-d.ts +0 -550
  98. package/test/types/import.ts +0 -2
  99. package/test/types/instance.test-d.ts +0 -588
  100. package/test/types/plugin.test-d.ts +0 -97
  101. package/test/types/register.test-d.ts +0 -237
  102. package/test/types/reply.test-d.ts +0 -254
  103. package/test/types/request.test-d.ts +0 -188
  104. package/test/types/route.test-d.ts +0 -553
  105. package/test/types/using.test-d.ts +0 -17
@@ -1,553 +0,0 @@
1
- import { FastifyError } from '@fastify/error'
2
- import * as http from 'node:http'
3
- import { expectAssignable, expectError, expectType } from 'tsd'
4
- import fastify, { FastifyInstance, FastifyReply, FastifyRequest, RouteHandlerMethod } from '../../fastify'
5
- import { RequestPayload } from '../../types/hooks'
6
- import { FindMyWayFindResult } from '../../types/instance'
7
- import { HTTPMethods, RawServerDefault } from '../../types/utils'
8
-
9
- /*
10
- * Testing Fastify HTTP Routes and Route Shorthands.
11
- * Verifies Request and Reply types as well.
12
- * For the route shorthand tests the argument orders are:
13
- * - `(path, handler)`
14
- * - `(path, options, handler)`
15
- * - `(path, options)`
16
- */
17
-
18
- declare module '../../fastify' {
19
- interface FastifyContextConfig {
20
- foo: string;
21
- bar: number;
22
- includeMessage?: boolean;
23
- }
24
-
25
- /* eslint-disable @typescript-eslint/no-unused-vars */
26
- interface FastifyRequest<
27
- RouteGeneric,
28
- RawServer,
29
- RawRequest,
30
- SchemaCompiler,
31
- TypeProvider,
32
- ContextConfig,
33
- Logger,
34
- RequestType
35
- > {
36
- message: ContextConfig extends { includeMessage: true }
37
- ? string
38
- : null;
39
- }
40
- }
41
-
42
- const routeHandler: RouteHandlerMethod = function (request, reply) {
43
- expectType<FastifyInstance>(this)
44
- expectType<FastifyRequest>(request)
45
- expectType<FastifyReply>(reply)
46
- }
47
-
48
- const routeHandlerWithReturnValue: RouteHandlerMethod = function (request, reply) {
49
- expectType<FastifyInstance>(this)
50
- expectType<FastifyRequest>(request)
51
- expectType<FastifyReply>(reply)
52
-
53
- return reply.send()
54
- }
55
-
56
- const asyncPreHandler = async (request: FastifyRequest) => {
57
- expectType<FastifyRequest>(request)
58
- }
59
-
60
- fastify().get('/', { preHandler: asyncPreHandler }, async () => 'this is an example')
61
-
62
- fastify().get(
63
- '/',
64
- { config: { foo: 'bar', bar: 100, includeMessage: true } },
65
- (req) => {
66
- expectType<string>(req.message)
67
- }
68
- )
69
-
70
- fastify().get(
71
- '/',
72
- { config: { foo: 'bar', bar: 100, includeMessage: false } },
73
- (req) => {
74
- expectType<null>(req.message)
75
- }
76
- )
77
-
78
- type LowerCaseHTTPMethods = 'delete' | 'get' | 'head' | 'patch' | 'post' | 'put' |
79
- 'options' | 'propfind' | 'proppatch' | 'mkcol' | 'copy' | 'move' | 'lock' |
80
- 'unlock' | 'trace' | 'search' | 'mkcalendar' | 'report'
81
-
82
- ;['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS', 'PROPFIND',
83
- 'PROPPATCH', 'MKCOL', 'COPY', 'MOVE', 'LOCK', 'UNLOCK', 'TRACE', 'SEARCH', 'MKCALENDAR', 'REPORT'
84
- ].forEach(method => {
85
- // route method
86
- expectType<FastifyInstance>(fastify().route({
87
- method: method as HTTPMethods,
88
- url: '/',
89
- handler: routeHandler
90
- }))
91
-
92
- const lowerCaseMethod: LowerCaseHTTPMethods = method.toLowerCase() as LowerCaseHTTPMethods
93
-
94
- // method as method
95
- expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', routeHandler))
96
- expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', {}, routeHandler))
97
- expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', { handler: routeHandler }))
98
-
99
- expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', {
100
- handler: routeHandler,
101
- errorHandler: (error, request, reply) => {
102
- expectType<FastifyError>(error)
103
- reply.send('error')
104
- },
105
- childLoggerFactory: function (logger, bindings, opts) {
106
- return logger.child(bindings, opts)
107
- }
108
- }))
109
-
110
- interface BodyInterface { prop: string }
111
- interface QuerystringInterface { prop: number }
112
- interface ParamsInterface { prop: boolean }
113
- interface HeadersInterface { prop: string }
114
- interface RouteSpecificContextConfigType {
115
- extra: boolean
116
- }
117
- interface RouteGeneric {
118
- Body: BodyInterface;
119
- Querystring: QuerystringInterface;
120
- Params: ParamsInterface;
121
- Headers: HeadersInterface;
122
- }
123
-
124
- fastify()[lowerCaseMethod]<RouteGeneric, RouteSpecificContextConfigType>('/', { config: { foo: 'bar', bar: 100, extra: true } }, (req, res) => {
125
- expectType<BodyInterface>(req.body)
126
- expectType<QuerystringInterface>(req.query)
127
- expectType<ParamsInterface>(req.params)
128
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
129
- expectType<string>(req.routeOptions.config.foo)
130
- expectType<number>(req.routeOptions.config.bar)
131
- expectType<boolean>(req.routeOptions.config.extra)
132
- expectType<string>(req.routeOptions.config.url)
133
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
134
- expectType<string>(res.routeOptions.config.foo)
135
- expectType<number>(res.routeOptions.config.bar)
136
- expectType<boolean>(res.routeOptions.config.extra)
137
- expectType<string>(req.routeOptions.config.url)
138
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
139
- })
140
-
141
- fastify().route<RouteGeneric>({
142
- url: '/',
143
- method: method as HTTPMethods,
144
- config: { foo: 'bar', bar: 100 },
145
- prefixTrailingSlash: 'slash',
146
- onRequest: (req, res, done) => { // these handlers are tested in `hooks.test-d.ts`
147
- expectType<BodyInterface>(req.body)
148
- expectType<QuerystringInterface>(req.query)
149
- expectType<ParamsInterface>(req.params)
150
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
151
- expectType<string>(req.routeOptions.config.foo)
152
- expectType<number>(req.routeOptions.config.bar)
153
- expectType<string>(req.routeOptions.config.url)
154
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
155
- expectType<string>(res.routeOptions.config.foo)
156
- expectType<number>(res.routeOptions.config.bar)
157
- expectType<string>(req.routeOptions.config.url)
158
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
159
- },
160
- preParsing: (req, res, payload, done) => {
161
- expectType<BodyInterface>(req.body)
162
- expectType<QuerystringInterface>(req.query)
163
- expectType<ParamsInterface>(req.params)
164
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
165
- expectType<string>(req.routeOptions.config.foo)
166
- expectType<number>(req.routeOptions.config.bar)
167
- expectType<string>(req.routeOptions.config.url)
168
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
169
- expectType<string>(res.routeOptions.config.foo)
170
- expectType<number>(res.routeOptions.config.bar)
171
- expectType<string>(req.routeOptions.config.url)
172
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
173
- expectType<RequestPayload>(payload)
174
- expectAssignable<(err?: FastifyError | null, res?: RequestPayload) => void>(done)
175
- expectAssignable<(err?: NodeJS.ErrnoException) => void>(done)
176
- },
177
- preValidation: (req, res, done) => {
178
- expectType<BodyInterface>(req.body)
179
- expectType<QuerystringInterface>(req.query)
180
- expectType<ParamsInterface>(req.params)
181
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
182
- expectType<string>(req.routeOptions.config.foo)
183
- expectType<number>(req.routeOptions.config.bar)
184
- expectType<string>(req.routeOptions.config.url)
185
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
186
- expectType<string>(res.routeOptions.config.foo)
187
- expectType<number>(res.routeOptions.config.bar)
188
- expectType<string>(req.routeOptions.config.url)
189
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
190
- },
191
- preHandler: (req, res, done) => {
192
- expectType<BodyInterface>(req.body)
193
- expectType<QuerystringInterface>(req.query)
194
- expectType<ParamsInterface>(req.params)
195
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
196
- expectType<string>(req.routeOptions.config.foo)
197
- expectType<number>(req.routeOptions.config.bar)
198
- expectType<string>(req.routeOptions.config.url)
199
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
200
- expectType<string>(res.routeOptions.config.foo)
201
- expectType<number>(res.routeOptions.config.bar)
202
- expectType<string>(req.routeOptions.config.url)
203
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
204
- },
205
- onResponse: (req, res, done) => {
206
- expectType<BodyInterface>(req.body)
207
- expectType<QuerystringInterface>(req.query)
208
- expectType<ParamsInterface>(req.params)
209
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
210
- expectType<string>(req.routeOptions.config.foo)
211
- expectType<number>(req.routeOptions.config.bar)
212
- expectType<string>(req.routeOptions.config.url)
213
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
214
- expectType<string>(res.routeOptions.config.foo)
215
- expectType<number>(res.routeOptions.config.bar)
216
- expectType<string>(req.routeOptions.config.url)
217
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
218
- expectType<number>(res.statusCode)
219
- },
220
- onError: (req, res, error, done) => {
221
- expectType<BodyInterface>(req.body)
222
- expectType<QuerystringInterface>(req.query)
223
- expectType<ParamsInterface>(req.params)
224
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
225
- expectType<string>(req.routeOptions.config.foo)
226
- expectType<number>(req.routeOptions.config.bar)
227
- expectType<string>(req.routeOptions.config.url)
228
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
229
- expectType<string>(res.routeOptions.config.foo)
230
- expectType<number>(res.routeOptions.config.bar)
231
- expectType<string>(req.routeOptions.config.url)
232
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
233
- },
234
- preSerialization: (req, res, payload, done) => {
235
- expectType<BodyInterface>(req.body)
236
- expectType<QuerystringInterface>(req.query)
237
- expectType<ParamsInterface>(req.params)
238
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
239
- expectType<string>(req.routeOptions.config.foo)
240
- expectType<number>(req.routeOptions.config.bar)
241
- expectType<string>(req.routeOptions.config.url)
242
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
243
- expectType<string>(res.routeOptions.config.foo)
244
- expectType<number>(res.routeOptions.config.bar)
245
- expectType<string>(req.routeOptions.config.url)
246
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
247
- },
248
- onSend: (req, res, payload, done) => {
249
- expectType<BodyInterface>(req.body)
250
- expectType<QuerystringInterface>(req.query)
251
- expectType<ParamsInterface>(req.params)
252
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
253
- expectType<string>(req.routeOptions.config.foo)
254
- expectType<number>(req.routeOptions.config.bar)
255
- expectType<string>(req.routeOptions.config.url)
256
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
257
- expectType<string>(res.routeOptions.config.foo)
258
- expectType<number>(res.routeOptions.config.bar)
259
- expectType<string>(req.routeOptions.config.url)
260
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
261
- },
262
- handler: (req, res) => {
263
- expectType<BodyInterface>(req.body)
264
- expectType<QuerystringInterface>(req.query)
265
- expectType<ParamsInterface>(req.params)
266
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
267
- expectType<string>(req.routeOptions.config.foo)
268
- expectType<number>(req.routeOptions.config.bar)
269
- expectType<string>(req.routeOptions.config.url)
270
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
271
- expectType<string>(res.routeOptions.config.foo)
272
- expectType<number>(res.routeOptions.config.bar)
273
- expectType<string>(req.routeOptions.config.url)
274
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
275
- }
276
- })
277
-
278
- fastify().route<RouteGeneric>({
279
- url: '/',
280
- method: method as HTTPMethods,
281
- config: { foo: 'bar', bar: 100 },
282
- prefixTrailingSlash: 'slash',
283
- onRequest: async (req, res, done) => { // these handlers are tested in `hooks.test-d.ts`
284
- expectType<BodyInterface>(req.body)
285
- expectType<QuerystringInterface>(req.query)
286
- expectType<ParamsInterface>(req.params)
287
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
288
- expectType<string>(req.routeOptions.config.foo)
289
- expectType<number>(req.routeOptions.config.bar)
290
- expectType<string>(req.routeOptions.config.url)
291
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
292
- expectType<string>(res.routeOptions.config.foo)
293
- expectType<number>(res.routeOptions.config.bar)
294
- expectType<string>(req.routeOptions.config.url)
295
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
296
- },
297
- preParsing: async (req, res, payload, done) => {
298
- expectType<BodyInterface>(req.body)
299
- expectType<QuerystringInterface>(req.query)
300
- expectType<ParamsInterface>(req.params)
301
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
302
- expectType<string>(req.routeOptions.config.foo)
303
- expectType<number>(req.routeOptions.config.bar)
304
- expectType<string>(req.routeOptions.config.url)
305
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
306
- expectType<string>(res.routeOptions.config.foo)
307
- expectType<number>(res.routeOptions.config.bar)
308
- expectType<string>(req.routeOptions.config.url)
309
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
310
- expectType<RequestPayload>(payload)
311
- expectAssignable<(err?: FastifyError | null, res?: RequestPayload) => void>(done)
312
- expectAssignable<(err?: NodeJS.ErrnoException) => void>(done)
313
- },
314
- preValidation: async (req, res, done) => {
315
- expectType<BodyInterface>(req.body)
316
- expectType<QuerystringInterface>(req.query)
317
- expectType<ParamsInterface>(req.params)
318
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
319
- expectType<string>(req.routeOptions.config.foo)
320
- expectType<number>(req.routeOptions.config.bar)
321
- expectType<string>(req.routeOptions.config.url)
322
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
323
- expectType<string>(res.routeOptions.config.foo)
324
- expectType<number>(res.routeOptions.config.bar)
325
- expectType<string>(req.routeOptions.config.url)
326
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
327
- },
328
- preHandler: async (req, res, done) => {
329
- expectType<BodyInterface>(req.body)
330
- expectType<QuerystringInterface>(req.query)
331
- expectType<ParamsInterface>(req.params)
332
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
333
- expectType<string>(req.routeOptions.config.foo)
334
- expectType<number>(req.routeOptions.config.bar)
335
- expectType<string>(req.routeOptions.config.url)
336
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
337
- expectType<string>(res.routeOptions.config.foo)
338
- expectType<number>(res.routeOptions.config.bar)
339
- expectType<string>(req.routeOptions.config.url)
340
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
341
- },
342
- onResponse: async (req, res, done) => {
343
- expectType<BodyInterface>(req.body)
344
- expectType<QuerystringInterface>(req.query)
345
- expectType<ParamsInterface>(req.params)
346
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
347
- expectType<string>(req.routeOptions.config.foo)
348
- expectType<number>(req.routeOptions.config.bar)
349
- expectType<string>(req.routeOptions.config.url)
350
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
351
- expectType<string>(res.routeOptions.config.foo)
352
- expectType<number>(res.routeOptions.config.bar)
353
- expectType<string>(req.routeOptions.config.url)
354
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
355
- expectType<number>(res.statusCode)
356
- },
357
- onError: async (req, res, error, done) => {
358
- expectType<BodyInterface>(req.body)
359
- expectType<QuerystringInterface>(req.query)
360
- expectType<ParamsInterface>(req.params)
361
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
362
- expectType<string>(req.routeOptions.config.foo)
363
- expectType<number>(req.routeOptions.config.bar)
364
- expectType<string>(req.routeOptions.config.url)
365
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
366
- expectType<string>(res.routeOptions.config.foo)
367
- expectType<number>(res.routeOptions.config.bar)
368
- expectType<string>(req.routeOptions.config.url)
369
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
370
- },
371
- preSerialization: async (req, res, payload, done) => {
372
- expectType<BodyInterface>(req.body)
373
- expectType<QuerystringInterface>(req.query)
374
- expectType<ParamsInterface>(req.params)
375
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
376
- expectType<string>(req.routeOptions.config.foo)
377
- expectType<number>(req.routeOptions.config.bar)
378
- expectType<string>(req.routeOptions.config.url)
379
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
380
- expectType<string>(res.routeOptions.config.foo)
381
- expectType<number>(res.routeOptions.config.bar)
382
- expectType<string>(req.routeOptions.config.url)
383
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
384
- },
385
- onSend: async (req, res, payload, done) => {
386
- expectType<BodyInterface>(req.body)
387
- expectType<QuerystringInterface>(req.query)
388
- expectType<ParamsInterface>(req.params)
389
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
390
- expectType<string>(req.routeOptions.config.foo)
391
- expectType<number>(req.routeOptions.config.bar)
392
- expectType<string>(req.routeOptions.config.url)
393
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
394
- expectType<string>(res.routeOptions.config.foo)
395
- expectType<number>(res.routeOptions.config.bar)
396
- expectType<string>(req.routeOptions.config.url)
397
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
398
- },
399
- handler: (req, res) => {
400
- expectType<BodyInterface>(req.body)
401
- expectType<QuerystringInterface>(req.query)
402
- expectType<ParamsInterface>(req.params)
403
- expectType<http.IncomingHttpHeaders & HeadersInterface>(req.headers)
404
- expectType<string>(req.routeOptions.config.foo)
405
- expectType<number>(req.routeOptions.config.bar)
406
- expectType<string>(req.routeOptions.config.url)
407
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
408
- expectType<string>(res.routeOptions.config.foo)
409
- expectType<number>(res.routeOptions.config.bar)
410
- expectType<string>(req.routeOptions.config.url)
411
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.config.method)
412
- }
413
- })
414
- })
415
-
416
- expectType<FastifyInstance>(fastify().route({
417
- url: '/',
418
- method: 'CONNECT', // not a valid method but could be implemented by the user
419
- handler: routeHandler
420
- }))
421
-
422
- expectType<FastifyInstance>(fastify().route({
423
- url: '/',
424
- method: 'OPTIONS',
425
- handler: routeHandler
426
- }))
427
-
428
- expectType<FastifyInstance>(fastify().route({
429
- url: '/',
430
- method: 'OPTION', // OPTION is a typo for OPTIONS
431
- handler: routeHandler
432
- }))
433
-
434
- expectType<FastifyInstance>(fastify().route({
435
- url: '/',
436
- method: ['GET', 'POST'],
437
- handler: routeHandler
438
- }))
439
-
440
- expectType<FastifyInstance>(fastify().route({
441
- url: '/',
442
- method: ['GET', 'POST', 'OPTION'], // OPTION is a typo for OPTIONS
443
- handler: routeHandler
444
- }))
445
-
446
- expectError(fastify().route({
447
- url: '/',
448
- method: 'GET',
449
- handler: routeHandler,
450
- schemaErrorFormatter: 500 // Not a valid formatter
451
- }))
452
-
453
- expectType<FastifyInstance>(fastify().route({
454
- url: '/',
455
- method: 'GET',
456
- handler: routeHandler,
457
- schemaErrorFormatter: (errors, dataVar) => new Error('')
458
- }))
459
-
460
- expectError(fastify().route({
461
- prefixTrailingSlash: true // Not a valid value
462
- }))
463
-
464
- expectType<FastifyInstance>(fastify().route({
465
- url: '/',
466
- method: 'GET',
467
- handler: routeHandlerWithReturnValue
468
- }))
469
-
470
- expectType<boolean>(fastify().hasRoute({
471
- url: '/',
472
- method: 'GET'
473
- }))
474
-
475
- expectType<boolean>(fastify().hasRoute({
476
- url: '/',
477
- method: 'GET',
478
- constraints: { version: '1.2.0' }
479
- }))
480
-
481
- expectType<boolean>(fastify().hasRoute({
482
- url: '/',
483
- method: 'GET',
484
- constraints: { host: 'auth.fastify.test' }
485
- }))
486
-
487
- expectType<boolean>(fastify().hasRoute({
488
- url: '/',
489
- method: 'GET',
490
- constraints: { host: /.*\.fastify\.test$/ }
491
- }))
492
-
493
- expectType<boolean>(fastify().hasRoute({
494
- url: '/',
495
- method: 'GET',
496
- constraints: { host: /.*\.fastify\.test$/, version: '1.2.3' }
497
- }))
498
-
499
- expectType<boolean>(fastify().hasRoute({
500
- url: '/',
501
- method: 'GET',
502
- constraints: {
503
- // constraints value should accept any value
504
- number: 12,
505
- date: new Date(),
506
- boolean: true,
507
- function: () => { },
508
- object: { foo: 'bar' }
509
- }
510
- }))
511
-
512
- expectType<Omit<FindMyWayFindResult<RawServerDefault>, 'store'>>(
513
- fastify().findRoute({
514
- url: '/',
515
- method: 'get'
516
- })
517
- )
518
-
519
- // we should not expose store
520
- expectError(fastify().findRoute({
521
- url: '/',
522
- method: 'get'
523
- }).store)
524
-
525
- expectType<FastifyInstance>(fastify().route({
526
- url: '/',
527
- method: 'get',
528
- handler: routeHandlerWithReturnValue
529
- }))
530
-
531
- expectType<FastifyInstance>(fastify().route({
532
- url: '/',
533
- method: ['put', 'patch'],
534
- handler: routeHandlerWithReturnValue
535
- }))
536
-
537
- expectType<FastifyInstance>(fastify().route({
538
- url: '/',
539
- method: 'GET',
540
- handler: (req) => {
541
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.method)
542
- expectAssignable<string | Array<string>>(req.routeOptions.method)
543
- }
544
- }))
545
-
546
- expectType<FastifyInstance>(fastify().route({
547
- url: '/',
548
- method: ['HEAD', 'GET'],
549
- handler: (req) => {
550
- expectType<HTTPMethods | HTTPMethods[]>(req.routeOptions.method)
551
- expectAssignable<string | Array<string>>(req.routeOptions.method)
552
- }
553
- }))
@@ -1,17 +0,0 @@
1
- import { expectAssignable } from 'tsd'
2
- import fastify, { FastifyInstance } from '../../fastify'
3
-
4
- async function hasSymbolDisposeWithUsing () {
5
- await using app = fastify()
6
- expectAssignable<FastifyInstance>(app)
7
- expectAssignable<FastifyInstance[typeof Symbol.asyncDispose]>(app.close)
8
- }
9
-
10
- async function hasSymbolDispose () {
11
- const app = fastify()
12
- expectAssignable<FastifyInstance>(app)
13
- expectAssignable<FastifyInstance[typeof Symbol.asyncDispose]>(app.close)
14
- }
15
-
16
- hasSymbolDisposeWithUsing()
17
- hasSymbolDispose()