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.
- package/.taprc +3 -0
- package/README.md +7 -7
- package/build/build-error-serializer.js +27 -0
- package/build/build-validation.js +47 -35
- package/docs/Guides/Database.md +320 -0
- package/docs/Guides/Ecosystem.md +9 -0
- package/docs/Guides/Getting-Started.md +7 -7
- package/docs/Guides/Plugins-Guide.md +1 -1
- package/docs/Guides/Serverless.md +3 -3
- package/docs/Guides/Testing.md +2 -2
- package/docs/Migration-Guide-V4.md +12 -0
- package/docs/Reference/ContentTypeParser.md +4 -0
- package/docs/Reference/Decorators.md +2 -2
- package/docs/Reference/Encapsulation.md +2 -2
- package/docs/Reference/Errors.md +51 -6
- package/docs/Reference/HTTP2.md +3 -3
- package/docs/Reference/Hooks.md +4 -7
- package/docs/Reference/LTS.md +5 -4
- package/docs/Reference/Plugins.md +3 -3
- package/docs/Reference/Reply.md +73 -22
- package/docs/Reference/Request.md +1 -3
- package/docs/Reference/Routes.md +22 -15
- package/docs/Reference/Server.md +69 -119
- package/docs/Reference/TypeScript.md +20 -22
- package/docs/Reference/Validation-and-Serialization.md +30 -55
- package/docs/Type-Providers.md +257 -0
- package/examples/asyncawait.js +1 -1
- package/examples/benchmark/hooks-benchmark-async-await.js +1 -1
- package/examples/benchmark/hooks-benchmark.js +1 -1
- package/examples/benchmark/simple.js +1 -1
- package/examples/hooks.js +2 -2
- package/examples/http2.js +1 -1
- package/examples/https.js +1 -1
- package/examples/parser.js +13 -3
- package/examples/route-prefix.js +1 -1
- package/examples/shared-schema.js +1 -1
- package/examples/simple-stream.js +18 -0
- package/examples/simple.js +1 -1
- package/examples/simple.mjs +1 -1
- package/examples/typescript-server.ts +1 -1
- package/examples/use-plugin.js +1 -1
- package/fastify.d.ts +34 -22
- package/fastify.js +40 -36
- package/lib/configValidator.js +902 -1023
- package/lib/contentTypeParser.js +6 -16
- package/lib/context.js +36 -10
- package/lib/decorate.js +3 -1
- package/lib/error-handler.js +158 -0
- package/lib/error-serializer.js +257 -0
- package/lib/errors.js +51 -9
- package/lib/fourOhFour.js +31 -20
- package/lib/handleRequest.js +10 -13
- package/lib/hooks.js +14 -9
- package/lib/pluginOverride.js +0 -3
- package/lib/pluginUtils.js +3 -2
- package/lib/reply.js +121 -175
- package/lib/request.js +13 -10
- package/lib/route.js +131 -138
- package/lib/schema-controller.js +2 -2
- package/lib/schemas.js +27 -1
- package/lib/server.js +242 -116
- package/lib/symbols.js +5 -3
- package/lib/validation.js +11 -9
- package/lib/warnings.js +4 -12
- package/lib/wrapThenable.js +4 -11
- package/package.json +37 -39
- package/test/404s.test.js +258 -125
- package/test/500s.test.js +3 -3
- package/test/als.test.js +1 -1
- package/test/async-await.test.js +20 -76
- package/test/bodyLimit.test.js +1 -1
- package/test/build-certificate.js +6 -7
- package/test/case-insensitive.test.js +4 -4
- package/test/close-pipelining.test.js +2 -2
- package/test/close.test.js +11 -11
- package/test/content-parser.test.js +32 -0
- package/test/context-config.test.js +52 -0
- package/test/custom-http-server.test.js +14 -7
- package/test/custom-parser-async.test.js +1 -66
- package/test/custom-parser.test.js +92 -159
- package/test/custom-querystring-parser.test.js +3 -3
- package/test/decorator.test.js +11 -13
- package/test/delete.test.js +6 -6
- 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/genReqId.test.js +1 -1
- package/test/get.test.js +4 -4
- package/test/handler-context.test.js +2 -2
- package/test/head.test.js +1 -1
- package/test/helper.js +19 -4
- package/test/hooks-async.test.js +15 -48
- package/test/hooks.on-ready.test.js +10 -5
- package/test/hooks.test.js +78 -119
- package/test/http2/closing.test.js +10 -16
- package/test/http2/constraint.test.js +1 -1
- package/test/http2/head.test.js +1 -1
- package/test/http2/plain.test.js +1 -1
- package/test/http2/secure-with-fallback.test.js +1 -1
- package/test/http2/secure.test.js +1 -1
- package/test/http2/unknown-http-method.test.js +4 -10
- package/test/https/custom-https-server.test.js +12 -6
- package/test/https/https.test.js +1 -1
- package/test/input-validation.js +3 -3
- package/test/internals/handleRequest.test.js +6 -43
- package/test/internals/initialConfig.test.js +41 -12
- package/test/internals/logger.test.js +2 -2
- package/test/internals/reply.test.js +317 -48
- package/test/internals/request.test.js +13 -7
- package/test/internals/server.test.js +88 -0
- package/test/listen.deprecated.test.js +202 -0
- package/test/listen.test.js +140 -145
- package/test/logger.test.js +82 -42
- package/test/maxRequestsPerSocket.test.js +8 -6
- package/test/middleware.test.js +2 -25
- package/test/nullable-validation.test.js +53 -16
- package/test/output-validation.test.js +1 -1
- package/test/plugin.test.js +47 -21
- package/test/pretty-print.test.js +22 -10
- package/test/promises.test.js +1 -1
- package/test/proto-poisoning.test.js +6 -6
- package/test/register.test.js +3 -3
- package/test/reply-error.test.js +126 -15
- package/test/reply-trailers.test.js +270 -0
- package/test/request-error.test.js +3 -6
- package/test/route-hooks.test.js +18 -18
- package/test/route-prefix.test.js +2 -1
- package/test/route.test.js +206 -22
- package/test/router-options.test.js +2 -2
- package/test/schema-examples.test.js +11 -5
- package/test/schema-feature.test.js +25 -20
- package/test/schema-serialization.test.js +9 -9
- package/test/schema-special-usage.test.js +5 -153
- package/test/schema-validation.test.js +9 -9
- package/test/skip-reply-send.test.js +2 -2
- package/test/stream.test.js +82 -23
- package/test/throw.test.js +8 -5
- package/test/trust-proxy.test.js +6 -6
- package/test/type-provider.test.js +20 -0
- package/test/types/fastify.test-d.ts +10 -18
- package/test/types/hooks.test-d.ts +61 -5
- package/test/types/import.js +2 -0
- package/test/types/import.ts +1 -0
- package/test/types/instance.test-d.ts +68 -17
- package/test/types/logger.test-d.ts +44 -15
- package/test/types/reply.test-d.ts +2 -1
- package/test/types/request.test-d.ts +71 -1
- 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 +424 -0
- package/test/url-rewriting.test.js +3 -3
- package/test/validation-error-handling.test.js +8 -8
- package/test/versioned-routes.test.js +30 -18
- package/test/wrapThenable.test.js +7 -6
- package/types/content-type-parser.d.ts +17 -8
- package/types/hooks.d.ts +182 -85
- package/types/instance.d.ts +286 -118
- package/types/logger.d.ts +18 -104
- package/types/plugin.d.ts +10 -4
- package/types/reply.d.ts +18 -12
- package/types/request.d.ts +13 -8
- package/types/route.d.ts +62 -34
- package/types/schema.d.ts +1 -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
|
@@ -7,7 +7,7 @@ import fastify, {
|
|
|
7
7
|
LightMyRequestChain,
|
|
8
8
|
LightMyRequestResponse,
|
|
9
9
|
LightMyRequestCallback,
|
|
10
|
-
InjectOptions
|
|
10
|
+
InjectOptions, FastifyBaseLogger
|
|
11
11
|
} from '../../fastify'
|
|
12
12
|
import * as http from 'http'
|
|
13
13
|
import * as https from 'https'
|
|
@@ -50,7 +50,9 @@ expectAssignable<InjectOptions>({ query: '' })
|
|
|
50
50
|
fastify({ http2: true, https: {} }).inject().then((resp) => {
|
|
51
51
|
expectAssignable<LightMyRequestResponse>(resp)
|
|
52
52
|
})
|
|
53
|
-
const lightMyRequestCallback: LightMyRequestCallback = (err: Error, response: LightMyRequestResponse) => {
|
|
53
|
+
const lightMyRequestCallback: LightMyRequestCallback = (err: Error, response: LightMyRequestResponse) => {
|
|
54
|
+
if (err) throw err
|
|
55
|
+
}
|
|
54
56
|
fastify({ http2: true, https: {} }).inject({}, lightMyRequestCallback)
|
|
55
57
|
|
|
56
58
|
// server options
|
|
@@ -70,9 +72,9 @@ expectAssignable<FastifyInstance>(fastify({ serializerOpts: { rounding: 'ceil' }
|
|
|
70
72
|
expectAssignable<FastifyInstance>(fastify({ serializerOpts: { ajv: { missingRefs: 'ignore' } } }))
|
|
71
73
|
expectAssignable<FastifyInstance>(fastify({ serializerOpts: { schema: { } } }))
|
|
72
74
|
expectAssignable<FastifyInstance>(fastify({ serializerOpts: { otherProp: { } } }))
|
|
73
|
-
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>>(fastify({ logger: true }))
|
|
74
|
-
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse,
|
|
75
|
-
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse,
|
|
75
|
+
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({ logger: true }))
|
|
76
|
+
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({ logger: true }))
|
|
77
|
+
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({
|
|
76
78
|
logger: {
|
|
77
79
|
level: 'info',
|
|
78
80
|
genReqId: () => 'request-id',
|
|
@@ -103,6 +105,7 @@ expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerR
|
|
|
103
105
|
}
|
|
104
106
|
}))
|
|
105
107
|
const customLogger = {
|
|
108
|
+
level: 'info',
|
|
106
109
|
info: () => { },
|
|
107
110
|
warn: () => { },
|
|
108
111
|
error: () => { },
|
|
@@ -111,7 +114,7 @@ const customLogger = {
|
|
|
111
114
|
debug: () => { },
|
|
112
115
|
child: () => customLogger
|
|
113
116
|
}
|
|
114
|
-
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse,
|
|
117
|
+
expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>>(fastify({ logger: customLogger }))
|
|
115
118
|
expectAssignable<FastifyInstance>(fastify({ serverFactory: () => http.createServer() }))
|
|
116
119
|
expectAssignable<FastifyInstance>(fastify({ caseSensitive: true }))
|
|
117
120
|
expectAssignable<FastifyInstance>(fastify({ requestIdHeader: 'request-id' }))
|
|
@@ -174,24 +177,13 @@ expectAssignable<FastifyInstance>(fastify({ return503OnClosing: true }))
|
|
|
174
177
|
expectAssignable<FastifyInstance>(fastify({
|
|
175
178
|
ajv: {
|
|
176
179
|
customOptions: {
|
|
177
|
-
|
|
180
|
+
removeAdditional: 'all'
|
|
178
181
|
},
|
|
179
182
|
plugins: [() => { }]
|
|
180
183
|
}
|
|
181
184
|
}))
|
|
182
185
|
expectAssignable<FastifyInstance>(fastify({
|
|
183
186
|
ajv: {
|
|
184
|
-
customOptions: {
|
|
185
|
-
nullable: false
|
|
186
|
-
},
|
|
187
|
-
plugins: [[() => { }, 'keyword']]
|
|
188
|
-
}
|
|
189
|
-
}))
|
|
190
|
-
expectAssignable<FastifyInstance>(fastify({
|
|
191
|
-
ajv: {
|
|
192
|
-
customOptions: {
|
|
193
|
-
nullable: false
|
|
194
|
-
},
|
|
195
187
|
plugins: [[() => { }, ['keyword1', 'keyword2']]]
|
|
196
188
|
}
|
|
197
189
|
}))
|
|
@@ -6,12 +6,16 @@ import fastify, {
|
|
|
6
6
|
FastifyRequest,
|
|
7
7
|
RawReplyDefaultExpression,
|
|
8
8
|
RawRequestDefaultExpression,
|
|
9
|
-
RawServerBase,
|
|
10
9
|
RouteOptions,
|
|
11
10
|
RegisterOptions,
|
|
12
|
-
FastifyPluginOptions
|
|
11
|
+
FastifyPluginOptions,
|
|
12
|
+
FastifySchema,
|
|
13
|
+
FastifyTypeProviderDefault,
|
|
14
|
+
ContextConfigDefault, FastifyContextConfig, RawServerDefault
|
|
13
15
|
} from '../../fastify'
|
|
14
16
|
import { preHandlerAsyncHookHandler, RequestPayload } from '../../types/hooks'
|
|
17
|
+
import { RouteGenericInterface } from '../../types/route'
|
|
18
|
+
import { ResolveFastifyRequestType } from '../../types/type-provider'
|
|
15
19
|
|
|
16
20
|
const server = fastify()
|
|
17
21
|
|
|
@@ -213,11 +217,15 @@ server.addHook('onClose', async (instance) => {
|
|
|
213
217
|
// Use case to monitor any regression on issue #3620
|
|
214
218
|
// ref.: https://github.com/fastify/fastify/issues/3620
|
|
215
219
|
const customTypedHook: preHandlerAsyncHookHandler<
|
|
216
|
-
|
|
220
|
+
RawServerDefault,
|
|
217
221
|
RawRequestDefaultExpression,
|
|
218
222
|
RawReplyDefaultExpression,
|
|
219
|
-
|
|
220
|
-
|
|
223
|
+
RouteGenericInterface,
|
|
224
|
+
ContextConfigDefault,
|
|
225
|
+
FastifySchema,
|
|
226
|
+
FastifyTypeProviderDefault,
|
|
227
|
+
ResolveFastifyRequestType<FastifyTypeProviderDefault, FastifySchema, RouteGenericInterface>
|
|
228
|
+
> = async function (request, reply): Promise<void> {
|
|
221
229
|
expectType<FastifyInstance>(this)
|
|
222
230
|
expectAssignable<FastifyRequest>(request)
|
|
223
231
|
expectAssignable<FastifyReply>(reply)
|
|
@@ -226,3 +234,51 @@ Record<string, unknown>
|
|
|
226
234
|
server.register(async (instance) => {
|
|
227
235
|
instance.addHook('preHandler', customTypedHook)
|
|
228
236
|
})
|
|
237
|
+
|
|
238
|
+
// Test custom Context Config types for hooks
|
|
239
|
+
type CustomContextConfig = FastifyContextConfig & {
|
|
240
|
+
foo: string;
|
|
241
|
+
bar: number;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
server.route<RouteGenericInterface, CustomContextConfig>({
|
|
245
|
+
method: 'GET',
|
|
246
|
+
url: '/',
|
|
247
|
+
handler: () => {},
|
|
248
|
+
onRequest: (request, reply) => {
|
|
249
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
250
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
251
|
+
},
|
|
252
|
+
preParsing: (request, reply) => {
|
|
253
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
254
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
255
|
+
},
|
|
256
|
+
preValidation: (request, reply) => {
|
|
257
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
258
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
259
|
+
},
|
|
260
|
+
preHandler: (request, reply) => {
|
|
261
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
262
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
263
|
+
},
|
|
264
|
+
preSerialization: (request, reply) => {
|
|
265
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
266
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
267
|
+
},
|
|
268
|
+
onSend: (request, reply) => {
|
|
269
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
270
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
271
|
+
},
|
|
272
|
+
onResponse: (request, reply) => {
|
|
273
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
274
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
275
|
+
},
|
|
276
|
+
onTimeout: (request, reply) => {
|
|
277
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
278
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
279
|
+
},
|
|
280
|
+
onError: (request, reply) => {
|
|
281
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
282
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
283
|
+
}
|
|
284
|
+
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { FastifyLogFn } from '../../fastify'
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { expectAssignable, expectDeprecated, expectError, expectNotDeprecated, expectType } from 'tsd'
|
|
1
2
|
import fastify, {
|
|
2
3
|
FastifyBodyParser,
|
|
3
4
|
FastifyError,
|
|
@@ -6,10 +7,9 @@ import fastify, {
|
|
|
6
7
|
RawRequestDefaultExpression,
|
|
7
8
|
RawServerDefault
|
|
8
9
|
} from '../../fastify'
|
|
9
|
-
import { expectAssignable, expectError, expectType } from 'tsd'
|
|
10
|
-
import { FastifyRequest } from '../../types/request'
|
|
11
|
-
import { FastifyReply } from '../../types/reply'
|
|
12
10
|
import { HookHandlerDoneFunction } from '../../types/hooks'
|
|
11
|
+
import { FastifyReply } from '../../types/reply'
|
|
12
|
+
import { FastifyRequest } from '../../types/request'
|
|
13
13
|
import { FastifySchemaControllerOptions } from '../../types/schema'
|
|
14
14
|
|
|
15
15
|
const server = fastify()
|
|
@@ -31,6 +31,7 @@ expectType<string>(server.printPlugins())
|
|
|
31
31
|
|
|
32
32
|
expectAssignable<FastifyInstance>(
|
|
33
33
|
server.setErrorHandler(function (error, request, reply) {
|
|
34
|
+
expectType<FastifyError>(error)
|
|
34
35
|
expectAssignable<FastifyInstance>(this)
|
|
35
36
|
})
|
|
36
37
|
)
|
|
@@ -47,10 +48,14 @@ server.setErrorHandler(fastifyErrorHandler)
|
|
|
47
48
|
async function asyncFastifyErrorHandler (this: FastifyInstance, error: FastifyError) {}
|
|
48
49
|
server.setErrorHandler(asyncFastifyErrorHandler)
|
|
49
50
|
|
|
50
|
-
function nodeJSErrorHandler (error: NodeJS.ErrnoException) {
|
|
51
|
+
function nodeJSErrorHandler (error: NodeJS.ErrnoException) {
|
|
52
|
+
if (error) { throw error }
|
|
53
|
+
}
|
|
51
54
|
server.setErrorHandler(nodeJSErrorHandler)
|
|
52
55
|
|
|
53
|
-
function asyncNodeJSErrorHandler (error: NodeJS.ErrnoException) {
|
|
56
|
+
function asyncNodeJSErrorHandler (error: NodeJS.ErrnoException) {
|
|
57
|
+
if (error) { throw error }
|
|
58
|
+
}
|
|
54
59
|
server.setErrorHandler(asyncNodeJSErrorHandler)
|
|
55
60
|
|
|
56
61
|
class CustomError extends Error {
|
|
@@ -82,20 +87,20 @@ expectError(server.setErrorHandler<CustomError, ReplyPayload>((error, request, r
|
|
|
82
87
|
reply.send({ test: 'foo' })
|
|
83
88
|
}))
|
|
84
89
|
// typed sync error handler return error
|
|
85
|
-
|
|
90
|
+
server.setErrorHandler<CustomError, ReplyPayload>((error, request, reply) => {
|
|
86
91
|
expectType<CustomError>(error)
|
|
87
92
|
return { test: 'foo' }
|
|
88
|
-
})
|
|
93
|
+
})
|
|
89
94
|
// typed async error handler send error
|
|
90
95
|
expectError(server.setErrorHandler<CustomError, ReplyPayload>(async (error, request, reply) => {
|
|
91
96
|
expectType<CustomError>(error)
|
|
92
97
|
reply.send({ test: 'foo' })
|
|
93
98
|
}))
|
|
94
99
|
// typed async error handler return error
|
|
95
|
-
|
|
100
|
+
server.setErrorHandler<CustomError, ReplyPayload>(async (error, request, reply) => {
|
|
96
101
|
expectType<CustomError>(error)
|
|
97
102
|
return { test: 'foo' }
|
|
98
|
-
})
|
|
103
|
+
})
|
|
99
104
|
|
|
100
105
|
function notFoundHandler (request: FastifyRequest, reply: FastifyReply) {}
|
|
101
106
|
function notFoundpreHandlerHandler (request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) { done() }
|
|
@@ -110,7 +115,9 @@ server.setNotFoundHandler({ preValidation: notFoundpreValidationHandler }, notFo
|
|
|
110
115
|
server.setNotFoundHandler({ preValidation: notFoundpreValidationAsyncHandler }, notFoundHandler)
|
|
111
116
|
server.setNotFoundHandler({ preHandler: notFoundpreHandlerHandler, preValidation: notFoundpreValidationHandler }, notFoundHandler)
|
|
112
117
|
|
|
113
|
-
function invalidErrorHandler (error: number) {
|
|
118
|
+
function invalidErrorHandler (error: number) {
|
|
119
|
+
if (error) throw error
|
|
120
|
+
}
|
|
114
121
|
expectError(server.setErrorHandler(invalidErrorHandler))
|
|
115
122
|
|
|
116
123
|
server.setSchemaController({
|
|
@@ -148,16 +155,30 @@ expectError(server.setReplySerializer(invalidReplySerializer))
|
|
|
148
155
|
function serializerWithInvalidReturn (payload: unknown, statusCode: number) {}
|
|
149
156
|
expectError(server.setReplySerializer(serializerWithInvalidReturn))
|
|
150
157
|
|
|
151
|
-
function invalidSchemaErrorFormatter () {
|
|
158
|
+
function invalidSchemaErrorFormatter (err: Error) {
|
|
159
|
+
if (err) { throw err }
|
|
160
|
+
}
|
|
152
161
|
expectError(server.setSchemaErrorFormatter(invalidSchemaErrorFormatter))
|
|
153
162
|
|
|
154
163
|
// test listen method callback
|
|
155
|
-
expectAssignable<void>(server.listen(3000, '', 0, (err, address) => {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
expectAssignable<void>(server.listen('3000', '', (err, address) => {
|
|
159
|
-
|
|
160
|
-
|
|
164
|
+
expectAssignable<void>(server.listen(3000, '', 0, (err, address) => {
|
|
165
|
+
expectType<Error | null>(err)
|
|
166
|
+
}))
|
|
167
|
+
expectAssignable<void>(server.listen('3000', '', 0, (err, address) => {
|
|
168
|
+
expectType<Error | null>(err)
|
|
169
|
+
}))
|
|
170
|
+
expectAssignable<void>(server.listen(3000, '', (err, address) => {
|
|
171
|
+
expectType<Error | null>(err)
|
|
172
|
+
}))
|
|
173
|
+
expectAssignable<void>(server.listen('3000', '', (err, address) => {
|
|
174
|
+
expectType<Error | null>(err)
|
|
175
|
+
}))
|
|
176
|
+
expectAssignable<void>(server.listen(3000, (err, address) => {
|
|
177
|
+
expectType<Error | null>(err)
|
|
178
|
+
}))
|
|
179
|
+
expectAssignable<void>(server.listen('3000', (err, address) => {
|
|
180
|
+
expectType<Error | null>(err)
|
|
181
|
+
}))
|
|
161
182
|
|
|
162
183
|
// test listen method callback types
|
|
163
184
|
expectAssignable<void>(server.listen('3000', (err, address) => {
|
|
@@ -173,13 +194,43 @@ expectAssignable<PromiseLike<string>>(server.listen('3000', '', 0))
|
|
|
173
194
|
expectAssignable<PromiseLike<string>>(server.listen(3000, ''))
|
|
174
195
|
expectAssignable<PromiseLike<string>>(server.listen('3000', ''))
|
|
175
196
|
|
|
197
|
+
// Test variadic listen signatures Typescript deprecation
|
|
198
|
+
expectDeprecated(server.listen(3000))
|
|
199
|
+
expectDeprecated(server.listen('3000'))
|
|
200
|
+
expectDeprecated(server.listen(3000, '', 0))
|
|
201
|
+
expectDeprecated(server.listen('3000', '', 0))
|
|
202
|
+
expectDeprecated(server.listen(3000, ''))
|
|
203
|
+
expectDeprecated(server.listen('3000', ''))
|
|
204
|
+
|
|
176
205
|
// test listen opts objects
|
|
206
|
+
expectAssignable<PromiseLike<string>>(server.listen())
|
|
177
207
|
expectAssignable<PromiseLike<string>>(server.listen({ port: 3000 }))
|
|
178
208
|
expectAssignable<PromiseLike<string>>(server.listen({ port: 3000, host: '0.0.0.0' }))
|
|
179
209
|
expectAssignable<PromiseLike<string>>(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42 }))
|
|
210
|
+
expectAssignable<PromiseLike<string>>(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42, exclusive: true }))
|
|
211
|
+
expectAssignable<PromiseLike<string>>(server.listen({ port: 3000, host: '::/0', ipv6Only: true }))
|
|
212
|
+
|
|
213
|
+
expectAssignable<void>(server.listen(() => {}))
|
|
180
214
|
expectAssignable<void>(server.listen({ port: 3000 }, () => {}))
|
|
181
215
|
expectAssignable<void>(server.listen({ port: 3000, host: '0.0.0.0' }, () => {}))
|
|
182
216
|
expectAssignable<void>(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42 }, () => {}))
|
|
217
|
+
expectAssignable<void>(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42, exclusive: true }, () => {}))
|
|
218
|
+
expectAssignable<void>(server.listen({ port: 3000, host: '::/0', ipv6Only: true }, () => {}))
|
|
219
|
+
|
|
220
|
+
// test listen opts objects Typescript deprectation exclusion
|
|
221
|
+
expectNotDeprecated(server.listen())
|
|
222
|
+
expectNotDeprecated(server.listen({ port: 3000 }))
|
|
223
|
+
expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0' }))
|
|
224
|
+
expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42 }))
|
|
225
|
+
expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42, exclusive: true }))
|
|
226
|
+
expectNotDeprecated(server.listen({ port: 3000, host: '::/0', ipv6Only: true }))
|
|
227
|
+
|
|
228
|
+
expectNotDeprecated(server.listen(() => {}))
|
|
229
|
+
expectNotDeprecated(server.listen({ port: 3000 }, () => {}))
|
|
230
|
+
expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0' }, () => {}))
|
|
231
|
+
expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42 }, () => {}))
|
|
232
|
+
expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42, exclusive: true }, () => {}))
|
|
233
|
+
expectNotDeprecated(server.listen({ port: 3000, host: '::/0', ipv6Only: true }, () => {}))
|
|
183
234
|
|
|
184
235
|
expectAssignable<void>(server.routing({} as RawRequestDefaultExpression, {} as RawReplyDefaultExpression))
|
|
185
236
|
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { expectType } from 'tsd'
|
|
2
|
-
import fastify, {
|
|
2
|
+
import fastify, {
|
|
3
|
+
FastifyLogFn,
|
|
4
|
+
LogLevel,
|
|
5
|
+
FastifyLoggerInstance,
|
|
6
|
+
FastifyRequest,
|
|
7
|
+
FastifyReply,
|
|
8
|
+
FastifyBaseLogger
|
|
9
|
+
} from '../../fastify'
|
|
3
10
|
import { Server, IncomingMessage, ServerResponse } from 'http'
|
|
4
|
-
import pino from 'pino'
|
|
5
11
|
import * as fs from 'fs'
|
|
12
|
+
import P from 'pino'
|
|
6
13
|
|
|
7
14
|
expectType<FastifyLoggerInstance>(fastify().log)
|
|
8
15
|
|
|
@@ -15,14 +22,17 @@ class Foo {}
|
|
|
15
22
|
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel]({ foo: 'bar' }))
|
|
16
23
|
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](new Error()))
|
|
17
24
|
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](new Foo()))
|
|
18
|
-
expectType<void>(fastify<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance>().log[logLevel as LogLevel](0))
|
|
19
25
|
})
|
|
20
26
|
|
|
21
|
-
|
|
27
|
+
/*
|
|
28
|
+
// TODO make pino export BaseLogger again
|
|
29
|
+
interface CustomLogger extends FastifyBaseLogger {
|
|
22
30
|
customMethod(msg: string, ...args: unknown[]): void;
|
|
23
31
|
}
|
|
24
32
|
|
|
33
|
+
// // ToDo https://github.com/pinojs/pino/issues/1100
|
|
25
34
|
class CustomLoggerImpl implements CustomLogger {
|
|
35
|
+
level = 'info'
|
|
26
36
|
customMethod (msg: string, ...args: unknown[]) { console.log(msg, args) }
|
|
27
37
|
|
|
28
38
|
// Implementation signature must be compatible with all overloads of FastifyLogFn
|
|
@@ -35,7 +45,9 @@ class CustomLoggerImpl implements CustomLogger {
|
|
|
35
45
|
fatal (...args: unknown[]) { console.log(args) }
|
|
36
46
|
trace (...args: unknown[]) { console.log(args) }
|
|
37
47
|
debug (...args: unknown[]) { console.log(args) }
|
|
38
|
-
|
|
48
|
+
silent (...args: unknown[]) { }
|
|
49
|
+
|
|
50
|
+
child (bindings: P.Bindings, options?: P.ChildLoggerOptions): CustomLoggerImpl { return new CustomLoggerImpl() }
|
|
39
51
|
}
|
|
40
52
|
|
|
41
53
|
const customLogger = new CustomLoggerImpl()
|
|
@@ -48,20 +60,21 @@ CustomLoggerImpl
|
|
|
48
60
|
>({ logger: customLogger })
|
|
49
61
|
|
|
50
62
|
expectType<CustomLoggerImpl>(serverWithCustomLogger.log)
|
|
63
|
+
*/
|
|
51
64
|
|
|
52
65
|
const serverWithPino = fastify<
|
|
53
66
|
Server,
|
|
54
67
|
IncomingMessage,
|
|
55
68
|
ServerResponse,
|
|
56
|
-
|
|
69
|
+
P.Logger
|
|
57
70
|
>({
|
|
58
|
-
logger:
|
|
71
|
+
logger: P({
|
|
59
72
|
level: 'info',
|
|
60
73
|
redact: ['x-userinfo']
|
|
61
74
|
})
|
|
62
75
|
})
|
|
63
76
|
|
|
64
|
-
expectType<
|
|
77
|
+
expectType<P.Logger>(serverWithPino.log)
|
|
65
78
|
|
|
66
79
|
const serverWithLogOptions = fastify<
|
|
67
80
|
Server,
|
|
@@ -94,16 +107,16 @@ const serverAutoInferringTypes = fastify({
|
|
|
94
107
|
}
|
|
95
108
|
})
|
|
96
109
|
|
|
97
|
-
expectType<
|
|
110
|
+
expectType<FastifyBaseLogger>(serverAutoInferringTypes.log)
|
|
98
111
|
|
|
99
112
|
const serverWithAutoInferredPino = fastify({
|
|
100
|
-
logger:
|
|
113
|
+
logger: P({
|
|
101
114
|
level: 'info',
|
|
102
115
|
redact: ['x-userinfo']
|
|
103
116
|
})
|
|
104
117
|
})
|
|
105
118
|
|
|
106
|
-
expectType<
|
|
119
|
+
expectType<P.Logger>(serverWithAutoInferredPino.log)
|
|
107
120
|
|
|
108
121
|
const serverAutoInferredFileOption = fastify({
|
|
109
122
|
logger: {
|
|
@@ -112,7 +125,7 @@ const serverAutoInferredFileOption = fastify({
|
|
|
112
125
|
}
|
|
113
126
|
})
|
|
114
127
|
|
|
115
|
-
expectType<
|
|
128
|
+
expectType<FastifyBaseLogger>(serverAutoInferredFileOption.log)
|
|
116
129
|
|
|
117
130
|
const serverAutoInferredPinoPrettyBooleanOption = fastify({
|
|
118
131
|
logger: {
|
|
@@ -120,7 +133,7 @@ const serverAutoInferredPinoPrettyBooleanOption = fastify({
|
|
|
120
133
|
}
|
|
121
134
|
})
|
|
122
135
|
|
|
123
|
-
expectType<
|
|
136
|
+
expectType<FastifyBaseLogger>(serverAutoInferredPinoPrettyBooleanOption.log)
|
|
124
137
|
|
|
125
138
|
const serverAutoInferredPinoPrettyObjectOption = fastify({
|
|
126
139
|
logger: {
|
|
@@ -141,7 +154,7 @@ const serverAutoInferredPinoPrettyObjectOption = fastify({
|
|
|
141
154
|
}
|
|
142
155
|
})
|
|
143
156
|
|
|
144
|
-
expectType<
|
|
157
|
+
expectType<FastifyBaseLogger>(serverAutoInferredPinoPrettyObjectOption.log)
|
|
145
158
|
|
|
146
159
|
const serverAutoInferredSerializerObjectOption = fastify({
|
|
147
160
|
logger: {
|
|
@@ -176,10 +189,26 @@ const serverAutoInferredSerializerObjectOption = fastify({
|
|
|
176
189
|
}
|
|
177
190
|
})
|
|
178
191
|
|
|
179
|
-
expectType<
|
|
192
|
+
expectType<FastifyBaseLogger>(serverAutoInferredSerializerObjectOption.log)
|
|
180
193
|
|
|
181
194
|
const passStreamAsOption = fastify({
|
|
182
195
|
logger: {
|
|
183
196
|
stream: fs.createWriteStream('/tmp/stream.out')
|
|
184
197
|
}
|
|
185
198
|
})
|
|
199
|
+
|
|
200
|
+
expectType<FastifyBaseLogger>(passStreamAsOption.log)
|
|
201
|
+
|
|
202
|
+
const passPinoOption = fastify({
|
|
203
|
+
logger: {
|
|
204
|
+
redact: ['custom'],
|
|
205
|
+
messageKey: 'msg',
|
|
206
|
+
nestedKey: 'nested',
|
|
207
|
+
prettyPrint: {
|
|
208
|
+
|
|
209
|
+
},
|
|
210
|
+
enabled: true
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
expectType<FastifyBaseLogger>(passPinoOption.log)
|
|
@@ -4,6 +4,7 @@ import { RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } fro
|
|
|
4
4
|
import { FastifyLoggerInstance } from '../../types/logger'
|
|
5
5
|
import { RouteGenericInterface } from '../../types/route'
|
|
6
6
|
import { FastifyInstance } from '../../types/instance'
|
|
7
|
+
import { Buffer } from 'buffer'
|
|
7
8
|
|
|
8
9
|
const getHandler: RouteHandlerMethod = function (_request, reply) {
|
|
9
10
|
expectType<RawReplyDefaultExpression>(reply.raw)
|
|
@@ -28,7 +29,7 @@ const getHandler: RouteHandlerMethod = function (_request, reply) {
|
|
|
28
29
|
expectType<() => number>(reply.getResponseTime)
|
|
29
30
|
expectType<(contentType: string) => FastifyReply>(reply.type)
|
|
30
31
|
expectType<(fn: (payload: any) => string) => FastifyReply>(reply.serializer)
|
|
31
|
-
expectType<(payload: any) => string>(reply.serialize)
|
|
32
|
+
expectType<(payload: any) => string | ArrayBuffer | Buffer>(reply.serialize)
|
|
32
33
|
expectType<(fulfilled: () => void, rejected: (err: Error) => void) => void>(reply.then)
|
|
33
34
|
expectType<FastifyInstance>(reply.server)
|
|
34
35
|
}
|
|
@@ -1,10 +1,27 @@
|
|
|
1
1
|
import { expectType } from 'tsd'
|
|
2
|
-
import
|
|
2
|
+
import pino from 'pino'
|
|
3
|
+
import fastify, {
|
|
4
|
+
RouteHandler,
|
|
5
|
+
RawRequestDefaultExpression,
|
|
6
|
+
RequestBodyDefault,
|
|
7
|
+
RequestGenericInterface,
|
|
8
|
+
FastifyContext,
|
|
9
|
+
ContextConfigDefault,
|
|
10
|
+
FastifyContextConfig,
|
|
11
|
+
FastifyLogFn,
|
|
12
|
+
RouteHandlerMethod,
|
|
13
|
+
RawServerDefault,
|
|
14
|
+
RawReplyDefaultExpression,
|
|
15
|
+
FastifySchema,
|
|
16
|
+
FastifyTypeProviderDefault
|
|
17
|
+
} from '../../fastify'
|
|
3
18
|
import { RequestParamsDefault, RequestHeadersDefault, RequestQuerystringDefault } from '../../types/utils'
|
|
4
19
|
import { FastifyLoggerInstance } from '../../types/logger'
|
|
5
20
|
import { FastifyRequest } from '../../types/request'
|
|
6
21
|
import { FastifyReply } from '../../types/reply'
|
|
7
22
|
import { FastifyInstance } from '../../types/instance'
|
|
23
|
+
import { RouteGenericInterface } from '../../types/route'
|
|
24
|
+
import { ResolveFastifyReplyReturnType, ResolveFastifyRequestType } from '../../types/type-provider'
|
|
8
25
|
|
|
9
26
|
interface RequestBody {
|
|
10
27
|
content: string;
|
|
@@ -38,6 +55,10 @@ type CustomRequest = FastifyRequest<{
|
|
|
38
55
|
Headers: RequestHeaders;
|
|
39
56
|
}>
|
|
40
57
|
|
|
58
|
+
interface CustomLoggerInterface extends FastifyLoggerInstance {
|
|
59
|
+
foo: FastifyLogFn; // custom severity logger method
|
|
60
|
+
}
|
|
61
|
+
|
|
41
62
|
const getHandler: RouteHandler = function (request, _reply) {
|
|
42
63
|
expectType<string>(request.url)
|
|
43
64
|
expectType<string>(request.method)
|
|
@@ -64,6 +85,10 @@ const getHandler: RouteHandler = function (request, _reply) {
|
|
|
64
85
|
expectType<FastifyInstance>(request.server)
|
|
65
86
|
}
|
|
66
87
|
|
|
88
|
+
const getHandlerWithCustomLogger: RouteHandlerMethod<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, RouteGenericInterface, ContextConfigDefault, FastifySchema, FastifyTypeProviderDefault, ResolveFastifyReplyReturnType<FastifyTypeProviderDefault, FastifySchema, RouteGenericInterface>, ResolveFastifyRequestType<FastifyTypeProviderDefault, FastifySchema, RouteGenericInterface>, CustomLoggerInterface> = function (request, _reply) {
|
|
89
|
+
expectType<CustomLoggerInterface>(request.log)
|
|
90
|
+
}
|
|
91
|
+
|
|
67
92
|
const postHandler: Handler = function (request) {
|
|
68
93
|
expectType<RequestBody>(request.body)
|
|
69
94
|
expectType<RequestParams>(request.params)
|
|
@@ -96,3 +121,48 @@ const server = fastify()
|
|
|
96
121
|
server.get('/get', getHandler)
|
|
97
122
|
server.post('/post', postHandler)
|
|
98
123
|
server.put('/put', putHandler)
|
|
124
|
+
|
|
125
|
+
const customLogger: CustomLoggerInterface = {
|
|
126
|
+
level: 'info',
|
|
127
|
+
version: '5.0',
|
|
128
|
+
useOnlyCustomLevels: false,
|
|
129
|
+
useLevelLabels: false,
|
|
130
|
+
levels: { labels: [], values: {} },
|
|
131
|
+
eventNames: () => [],
|
|
132
|
+
listenerCount: (eventName: string | symbol) => 0,
|
|
133
|
+
bindings: () => ({}),
|
|
134
|
+
flush: () => () => {},
|
|
135
|
+
customLevels: { foo: 1 },
|
|
136
|
+
isLevelEnabled: () => false,
|
|
137
|
+
levelVal: 0,
|
|
138
|
+
silent: () => { },
|
|
139
|
+
info: () => { },
|
|
140
|
+
warn: () => { },
|
|
141
|
+
error: () => { },
|
|
142
|
+
fatal: () => { },
|
|
143
|
+
trace: () => { },
|
|
144
|
+
debug: () => { },
|
|
145
|
+
foo: () => { }, // custom severity logger method
|
|
146
|
+
on: (event, listener) => customLogger,
|
|
147
|
+
emit: (event, listener) => false,
|
|
148
|
+
off: (event, listener) => customLogger,
|
|
149
|
+
addListener: (event, listener) => customLogger,
|
|
150
|
+
prependListener: (event, listener) => customLogger,
|
|
151
|
+
prependOnceListener: (event, listener) => customLogger,
|
|
152
|
+
removeListener: (event, listener) => customLogger,
|
|
153
|
+
removeAllListeners: (event) => customLogger,
|
|
154
|
+
setMaxListeners: (n) => customLogger,
|
|
155
|
+
getMaxListeners: () => 0,
|
|
156
|
+
listeners: () => [],
|
|
157
|
+
rawListeners: () => [],
|
|
158
|
+
once: (event, listener) => customLogger,
|
|
159
|
+
child: () => customLogger as pino.Logger<never>
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const serverWithCustomLogger = fastify({ logger: customLogger })
|
|
163
|
+
expectType<
|
|
164
|
+
FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>
|
|
165
|
+
& PromiseLike<FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>>
|
|
166
|
+
>(serverWithCustomLogger)
|
|
167
|
+
|
|
168
|
+
serverWithCustomLogger.get('/get', getHandlerWithCustomLogger)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import fastify, { FastifyInstance, FastifyRequest, FastifyReply, RouteHandlerMethod } from '../../fastify'
|
|
2
|
-
import { expectType, expectError, expectAssignable } from 'tsd'
|
|
2
|
+
import { expectType, expectError, expectAssignable, printType } from 'tsd'
|
|
3
3
|
import { HTTPMethods } from '../../types/utils'
|
|
4
4
|
import * as http from 'http'
|
|
5
5
|
import { RequestPayload } from '../../types/hooks'
|
|
@@ -44,7 +44,13 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
|
|
|
44
44
|
expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', {}, routeHandler))
|
|
45
45
|
expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', { handler: routeHandler }))
|
|
46
46
|
|
|
47
|
-
expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', {
|
|
47
|
+
expectType<FastifyInstance>(fastify()[lowerCaseMethod]('/', {
|
|
48
|
+
handler: routeHandler,
|
|
49
|
+
errorHandler: (error, request, reply) => {
|
|
50
|
+
expectType<FastifyError>(error)
|
|
51
|
+
reply.send('error')
|
|
52
|
+
}
|
|
53
|
+
}))
|
|
48
54
|
|
|
49
55
|
interface BodyInterface { prop: string }
|
|
50
56
|
interface QuerystringInterface { prop: number }
|