fastify 3.24.0 → 3.25.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.
Files changed (67) hide show
  1. package/README.md +30 -29
  2. package/docs/{Benchmarking.md → Guides/Benchmarking.md} +14 -5
  3. package/docs/Guides/Ecosystem.md +513 -0
  4. package/docs/{Fluent-Schema.md → Guides/Fluent-Schema.md} +16 -7
  5. package/docs/{Getting-Started.md → Guides/Getting-Started.md} +180 -60
  6. package/docs/Guides/Index.md +30 -4
  7. package/docs/{Migration-Guide-V3.md → Guides/Migration-Guide-V3.md} +43 -37
  8. package/docs/{Plugins-Guide.md → Guides/Plugins-Guide.md} +196 -82
  9. package/docs/{Recommendations.md → Guides/Recommendations.md} +17 -10
  10. package/docs/{Serverless.md → Guides/Serverless.md} +200 -42
  11. package/docs/Guides/Style-Guide.md +246 -0
  12. package/docs/{Testing.md → Guides/Testing.md} +26 -12
  13. package/docs/Guides/Write-Plugin.md +102 -0
  14. package/docs/{ContentTypeParser.md → Reference/ContentTypeParser.md} +68 -30
  15. package/docs/{Decorators.md → Reference/Decorators.md} +52 -47
  16. package/docs/{Encapsulation.md → Reference/Encapsulation.md} +3 -3
  17. package/docs/{Errors.md → Reference/Errors.md} +77 -47
  18. package/docs/{HTTP2.md → Reference/HTTP2.md} +13 -13
  19. package/docs/{Hooks.md → Reference/Hooks.md} +157 -70
  20. package/docs/Reference/Index.md +71 -0
  21. package/docs/{LTS.md → Reference/LTS.md} +31 -32
  22. package/docs/{Lifecycle.md → Reference/Lifecycle.md} +15 -7
  23. package/docs/{Logging.md → Reference/Logging.md} +68 -28
  24. package/docs/Reference/Middleware.md +78 -0
  25. package/docs/{Plugins.md → Reference/Plugins.md} +91 -34
  26. package/docs/{Reply.md → Reference/Reply.md} +205 -94
  27. package/docs/{Request.md → Reference/Request.md} +32 -16
  28. package/docs/{Routes.md → Reference/Routes.md} +243 -113
  29. package/docs/{Server.md → Reference/Server.md} +516 -267
  30. package/docs/{TypeScript.md → Reference/TypeScript.md} +451 -191
  31. package/docs/{Validation-and-Serialization.md → Reference/Validation-and-Serialization.md} +178 -86
  32. package/docs/index.md +24 -0
  33. package/examples/typescript-server.ts +1 -1
  34. package/fastify.js +2 -3
  35. package/lib/contentTypeParser.js +11 -6
  36. package/lib/decorate.js +6 -3
  37. package/lib/logger.js +1 -1
  38. package/lib/route.js +1 -1
  39. package/lib/server.js +9 -8
  40. package/package.json +9 -4
  41. package/test/als.test.js +74 -0
  42. package/test/constrained-routes.test.js +220 -0
  43. package/test/custom-parser.test.js +11 -2
  44. package/test/decorator.test.js +38 -0
  45. package/test/handler-context.test.js +11 -4
  46. package/test/http2/closing.test.js +14 -5
  47. package/test/http2/constraint.test.js +91 -0
  48. package/test/listen.test.js +36 -22
  49. package/test/logger.test.js +16 -0
  50. package/test/maxRequestsPerSocket.test.js +10 -0
  51. package/test/request-error.test.js +2 -8
  52. package/test/requestTimeout.test.js +4 -1
  53. package/test/router-options.test.js +10 -1
  54. package/test/schema-feature.test.js +146 -0
  55. package/test/stream.test.js +14 -3
  56. package/test/trust-proxy.test.js +15 -7
  57. package/test/types/instance.test-d.ts +52 -1
  58. package/test/types/request.test-d.ts +7 -1
  59. package/test/types/route.test-d.ts +21 -0
  60. package/types/hooks.d.ts +12 -1
  61. package/types/instance.d.ts +16 -6
  62. package/types/request.d.ts +4 -1
  63. package/types/route.d.ts +1 -1
  64. package/docs/Ecosystem.md +0 -211
  65. package/docs/Middleware.md +0 -53
  66. package/docs/Style-Guide.md +0 -185
  67. package/docs/Write-Plugin.md +0 -58
