fastify 3.19.1 → 3.20.2
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/docs/ContentTypeParser.md +56 -2
- package/docs/Decorators.md +3 -0
- package/docs/Ecosystem.md +5 -1
- package/docs/Getting-Started.md +2 -2
- package/docs/Reply.md +6 -5
- package/docs/Request.md +20 -1
- package/docs/Routes.md +3 -3
- package/docs/Server.md +7 -5
- package/docs/TypeScript.md +3 -3
- package/fastify.d.ts +4 -2
- package/fastify.js +28 -6
- package/lib/contentTypeParser.js +46 -1
- package/lib/logger.js +1 -1
- package/lib/pluginUtils.js +3 -1
- package/lib/reply.js +6 -2
- package/lib/request.js +6 -0
- package/lib/route.js +6 -3
- package/package.json +4 -4
- package/test/async-await.test.js +73 -0
- package/test/content-parser.test.js +76 -0
- package/test/custom-parser.test.js +203 -0
- package/test/internals/version.test.js +43 -0
- package/test/request-error.test.js +72 -0
- package/test/route.test.js +56 -0
- package/test/schema-special-usage.test.js +32 -0
- package/test/types/content-type-parser.test-d.ts +8 -0
- package/test/types/fastify.test-d.ts +16 -0
- package/test/types/instance.test-d.ts +40 -2
- package/test/types/logger.test-d.ts +23 -1
- package/test/types/reply.test-d.ts +2 -0
- package/test/types/request.test-d.ts +4 -0
- package/types/content-type-parser.d.ts +4 -0
- package/types/instance.d.ts +37 -6
- package/types/logger.d.ts +1 -0
- package/types/reply.d.ts +2 -0
- package/types/request.d.ts +2 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { expectType, expectError } from 'tsd'
|
|
2
2
|
import fastify, { FastifyLogFn, LogLevel, FastifyLoggerInstance, FastifyError, FastifyRequest, FastifyReply } from '../../fastify'
|
|
3
3
|
import { Server, IncomingMessage, ServerResponse } from 'http'
|
|
4
|
-
import
|
|
4
|
+
import pino from 'pino'
|
|
5
5
|
import * as fs from 'fs'
|
|
6
6
|
|
|
7
7
|
expectType<FastifyLoggerInstance>(fastify().log)
|
|
@@ -75,6 +75,19 @@ ServerResponse
|
|
|
75
75
|
|
|
76
76
|
expectType<FastifyLoggerInstance>(serverWithLogOptions.log)
|
|
77
77
|
|
|
78
|
+
const serverWithFileOption = fastify<
|
|
79
|
+
Server,
|
|
80
|
+
IncomingMessage,
|
|
81
|
+
ServerResponse
|
|
82
|
+
>({
|
|
83
|
+
logger: {
|
|
84
|
+
level: 'info',
|
|
85
|
+
file: '/path/to/file'
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
expectType<FastifyLoggerInstance>(serverWithFileOption.log)
|
|
90
|
+
|
|
78
91
|
const serverAutoInferringTypes = fastify({
|
|
79
92
|
logger: {
|
|
80
93
|
level: 'info'
|
|
@@ -92,6 +105,15 @@ const serverWithAutoInferredPino = fastify({
|
|
|
92
105
|
|
|
93
106
|
expectType<pino.Logger>(serverWithAutoInferredPino.log)
|
|
94
107
|
|
|
108
|
+
const serverAutoInferredFileOption = fastify({
|
|
109
|
+
logger: {
|
|
110
|
+
level: 'info',
|
|
111
|
+
file: '/path/to/file'
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
expectType<FastifyLoggerInstance>(serverAutoInferredFileOption.log)
|
|
116
|
+
|
|
95
117
|
const serverAutoInferredPinoPrettyBooleanOption = fastify({
|
|
96
118
|
logger: {
|
|
97
119
|
prettyPrint: true
|
|
@@ -3,6 +3,7 @@ import fastify, { RouteHandlerMethod, RouteHandler, RawRequestDefaultExpression,
|
|
|
3
3
|
import { RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from '../../types/utils'
|
|
4
4
|
import { FastifyLoggerInstance } from '../../types/logger'
|
|
5
5
|
import { RouteGenericInterface } from '../../types/route'
|
|
6
|
+
import { FastifyInstance } from '../../types/instance'
|
|
6
7
|
|
|
7
8
|
const getHandler: RouteHandlerMethod = function (_request, reply) {
|
|
8
9
|
expectType<RawReplyDefaultExpression>(reply.raw)
|
|
@@ -29,6 +30,7 @@ const getHandler: RouteHandlerMethod = function (_request, reply) {
|
|
|
29
30
|
expectType<(fn: (payload: any) => string) => FastifyReply>(reply.serializer)
|
|
30
31
|
expectType<(payload: any) => string>(reply.serialize)
|
|
31
32
|
expectType<(fulfilled: () => void, rejected: (err: Error) => void) => void>(reply.then)
|
|
33
|
+
expectType<FastifyInstance>(reply.server)
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
interface ReplyPayload {
|
|
@@ -4,6 +4,7 @@ import { RawServerDefault, RequestParamsDefault, RequestHeadersDefault, RequestQ
|
|
|
4
4
|
import { FastifyLoggerInstance } from '../../types/logger'
|
|
5
5
|
import { FastifyRequest } from '../../types/request'
|
|
6
6
|
import { FastifyReply } from '../../types/reply'
|
|
7
|
+
import { FastifyInstance } from '../../types/instance'
|
|
7
8
|
|
|
8
9
|
interface RequestBody {
|
|
9
10
|
content: string;
|
|
@@ -55,6 +56,7 @@ const getHandler: RouteHandler = function (request, _reply) {
|
|
|
55
56
|
expectType<FastifyLoggerInstance>(request.log)
|
|
56
57
|
expectType<RawRequestDefaultExpression['socket']>(request.socket)
|
|
57
58
|
expectType<Error & { validation: any; validationContext: string } | undefined>(request.validationError)
|
|
59
|
+
expectType<FastifyInstance>(request.server)
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
const postHandler: Handler = function (request) {
|
|
@@ -66,6 +68,7 @@ const postHandler: Handler = function (request) {
|
|
|
66
68
|
expectType<string>(request.query.from)
|
|
67
69
|
expectType<number>(request.params.id)
|
|
68
70
|
expectType<string>(request.headers['x-foobar'])
|
|
71
|
+
expectType<FastifyInstance>(request.server)
|
|
69
72
|
}
|
|
70
73
|
|
|
71
74
|
function putHandler (request: CustomRequest, reply: FastifyReply) {
|
|
@@ -77,6 +80,7 @@ function putHandler (request: CustomRequest, reply: FastifyReply) {
|
|
|
77
80
|
expectType<string>(request.query.from)
|
|
78
81
|
expectType<number>(request.params.id)
|
|
79
82
|
expectType<string>(request.headers['x-foobar'])
|
|
83
|
+
expectType<FastifyInstance>(request.server)
|
|
80
84
|
}
|
|
81
85
|
|
|
82
86
|
const server = fastify()
|
|
@@ -60,3 +60,7 @@ export type ProtoAction = 'error' | 'remove' | 'ignore'
|
|
|
60
60
|
export type ConstructorAction = 'error' | 'remove' | 'ignore'
|
|
61
61
|
|
|
62
62
|
export type getDefaultJsonParser = (onProtoPoisoning: ProtoAction, onConstructorPoisoning: ConstructorAction) => FastifyBodyParser<string>
|
|
63
|
+
|
|
64
|
+
export type removeContentTypeParser = (contentType: string | RegExp | (string | RegExp)[]) => void
|
|
65
|
+
|
|
66
|
+
export type removeAllContentTypeParsers = () => void
|
package/types/instance.d.ts
CHANGED
|
@@ -8,7 +8,13 @@ import { onRequestHookHandler, preParsingHookHandler, onSendHookHandler, preVali
|
|
|
8
8
|
import { FastifyRequest } from './request'
|
|
9
9
|
import { FastifyReply } from './reply'
|
|
10
10
|
import { FastifyError } from 'fastify-error'
|
|
11
|
-
import { AddContentTypeParser, hasContentTypeParser, getDefaultJsonParser, ProtoAction, ConstructorAction, FastifyBodyParser } from './content-type-parser'
|
|
11
|
+
import { AddContentTypeParser, hasContentTypeParser, getDefaultJsonParser, ProtoAction, ConstructorAction, FastifyBodyParser, removeContentTypeParser, removeAllContentTypeParsers } from './content-type-parser'
|
|
12
|
+
|
|
13
|
+
export interface PrintRoutesOptions {
|
|
14
|
+
includeMeta?: boolean | (string | symbol)[]
|
|
15
|
+
commonPrefix?: boolean
|
|
16
|
+
includeHooks?: boolean
|
|
17
|
+
}
|
|
12
18
|
|
|
13
19
|
/**
|
|
14
20
|
* Fastify server instance. Returned by the core `fastify()` method.
|
|
@@ -34,10 +40,27 @@ export interface FastifyInstance<
|
|
|
34
40
|
close(): FastifyInstance<RawServer, RawRequest, RawReply, Logger> & PromiseLike<undefined>;
|
|
35
41
|
close(closeListener: () => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
|
|
36
42
|
|
|
37
|
-
// should be able to define something useful with the decorator getter/setter pattern using Generics to
|
|
38
|
-
decorate(property: string | symbol,
|
|
39
|
-
|
|
40
|
-
|
|
43
|
+
// should be able to define something useful with the decorator getter/setter pattern using Generics to enforce the users function returns what they expect it to
|
|
44
|
+
decorate<T>(property: string | symbol,
|
|
45
|
+
value: T extends (...args: any[]) => any
|
|
46
|
+
? (this: FastifyInstance<RawServer, RawRequest, RawReply, Logger>, ...args: Parameters<T>) => ReturnType<T>
|
|
47
|
+
: T,
|
|
48
|
+
dependencies?: string[]
|
|
49
|
+
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
|
|
50
|
+
|
|
51
|
+
decorateRequest<T>(property: string | symbol,
|
|
52
|
+
value: T extends (...args: any[]) => any
|
|
53
|
+
? (this: FastifyRequest, ...args: Parameters<T>) => ReturnType<T>
|
|
54
|
+
: T,
|
|
55
|
+
dependencies?: string[]
|
|
56
|
+
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
|
|
57
|
+
|
|
58
|
+
decorateReply<T>(property: string | symbol,
|
|
59
|
+
value: T extends (...args: any[]) => any
|
|
60
|
+
? (this: FastifyReply, ...args: Parameters<T>) => ReturnType<T>
|
|
61
|
+
: T,
|
|
62
|
+
dependencies?: string[]
|
|
63
|
+
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
|
|
41
64
|
|
|
42
65
|
hasDecorator(decorator: string | symbol): boolean;
|
|
43
66
|
hasRequestDecorator(decorator: string | symbol): boolean;
|
|
@@ -360,6 +383,14 @@ export interface FastifyInstance<
|
|
|
360
383
|
*/
|
|
361
384
|
addContentTypeParser: AddContentTypeParser<RawServer, RawRequest>;
|
|
362
385
|
hasContentTypeParser: hasContentTypeParser;
|
|
386
|
+
/**
|
|
387
|
+
* Remove an existing content type parser
|
|
388
|
+
*/
|
|
389
|
+
removeContentTypeParser: removeContentTypeParser
|
|
390
|
+
/**
|
|
391
|
+
* Remove all content type parsers, including the default ones
|
|
392
|
+
*/
|
|
393
|
+
removeAllContentTypeParsers: removeAllContentTypeParsers
|
|
363
394
|
/**
|
|
364
395
|
* Fastify default JSON parser
|
|
365
396
|
*/
|
|
@@ -372,7 +403,7 @@ export interface FastifyInstance<
|
|
|
372
403
|
/**
|
|
373
404
|
* Prints the representation of the internal radix tree used by the router
|
|
374
405
|
*/
|
|
375
|
-
printRoutes(): string;
|
|
406
|
+
printRoutes(opts?: PrintRoutesOptions): string;
|
|
376
407
|
|
|
377
408
|
/**
|
|
378
409
|
* Prints the representation of the plugin tree used by avvio, the plugin registration system
|
package/types/logger.d.ts
CHANGED
package/types/reply.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { FastifyContext } from './context'
|
|
|
3
3
|
import { FastifyLoggerInstance } from './logger'
|
|
4
4
|
import { FastifyRequest } from './request'
|
|
5
5
|
import { RouteGenericInterface } from './route'
|
|
6
|
+
import { FastifyInstance } from './instance'
|
|
6
7
|
|
|
7
8
|
export interface ReplyGenericInterface {
|
|
8
9
|
Reply?: ReplyDefault;
|
|
@@ -23,6 +24,7 @@ export interface FastifyReply<
|
|
|
23
24
|
context: FastifyContext<ContextConfig>;
|
|
24
25
|
log: FastifyLoggerInstance;
|
|
25
26
|
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>;
|
|
27
|
+
server: FastifyInstance;
|
|
26
28
|
code(statusCode: number): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
|
|
27
29
|
status(statusCode: number): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
|
|
28
30
|
statusCode: number;
|
package/types/request.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { FastifyLoggerInstance } from './logger'
|
|
2
2
|
import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault } from './utils'
|
|
3
3
|
import { RouteGenericInterface } from './route'
|
|
4
|
+
import { FastifyInstance } from './instance'
|
|
4
5
|
|
|
5
6
|
export interface RequestGenericInterface {
|
|
6
7
|
Body?: RequestBodyDefault;
|
|
@@ -23,6 +24,7 @@ export interface FastifyRequest<
|
|
|
23
24
|
raw: RawRequest;
|
|
24
25
|
query: RouteGeneric['Querystring'];
|
|
25
26
|
log: FastifyLoggerInstance;
|
|
27
|
+
server: FastifyInstance;
|
|
26
28
|
body: RouteGeneric['Body'];
|
|
27
29
|
|
|
28
30
|
/** in order for this to be used the user should ensure they have set the attachValidation option. */
|