fastify 4.19.1 → 4.20.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.
- package/.c8rc.json +8 -0
- package/.taprc +3 -2
- package/SECURITY.md +9 -0
- package/docs/Guides/Prototype-Poisoning.md +2 -2
- package/docs/Reference/Errors.md +39 -17
- package/docs/Reference/Logging.md +1 -1
- package/docs/Reference/Plugins.md +4 -0
- package/docs/Reference/Routes.md +8 -0
- package/docs/Reference/Server.md +38 -0
- package/fastify.d.ts +3 -2
- package/fastify.js +51 -24
- package/lib/context.js +6 -0
- package/lib/errors.js +50 -19
- package/lib/fourOhFour.js +5 -9
- package/lib/handleRequest.js +3 -5
- package/lib/hooks.js +91 -25
- package/lib/logger.js +40 -3
- package/lib/reply.js +3 -9
- package/lib/reqIdGenFactory.js +18 -3
- package/lib/route.js +14 -61
- package/lib/schema-controller.js +2 -0
- package/lib/server.js +23 -8
- package/lib/symbols.js +1 -0
- package/package.json +8 -7
- package/test/500s.test.js +22 -0
- package/test/childLoggerFactory.test.js +91 -0
- package/test/custom-http-server.test.js +42 -0
- package/test/encapsulated-child-logger-factory.test.js +69 -0
- package/test/fastify-instance.test.js +43 -10
- package/test/inject.test.js +1 -2
- package/test/internals/errors.test.js +843 -0
- package/test/internals/hookRunner.test.js +22 -8
- package/test/internals/initialConfig.test.js +9 -2
- package/test/internals/reply.test.js +49 -43
- package/test/internals/reqIdGenFactory.test.js +129 -0
- package/test/internals/request-validate.test.js +40 -1
- package/test/internals/request.test.js +14 -4
- package/test/reply-error.test.js +25 -0
- package/test/request-id.test.js +131 -0
- package/test/route.test.js +135 -0
- package/test/server.test.js +64 -2
- package/test/types/errors.test-d.ts +82 -0
- package/test/types/fastify.test-d.ts +4 -0
- package/test/types/hooks.test-d.ts +65 -64
- package/test/types/instance.test-d.ts +139 -0
- package/test/types/reply.test-d.ts +9 -8
- package/test/types/request.test-d.ts +17 -18
- package/test/types/route.test-d.ts +10 -7
- package/test/types/type-provider.test-d.ts +4 -0
- package/types/context.d.ts +3 -3
- package/types/errors.d.ts +29 -23
- package/types/instance.d.ts +39 -7
- package/types/logger.d.ts +25 -0
- package/types/reply.d.ts +10 -5
- package/types/request.d.ts +5 -5
- package/types/route.d.ts +8 -7
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { expectType, expectError, expectAssignable, printType } from 'tsd'
|
|
3
|
-
import { HTTPMethods } from '../../types/utils'
|
|
1
|
+
import { FastifyError } from '@fastify/error'
|
|
4
2
|
import * as http from 'http'
|
|
3
|
+
import { expectAssignable, expectError, expectType } from 'tsd'
|
|
4
|
+
import fastify, { FastifyInstance, FastifyReply, FastifyRequest, RouteHandlerMethod } from '../../fastify'
|
|
5
5
|
import { RequestPayload } from '../../types/hooks'
|
|
6
|
-
import {
|
|
6
|
+
import { HTTPMethods } from '../../types/utils'
|
|
7
7
|
|
|
8
8
|
/*
|
|
9
9
|
* Testing Fastify HTTP Routes and Route Shorthands.
|
|
@@ -57,6 +57,9 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
|
|
|
57
57
|
errorHandler: (error, request, reply) => {
|
|
58
58
|
expectType<FastifyError>(error)
|
|
59
59
|
reply.send('error')
|
|
60
|
+
},
|
|
61
|
+
childLoggerFactory: function (logger, bindings, opts) {
|
|
62
|
+
return logger.child(bindings, opts)
|
|
60
63
|
}
|
|
61
64
|
}))
|
|
62
65
|
|
|
@@ -74,7 +77,7 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
|
|
|
74
77
|
Headers: HeadersInterface;
|
|
75
78
|
}
|
|
76
79
|
|
|
77
|
-
fastify()[lowerCaseMethod]<RouteGeneric, RouteSpecificContextConfigType>('/', { config: { foo: 'bar', bar: 100, extra: true
|
|
80
|
+
fastify()[lowerCaseMethod]<RouteGeneric, RouteSpecificContextConfigType>('/', { config: { foo: 'bar', bar: 100, extra: true } }, (req, res) => {
|
|
78
81
|
expectType<BodyInterface>(req.body)
|
|
79
82
|
expectType<QuerystringInterface>(req.query)
|
|
80
83
|
expectType<ParamsInterface>(req.params)
|
|
@@ -94,7 +97,7 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
|
|
|
94
97
|
fastify().route<RouteGeneric>({
|
|
95
98
|
url: '/',
|
|
96
99
|
method: method as HTTPMethods,
|
|
97
|
-
config: { foo: 'bar', bar: 100
|
|
100
|
+
config: { foo: 'bar', bar: 100 },
|
|
98
101
|
prefixTrailingSlash: 'slash',
|
|
99
102
|
onRequest: (req, res, done) => { // these handlers are tested in `hooks.test-d.ts`
|
|
100
103
|
expectType<BodyInterface>(req.body)
|
|
@@ -231,7 +234,7 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
|
|
|
231
234
|
fastify().route<RouteGeneric>({
|
|
232
235
|
url: '/',
|
|
233
236
|
method: method as HTTPMethods,
|
|
234
|
-
config: { foo: 'bar', bar: 100
|
|
237
|
+
config: { foo: 'bar', bar: 100 },
|
|
235
238
|
prefixTrailingSlash: 'slash',
|
|
236
239
|
onRequest: async (req, res, done) => { // these handlers are tested in `hooks.test-d.ts`
|
|
237
240
|
expectType<BodyInterface>(req.body)
|
|
@@ -299,6 +299,8 @@ expectAssignable(server.withTypeProvider<TypeBoxProvider>().get(
|
|
|
299
299
|
res.send('hello')
|
|
300
300
|
res.send(42)
|
|
301
301
|
res.send({ error: 'error' })
|
|
302
|
+
expectType<(payload?: string | number | { error: string }) => typeof res>(res.code(200).send)
|
|
303
|
+
expectError<(payload?: unknown) => typeof res>(res.code(200).send)
|
|
302
304
|
}
|
|
303
305
|
))
|
|
304
306
|
|
|
@@ -527,6 +529,8 @@ expectAssignable(server.withTypeProvider<JsonSchemaToTsProvider>().get(
|
|
|
527
529
|
res.send('hello')
|
|
528
530
|
res.send(42)
|
|
529
531
|
res.send({ error: 'error' })
|
|
532
|
+
expectType<(payload?: string | number | { [x: string]: unknown, error?: string | undefined }) => typeof res>(res.code(200).send)
|
|
533
|
+
expectError<(payload?: unknown) => typeof res>(res.code(200).send)
|
|
530
534
|
}
|
|
531
535
|
))
|
|
532
536
|
|
package/types/context.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { ContextConfigDefault } from './utils'
|
|
2
1
|
import { FastifyRouteConfig } from './route'
|
|
2
|
+
import { ContextConfigDefault } from './utils'
|
|
3
3
|
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
5
|
-
export interface FastifyContextConfig
|
|
5
|
+
export interface FastifyContextConfig {
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -12,5 +12,5 @@ export interface FastifyContext<ContextConfig = ContextConfigDefault> {
|
|
|
12
12
|
/**
|
|
13
13
|
* @deprecated Use Request#routeConfig or Request#routeSchema instead
|
|
14
14
|
*/
|
|
15
|
-
config: FastifyContextConfig & ContextConfig;
|
|
15
|
+
config: FastifyContextConfig & FastifyRouteConfig & ContextConfig;
|
|
16
16
|
}
|
package/types/errors.d.ts
CHANGED
|
@@ -2,6 +2,14 @@ import { FastifyErrorConstructor } from '@fastify/error'
|
|
|
2
2
|
|
|
3
3
|
export type FastifyErrorCodes = Record<
|
|
4
4
|
'FST_ERR_NOT_FOUND' |
|
|
5
|
+
'FST_ERR_OPTIONS_NOT_OBJ' |
|
|
6
|
+
'FST_ERR_QSP_NOT_FN' |
|
|
7
|
+
'FST_ERR_SCHEMA_CONTROLLER_BUCKET_OPT_NOT_FN' |
|
|
8
|
+
'FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN' |
|
|
9
|
+
'FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_OBJ' |
|
|
10
|
+
'FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_ARR' |
|
|
11
|
+
'FST_ERR_VERSION_CONSTRAINT_NOT_STR' |
|
|
12
|
+
'FST_ERR_VALIDATION' |
|
|
5
13
|
'FST_ERR_CTP_ALREADY_PRESENT' |
|
|
6
14
|
'FST_ERR_CTP_INVALID_TYPE' |
|
|
7
15
|
'FST_ERR_CTP_EMPTY_TYPE' |
|
|
@@ -11,65 +19,63 @@ export type FastifyErrorCodes = Record<
|
|
|
11
19
|
'FST_ERR_CTP_INVALID_MEDIA_TYPE' |
|
|
12
20
|
'FST_ERR_CTP_INVALID_CONTENT_LENGTH' |
|
|
13
21
|
'FST_ERR_CTP_EMPTY_JSON_BODY' |
|
|
22
|
+
'FST_ERR_CTP_INSTANCE_ALREADY_STARTED' |
|
|
14
23
|
'FST_ERR_DEC_ALREADY_PRESENT' |
|
|
15
24
|
'FST_ERR_DEC_DEPENDENCY_INVALID_TYPE' |
|
|
16
25
|
'FST_ERR_DEC_MISSING_DEPENDENCY' |
|
|
17
26
|
'FST_ERR_DEC_AFTER_START' |
|
|
18
27
|
'FST_ERR_HOOK_INVALID_TYPE' |
|
|
19
28
|
'FST_ERR_HOOK_INVALID_HANDLER' |
|
|
29
|
+
'FST_ERR_HOOK_INVALID_ASYNC_HANDLER' |
|
|
30
|
+
'FST_ERR_HOOK_NOT_SUPPORTED' |
|
|
20
31
|
'FST_ERR_MISSING_MIDDLEWARE' |
|
|
21
32
|
'FST_ERR_HOOK_TIMEOUT' |
|
|
22
33
|
'FST_ERR_LOG_INVALID_DESTINATION' |
|
|
23
34
|
'FST_ERR_LOG_INVALID_LOGGER' |
|
|
24
35
|
'FST_ERR_REP_INVALID_PAYLOAD_TYPE' |
|
|
25
36
|
'FST_ERR_REP_ALREADY_SENT' |
|
|
26
|
-
'FST_ERR_REP_SENT_VALUE'|
|
|
27
|
-
'FST_ERR_SEND_INSIDE_ONERR'|
|
|
28
|
-
'FST_ERR_SEND_UNDEFINED_ERR'|
|
|
29
|
-
'FST_ERR_BAD_STATUS_CODE'|
|
|
37
|
+
'FST_ERR_REP_SENT_VALUE' |
|
|
38
|
+
'FST_ERR_SEND_INSIDE_ONERR' |
|
|
39
|
+
'FST_ERR_SEND_UNDEFINED_ERR' |
|
|
40
|
+
'FST_ERR_BAD_STATUS_CODE' |
|
|
30
41
|
'FST_ERR_BAD_TRAILER_NAME' |
|
|
31
42
|
'FST_ERR_BAD_TRAILER_VALUE' |
|
|
32
43
|
'FST_ERR_FAILED_ERROR_SERIALIZATION' |
|
|
33
44
|
'FST_ERR_MISSING_SERIALIZATION_FN' |
|
|
45
|
+
'FST_ERR_MISSING_CONTENTTYPE_SERIALIZATION_FN' |
|
|
34
46
|
'FST_ERR_REQ_INVALID_VALIDATION_INVOCATION' |
|
|
35
47
|
'FST_ERR_SCH_MISSING_ID' |
|
|
36
48
|
'FST_ERR_SCH_ALREADY_PRESENT' |
|
|
49
|
+
'FST_ERR_SCH_CONTENT_MISSING_SCHEMA' |
|
|
37
50
|
'FST_ERR_SCH_DUPLICATE' |
|
|
38
51
|
'FST_ERR_SCH_VALIDATION_BUILD' |
|
|
39
52
|
'FST_ERR_SCH_SERIALIZATION_BUILD' |
|
|
53
|
+
'FST_ERR_SCH_RESPONSE_SCHEMA_NOT_NESTED_2XX' |
|
|
40
54
|
'FST_ERR_HTTP2_INVALID_VERSION' |
|
|
41
55
|
'FST_ERR_INIT_OPTS_INVALID' |
|
|
42
56
|
'FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE' |
|
|
43
57
|
'FST_ERR_DUPLICATED_ROUTE' |
|
|
44
58
|
'FST_ERR_BAD_URL' |
|
|
59
|
+
'FST_ERR_ASYNC_CONSTRAINT' |
|
|
45
60
|
'FST_ERR_DEFAULT_ROUTE_INVALID_TYPE' |
|
|
46
61
|
'FST_ERR_INVALID_URL' |
|
|
47
|
-
'FST_ERR_REOPENED_CLOSE_SERVER' |
|
|
48
|
-
'FST_ERR_REOPENED_SERVER' |
|
|
49
|
-
'FST_ERR_PLUGIN_VERSION_MISMATCH' |
|
|
50
|
-
'FST_ERR_PLUGIN_CALLBACK_NOT_FN' |
|
|
51
|
-
'FST_ERR_PLUGIN_NOT_VALID' |
|
|
52
|
-
'FST_ERR_ROOT_PLG_BOOTED' |
|
|
53
|
-
'FST_ERR_PARENT_PLUGIN_BOOTED' |
|
|
54
|
-
'FST_ERR_PLUGIN_TIMEOUT' |
|
|
55
|
-
'FST_ERR_OPTIONS_NOT_OBJ' |
|
|
56
|
-
'FST_ERR_QSP_NOT_FN' |
|
|
57
|
-
'FST_ERR_SCHEMA_CONTROLLER_BUCKET_OPT_NOT_FN' |
|
|
58
|
-
'FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN' |
|
|
59
|
-
'FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_OBJ' |
|
|
60
|
-
'FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_ARR' |
|
|
61
|
-
'FST_ERR_VERSION_CONSTRAINT_NOT_STR' |
|
|
62
|
-
'FST_ERR_CTP_INSTANCE_ALREADY_STARTED' |
|
|
63
|
-
'FST_ERR_HOOK_NOT_SUPPORTED' |
|
|
64
|
-
'FST_ERR_SCH_RESPONSE_SCHEMA_NOT_NESTED_2XX' |
|
|
65
62
|
'FST_ERR_ROUTE_OPTIONS_NOT_OBJ' |
|
|
66
63
|
'FST_ERR_ROUTE_DUPLICATED_HANDLER' |
|
|
67
64
|
'FST_ERR_ROUTE_HANDLER_NOT_FN' |
|
|
68
65
|
'FST_ERR_ROUTE_MISSING_HANDLER' |
|
|
66
|
+
'FST_ERR_ROUTE_METHOD_INVALID' |
|
|
69
67
|
'FST_ERR_ROUTE_METHOD_NOT_SUPPORTED' |
|
|
70
68
|
'FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED' |
|
|
71
69
|
'FST_ERR_ROUTE_BODY_LIMIT_OPTION_NOT_INT' |
|
|
72
70
|
'FST_ERR_ROUTE_REWRITE_NOT_STR' |
|
|
71
|
+
'FST_ERR_REOPENED_CLOSE_SERVER' |
|
|
72
|
+
'FST_ERR_REOPENED_SERVER' |
|
|
73
|
+
'FST_ERR_INSTANCE_ALREADY_LISTENING' |
|
|
74
|
+
'FST_ERR_PLUGIN_VERSION_MISMATCH' |
|
|
73
75
|
'FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE' |
|
|
74
|
-
'
|
|
76
|
+
'FST_ERR_PLUGIN_CALLBACK_NOT_FN' |
|
|
77
|
+
'FST_ERR_PLUGIN_NOT_VALID' |
|
|
78
|
+
'FST_ERR_ROOT_PLG_BOOTED' |
|
|
79
|
+
'FST_ERR_PARENT_PLUGIN_BOOTED' |
|
|
80
|
+
'FST_ERR_PLUGIN_TIMEOUT'
|
|
75
81
|
, FastifyErrorConstructor>
|
package/types/instance.d.ts
CHANGED
|
@@ -4,11 +4,11 @@ import * as http from 'http'
|
|
|
4
4
|
import { CallbackFunc as LightMyRequestCallback, Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse } from 'light-my-request'
|
|
5
5
|
import { AddContentTypeParser, ConstructorAction, FastifyBodyParser, getDefaultJsonParser, hasContentTypeParser, ProtoAction, removeAllContentTypeParsers, removeContentTypeParser } from './content-type-parser'
|
|
6
6
|
import { onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onRegisterHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onRequestAbortAsyncHookHandler, onRequestAbortHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler, preCloseHookHandler, preCloseAsyncHookHandler } from './hooks'
|
|
7
|
-
import { FastifyBaseLogger } from './logger'
|
|
7
|
+
import { FastifyBaseLogger, FastifyChildLoggerFactory } from './logger'
|
|
8
8
|
import { FastifyRegister } from './register'
|
|
9
9
|
import { FastifyReply } from './reply'
|
|
10
10
|
import { FastifyRequest } from './request'
|
|
11
|
-
import { DefaultRoute, RouteGenericInterface, RouteOptions, RouteShorthandMethod } from './route'
|
|
11
|
+
import { DefaultRoute, RouteGenericInterface, RouteOptions, RouteShorthandMethod, RouteHandlerMethod } from './route'
|
|
12
12
|
import {
|
|
13
13
|
FastifySchema,
|
|
14
14
|
FastifySchemaCompiler,
|
|
@@ -103,6 +103,12 @@ type DecorationMethod<This, Return = This> = {
|
|
|
103
103
|
>,
|
|
104
104
|
dependencies?: string[]
|
|
105
105
|
): Return;
|
|
106
|
+
|
|
107
|
+
(property: string | symbol): Return;
|
|
108
|
+
|
|
109
|
+
(property: string | symbol, value: null): Return;
|
|
110
|
+
|
|
111
|
+
(property: string | symbol, value: null|undefined, dependencies: string[]): Return;
|
|
106
112
|
}
|
|
107
113
|
|
|
108
114
|
/**
|
|
@@ -520,10 +526,10 @@ export interface FastifyInstance<
|
|
|
520
526
|
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
|
|
521
527
|
|
|
522
528
|
/**
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
setNotFoundHandler<RouteGeneric extends RouteGenericInterface = RouteGenericInterface, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, SchemaCompiler extends FastifySchema = FastifySchema> (
|
|
526
|
-
handler:
|
|
529
|
+
* Set the 404 handler
|
|
530
|
+
*/
|
|
531
|
+
setNotFoundHandler<RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig extends ContextConfigDefault = ContextConfigDefault, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, SchemaCompiler extends FastifySchema = FastifySchema> (
|
|
532
|
+
handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
527
533
|
): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
|
|
528
534
|
|
|
529
535
|
setNotFoundHandler<RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig extends ContextConfigDefault = ContextConfigDefault, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, SchemaCompiler extends FastifySchema = FastifySchema> (
|
|
@@ -531,7 +537,7 @@ export interface FastifyInstance<
|
|
|
531
537
|
preValidation?: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> | preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>[];
|
|
532
538
|
preHandler?: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> | preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>[];
|
|
533
539
|
},
|
|
534
|
-
handler:
|
|
540
|
+
handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
535
541
|
): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>
|
|
536
542
|
|
|
537
543
|
/**
|
|
@@ -546,6 +552,32 @@ export interface FastifyInstance<
|
|
|
546
552
|
handler: (this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>, error: TError, request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>, reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfigDefault, SchemaCompiler, TypeProvider>) => any | Promise<any>
|
|
547
553
|
): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
|
|
548
554
|
|
|
555
|
+
/**
|
|
556
|
+
* Hook function that is called when creating a child logger instance for each request
|
|
557
|
+
* which allows for modifying or adding child logger bindings and logger options, or
|
|
558
|
+
* returning a completely custom child logger implementation.
|
|
559
|
+
*/
|
|
560
|
+
childLoggerFactory: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* Hook function that is called when creating a child logger instance for each request
|
|
564
|
+
* which allows for modifying or adding child logger bindings and logger options, or
|
|
565
|
+
* returning a completely custom child logger implementation.
|
|
566
|
+
*
|
|
567
|
+
* Child logger bindings have a performance advantage over per-log bindings, because
|
|
568
|
+
* they are pre-serialised by Pino when the child logger is created.
|
|
569
|
+
*
|
|
570
|
+
* For example:
|
|
571
|
+
* ```
|
|
572
|
+
* function childLoggerFactory(logger, bindings, opts, rawReq) {
|
|
573
|
+
* // Calculate additional bindings from the request
|
|
574
|
+
* bindings.traceContext = rawReq.headers['x-cloud-trace-context']
|
|
575
|
+
* return logger.child(bindings, opts);
|
|
576
|
+
* }
|
|
577
|
+
* ```
|
|
578
|
+
*/
|
|
579
|
+
setChildLoggerFactory(factory: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, Logger, TypeProvider>): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
|
|
580
|
+
|
|
549
581
|
/**
|
|
550
582
|
* Fastify schema validator for all routes.
|
|
551
583
|
*/
|
package/types/logger.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { FastifyReply } from './reply'
|
|
|
5
5
|
import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
|
|
6
6
|
import { FastifyTypeProvider, FastifyTypeProviderDefault } from './type-provider'
|
|
7
7
|
import { FastifySchema } from './schema'
|
|
8
|
+
import { FastifyInstance } from './instance'
|
|
8
9
|
|
|
9
10
|
import pino from 'pino'
|
|
10
11
|
|
|
@@ -78,3 +79,27 @@ export interface FastifyLoggerOptions<
|
|
|
78
79
|
genReqId?: (req: RawRequest) => string;
|
|
79
80
|
stream?: FastifyLoggerStreamDestination;
|
|
80
81
|
}
|
|
82
|
+
|
|
83
|
+
export interface FastifyChildLoggerFactory<
|
|
84
|
+
RawServer extends RawServerBase = RawServerDefault,
|
|
85
|
+
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
|
|
86
|
+
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
|
|
87
|
+
Logger extends FastifyBaseLogger = FastifyBaseLogger,
|
|
88
|
+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
|
|
89
|
+
> {
|
|
90
|
+
/**
|
|
91
|
+
* @param logger The parent logger
|
|
92
|
+
* @param bindings The bindings object that will be passed to the child logger
|
|
93
|
+
* @param childLoggerOpts The logger options that will be passed to the child logger
|
|
94
|
+
* @param rawReq The raw request
|
|
95
|
+
* @this The fastify instance
|
|
96
|
+
* @returns The child logger instance
|
|
97
|
+
*/
|
|
98
|
+
(
|
|
99
|
+
this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
|
|
100
|
+
logger: Logger,
|
|
101
|
+
bindings: Bindings,
|
|
102
|
+
childLoggerOpts: ChildLoggerOptions,
|
|
103
|
+
rawReq: RawRequest
|
|
104
|
+
): Logger
|
|
105
|
+
}
|
package/types/reply.d.ts
CHANGED
|
@@ -6,17 +6,22 @@ import { FastifyRequest } from './request'
|
|
|
6
6
|
import { RouteGenericInterface } from './route'
|
|
7
7
|
import { FastifySchema } from './schema'
|
|
8
8
|
import { FastifyReplyType, FastifyTypeProvider, FastifyTypeProviderDefault, ResolveFastifyReplyType } from './type-provider'
|
|
9
|
-
import { CodeToReplyKey, ContextConfigDefault,
|
|
9
|
+
import { CodeToReplyKey, ContextConfigDefault, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault, ReplyDefault, ReplyKeysToCodes } from './utils'
|
|
10
10
|
|
|
11
11
|
export interface ReplyGenericInterface {
|
|
12
12
|
Reply?: ReplyDefault;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
type ReplyTypeConstrainer<RouteGenericReply, Code extends ReplyKeysToCodes<keyof RouteGenericReply>, ReplyKey = CodeToReplyKey<Code>> =
|
|
16
16
|
Code extends keyof RouteGenericReply ? RouteGenericReply[Code] :
|
|
17
17
|
[ReplyKey] extends [never] ? unknown :
|
|
18
18
|
ReplyKey extends keyof RouteGenericReply ? RouteGenericReply[ReplyKey] :
|
|
19
|
-
|
|
19
|
+
RouteGenericReply;
|
|
20
|
+
|
|
21
|
+
export type ResolveReplyTypeWithRouteGeneric<RouteGenericReply, Code extends ReplyKeysToCodes<keyof RouteGenericReply>,
|
|
22
|
+
SchemaCompiler extends FastifySchema = FastifySchema,
|
|
23
|
+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault> =
|
|
24
|
+
ResolveFastifyReplyType<TypeProvider, SchemaCompiler, { Reply: ReplyTypeConstrainer<RouteGenericReply, Code> }>
|
|
20
25
|
/**
|
|
21
26
|
* FastifyReply is an instance of the standard http or http2 reply types.
|
|
22
27
|
* It defaults to http.ServerResponse, and it also extends the relative reply object.
|
|
@@ -36,8 +41,8 @@ export interface FastifyReply<
|
|
|
36
41
|
log: FastifyBaseLogger;
|
|
37
42
|
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>;
|
|
38
43
|
server: FastifyInstance;
|
|
39
|
-
code<Code extends ReplyKeysToCodes<keyof RouteGeneric['Reply']>>(statusCode: Code): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider,
|
|
40
|
-
status<Code extends ReplyKeysToCodes<keyof RouteGeneric['Reply']>>(statusCode: Code): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider,
|
|
44
|
+
code<Code extends ReplyKeysToCodes<keyof RouteGeneric['Reply']>>(statusCode: Code): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, ResolveReplyTypeWithRouteGeneric<RouteGeneric['Reply'], Code, SchemaCompiler, TypeProvider>>;
|
|
45
|
+
status<Code extends ReplyKeysToCodes<keyof RouteGeneric['Reply']>>(statusCode: Code): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, ResolveReplyTypeWithRouteGeneric<RouteGeneric['Reply'], Code, SchemaCompiler, TypeProvider>>;
|
|
41
46
|
statusCode: number;
|
|
42
47
|
sent: boolean;
|
|
43
48
|
send(payload?: ReplyType): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
|
package/types/request.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { ErrorObject } from '@fastify/ajv-compiler'
|
|
2
|
+
import { FastifyContext } from './context'
|
|
3
|
+
import { FastifyInstance } from './instance'
|
|
2
4
|
import { FastifyBaseLogger } from './logger'
|
|
3
|
-
import { ContextConfigDefault, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault } from './utils'
|
|
4
5
|
import { RouteGenericInterface } from './route'
|
|
5
|
-
import { FastifyInstance } from './instance'
|
|
6
|
-
import { FastifyTypeProvider, FastifyTypeProviderDefault, FastifyRequestType, ResolveFastifyRequestType } from './type-provider'
|
|
7
6
|
import { FastifySchema } from './schema'
|
|
8
|
-
import {
|
|
7
|
+
import { FastifyRequestType, FastifyTypeProvider, FastifyTypeProviderDefault, ResolveFastifyRequestType } from './type-provider'
|
|
8
|
+
import { ContextConfigDefault, RawRequestDefaultExpression, RawServerBase, RawServerDefault, RequestBodyDefault, RequestHeadersDefault, RequestParamsDefault, RequestQuerystringDefault } from './utils'
|
|
9
9
|
|
|
10
10
|
type HTTPRequestPart = 'body' | 'query' | 'querystring' | 'params' | 'headers'
|
|
11
11
|
export interface RequestGenericInterface {
|
|
@@ -59,7 +59,7 @@ export interface FastifyRequest<RouteGeneric extends RouteGenericInterface = Rou
|
|
|
59
59
|
server: FastifyInstance;
|
|
60
60
|
body: RequestType['body'];
|
|
61
61
|
context: FastifyContext<ContextConfig>;
|
|
62
|
-
routeConfig:
|
|
62
|
+
routeConfig: FastifyContext<ContextConfig>['config'];
|
|
63
63
|
routeSchema: FastifySchema
|
|
64
64
|
|
|
65
65
|
/** in order for this to be used the user should ensure they have set the attachValidation option. */
|
package/types/route.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
+
import { FastifyError } from '@fastify/error'
|
|
2
|
+
import { FastifyContext } from './context'
|
|
3
|
+
import { onErrorHookHandler, onRequestAbortHookHandler, onRequestHookHandler, onResponseHookHandler, onSendHookHandler, onTimeoutHookHandler, preHandlerHookHandler, preParsingHookHandler, preSerializationHookHandler, preValidationHookHandler } from './hooks'
|
|
1
4
|
import { FastifyInstance } from './instance'
|
|
2
|
-
import {
|
|
5
|
+
import { FastifyBaseLogger, FastifyChildLoggerFactory, LogLevel } from './logger'
|
|
3
6
|
import { FastifyReply, ReplyGenericInterface } from './reply'
|
|
7
|
+
import { FastifyRequest, RequestGenericInterface } from './request'
|
|
4
8
|
import { FastifySchema, FastifySchemaCompiler, FastifySerializerCompiler, SchemaErrorFormatter } from './schema'
|
|
5
|
-
import { HTTPMethods, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
|
|
6
|
-
import { preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onRequestHookHandler, preParsingHookHandler, onResponseHookHandler, onSendHookHandler, onErrorHookHandler, onTimeoutHookHandler, onRequestAbortHookHandler } from './hooks'
|
|
7
|
-
import { FastifyError } from '@fastify/error'
|
|
8
|
-
import { FastifyContext } from './context'
|
|
9
9
|
import {
|
|
10
10
|
FastifyTypeProvider,
|
|
11
11
|
FastifyTypeProviderDefault,
|
|
12
12
|
ResolveFastifyReplyReturnType
|
|
13
13
|
} from './type-provider'
|
|
14
|
-
import {
|
|
14
|
+
import { ContextConfigDefault, HTTPMethods, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault } from './utils'
|
|
15
15
|
|
|
16
16
|
export interface FastifyRouteConfig {
|
|
17
17
|
url: string;
|
|
@@ -41,11 +41,12 @@ export interface RouteShorthandOptions<
|
|
|
41
41
|
serializerCompiler?: FastifySerializerCompiler<SchemaCompiler>;
|
|
42
42
|
bodyLimit?: number;
|
|
43
43
|
logLevel?: LogLevel;
|
|
44
|
-
config?: FastifyContext<ContextConfig>['config']
|
|
44
|
+
config?: Omit<FastifyContext<ContextConfig>['config'], 'url' | 'method'>;
|
|
45
45
|
version?: string;
|
|
46
46
|
constraints?: { [name: string]: any },
|
|
47
47
|
prefixTrailingSlash?: 'slash'|'no-slash'|'both';
|
|
48
48
|
errorHandler?: (this: FastifyInstance, error: FastifyError, request: FastifyRequest, reply: FastifyReply) => void;
|
|
49
|
+
childLoggerFactory?: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
|
|
49
50
|
schemaErrorFormatter?: SchemaErrorFormatter;
|
|
50
51
|
|
|
51
52
|
// hooks
|