@@ -1,37 +1,43 @@
1
1
  <h1 align="center">Serverless</h1>
2
2
 
3
- Run serverless applications and REST APIs using your existing Fastify application.
4
- By default, Fastify will not work on your serverless platform of choice, you will need
5
- to make some small changes to fix this. This document contains a small guide for
6
- the most popular serverless providers and how to use Fastify with them.
3
+ Run serverless applications and REST APIs using your existing Fastify
4
+ application. By default, Fastify will not work on your serverless platform of
5
+ choice, you will need to make some small changes to fix this. This document
6
+ contains a small guide for the most popular serverless providers and how to use
7
+ Fastify with them.
7
8
 
8
9
  #### Should you use Fastify in a serverless platform?
9
10
 
10
11
  That is up to you! Keep in mind that functions as a service should always use
11
- small and focused functions, but you can also run an entire web application with them.
12
- It is important to remember that the bigger the application the slower the initial boot will be.
13
- The best way to run Fastify applications in serverless environments
14
- is to use platforms like Google Cloud Run, AWS Fargate, and Azure Container Instances,
15
- where the server can handle multiple requests at the same time and make full use of Fastify's features.
16
-
17
- One of the best features of using Fastify in serverless applications is the ease of development.
18
- In your local environment, you will always run the Fastify application directly without the need
19
- for any additional tools, while the same code will be executed in your serverless platform of
20
- choice with an additional snippet of code.
12
+ small and focused functions, but you can also run an entire web application with
13
+ them. It is important to remember that the bigger the application the slower the
14
+ initial boot will be. The best way to run Fastify applications in serverless
15
+ environments is to use platforms like Google Cloud Run, AWS Fargate, and Azure
16
+ Container Instances, where the server can handle multiple requests at the same
17
+ time and make full use of Fastify's features.
18
+
19
+ One of the best features of using Fastify in serverless applications is the ease
20
+ of development. In your local environment, you will always run the Fastify
21
+ application directly without the need for any additional tools, while the same
22
+ code will be executed in your serverless platform of choice with an additional
23
+ snippet of code.
21
24
 
22
25
  ### Contents
23
26
 
