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.
@@ -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 * as pino from 'pino'
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
@@ -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 enfore the users function returns what they expect it to
38
- decorate(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
39
- decorateRequest(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
40
- decorateReply(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
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
@@ -145,6 +145,7 @@ export interface FastifyLoggerOptions<
145
145
  };
146
146
  };
147
147
  level?: string;
148
+ file?: string;
148
149
  genReqId?: (req: RawRequest) => string;
149
150
  prettyPrint?: boolean | PrettyOptions;
150
151
  stream?: FastifyLoggerStreamDestination;
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;
@@ -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. */