fastify 4.0.3 → 4.2.1

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.
Files changed (47) hide show
  1. package/.eslintrc +1 -0
  2. package/README.md +13 -14
  3. package/docs/Guides/Database.md +7 -7
  4. package/docs/Guides/Delay-Accepting-Requests.md +1 -1
  5. package/docs/Guides/Ecosystem.md +29 -16
  6. package/docs/Guides/Migration-Guide-V4.md +86 -1
  7. package/docs/Guides/Plugins-Guide.md +5 -0
  8. package/docs/Guides/Serverless.md +23 -10
  9. package/docs/Reference/Hooks.md +52 -0
  10. package/docs/Reference/Plugins.md +1 -1
  11. package/docs/Reference/Server.md +1 -1
  12. package/docs/Reference/Type-Providers.md +3 -17
  13. package/docs/Reference/TypeScript.md +65 -28
  14. package/docs/Reference/Validation-and-Serialization.md +11 -0
  15. package/docs/index.md +1 -1
  16. package/fastify.d.ts +3 -3
  17. package/fastify.js +19 -19
  18. package/integration/server.js +27 -0
  19. package/integration/test.sh +23 -0
  20. package/lib/context.js +5 -2
  21. package/lib/error-serializer.js +24 -27
  22. package/lib/handleRequest.js +1 -1
  23. package/lib/reply.js +22 -21
  24. package/lib/route.js +39 -29
  25. package/lib/symbols.js +2 -1
  26. package/lib/validation.js +2 -0
  27. package/package.json +10 -10
  28. package/test/404s.test.js +2 -2
  29. package/test/build/error-serializer.test.js +9 -2
  30. package/test/hooks.test.js +21 -0
  31. package/test/internals/reply.test.js +12 -0
  32. package/test/pretty-print.test.js +3 -3
  33. package/test/reply-error.test.js +1 -1
  34. package/test/schema-feature.test.js +2 -2
  35. package/test/schema-validation.test.js +71 -0
  36. package/test/stream.test.js +1 -1
  37. package/test/types/fastify.test-d.ts +24 -2
  38. package/test/types/instance.test-d.ts +5 -2
  39. package/test/types/register.test-d.ts +77 -2
  40. package/test/types/request.test-d.ts +8 -4
  41. package/test/types/type-provider.test-d.ts +11 -2
  42. package/test/validation-error-handling.test.js +38 -1
  43. package/types/instance.d.ts +59 -91
  44. package/types/register.d.ts +9 -7
  45. package/types/route.d.ts +10 -12
  46. package/types/schema.d.ts +5 -2
  47. package/types/type-provider.d.ts +12 -5
package/.eslintrc CHANGED
@@ -1,3 +1,4 @@
1
1
  {
2
+ "root": true,
2
3
  "extends": "standard"
3
4
  }
package/README.md CHANGED
@@ -38,18 +38,18 @@ resources of your server, knowing that you are serving the highest number of
38
38
  requests as possible, without sacrificing security validations and handy
39
39
  development?
40
40
 
