fastify 4.9.0 → 4.9.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.
@@ -114,10 +114,39 @@ fastify.get('/', async function (request, reply) {
114
114
  The `dependencies` parameter is an optional list of decorators that the
115
115
  decorator being defined relies upon. This list is simply a list of string names
116
116
  of other decorators. In the following example, the "utility" decorator depends
117
- upon "greet" and "log" decorators:
117
+ upon "greet" and "hi" decorators:
118
118
 
119
119
  ```js
120
- fastify.decorate('utility', fn, ['greet', 'log'])
120
+ async function greetDecorator (fastify, opts) {
121
+ fastify.decorate('greet', () => {
122
+ return 'greet message'
123
+ })
124
+ }
125
+
126
+ async function hiDecorator (fastify, opts) {
127
+ fastify.decorate('hi', () => {
128
+ return 'hi message'
129
+ })
130
+ }
131
+
132
+ async function utilityDecorator (fastify, opts) {
133
+ fastify.decorate('utility', () => {
134
+ return `${fastify.greet()} | ${fastify.hi()}`
135
+ })
136
+ }
137
+
138
+ fastify.register(fastifyPlugin(greetDecorator, { name: 'greet' }))
139
+ fastify.register(fastifyPlugin(hiDecorator, { name: 'hi' }))
140
+ fastify.register(fastifyPlugin(utilityDecorator, { dependencies: ['greet', 'hi'] }))
141
+
142
+ fastify.get('/', function (req, reply) {
143
+ // Response: {"hello":"greet message | hi message"}
144
+ reply.send({ hello: fastify.utility() })
145
+ })
146
+
147
+ fastify.listen({ port: 3000 }, (err, address) => {
148
+ if (err) throw err
149
+ })
121
150
  ```
122
151
 
123
152
  Note: using an arrow function will break the binding of `this` to the
@@ -332,7 +332,7 @@ The router received an invalid url.
332
332
  ### FST_ERR_ASYNC_CONSTRAINT
333
333
  <a id="FST_ERR_ASYNC_CONSTRAINT"></a>
334
334
 
335
- The router received error when using asynchronous constraints.
335
+ The router received an error when using asynchronous constraints.
336
336
 
337
337
  #### FST_ERR_DEFAULT_ROUTE_INVALID_TYPE
338
338
  <a id="FST_ERR_DEFAULT_ROUTE_INVALID_TYPE"></a>
@@ -449,9 +449,9 @@ fastify.addHook('onRoute', (routeOptions) => {
449
449
  })
450
450
  ```
451
451
 
452
- If you want to add more routes within an onRoute hook, you have to tag these
453
- routes properly. If you don't, the hook will run into an infinite loop. The
454
- recommended approach is shown below.
452
+ To add more routes within an onRoute hook, the routes must
453
+ be tagged correctly. The hook will run into an infinite loop if
454
+ not tagged. The recommended approach is shown below.
455
455
 
456
456
  ```js
457
457
  const kRouteAlreadyProcessed = Symbol('route-already-processed')
@@ -656,13 +656,13 @@ fastify.get('/json', options, function (request, reply) {
656
656
  #### Streams
657
657
  <a id="send-streams"></a>
658
658
 
659
- *send* can also handle streams out of the box. If you are sending a stream and
660
- you have not set a `'Content-Type'` header, *send* will set it at
659
+ *send* can also handle streams by setting the `'Content-Type'` header to
661
660
  `'application/octet-stream'`.
662
661
  ```js
663
662
  fastify.get('/streams', function (request, reply) {
664
663
  const fs = require('fs')
665
664
  const stream = fs.createReadStream('some-file', 'utf8')
665
+ reply.header('Content-Type', 'application/octet-stream')
666
666
  reply.send(stream)
667
667
  })
668
668
  ```
@@ -734,9 +734,9 @@ fastify.route({
734
734
 
735
735
  #### Asynchronous Custom Constraints
736
736
 
737
- You can provide your custom constraints and the `constraint` criteria can be
738
- fetched from other source, for example `database`. Usage of asynchronous custom
739
- constraint should place at the last resort since it impacts the router
737
+ Custom constraints can be provided and the `constraint` criteria can be
738
+ fetched from another source such as `database`. The use of asynchronous
739
+ custom constraints should be a last resort as it impacts router
740
740
  performance.
741
741
 
742
742
  ```js
package/fastify.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const VERSION = '4.9.0'
3
+ const VERSION = '4.9.2'
4
4
 
5
5
  const Avvio = require('avvio')
6
6
  const http = require('http')
package/lib/route.js CHANGED
@@ -252,7 +252,7 @@ function buildRouting (options) {
252
252
  throw new FST_ERR_HOOK_INVALID_HANDLER(hook, typeof func)
253
253
  }
254
254
  }
255
- } else if (typeof opts[hook] !== 'function') {
255
+ } else if (opts[hook] !== undefined && typeof opts[hook] !== 'function') {
256
256
  throw new FST_ERR_HOOK_INVALID_HANDLER(hook, typeof opts[hook])
257
257
  }
258
258
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify",
3
- "version": "4.9.0",
3
+ "version": "4.9.2",
4
4
  "description": "Fast and low overhead web framework, for Node.js",
5
5
  "main": "fastify.js",
6
6
  "type": "commonjs",
@@ -3322,12 +3322,22 @@ test('registering invalid hooks should throw an error', async t => {
3322
3322
  fastify.route({
3323
3323
  method: 'GET',
3324
3324
  path: '/invalidHook',
3325
- onRequest: undefined,
3325
+ onRequest: null,
3326
3326
  async handler () {
3327
3327
  return 'hello world'
3328
3328
  }
3329
3329
  })
3330
- }, new Error('onRequest hook should be a function, instead got undefined'))
3330
+ }, new Error('onRequest hook should be a function, instead got object'))
3331
+
3332
+ // undefined is ok
3333
+ fastify.route({
3334
+ method: 'GET',
3335
+ path: '/validhook',
3336
+ onRequest: undefined,
3337
+ async handler () {
3338
+ return 'hello world'
3339
+ }
3340
+ })
3331
3341
 
3332
3342
  t.throws(() => {
3333
3343
  fastify.addHook('onRoute', (routeOptions) => {
@@ -75,6 +75,8 @@ const getHandler: RouteHandler = function (request, _reply) {
75
75
  expectType<RequestParamsDefault>(request.params)
76
76
  expectType<FastifyContext<ContextConfigDefault>>(request.context)
77
77
  expectType<FastifyContextConfig>(request.context.config)
78
+ expectType<FastifyContextConfig>(request.routeConfig)
79
+ expectType<FastifySchema>(request.routeSchema)
78
80
 
79
81
  expectType<RequestHeadersDefault & RawRequestDefaultExpression['headers']>(request.headers)
80
82
  request.headers = {}
@@ -8,5 +8,8 @@ export interface FastifyContextConfig {
8
8
  * Route context object. Properties defined here will be available in the route's handler
9
9
  */
10
10
  export interface FastifyContext<ContextConfig = ContextConfigDefault> {
11
+ /**
12
+ * @deprecated Use Request#routeConfig or Request#routeSchema instead
13
+ */
11
14
  config: FastifyContextConfig & ContextConfig;
12
15
  }
@@ -5,7 +5,7 @@ import { RouteGenericInterface } from './route'
5
5
  import { FastifyInstance } from './instance'
6
6
  import { FastifyTypeProvider, FastifyTypeProviderDefault, FastifyRequestType, ResolveFastifyRequestType } from './type-provider'
7
7
  import { FastifySchema } from './schema'
8
- import { FastifyContext } from './context'
8
+ import { FastifyContext, FastifyContextConfig } from './context'
9
9
 
10
10
  type HTTPRequestPart = 'body' | 'query' | 'querystring' | 'params' | 'headers'
11
11
  export interface RequestGenericInterface {
@@ -48,6 +48,8 @@ export interface FastifyRequest<RouteGeneric extends RouteGenericInterface = Rou
48
48
  server: FastifyInstance;
49
49
  body: RequestType['body'];
50
50
  context: FastifyContext<ContextConfig>;
51
+ routeConfig: FastifyContextConfig & ContextConfig;
52
+ routeSchema: FastifySchema
51
53
 
52
54
  /** in order for this to be used the user should ensure they have set the attachValidation option. */
53
55
  validationError?: Error & { validation: any; validationContext: string };