24
27
  - [AWS Lambda](#aws-lambda)
28
+ - [Google Cloud Functions](#google-cloud-functions)
25
29
  - [Google Cloud Run](#google-cloud-run)
26
30
  - [Netlify Lambda](#netlify-lambda)
27
31
  - [Vercel](#vercel)
28
32
 
29
33
  ## AWS Lambda
30
34
 
31
- The sample provided allows you to easily build serverless web applications/services
32
- and RESTful APIs using Fastify on top of AWS Lambda and Amazon API Gateway.
35
+ The sample provided allows you to easily build serverless web
36
+ applications/services and RESTful APIs using Fastify on top of AWS Lambda and
37
+ Amazon API Gateway.
33
38
 
34
- *Note: Using [aws-lambda-fastify](https://github.com/fastify/aws-lambda-fastify) is just one possible way.*
39
+ *Note: Using [aws-lambda-fastify](https://github.com/fastify/aws-lambda-fastify)
40
+ is just one possible way.*
35
41
 
36
42
  ### app.js
37
43
 
@@ -56,13 +62,14 @@ if (require.main === module) {
56
62
  }
57
63
  ```
58
64
 
59
- When executed in your lambda function we do not need to listen to a specific port,
60
- so we just export the wrapper function `init` in this case.
61
- The [`lambda.js`](https://www.fastify.io/docs/latest/Serverless/#lambda-js) file will use this export.
65
+ When executed in your lambda function we do not need to listen to a specific
66
+ port, so we just export the wrapper function `init` in this case. The
67
+ [`lambda.js`](#lambdajs) file
68
+ will use this export.
62
69
 
63
- When you execute your Fastify application like always,
64
- i.e. `node app.js` *(the detection for this could be `require.main === module`)*,
65
- you can normally listen to your port, so you can still run your Fastify function locally.
70
+ When you execute your Fastify application like always, i.e. `node app.js` *(the
71
+ detection for this could be `require.main === module`)*, you can normally listen
72
+ to your port, so you can still run your Fastify function locally.
66
73
 
67
74
  ### lambda.js
68
75
 
@@ -83,32 +90,177 @@ exports.handler = proxy;
83
90
  // exports.handler = async (event, context) => proxy(event, context);
84
91
  ```
85
92
 
86
- We just require [aws-lambda-fastify](https://github.com/fastify/aws-lambda-fastify)
87
- (make sure you install the dependency `npm i --save aws-lambda-fastify`) and our
88
- [`app.js`](https://www.fastify.io/docs/latest/Serverless/#app-js) file and call the
89
- exported `awsLambdaFastify` function with the `app` as the only parameter.
90
- The resulting `proxy` function has the correct signature to be used as a lambda `handler` function.
91
- This way all the incoming events (API Gateway requests) are passed to the `proxy` function of [aws-lambda-fastify](https://github.com/fastify/aws-lambda-fastify).
93
+ We just require
94
+ [aws-lambda-fastify](https://github.com/fastify/aws-lambda-fastify) (make sure
95
+ you install the dependency `npm i --save aws-lambda-fastify`) and our
96
+ [`app.js`](#appjs) file and call
97
+ the exported `awsLambdaFastify` function with the `app` as the only parameter.
98
+ The resulting `proxy` function has the correct signature to be used as a lambda
99
+ `handler` function. This way all the incoming events (API Gateway requests) are
100
+ passed to the `proxy` function of
101
+ [aws-lambda-fastify](https://github.com/fastify/aws-lambda-fastify).
92
102
 
93
103
  ### Example
94
104
 
95
- An example deployable with [claudia.js](https://claudiajs.com/tutorials/serverless-express.html) can be found [here](https://github.com/claudiajs/example-projects/tree/master/fastify-app-lambda).
105
+ An example deployable with
106
+ [claudia.js](https://claudiajs.com/tutorials/serverless-express.html) can be
107
+ found
108
+ [here](https://github.com/claudiajs/example-projects/tree/master/fastify-app-lambda).
96
109
 
97
110
 
98
111
  ### Considerations
99
112
 
100
- - API Gateway does not support streams yet, so you are not able to handle [streams](https://www.fastify.io/docs/latest/Reply/#streams).
101
- - API Gateway has a timeout of 29 seconds, so it is important to provide a reply during this time.
113
+ - API Gateway does not support streams yet, so you are not able to handle
114
+ [streams](../Reference/Reply.md#streams).
115
+ - API Gateway has a timeout of 29 seconds, so it is important to provide a reply
116
+ during this time.
117
+
118
+ ## Google Cloud Functions
119
+
120
+ ### Creation of Fastify instance
121
+ ```js
122
+ const fastify = require("fastify")({
123
+ logger: true // you can also define the level passing an object configuration to logger: {level: 'debug'}
124
+ });
125
+ ```
126
+
127
+ ### Add Custom `contentTypeParser` to Fastify instance
128
+
129
+ As explained [in issue
130
+ #946](https://github.com/fastify/fastify/issues/946#issuecomment-766319521),
131
+ since the Google Cloud Functions platform parses the body of the request before
132
+ it arrives into Fastify instance, troubling the body request in case of `POST`
133
+ and `PATCH` methods, you need to add a custom [`Content-Type
134
+ Parser`](../Reference/ContentTypeParser.md) to mitigate this behavior.
135
+
136
+ ```js
137
+ fastify.addContentTypeParser('application/json', {}, (req, body, done) => {
138
+ done(null, body.body);
139
+ });
140
+ ```
141
+
142
+ ### Define your endpoint (examples)
143
+
144
+ A simple `GET` endpoint:
145
+ ```js
146
+ fastify.get('/', async (request, reply) => {
147
+ reply.send({message: 'Hello World!'})
148
+ })
149
+ ```
150
+
151
+ Or a more complete `POST` endpoint with schema validation:
152
+ ```js
153
+ fastify.route({
154
+ method: 'POST',
155
+ url: '/hello',
156
+ schema: {
157
+ body: {
158
+ type: 'object',
159
+ properties: {
160
+ name: { type: 'string'}
161
+ },
162
+ required: ['name']
163
+ },
164
+ response: {
165
+ 200: {
166
+ type: 'object',
167
+ properties: {
168
+ message: {type: 'string'}
169
+ }
170
+ }
171
+ },
172
+ },
173
+ handler: async (request, reply) => {
174
+ const { name } = request.body;
175
+ reply.code(200).send({
176
+ message: `Hello ${name}!`
177
+ })
178
+ }
179
+ })
180
+ ```
181
+
182
+ ### Implement and export the function
183
+
184
+ Final step, implement the function to handle the request and pass it to Fastify
185
+ by emitting `request` event to `fastify.server`:
186
+
187
+ ```js
188
+ const fastifyFunction = async (request, reply) => {
189
+ await fastify.ready();
190
+ fastify.server.emit('request', request, reply)
191
+ }
192
+
193
+ export.fastifyFunction = fastifyFunction;
194
+ ```
195
+
196
+ ### Local test
197
+
198
+ Install [Google Functions Framework for
199
+ Node.js](https://github.com/GoogleCloudPlatform/functions-framework-nodejs).
200
+
201
+ You can install it globally:
202
+ ```bash
203
+ npm i -g @google-cloud/functions-framework
204
+ ```
205
+
206
+ Or as a development library:
207
+ ```bash
208
+ npm i --save-dev @google-cloud/functions-framework
209
+ ```
210
+
211
+ Than you can run your function locally with Functions Framework:
212
+ ```bash
213
+ npx @google-cloud/functions-framework --target=fastifyFunction
214
+ ```
215
+
216
+ Or add this command to your `package.json` scripts:
217
+ ```json
218
+ "scripts": {
219
+ ...
220
+ "dev": "npx @google-cloud/functions-framework --target=fastifyFunction"
221
+ ...
222
+ }
223
+ ```
224
+ and run it with `npm run dev`.
225
+
226
+
227
+ ### Deploy
228
+ ```bash
229
+ gcloud functions deploy fastifyFunction \
230
+ --runtime nodejs14 --trigger-http --region $GOOGLE_REGION --allow-unauthenticated
231
+ ```
232
+
233
+ #### Read logs
234
+ ```bash
235
+ gcloud functions logs read
236
+ ```
237
+
238
+ #### Example request to `/hello` endpoint
239
+ ```bash
240
+ curl -X POST https://$GOOGLE_REGION-$GOOGLE_PROJECT.cloudfunctions.net/me -H "Content-Type: application/json" -d '{ "name": "Fastify" }'
241
+ {"message":"Hello Fastify!"}
242
+ ```
243
+
244
+ ### References
245
+ - [Google Cloud Functions - Node.js Quickstart
246
+ ](https://cloud.google.com/functions/docs/quickstart-nodejs)
102
247
 
103
248
  ## Google Cloud Run
104
249
 
105
- Unlike AWS Lambda or Google Cloud Functions, Google Cloud Run is a serverless **container** environment. Its primary purpose is to provide an infrastructure-abstracted environment to run arbitrary containers. As a result, Fastify can be deployed to Google Cloud Run with little-to-no code changes from the way you would write your Fastify app normally.
250
+ Unlike AWS Lambda or Google Cloud Functions, Google Cloud Run is a serverless
251
+ **container** environment. Its primary purpose is to provide an
252
+ infrastructure-abstracted environment to run arbitrary containers. As a result,
253
+ Fastify can be deployed to Google Cloud Run with little-to-no code changes from
254
+ the way you would write your Fastify app normally.
106
255
 
107
- *Follow the steps below to deploy to Google Cloud Run if you are already familiar with gcloud or just follow their [quickstart](https://cloud.google.com/run/docs/quickstarts/build-and-deploy)*.
256
+ *Follow the steps below to deploy to Google Cloud Run if you are already
257
+ familiar with gcloud or just follow their
258
+ [quickstart](https://cloud.google.com/run/docs/quickstarts/build-and-deploy)*.
108
259
 
109
260
  ### Adjust Fastify server
110
261
 
111
- In order for Fastify to properly listen for requests within the container, be sure to set the correct port and address:
262
+ In order for Fastify to properly listen for requests within the container, be
263
+ sure to set the correct port and address:
112
264
 
113
265
  ```js
114
266
  function build() {
@@ -146,7 +298,9 @@ if (require.main === module) {
146
298
 
147
299
  ### Add a Dockerfile
148
300
 
149
- You can add any valid `Dockerfile` that packages and runs a Node app. A basic `Dockerfile` can be found in the official [gcloud docs](https://github.com/knative/docs/blob/2d654d1fd6311750cc57187a86253c52f273d924/docs/serving/samples/hello-world/helloworld-nodejs/Dockerfile).
301
+ You can add any valid `Dockerfile` that packages and runs a Node app. A basic
302
+ `Dockerfile` can be found in the official [gcloud
303
+ docs](https://github.com/knative/docs/blob/2d654d1fd6311750cc57187a86253c52f273d924/docs/serving/samples/hello-world/helloworld-nodejs/Dockerfile).
150
304
 
151
305
  ```Dockerfile
152
306
  # Use the official Node.js 10 image.
@@ -173,7 +327,8 @@ CMD [ "npm", "start" ]
173
327
 
174
328
  ### Add a .dockerignore
175
329
 
176
- To keep build artifacts out of your container (which keeps it small and improves build times) add a `.dockerignore` file like the one below:
330
+ To keep build artifacts out of your container (which keeps it small and improves
331
+ build times) add a `.dockerignore` file like the one below:
177
332
 
178
333
  ```.dockerignore
179
334
  Dockerfile
@@ -184,7 +339,9 @@ npm-debug.log
184
339
 
185
340
  ### Submit build
186
341
 
187
- Next, submit your app to be built into a Docker image by running the following command (replacing `PROJECT-ID` and `APP-NAME` with your GCP project id and an app name):
342
+ Next, submit your app to be built into a Docker image by running the following
343
+ command (replacing `PROJECT-ID` and `APP-NAME` with your GCP project id and an
344
+ app name):
188
345
 
189
346
  ```bash
190
347
  gcloud builds submit --tag gcr.io/PROJECT-ID/APP-NAME
@@ -205,7 +362,8 @@ Your app will be accessible from the URL GCP provides.
205
362
 
206
363
  First, please perform all preparation steps related to **AWS Lambda**.
207
364
 
208
- Create a folder called `functions`, then create `server.js` (and your endpoint path will be `server.js`) inside the `functions` folder.
365
+ Create a folder called `functions`, then create `server.js` (and your endpoint
366
+ path will be `server.js`) inside the `functions` folder.
209
367
 
210
368
  ### functions/server.js
211
369
 
@@ -284,9 +442,9 @@ Then it should work fine
284
442
 
285
443
  ## Vercel
286
444
 
287
- [Vercel](https://vercel.com) provides zero-configuration deployment for
288
- Node.js applications. In order to use it now, it is as simple as
289
- configuring your `vercel.json` file like the following:
445
+ [Vercel](https://vercel.com) provides zero-configuration deployment for Node.js
446
+ applications. In order to use it now, it is as simple as configuring your
447
+ `vercel.json` file like the following:
290
448
 
291
449
  ```json
292
450
  {
@@ -0,0 +1,246 @@
1
+ # Fastify Style Guide
2
+
3
+ ## Welcome
4
+
5
+ Welcome to *Fastify Style Guide*. This guide is here to provide you with a
6
+ conventional writing style for users writing developer documentation on our Open
7
+ Source framework. Each topic is precise and well explained to help you write
8
+ documentation users can easily understand and implement.
9
+
10
+ ## Who is this guide for?
11
+
12
+ This guide is for anyone who loves to build with Fastify or wants to contribute
13
+ to our documentation. You do not need to be an expert in writing technical
14
+ documentation. This guide is here to help you.
15
+
16
+ Visit the [contribute](https://www.fastify.io/contribute) page on our website or
17
+ read the
18
+ [CONTRIBUTING.md](https://github.com/fastify/fastify/blob/main/CONTRIBUTING.md)
19
+ file on GitHub to join our Open Source folks.
20
+
21
+ ## Before you write
22
+
23
+ You need to know the following:
24
+
25
+ * JavaScript
26
+ * Node.js
27
+ * Git
28
+ * GitHub
29
+ * Markdown
30
+ * HTTP
31
+ * NPM
32
+
33
+ ### Consider your Audience
34
+
35
+ Before you start writing, think about your audience. In this case, your audience
36
+ should already know HTTP, JavaScript, NPM, and Node.js. It is necessary to keep
37
+ your readers in mind because they are the ones consuming your content. You want
38
+ to give as much useful information as possible. Consider the vital things they
39
+ need to know and how they can understand them. Use words and references that
40
+ readers can relate to easily. Ask for feedback from the community, it can help
41
+ you write better documentation that focuses on the user and what you want to
42
+ achieve.
43
+
44
+ ### Get straight to the point
45
+
46
+ Give your readers a clear and precise action to take. Start with what is most
47
+ important. This way, you can help them find what they need faster. Mostly,
48
+ readers tend to read the first content on a page, and many will not scroll
49
+ further.
50
+
51
+ **Example**
52
+
53
+ Less like this: Colons are very important to register a parametric path. It lets
54
+ the framework know there is a new parameter created. You can place the colon
55
+ before the parameter name so the parametric path can be created.
56
+
57
+ More Like this: To register a parametric path, put a colon before the parameter
58
+ name. Using a colon lets the framework know it is a parametric path and not a
59
+ static path.
60
+
61
+ ### Avoid adding video or image content
62
+
63
+
64
+ Do not add videos or screenshots in the documentation. It is easier to keep
65
+ under version control. Videos and images will eventually end up becoming
66
+ outdated as new updates keep developing. Instead, make a referral link or a
67
+ YouTube video. You can add links by using `[Title](www.websitename.com)` in the
68
+ markdown.
69
+
70
+ **Example**
71
+
72
+ ```
73
+ To learn more about hooks, see [Fastify hooks](https://www.fastify.io/docs/latest/Reference/Hooks/).
74
+ ```
75
+
76
+ Result:
77
+ >To learn more about hooks, see [Fastify
78
+ >hooks](https://www.fastify.io/docs/latest/Reference/Hooks/).
79
+
80
+
81
+
82
+ ### Avoid plagiarism
83
+
84
+ Make sure you avoid copying other people's work. Keep it as original as
85
+ possible. You can learn from what they have done and reference where it is from
86
+ if you used a particular quote from their work.
87
+
88
+
89
+ ## Word Choice
90
+
91
+ There are a few things you need to use and avoid when writing your documentation
92
+ to improve readability for readers and make documentation neat, direct, and
93
+ clean.
94
+
95
+
96
+ ### When to use the second person "you" as the pronoun
97
+
98
+ When writing articles or guides, your content should communicate directly to
99
+ readers in the second person ("you") addressed form. It is easier to give them
100
+ direct instruction on what to do on a particular topic. To see an example, visit
101
+ the [Plugins Guide](./Plugins-Guide.md).
102
+
103
+ **Example**
104
+
105
+ Less like this: we can use the following plugins.
106
+
107
+ More like this: You can use the following plugins.
108
+
109
+ > According to [Wikipedia](#), ***You*** is usually a second person pronoun.
110
+ > Also, used to refer to an indeterminate person, as a more common alternative
111
+ > to a very formal indefinite pronoun.
112
+
113
+ ## When to avoid the second person "you" as the pronoun
114
+
115
+ One of the main rules of formal writing such as reference documentation, or API
116
+ documentation, is to avoid the second person ("you") or directly addressing the
117
+ reader.
118
+
119
+ **Example**
120
+
121
+ Less like this: You can use the following recommendation as an example.
122
+
123
+ More like this: As an example, the following recommendations should be
124
+ referenced.
125
+
126
+ To view a live example, refer to the [Decorators](../Reference/Decorators.md)
127
+ reference document.
128
+
129
+
130
+ ### Avoid using contractions
131
+
132
+ Contractions are the shortened version of written and spoken forms of a word,
133
+ i.e. using "don't" instead of "do not". Avoid contractions to provide a more
134
+ formal tone.
135
+
136
+ ### Avoid using condescending terms
137
+
138
+ Condescending terms are words that include:
139
+
140
+ * Just
141
+ * Easy
142
+ * Simply
143
+ * Basically
144
+ * Obviously
145
+
146
+ The reader may not find it easy to use Fastify's framework and plugins; avoid
147
+ words that make it sound simple, easy, offensive, or insensitive. Not everyone
148
+ who reads the documentation has the same level of understanding.
149
+
150
+
151
+ ### Starting with a verb
152
+
153
+ Mostly start your description with a verb, which makes it simple and precise for
154
+ the reader to follow. Prefer using present tense because it is easier to read
155
+ and understand than the past or future tense.
156
+
157
+ **Example**
158
+
159
+ Less like this: There is a need for Node.js to be installed before you can be
160
+ able to use Fastify.
161
+
162
+ More like this: Install Node.js to make use of Fastify.
163
+
164
+ ### Grammatical moods
165
+
166
+ Grammatical moods are a great way to express your writing. Avoid sounding too
167
+ bossy while making a direct statement. Know when to switch between indicative,
168
+ imperative, and subjunctive moods.
169
+
170
+
171
+ **Indicative** - Use when making a factual statement or question.
172
+
173
+ Example: Since there is no testing framework available, "Fastify recommends ways
174
+ to write tests".
175
+
176
+ **Imperative** - Use when giving instructions, actions, commands, or when you
177
+ write your headings.
178
+
179
+ Example: Install dependencies before starting development.
180
+
181
+
182
+ **Subjunctive** - Use when making suggestions, hypotheses, or non-factual
183
+ statements.
184
+
185
+ Example: Reading the documentation on our website is recommended to get
186
+ comprehensive knowledge of the framework.
187
+
188
+ ### Use **active** voice instead of **passive**
189
+
190
+ Using active voice is a more compact and direct way of conveying your
191
+ documentation.
192
+
193
+ **Example**
194
+
195
+
196
+ Passive: The node dependencies and packages are installed by npm.
197
+
198
+ Active: npm installs packages and node dependencies.
199
+
200
+ ## Writing Style
201
+
202
+ ### Documentation titles
203
+
204
+ When creating a new guide, API, or reference in the `/docs/` directory, use
205
+ short titles that best describe the topic of your documentation. Name your files
206
+ in kebab-cases and avoid Raw or camelCase. To learn more about kebab-case you
207
+ can visit this medium article on [Case
208
+ Styles](https://medium.com/better-programming/string-case-styles-camel-pascal-snake-and-kebab-case-981407998841).
209
+
210
+ **Examples**:
211
+
212
+ >`hook-and-plugins.md`,
213
+
214
+ `adding-test-plugins.md`,
215
+
216
+ `removing-requests.md`.
217
+
218
+ ### Hyperlinks
219
+
220
+ Hyperlinks should have a clear title of what it references. Here is how your
221
+ hyperlink should look:
222
+
223
+ ```MD
224
+ <!-- More like this -->
225
+
226
+ // Add clear & brief description
227
+ [Fastify Plugins] (https://www.fastify.io/docs/latest/Plugins/)
228
+
229
+ <!--Less like this -->
230
+
231
+ // incomplete description
232
+ [Fastify] (https://www.fastify.io/docs/latest/Plugins/)
233
+
234
+ // Adding title in link brackets
235
+ [](https://www.fastify.io/docs/latest/Plugins/ "fastify plugin")
236
+
237
+ // Empty title
238
+ [](https://www.fastify.io/docs/latest/Plugins/)
239
+
240
+ // Adding links localhost URLs instead of using code strings (``)
241
+ [http://localhost:3000/](http://localhost:3000/)
242
+
243
+ ```
244
+
245
+ Include in your documentation as many essential references as possible, but
246
+ avoid having numerous links when writing for beginners to avoid distractions.
@@ -2,9 +2,13 @@
2
2
 
3
3
  ## Testing
4
4
 
5
- Testing is one of the most important parts of developing an application. Fastify is very flexible when it comes to testing and is compatible with most testing frameworks (such as [Tap](https://www.npmjs.com/package/tap), which is used in the examples below).
5
+ Testing is one of the most important parts of developing an application. Fastify
6
+ is very flexible when it comes to testing and is compatible with most testing
7
+ frameworks (such as [Tap](https://www.npmjs.com/package/tap), which is used in
8
+ the examples below).
6
9
 
7
- Let's `cd` into a fresh directory called 'testing-example' and type `npm init -y` in our terminal.
10
+ Let's `cd` into a fresh directory called 'testing-example' and type `npm init
11
+ -y` in our terminal.
8
12
 
9
13
  Run `npm install fastify && npm install tap pino-pretty --save-dev`
10
14
 
@@ -45,7 +49,7 @@ const server = require('./app')({
45
49
 
46
50
  server.listen(3000, (err, address) => {
47
51
  if (err) {
48
- console.log(err)
52
+ server.log.error(err)
49
53
  process.exit(1)
50
54
  }
51
55
  })
@@ -53,9 +57,11 @@ server.listen(3000, (err, address) => {
53
57
 
54
58
  ### Benefits of using fastify.inject()
55
59
 
56
- Fastify comes with built-in support for fake HTTP injection thanks to [`light-my-request`](https://github.com/fastify/light-my-request).
60
+ Fastify comes with built-in support for fake HTTP injection thanks to
61
+ [`light-my-request`](https://github.com/fastify/light-my-request).
57
62
 
58
- Before introducing any tests, we will use the `.inject` method to make a fake request to our route:
63
+ Before introducing any tests, we will use the `.inject` method to make a fake
64
+ request to our route:
59
65
 
60
66
  **app.test.js**:
61
67
 
@@ -78,9 +84,12 @@ const test = async () => {
78
84
  test()
79
85
  ```
80
86
 
81
- First, our code will run inside an asynchronous function, giving us access to async/await.
87
+ First, our code will run inside an asynchronous function, giving us access to
88
+ async/await.
82
89
 
83
- `.inject` ensures all registered plugins have booted up and our application is ready to test. Finally, we pass the request method we want to use and a route. Using await we can store the response without a callback.
90
+ `.inject` ensures all registered plugins have booted up and our application is
91
+ ready to test. Finally, we pass the request method we want to use and a route.
92
+ Using await we can store the response without a callback.
84
93
 
85
94
 
86
95
 
@@ -225,13 +234,15 @@ tap.test('GET `/` route', t => {
225
234
  ```
226
235
 
227
236
  ### Testing with a running server
228
- Fastify can also be tested after starting the server with `fastify.listen()` or after initializing routes and plugins with `fastify.ready()`.
237
+ Fastify can also be tested after starting the server with `fastify.listen()` or
238
+ after initializing routes and plugins with `fastify.ready()`.
229
239
 
230
240
  #### Example:
231
241
 
232
242
  Uses **app.js** from the previous example.
233
243
 
234
- **test-listen.js** (testing with [`Request`](https://www.npmjs.com/package/request))
244
+ **test-listen.js** (testing with
245
+ [`Request`](https://www.npmjs.com/package/request))
235
246
  ```js
236
247
  const tap = require('tap')
237
248
  const request = require('request')
@@ -260,7 +271,8 @@ tap.test('GET `/` route', t => {
260
271
  })
261
272
  ```
262
273
 
263
- **test-ready.js** (testing with [`SuperTest`](https://www.npmjs.com/package/supertest))
274
+ **test-ready.js** (testing with
275
+ [`SuperTest`](https://www.npmjs.com/package/supertest))
264
276
  ```js
265
277
  const tap = require('tap')
266
278
  const supertest = require('supertest')
@@ -293,6 +305,8 @@ test('should ...', {only: true}, t => ...)
293
305
  - `-O` specifies to run tests with the `only` option enabled
294
306
  - `-T` specifies not to timeout (while you're debugging)
295
307
  - `--node-arg=--inspect-brk` will launch the node debugger
296
- 3. In VS Code, create and launch a `Node.js: Attach` debug configuration. No modification should be necessary.
308
+ 3. In VS Code, create and launch a `Node.js: Attach` debug configuration. No
309
+ modification should be necessary.
297
310
 
298
- Now you should be able to step through your test file (and the rest of `Fastify`) in your code editor.
311
+ Now you should be able to step through your test file (and the rest of
312
+ `Fastify`) in your code editor.