41
- - [Quick start](./README.md#quick-start)
42
- - [Install](./README.md#install)
43
- - [Example](./README.md#example)
44
- - [Fastify v1.x and v2.x](./README.md#fastify-v1x-and-v2x)
45
- - [Core features](./README.md#core-features)
46
- - [Benchmarks](./README.md#benchmarks)
47
- - [Documentation](./README.md#documentation)
48
- - [Ecosystem](./README.md#ecosystem)
49
- - [Support](./README.md#support)
50
- - [Team](./README.md#team)
51
- - [Hosted by](./README.md#hosted-by)
52
- - [License](./README.md#license)
41
+ - [Quick start](#quick-start)
42
+ - [Install](#install)
43
+ - [Example](#example)
44
+ - [Fastify v1.x and v2.x](#fastify-v1x-and-v2x)
45
+ - [Core features](#core-features)
46
+ - [Benchmarks](#benchmarks)
47
+ - [Documentation](#documentation)
48
+ - [Ecosystem](#ecosystem)
49
+ - [Support](#support)
50
+ - [Team](#team)
51
+ - [Hosted by](#hosted-by)
52
+ - [License](#license)
53
53
 
54
54
  Enter Fastify. Fastify is a web framework highly focused on providing the best
55
55
  developer experience with the least overhead and a powerful plugin architecture.
@@ -99,8 +99,7 @@ generate functionality of [Fastify CLI](https://github.com/fastify/fastify-cli).
99
99
 
100
100
  ### Install
101
101
 
102
- If installing in an existing project, then Fastify can be installed into the
103
- project as a dependency:
102
+ To install Fastify in an existing project as a dependency:
104
103
 
105
104
  Install with npm:
106
105
  ```sh
@@ -37,7 +37,7 @@ fastify.get('/user/:id', function(req, reply) {
37
37
  )
38
38
  })
39
39
 
40
- fastify.listen(3000, err => {
40
+ fastify.listen({ port: 3000 }, err => {
41
41
  if (err) throw err
42
42
  console.log(`server listening on ${fastify.server.address().port}`)
43
43
  })
@@ -64,7 +64,7 @@ fastify.get('/user/:id', function (req, reply) {
64
64
  )
65
65
  })
66
66
 
67
- fastify.listen(3000, err => {
67
+ fastify.listen({ port: 3000 }, err => {
68
68
  if (err) throw err
69
69
  console.log(`server listening on ${fastify.server.address().port}`)
70
70
  })
@@ -98,7 +98,7 @@ fastify.post('/foo', function (req, reply) {
98
98
  })
99
99
  })
100
100
 
101
- fastify.listen(3000, err => {
101
+ fastify.listen({ port: 3000 }, err => {
102
102
  if (err) throw err
103
103
  console.log(`server listening on ${fastify.server.address().port}`)
104
104
  })
@@ -145,7 +145,7 @@ fastify.get('/user/:id', function (req, reply) {
145
145
  })
146
146
  })
147
147
 
148
- fastify.listen(3000, err => {
148
+ fastify.listen({ port: 3000 }, err => {
149
149
  if (err) throw err
150
150
  })
151
151
  ```
@@ -172,7 +172,7 @@ fastify.post('/foo', async function (req, reply) {
172
172
  return { status: 'ok' }
173
173
  })
174
174
 
175
- fastify.listen(3000, err => {
175
+ fastify.listen({ port: 3000 }, err => {
176
176
  if (err) throw err
177
177
  console.log(`server listening on ${fastify.server.address().port}`)
178
178
  })
@@ -204,7 +204,7 @@ function knexPlugin(fastify, options, done) {
204
204
  done()
205
205
  }
206
206
 
207
- export default fp(plugin, { name: 'fastify-knex-example' })
207
+ export default fp(knexPlugin, { name: 'fastify-knex-example' })
208
208
  ```
209
209
 
210
210
  ### Writing a plugin for a database engine
@@ -213,7 +213,7 @@ In this example, we will create a basic Fastify MySQL plugin from scratch (it is
213
213
  a stripped-down example, please use the official plugin in production).
214
214
 
215
215
  ```javascript
216
- const fp = require('fp')
216
+ const fp = require('fastify-plugin')
217
217
  const mysql = require('mysql2/promise')
218
218
 
219
219
  function fastifyMysql(fastify, options, done) {
@@ -48,7 +48,7 @@ server (fewer resources allocated to a bound-to-fail task) and for the client
48
48
  That will be achieved by wrapping into a custom plugin two main features:
49
49
 
50
50
  1. the mechanism for authenticating with the provider
51
- [decorating](../Referece/Decorators.md) the `fastify` object with the
51
+ [decorating](../Reference/Decorators.md) the `fastify` object with the
52
52
  authentication key (`magicKey` from here onwards)
53
53
  1. the mechanism for denying requests that would, otherwise, fail
54
54
 
@@ -132,9 +132,15 @@ section.
132
132
  sampling process metrics.
133
133
  - [`@dnlup/fastify-traps`](https://github.com/dnlup/fastify-traps) A plugin to
134
134
  close the server gracefully on `SIGINT` and `SIGTERM` signals.
135
+ - [`@eropple/fastify-openapi3`](https://github.com/eropple/fastify-openapi3) Provides
136
+ easy, developer-friendly OpenAPI 3.1 specs + doc explorer based on your routes.
135
137
  - [`@gquittet/graceful-server`](https://github.com/gquittet/graceful-server)
136
138
  Tiny (~5k), Fast, KISS, and dependency-free Node.JS library to make your
137
139
  Fastify API graceful.
140
+ - [`@h4ad/serverless-adapter`](https://github.com/H4ad/serverless-adapter)
141
+ Run REST APIs and other web applications using your existing Node.js
142
+ application framework (Express, Koa, Hapi and Fastify), on top of AWS Lambda,
143
+ Huawei and many other clouds.
138
144
  - [`@immobiliarelabs/fastify-metrics`](https://github.com/immobiliare/fastify-metrics)
139
145
  Minimalistic and opinionated plugin that collects usage/process metrics and
140
146
  dispatches to [statsd](https://github.com/statsd/statsd).
@@ -145,12 +151,12 @@ section.
145
151
  A plugin to close the server gracefully
146
152
  - [`@mgcrea/fastify-request-logger`](https://github.com/mgcrea/fastify-request-logger)
147
153
  A plugin to enable compact request logging for Fastify
154
+ - [`@mgcrea/fastify-session`](https://github.com/mgcrea/fastify-session) Session
155
+ plugin for Fastify that supports both stateless and stateful sessions
148
156
  - [`@mgcrea/fastify-session-redis-store`](https://github.com/mgcrea/fastify-session-redis-store)
149
157
  Redis store for @mgcrea/fastify-session using ioredis
150
158
  - [`@mgcrea/fastify-session-sodium-crypto`](https://github.com/mgcrea/fastify-session-sodium-crypto)
151
159
  Fast sodium-based crypto for @mgcrea/fastify-session
152
- - [`@mgcrea/fastify-session`](https://github.com/mgcrea/fastify-session) Session
153
- plugin for Fastify that supports both stateless and stateful sessions
154
160
  - [`@mgcrea/pino-pretty-compact`](https://github.com/mgcrea/pino-pretty-compact)
155
161
  A custom compact pino-base prettifier
156
162
  - [`@trubavuong/fastify-seaweedfs`](https://github.com/trubavuong/fastify-seaweedfs)
@@ -163,6 +169,8 @@ section.
163
169
  - [`cls-rtracer`](https://github.com/puzpuzpuz/cls-rtracer) Fastify middleware
164
170
  for CLS-based request ID generation. An out-of-the-box solution for adding
165
171
  request IDs into your logs.
172
+ - [`fast-water`](https://github.com/tswayne/fast-water) A Fastify plugin for
173
+ waterline. Decorates Fastify with waterline models.
166
174
  - [`fastify-405`](https://github.com/Eomm/fastify-405) Fastify plugin that adds
167
175
  405 HTTP status to your routes
168
176
  - [`fastify-allow`](https://github.com/mattbishop/fastify-allow) Fastify plugin
@@ -281,13 +289,13 @@ section.
281
289
  good Fastify sessions plugin focused on speed.
282
290
  - [`fastify-google-cloud-storage`](https://github.com/carlozamagni/fastify-google-cloud-storage)
283
291
  Fastify plugin that exposes a GCP Cloud Storage client instance.
292
+ - [`fastify-graceful-shutdown`](https://github.com/hemerajs/fastify-graceful-shutdown)
293
+ Shutdown Fastify gracefully and asynchronously.
284
294
  - [`fastify-grant`](https://github.com/simov/fastify-grant)
285
295
  Authentication/Authorization plugin for Fastify that supports 200+ OAuth
286
296
  Providers.
287
297
  - [`fastify-guard`](https://github.com/hsynlms/fastify-guard) A Fastify plugin
288
298
  that protects endpoints by checking authenticated user roles and/or scopes.
289
- - [`fastify-graceful-shutdown`](https://github.com/hemerajs/fastify-graceful-shutdown)
290
- Shutdown Fastify gracefully and asynchronously.
291
299
  - [`fastify-hasura`](https://github.com/ManUtopiK/fastify-hasura) A Fastify
292
300
  plugin to have fun with [Hasura](https://github.com/hasura/graphql-engine).
293
301
  - [`fastify-healthcheck`](https://github.com/smartiniOnGitHub/fastify-healthcheck)
@@ -295,20 +303,19 @@ section.
295
303
  - [`fastify-hemera`](https://github.com/hemerajs/fastify-hemera) Fastify Hemera
296
304
  plugin, for writing reliable & fault-tolerant microservices with
297
305
  [nats.io](https://nats.io/).
306
+ - [`fastify-http-client`](https://github.com/kenuyx/fastify-http-client) Plugin
307
+ to send HTTP(s) requests. Built upon [urllib](https://github.com/node-modules/urllib).
298
308
  - [`fastify-http-context`](https://github.com/thorough-developer/fastify-http-context)
299
309
  Fastify plugin for "simulating" a thread of execution to allow for true HTTP
300
310
  context to take place per API call within the Fastify lifecycle of calls.
311
+ - [`fastify-http-errors-enhanced`](https://github.com/ShogunPanda/fastify-http-errors-enhanced)
312
+ An error handling plugin for Fastify that uses enhanced HTTP errors.
301
313
  - [`fastify-http2https`](https://github.com/lolo32/fastify-http2https) Redirect
302
314
  HTTP requests to HTTPS, both using the same port number, or different response
303
315
  on HTTP and HTTPS.
304
- - [`fastify-http-client`](https://github.com/kenuyx/fastify-http-client) Plugin
305
- to send HTTP(s) requests. Built upon
306
- [urllib](https://github.com/node-modules/urllib).
307
- - [`fastify-http-errors-enhanced`](https://github.com/ShogunPanda/fastify-http-errors-enhanced)
308
- An error handling plugin for Fastify that uses enhanced HTTP errors.
309
316
  - [`fastify-https-redirect`](https://github.com/tomsvogel/fastify-https-redirect)
310
317
  Fastify plugin for auto-redirect from HTTP to HTTPS.
311
- - [`fatify-impressions`](https://github.com/manju4ever/fastify-impressions)
318
+ - [`fastify-impressions`](https://github.com/manju4ever/fastify-impressions)
312
319
  Fastify plugin to track impressions of all the routes.
313
320
  - [`fastify-influxdb`](https://github.com/alex-ppg/fastify-influxdb) Fastify
314
321
  InfluxDB plugin connecting to an InfluxDB instance via the Influx default
@@ -327,6 +334,8 @@ section.
327
334
  Kubernetes client plugin.
328
335
  - [`fastify-language-parser`](https://github.com/lependu/fastify-language-parser)
329
336
  Fastify plugin to parse request language.
337
+ - [`fastify-lcache`](https://github.com/denbon05/fastify-lcache)
338
+ Lightweight cache plugin
330
339
  - [`fastify-loader`](https://github.com/TheNoim/fastify-loader) Load routes from
331
340
  a directory and inject the Fastify instance in each file.
332
341
  - [`fastify-lured`](https://github.com/lependu/fastify-lured) Plugin to load lua
@@ -400,10 +409,10 @@ section.
400
409
  - [`fastify-orientdb`](https://github.com/mahmed8003/fastify-orientdb) Fastify
401
410
  OrientDB connection plugin, with which you can share the OrientDB connection
402
411
  across every part of your server.
403
- - [`fastify-piscina`](https://github.com/piscinajs/fastify-piscina) A worker
404
- thread pool plugin using [Piscina](https://github.com/piscinajs/piscina).
405
412
  - [`fastify-peekaboo`](https://github.com/simone-sanfratello/fastify-peekaboo)
406
413
  Fastify plugin for memoize responses by expressive settings.
414
+ - [`fastify-piscina`](https://github.com/piscinajs/fastify-piscina) A worker
415
+ thread pool plugin using [Piscina](https://github.com/piscinajs/piscina).
407
416
  - [`fastify-polyglot`](https://github.com/heply/fastify-polyglot) A plugin to
408
417
  handle i18n using
409
418
  [node-polyglot](https://www.npmjs.com/package/node-polyglot).
@@ -436,10 +445,10 @@ section.
436
445
  - [`fastify-register-routes`](https://github.com/israeleriston/fastify-register-routes)
437
446
  Plugin to automatically load routes from a specified path and optionally limit
438
447
  loaded file names by a regular expression.
439
- - [`fastify-response-time`](https://github.com/lolo32/fastify-response-time) Add
440
- `X-Response-Time` header at each request for Fastify, in milliseconds.
441
448
  - [`fastify-response-caching`](https://github.com/codeaholicguy/fastify-response-caching)
442
449
  A Fastify plugin for caching the response.
450
+ - [`fastify-response-time`](https://github.com/lolo32/fastify-response-time) Add
451
+ `X-Response-Time` header at each request for Fastify, in milliseconds.
443
452
  - [`fastify-resty`](https://github.com/FastifyResty/fastify-resty) Fastify-based
444
453
  web framework with REST API routes auto-generation for TypeORM entities using
445
454
  DI and decorators.
@@ -478,6 +487,8 @@ section.
478
487
  Events with `reply.sse( … )` to Fastify.
479
488
  - [`fastify-sse-v2`](https://github.com/nodefactoryio/fastify-sse-v2) to provide
480
489
  Server-Sent Events using Async Iterators (supports newer versions of Fastify).
490
+ - [`fastify-ssr-vite`](https://github.com/nineohnine/fastify-ssr-vite) A simple
491
+ plugin for setting up server side rendering with vite.
481
492
  - [`fastify-stripe`](https://github.com/coopflow/fastify-stripe) Plugin to
482
493
  initialize and encapsulate [Stripe
483
494
  Node.js](https://github.com/stripe/stripe-node) instances in Fastify.
@@ -508,8 +519,6 @@ section.
508
519
  should use.
509
520
  - [`fastify-wamp-router`](https://github.com/lependu/fastify-wamp-router) Web
510
521
  Application Messaging Protocol router for Fastify.
511
- - [`fast-water`](https://github.com/tswayne/fast-water) A Fastify plugin for
512
- waterline. Decorates Fastify with waterline models.
513
522
  - [`fastify-webpack-hmr`](https://github.com/lependu/fastify-webpack-hmr)
514
523
  Webpack hot module reloading plugin for Fastify.
515
524
  - [`fastify-webpack-hot`](https://github.com/gajus/fastify-webpack-hot) Webpack
@@ -542,7 +551,11 @@ section.
542
551
  Fastify.
543
552
  - [`sequelize-fastify`](https://github.com/hsynlms/sequelize-fastify) A simple
544
553
  and lightweight Sequelize plugin for Fastify.
554
+ - [`typeorm-fastify-plugin`](https://github.com/jclemens24/fastify-typeorm) A simple
555
+ and updated Typeorm plugin for use with Fastify.
545
556
 
546
557
  #### [Community Tools](#community-tools)
547
558
  - [`fast-maker`](https://github.com/imjuni/fast-maker) route configuration
548
559
  generator by directory structure.
560
+ - [`simple-tjscli`](https://github.com/imjuni/simple-tjscli) CLI tool to
561
+ generate JSON Schema from TypeScript interfaces.
@@ -8,7 +8,39 @@ work after upgrading.
8
8
 
9
9
  ## Breaking Changes
10
10
 
11
- ### Deprecation of `app.use()`
11
+ ### Error handling composition ([#3261](https://github.com/fastify/fastify/pull/3261))
12
+
13
+ When an error is thrown in a async error handler function,
14
+ the upper-level error handler is executed if set.
15
+ If there is not a upper-level error handler, the default will
16
+ be executed as it was previously.
17
+
18
+ ```
19
+ import Fastify from 'fastify'
20
+
21
+ const fastify = Fastify()
22
+
23
+ fastify.register(async fastify => {
24
+ fastify.setErrorHandler(async err => {
25
+ console.log(err.message) // 'kaboom'
26
+ throw new Error('caught')
27
+ })
28
+
29
+ fastify.get('/encapsulated', async () => {
30
+ throw new Error('kaboom')
31
+ })
32
+ })
33
+
34
+ fastify.setErrorHandler(async err => {
35
+ console.log(err.message) // 'caught'
36
+ throw new Error('wrapped')
37
+ })
38
+
39
+ const res = await fastify.inject('/encapsulated')
40
+ console.log(res.json().message) // 'wrapped'
41
+ ```
42
+
43
+ ### Deprecation of `app.use()` ([#3506](https://github.com/fastify/fastify/pull/3506))
12
44
 
13
45
  Starting this version of Fastify, we have deprecated the use of `app.use()`. We
14
46
  have decided not to support the use of middlewares. Both
@@ -16,6 +48,52 @@ have decided not to support the use of middlewares. Both
16
48
  [`@fastify/express`](https://github.com/fastify/fastify-express) will still be
17
49
  there and maintained. Use Fastify's [hooks](../Reference/Hooks.md) instead.
18
50
 
51
+ ### `reply.res` moved to `reply.raw`
52
+
53
+ If you previously used the `reply.res` attribute to access the underlying Request
54
+ object you'll instead need to depend on `reply.raw`.
55
+
56
+ ### Need to `return reply` to signal a "fork" of the promise chain
57
+
58
+ In some situations, like when a response is sent asynchronously or when you're
59
+ just not explicitly returning a response, you'll need to return the `reply`
60
+ argument from your router handler.
61
+
62
+ ### `exposeHeadRoutes` true by default
63
+
64
+ Starting from v4, all the `GET` routes will create a sibling `HEAD` route.
65
+ You can revert this behaviour by setting the server's option `exposeHeadRoutes`
66
+ to `false`.
67
+
68
+ ### Synchronous route definitions
69
+
70
+ The route registration has been made synchronous from v4.
71
+ This change was done to provide better error reporting for route definition.
72
+ As a result if you specify an `onRoute` hook in a plugin you should either:
73
+ * wrap your routes in a plugin (recommended)
74
+ * use `await register(...)`
75
+
76
+ For example refactor this:
77
+ ```
78
+ fastify.register((instance, opts, done) => {
79
+ instance.addHook('onRoute', (routeOptions) => {
80
+ const { path, method } = routeOptions;
81
+ console.log({ path, method });
82
+ });
83
+ done();
84
+ });
85
+ ```
86
+ Into this:
87
+ ```
88
+ await fastify.register((instance, opts, done) => {
89
+ instance.addHook('onRoute', (routeOptions) => {
90
+ const { path, method } = routeOptions;
91
+ console.log({ path, method });
92
+ });
93
+ done();
94
+ });
95
+ ```
96
+
19
97
  ## Non Breaking Changes
20
98
 
21
99
  ### Change of schema for multiple types
@@ -53,3 +131,10 @@ properties: {
53
131
  }
54
132
  }
55
133
  ```
134
+
135
+ ### Add `reply.trailers` methods ([#3794](https://github.com/fastify/fastify/pull/3794))
136
+
137
+ Fastify now supports the [HTTP Trailer] response headers.
138
+
139
+
140
+ [HTTP Trailer]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Trailer
@@ -196,6 +196,11 @@ fastify.get('/html', (request, reply) => {
196
196
  reply.html({ hello: 'world' })
197
197
  })
198
198
  ```
199
+ Reminder that the `this` keyword is not available on *arrow functions*,
200
+ so when passing functions in *`decorateReply`* and *`decorateRequest`* as
201
+ a utility that also needs access to the `request` and `reply` instance,
202
+ a function that is defined using the `function` keyword is needed instead
203
+ of an *arrow function expression*.
199
204
 
200
205
  In the same way you can do this for the `request` object:
201
206
  ```js
@@ -24,23 +24,31 @@ snippet of code.
24
24
 
25
25
  ### Contents
26
26
 
27
- - [AWS Lambda](#aws-lambda)
27
+ - [AWS](#aws)
28
28
  - [Google Cloud Functions](#google-cloud-functions)
29
29
  - [Google Cloud Run](#google-cloud-run)
30
30
  - [Netlify Lambda](#netlify-lambda)
31
31
  - [Vercel](#vercel)
32
32
 
33
- ## AWS Lambda
33
+ ## AWS
34
+
35
+ To integrate with AWS, you have two choices of library:
36
+
37
+ - Using [@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify)
38
+ which only adds API Gateway support but has heavy optimizations for fastify.
39
+ - Using [@h4ad/serverless-adapter](https://github.com/H4ad/serverless-adapter)
40
+ which is a little slower as it creates an HTTP request for each AWS event but
41
+ has support for more AWS services such as: AWS SQS, AWS SNS and others.
42
+
43
+ So you can decide which option is best for you, but you can test both libraries.
44
+
45
+ ### Using @fastify/aws-lambda
34
46
 
35
47
  The sample provided allows you to easily build serverless web
36
48
  applications/services and RESTful APIs using Fastify on top of AWS Lambda and
37
49
  Amazon API Gateway.
38
50
 
39
- *Note: Using
40
- [@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify) is just one
41
- possible way.*
42
-
43
- ### app.js
51
+ #### app.js
44
52
 
45
53
  ```js
46
54
  const fastify = require('fastify');
@@ -71,7 +79,7 @@ When you execute your Fastify application like always, i.e. `node app.js` *(the
71
79
  detection for this could be `require.main === module`)*, you can normally listen
72
80
  to your port, so you can still run your Fastify function locally.
73
81
 
74
- ### lambda.js
82
+ #### lambda.js
75
83
 
76
84
  ```js
77
85
  const awsLambdaFastify = require('@fastify/aws-lambda')
@@ -99,14 +107,13 @@ signature to be used as a lambda `handler` function. This way all the incoming
99
107
  events (API Gateway requests) are passed to the `proxy` function of
100
108
  [@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify).
101
109
 
102
- ### Example
110
+ #### Example
103
111
 
104
112
  An example deployable with
105
113
  [claudia.js](https://claudiajs.com/tutorials/serverless-express.html) can be
106
114
  found
107
115
  [here](https://github.com/claudiajs/example-projects/tree/master/fastify-app-lambda).
108
116
 
109
-
110
117
  ### Considerations
111
118
 
112
119
  - API Gateway does not support streams yet, so you are not able to handle
@@ -114,6 +121,12 @@ found
114
121
  - API Gateway has a timeout of 29 seconds, so it is important to provide a reply
115
122
  during this time.
116
123
 
124
+ #### Beyond API Gateway
125
+
126
+ If you need to integrate with more AWS services, take a look at
127
+ [@h4ad/serverless-adapter](https://viniciusl.com.br/serverless-adapter/docs/main/frameworks/fastify)
128
+ on Fastify to find out how to integrate.
129
+
117
130
  ## Google Cloud Functions
118
131
 
119
132
  ### Creation of Fastify instance
@@ -28,6 +28,7 @@ are Request/Reply hooks and application hooks:
28
28
  - [onRegister](#onregister)
29
29
  - [Scope](#scope)
30
30
  - [Route level hooks](#route-level-hooks)
31
+ - [Using Hooks to Inject Custom Properties](#using-hooks-to-inject-custom-properties)
31
32
  - [Diagnostics Channel Hooks](#diagnostics-channel-hooks)
32
33
 
33
34
  **Notice:** the `done` callback is not available when using `async`/`await` or
@@ -649,6 +650,57 @@ fastify.route({
649
650
 
650
651
  **Note**: both options also accept an array of functions.
651
652
 
653
+ ## Using Hooks to Inject Custom Properties
654
+ <a id="using-hooks-to-inject-custom-properties"></a>
655
+
656
+ You can use a hook to inject custom properties into incoming requests.
657
+ This is useful for reusing processed data from hooks in controllers.
658
+
659
+ A very common use case is, for example, checking user authentication based
660
+ on their token and then storing their recovered data into
661
+ the [Request](./Request.md) instance. This way, your controllers can read it
662
+ easily with `request.authenticatedUser` or whatever you want to call it.
663
+ That's how it might look like:
664
+
665
+ ```js
666
+ fastify.addHook('preParsing', async (request) => {
667
+ request.authenticatedUser = {
668
+ id: 42,
669
+ name: 'Jane Doe',
670
+ role: 'admin'
671
+ }
672
+ })
673
+
674
+ fastify.get('/me/is-admin', async function (req, reply) {
675
+ return { isAdmin: req.authenticatedUser?.role === 'admin' || false }
676
+ })
677
+ ```
678
+
679
+ Note that `.authenticatedUser` could actually be any property name
680
+ choosen by yourself. Using your own custom property prevents you
681
+ from mutating existing properties, which
682
+ would be a dangerous and destructive operation. So be careful and
683
+ make sure your property is entirely new, also using this approach
684
+ only for very specific and small cases like this example.
685
+
686
+ Regarding TypeScript in this example, you'd need to update the
687
+ `FastifyRequest` core interface to include your new property typing
688
+ (for more about it, see [TypeScript](./TypeScript.md) page), like:
689
+
690
+ ```ts
691
+ interface AuthenticatedUser { /* ... */ }
692
+
693
+ declare module 'fastify' {
694
+ export interface FastifyRequest {
695
+ authenticatedUser?: AuthenticatedUser;
696
+ }
697
+ }
698
+ ```
699
+
700
+ Although this is a very pragmatic approach, if you're trying to do
701
+ something more complex that changes these core objects, then
702
+ consider creating a custom [Plugin](./Plugins.md) instead.
703
+
652
704
  ## Diagnostics Channel Hooks
653
705
 
654
706
  > **Note:** The `diagnostics_channel` is currently experimental on Node.js, so
@@ -13,7 +13,7 @@ way we create a *directed acyclic graph* (DAG) and we will not have issues
13
13
  caused by cross dependencies.
14
14
 
15
15
  You may have already seen in the [Getting
16
- Started]((../Guides/Getting-Started.md#your-first-plugin)) guide how easy it is
16
+ Started](../Guides/Getting-Started.md#your-first-plugin) guide how easy it is
17
17
  to use this API:
18
18
  ```
19
19
  fastify.register(plugin, [options])
@@ -1278,7 +1278,7 @@ const fastify = Fastify({
1278
1278
  */
1279
1279
  bucket: function factory (parentSchemas) {
1280
1280
  return {
1281
- addSchema (inputSchema) {
1281
+ add (inputSchema) {
1282
1282
  // This function must store the schema added by the user.
1283
1283
  // This function is invoked when `fastify.addSchema()` is called.
1284
1284
  },
@@ -30,11 +30,11 @@ $ npm i @fastify/type-provider-json-schema-to-ts
30
30
  ```
31
31
 
32
32
  ```typescript
33
- import { JsonSchemaToTsTypeProvider } from '@fastify/type-provider-json-schema-to-ts'
33
+ import { JsonSchemaToTsProvider } from '@fastify/type-provider-json-schema-to-ts'
34
34
 
35
35
  import fastify from 'fastify'
36
36
 
37
- const server = fastify().withTypeProvider<JsonSchemaToTsTypeProvider>()
37
+ const server = fastify().withTypeProvider<JsonSchemaToTsProvider>()
38
38
 
39
39
  server.get('/route', {
40
40
  schema: {
@@ -70,14 +70,7 @@ import { Type } from '@sinclair/typebox'
70
70
 
71
71
  import fastify from 'fastify'
72
72
 
73
- const server = fastify({
74
- ajv: {
75
- customOptions: {
76
- strict: 'log',
77
- keywords: ['kind', 'modifier'],
78
- },
79
- },
80
- }).withTypeProvider<TypeBoxTypeProvider>()
73
+ const server = fastify().withTypeProvider<TypeBoxTypeProvider>()
81
74
 
82
75
  server.get('/route', {
83
76
  schema: {
@@ -94,13 +87,6 @@ server.get('/route', {
94
87
  })
95
88
  ```
96
89
 
97
- TypeBox uses the properties `kind` and `modifier` internally. These properties
98
- are not strictly valid JSON schema which will cause `AJV@7` and newer versions
99
- to throw an invalid schema error. To remove the error it's either necessary to
100
- omit the properties by using
101
- [`Type.Strict()`](https://github.com/sinclairzx81/typebox#strict) or use the AJV
102
- options for adding custom keywords.
103
-
104
90
  See also the [TypeBox
105
91
  documentation](https://github.com/sinclairzx81/typebox#validation) on how to set
106
92
  up AJV to work with TypeBox.