fastify 3.11.0 → 3.14.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.
- package/README.md +15 -10
- package/SECURITY.md +2 -2
- package/build/build-validation.js +25 -1
- package/docs/Benchmarking.md +3 -3
- package/docs/ContentTypeParser.md +21 -0
- package/docs/Decorators.md +1 -1
- package/docs/Ecosystem.md +38 -26
- package/docs/Encapsulation.md +4 -1
- package/docs/Errors.md +41 -41
- package/docs/Getting-Started.md +2 -2
- package/docs/HTTP2.md +1 -1
- package/docs/Hooks.md +35 -4
- package/docs/LTS.md +1 -1
- package/docs/Lifecycle.md +1 -1
- package/docs/Logging.md +4 -4
- package/docs/Middleware.md +3 -3
- package/docs/Migration-Guide-V3.md +3 -3
- package/docs/Plugins-Guide.md +15 -15
- package/docs/Plugins.md +7 -7
- package/docs/Recommendations.md +3 -4
- package/docs/Reply.md +4 -4
- package/docs/Request.md +1 -1
- package/docs/Routes.md +65 -10
- package/docs/Server.md +80 -14
- package/docs/Serverless.md +43 -64
- package/docs/Style-Guide.md +24 -19
- package/docs/Testing.md +5 -5
- package/docs/TypeScript.md +159 -17
- package/docs/Validation-and-Serialization.md +62 -6
- package/docs/Write-Plugin.md +3 -3
- package/fastify.d.ts +14 -3
- package/fastify.js +52 -18
- package/lib/configValidator.js +288 -53
- package/lib/contentTypeParser.js +28 -7
- package/lib/errors.js +1 -1
- package/lib/pluginOverride.js +5 -5
- package/lib/reply.js +35 -28
- package/lib/reqIdGenFactory.js +2 -1
- package/lib/request.js +1 -1
- package/lib/route.js +20 -27
- package/lib/schema-compilers.js +5 -3
- package/lib/schema-controller.js +106 -0
- package/lib/schemas.js +13 -23
- package/lib/symbols.js +1 -3
- package/lib/warnings.js +4 -0
- package/package.json +22 -17
- package/test/constrained-routes.test.js +184 -0
- package/test/content-parser.test.js +179 -7
- package/test/context-config.test.js +52 -0
- package/test/custom-parser.test.js +262 -2
- package/test/hooks-async.test.js +46 -0
- package/test/hooks.test.js +47 -0
- package/test/internals/initialConfig.test.js +30 -5
- package/test/internals/reply.test.js +2 -2
- package/test/internals/request.test.js +3 -9
- package/test/pretty-print.test.js +28 -0
- package/test/route.test.js +0 -2
- package/test/schema-feature.test.js +134 -4
- package/test/schema-serialization.test.js +42 -0
- package/test/schema-special-usage.test.js +234 -0
- package/test/schema-validation.test.js +1 -1
- package/test/stream.test.js +90 -0
- package/test/throw.test.js +1 -1
- package/test/types/content-type-parser.test-d.ts +8 -2
- package/test/types/fastify.test-d.ts +27 -0
- package/test/types/instance.test-d.ts +43 -1
- package/test/types/logger.test-d.ts +8 -3
- package/test/types/reply.test-d.ts +2 -1
- package/test/types/schema.test-d.ts +52 -1
- package/test/versioned-routes.test.js +99 -18
- package/types/content-type-parser.d.ts +10 -4
- package/types/instance.d.ts +57 -7
- package/types/logger.d.ts +1 -1
- package/types/reply.d.ts +2 -1
- package/types/route.d.ts +15 -11
- package/types/schema.d.ts +5 -4
package/docs/Server.md
CHANGED
|
@@ -4,10 +4,9 @@
|
|
|
4
4
|
## Factory
|
|
5
5
|
|
|
6
6
|
The Fastify module exports a factory function that is used to create new
|
|
7
|
-
<
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
available in that options object.
|
|
7
|
+
<code><b>Fastify server</b></code> instances. This factory function accepts
|
|
8
|
+
an options object which is used to customize the resulting instance. This
|
|
9
|
+
document describes the properties available in that options object.
|
|
11
10
|
|
|
12
11
|
<a name="factory-http2"></a>
|
|
13
12
|
### `http2`
|
|
@@ -133,8 +132,6 @@ logger will point to this instance.
|
|
|
133
132
|
+ `object`: a standard Pino [options object](https://github.com/pinojs/pino/blob/c77d8ec5ce/docs/API.md#constructor).
|
|
134
133
|
This will be passed directly to the Pino constructor. If the following properties
|
|
135
134
|
are not present on the object, they will be added accordingly:
|
|
136
|
-
* `genReqId`: a synchronous function that will be used to generate identifiers
|
|
137
|
-
for incoming requests. The default function generates sequential identifiers.
|
|
138
135
|
* `level`: the minimum logging level. If not set, it will be set to `'info'`.
|
|
139
136
|
* `serializers`: a hash of serialization functions. By default, serializers
|
|
140
137
|
are added for `req` (incoming request objects), `res` (outgoing response
|
|
@@ -194,7 +191,7 @@ fastify.addHook('onResponse', (req, reply, done) => {
|
|
|
194
191
|
})
|
|
195
192
|
```
|
|
196
193
|
|
|
197
|
-
Please note that this setting will also disable an error log written by the
|
|
194
|
+
Please note that this setting will also disable an error log written by the default `onResponse` hook on reply callback errors.
|
|
198
195
|
|
|
199
196
|
<a name="custom-http-server"></a>
|
|
200
197
|
### `serverFactory`
|
|
@@ -291,7 +288,7 @@ const fastify = Fastify({ trustProxy: true })
|
|
|
291
288
|
}
|
|
292
289
|
```
|
|
293
290
|
|
|
294
|
-
For more examples refer to [proxy-addr](https://www.npmjs.com/package/proxy-addr) package.
|
|
291
|
+
For more examples refer to [`@fastify/proxy-addr`](https://www.npmjs.com/package/@fastify/proxy-addr) package.
|
|
295
292
|
|
|
296
293
|
You may access the `ip`, `ips`, `hostname` and `protocol` values on the [`request`](Request.md) object.
|
|
297
294
|
|
|
@@ -335,13 +332,13 @@ Automatically creates a sibling `HEAD` route for each `GET` route defined. If yo
|
|
|
335
332
|
|
|
336
333
|
+ Default: `false`
|
|
337
334
|
|
|
338
|
-
<a name="
|
|
339
|
-
### `
|
|
335
|
+
<a name="constraints"></a>
|
|
336
|
+
### `constraints`
|
|
340
337
|
|
|
341
|
-
|
|
338
|
+
Fastify's built in route constraints are provided by `find-my-way`, which allow constraining routes by `version` or `host`. You are able to add new constraint strategies, or override the built in strategies by providing a `constraints` object with strategies for `find-my-way`. You can find more information on constraint strategies in the [find-my-way](https://github.com/delvedor/find-my-way) documentation.
|
|
342
339
|
|
|
343
340
|
```js
|
|
344
|
-
const
|
|
341
|
+
const customVersionStrategy = {
|
|
345
342
|
storage: function () {
|
|
346
343
|
let versions = {}
|
|
347
344
|
return {
|
|
@@ -357,7 +354,9 @@ const versioning = {
|
|
|
357
354
|
}
|
|
358
355
|
|
|
359
356
|
const fastify = require('fastify')({
|
|
360
|
-
|
|
357
|
+
constraints: {
|
|
358
|
+
version: customVersionStrategy
|
|
359
|
+
}
|
|
361
360
|
})
|
|
362
361
|
```
|
|
363
362
|
|
|
@@ -840,6 +839,47 @@ The input `schema` can access all the shared schemas added with [`.addSchema`](#
|
|
|
840
839
|
#### schemaErrorFormatter
|
|
841
840
|
This property can be used set a function to format errors that happen while the `validationCompiler` fails to validate the schema. See [#error-handling](Validation-and-Serialization.md#schemaerrorformatter).
|
|
842
841
|
|
|
842
|
+
<a name="schema-controller"></a>
|
|
843
|
+
#### schemaController
|
|
844
|
+
This property can be used to fully manage where the schemas of your application will be stored.
|
|
845
|
+
It can be useful when your schemas are stored in another data structure that is unknown to Fastify.
|
|
846
|
+
See [issue #2446](https://github.com/fastify/fastify/issues/2446) for an example of what
|
|
847
|
+
this property helps to resolve.
|
|
848
|
+
|
|
849
|
+
```js
|
|
850
|
+
const fastify = Fastify({
|
|
851
|
+
schemaController: {
|
|
852
|
+
/**
|
|
853
|
+
* This factory is called whenever `fastify.register()` is called.
|
|
854
|
+
* It may receive as input the schemas of the parent context if some schemas has been added.
|
|
855
|
+
* @param {object} parentSchemas these schemas will be returned by the `getSchemas()` method function of the returned `bucket`.
|
|
856
|
+
*/
|
|
857
|
+
bucket: function factory (parentSchemas) {
|
|
858
|
+
return {
|
|
859
|
+
addSchema (inputSchema) {
|
|
860
|
+
// This function must store the schema added by the user.
|
|
861
|
+
// This function is invoked when `fastify.addSchema()` is called.
|
|
862
|
+
},
|
|
863
|
+
getSchema (schema$id) {
|
|
864
|
+
// This function must return the raw schema requested by the `schema$id`.
|
|
865
|
+
// This function is invoked when `fastify.getSchema(id)` is called.
|
|
866
|
+
return aSchema
|
|
867
|
+
},
|
|
868
|
+
getSchemas () {
|
|
869
|
+
// This function must return all the schemas referenced by the routes schemas' $ref
|
|
870
|
+
// It must return a JSON where the property is the schema `$id` and the value is the raw JSON Schema.
|
|
871
|
+
const allTheSchemaStored = {
|
|
872
|
+
'schema$id1': schema1,
|
|
873
|
+
'schema$id2': schema2
|
|
874
|
+
}
|
|
875
|
+
return allTheSchemaStored
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
});
|
|
881
|
+
```
|
|
882
|
+
|
|
843
883
|
<a name="set-not-found-handler"></a>
|
|
844
884
|
#### setNotFoundHandler
|
|
845
885
|
|
|
@@ -920,10 +960,32 @@ fastify.ready(() => {
|
|
|
920
960
|
})
|
|
921
961
|
```
|
|
922
962
|
|
|
963
|
+
<a name="print-plugins"></a>
|
|
964
|
+
#### printPlugins
|
|
965
|
+
|
|
966
|
+
`fastify.printPlugins()`: Prints the representation of the internal plugin tree used by the avvio, useful for debugging require order issues.<br/>
|
|
967
|
+
*Remember to call it inside or after a `ready` call.*
|
|
968
|
+
|
|
969
|
+
```js
|
|
970
|
+
fastify.register(async function foo (instance) {
|
|
971
|
+
instance.register(async function bar () {})
|
|
972
|
+
})
|
|
973
|
+
fastify.register(async function baz () {})
|
|
974
|
+
|
|
975
|
+
fastify.ready(() => {
|
|
976
|
+
console.error(fastify.printPlugins())
|
|
977
|
+
// will output the following to stderr:
|
|
978
|
+
// └── root
|
|
979
|
+
// ├── foo
|
|
980
|
+
// │ └── bar
|
|
981
|
+
// └── baz
|
|
982
|
+
})
|
|
983
|
+
```
|
|
984
|
+
|
|
923
985
|
<a name="addContentTypeParser"></a>
|
|
924
986
|
#### addContentTypeParser
|
|
925
987
|
|
|
926
|
-
`fastify.addContentTypeParser(content-type, options, parser)` is used to pass custom parser for a given content type. Useful for adding parsers for custom content types, e.g. `text/json, application/vnd.oasis.opendocument.text`. `content-type` can be a string
|
|
988
|
+
`fastify.addContentTypeParser(content-type, options, parser)` is used to pass custom parser for a given content type. Useful for adding parsers for custom content types, e.g. `text/json, application/vnd.oasis.opendocument.text`. `content-type` can be a string, string array or RegExp.
|
|
927
989
|
|
|
928
990
|
```js
|
|
929
991
|
// The two arguments passed to getDefaultJsonParser are for ProtoType poisoning and Constructor Poisoning configuration respectively. The possible values are 'ignore', 'remove', 'error'. ignore skips all validations and it is similar to calling JSON.parse() directly. See the <a href="https://github.com/fastify/secure-json-parse#api">`secure-json-parse` documentation</a> for more information.
|
|
@@ -978,10 +1040,14 @@ Currently the properties that can be exposed are:
|
|
|
978
1040
|
- http2
|
|
979
1041
|
- https (it will return `false`/`true` or `{ allowHTTP1: true/false }` if explicitly passed)
|
|
980
1042
|
- ignoreTrailingSlash
|
|
1043
|
+
- disableRequestLogging
|
|
981
1044
|
- maxParamLength
|
|
982
1045
|
- onProtoPoisoning
|
|
1046
|
+
- onConstructorPoisoning
|
|
983
1047
|
- pluginTimeout
|
|
984
1048
|
- requestIdHeader
|
|
1049
|
+
- requestIdLogLabel
|
|
1050
|
+
- http2SessionTimeout
|
|
985
1051
|
|
|
986
1052
|
```js
|
|
987
1053
|
const { readFileSync } = require('fs')
|
package/docs/Serverless.md
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
<h1 align="center">Serverless</h1>
|
|
2
2
|
|
|
3
3
|
Run serverless applications and REST APIs using your existing Fastify application.
|
|
4
|
+
By default, Fastify won't work on your serverless platform of choice, you'll need
|
|
5
|
+
to make some small changes to fix this. This document contains a small guide for
|
|
6
|
+
the most famous serverless providers and how to use Fastify with them.
|
|
7
|
+
|
|
8
|
+
#### Should you use Fastify in a serverless platform?
|
|
9
|
+
|
|
10
|
+
That's 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's important to remember that the bigger the application, the slower the initial boot will be.
|
|
13
|
+
The best way to use for running 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 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 directly the Fastify application without the need
|
|
19
|
+
for any additional tool, while the same code will be executed in your serverless platform of
|
|
20
|
+
choice with an additional snippet of code.
|
|
4
21
|
|
|
5
22
|
### Contents
|
|
6
23
|
|
|
@@ -9,16 +26,6 @@ Run serverless applications and REST APIs using your existing Fastify applicatio
|
|
|
9
26
|
- [Netlify Lambda](#netlify-lambda)
|
|
10
27
|
- [Vercel](#vercel)
|
|
11
28
|
|
|
12
|
-
### Attention Readers:
|
|
13
|
-
> Fastify is not designed to run on serverless environments.
|
|
14
|
-
The Fastify framework is designed to make implementing a traditional HTTP/S server easy.
|
|
15
|
-
Serverless environments requests differently than a standard HTTP/S server;
|
|
16
|
-
thus, we cannot guarantee it will work as expected with Fastify.
|
|
17
|
-
Regardless, based on the examples given in this document,
|
|
18
|
-
it is possible to use Fastify in a serverless environment.
|
|
19
|
-
Again, keep in mind that this is not Fastify's intended use case and
|
|
20
|
-
we do not test for such integration scenarios.
|
|
21
|
-
|
|
22
29
|
## AWS Lambda
|
|
23
30
|
|
|
24
31
|
The sample provided allows you to easily build serverless web applications/services
|
|
@@ -49,7 +56,7 @@ if (require.main === module) {
|
|
|
49
56
|
}
|
|
50
57
|
```
|
|
51
58
|
|
|
52
|
-
When executed in your lambda function we
|
|
59
|
+
When executed in your lambda function we do not need to listen to a specific port,
|
|
53
60
|
so we just export the wrapper function `init` in this case.
|
|
54
61
|
The [`lambda.js`](https://www.fastify.io/docs/latest/Serverless/#lambda-js) file will use this export.
|
|
55
62
|
|
|
@@ -90,8 +97,8 @@ An example deployable with [claudia.js](https://claudiajs.com/tutorials/serverle
|
|
|
90
97
|
|
|
91
98
|
### Considerations
|
|
92
99
|
|
|
93
|
-
- API Gateway
|
|
94
|
-
- API Gateway has a timeout of 29 seconds, so it
|
|
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.
|
|
95
102
|
|
|
96
103
|
## Google Cloud Run
|
|
97
104
|
|
|
@@ -262,7 +269,7 @@ module.exports = {
|
|
|
262
269
|
|
|
263
270
|
### Scripts
|
|
264
271
|
|
|
265
|
-
Add this command to your `package.json` *scripts*
|
|
272
|
+
Add this command to your `package.json` *scripts*
|
|
266
273
|
|
|
267
274
|
```json
|
|
268
275
|
"scripts": {
|
|
@@ -279,69 +286,41 @@ Then it should work fine
|
|
|
279
286
|
|
|
280
287
|
[Vercel](https://vercel.com) provides zero configuration deployment for
|
|
281
288
|
Node.js applications. In order to use now, it is as simple as
|
|
282
|
-
configuring your `
|
|
289
|
+
configuring your `vercel.json` file like the following:
|
|
283
290
|
|
|
284
291
|
```json
|
|
285
292
|
{
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
"helpers": false
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
],
|
|
296
|
-
"routes": [
|
|
297
|
-
{ "src": "/.*", "dest": "/api/serverless.js"}
|
|
298
|
-
]
|
|
293
|
+
"rewrites": [
|
|
294
|
+
{
|
|
295
|
+
"source": "/(.*)",
|
|
296
|
+
"destination": "/api/serverless.js"
|
|
297
|
+
}
|
|
298
|
+
]
|
|
299
299
|
}
|
|
300
300
|
```
|
|
301
301
|
|
|
302
302
|
Then, write a `api/serverless.js` like so:
|
|
303
303
|
|
|
304
304
|
```js
|
|
305
|
-
|
|
305
|
+
"use strict";
|
|
306
306
|
|
|
307
|
-
|
|
307
|
+
// Read the .env file.
|
|
308
|
+
import * as dotenv from "dotenv";
|
|
309
|
+
dotenv.config();
|
|
308
310
|
|
|
309
|
-
|
|
311
|
+
// Require the framework
|
|
312
|
+
import Fastify from "fastify";
|
|
310
313
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
}
|
|
315
|
-
```
|
|
314
|
+
// Instantiate Fastify with some config
|
|
315
|
+
const app = Fastify({
|
|
316
|
+
logger: true,
|
|
317
|
+
});
|
|
316
318
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
```js
|
|
320
|
-
'use strict'
|
|
319
|
+
// Register your application as a normal plugin.
|
|
320
|
+
app.register(import("../src/app"));
|
|
321
321
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
const app = fastify({
|
|
326
|
-
logger: true
|
|
327
|
-
})
|
|
328
|
-
|
|
329
|
-
app.get('/', async (req, res) => {
|
|
330
|
-
const { name = 'World' } = req.query
|
|
331
|
-
req.log.info({ name }, 'hello world!')
|
|
332
|
-
return `Hello ${name}!`
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
return app
|
|
322
|
+
export default async (req, res) => {
|
|
323
|
+
await app.ready();
|
|
324
|
+
app.server.emit('request', req, res);
|
|
336
325
|
}
|
|
337
|
-
|
|
338
|
-
module.exports = build
|
|
339
|
-
```
|
|
340
|
-
|
|
341
|
-
Note that you'll need to use Node 10 by setting it in `package.json`:
|
|
342
|
-
|
|
343
|
-
```js
|
|
344
|
-
"engines": {
|
|
345
|
-
"node": "10.x"
|
|
346
|
-
},
|
|
347
326
|
```
|
package/docs/Style-Guide.md
CHANGED
|
@@ -6,9 +6,9 @@ Welcome to *Fastify Style Guide*. This guide is here to provide you with a conve
|
|
|
6
6
|
|
|
7
7
|
## Who is this guide for?
|
|
8
8
|
|
|
9
|
-
This guide is for anyone who loves to build with Fastify or wants to contribute to our documentation. You
|
|
9
|
+
This guide is for anyone who loves to build with Fastify or wants to contribute to our documentation. You do not need to be an expert in writing technical documentation. This guide is here to help you.
|
|
10
10
|
|
|
11
|
-
Visit the [contribute](https://www.fastify.io/contribute) page on our website or read the [
|
|
11
|
+
Visit the [contribute](https://www.fastify.io/contribute) page on our website or read the [CONTRIBUTING.md](/CONTRIBUTING.md) file on GitHub to join our Open Source folks.
|
|
12
12
|
|
|
13
13
|
## Before you write
|
|
14
14
|
|
|
@@ -17,14 +17,14 @@ You need to know the following:
|
|
|
17
17
|
* JavaScript
|
|
18
18
|
* Node.js
|
|
19
19
|
* Git
|
|
20
|
-
*
|
|
20
|
+
* GitHub
|
|
21
21
|
* Markdown
|
|
22
22
|
* HTTP
|
|
23
23
|
* NPM
|
|
24
24
|
|
|
25
25
|
### Consider your Audience
|
|
26
26
|
|
|
27
|
-
Before you start writing, think about your audience. In this case, your audience should already know HTTP, JavaScript, NPM and
|
|
27
|
+
Before you start writing, think about your audience. In this case, your audience should already know HTTP, JavaScript, NPM, and Node.js. It is necessary to keep your readers in mind because they are the ones consuming your content. You want to give as much useful information as possible. Consider the vital things they need to know and how they can understand them. Use words and references that readers can relate with easily. Ask for feedback from the community, it can help you write better documentation that focuses on the user and what you want to achieve.
|
|
28
28
|
|
|
29
29
|
### Get straight to the point
|
|
30
30
|
|
|
@@ -39,27 +39,27 @@ More Like this: To register a parametric path, put a colon before the parameter
|
|
|
39
39
|
### Avoid adding video or image content
|
|
40
40
|
|
|
41
41
|
|
|
42
|
-
Do not add videos or screenshots in the documentation. It is easier to keep under version control. Videos and images will eventually end up becoming outdated as new updates keep developing. Instead, make a referral link or a
|
|
42
|
+
Do not add videos or screenshots in the documentation. It is easier to keep under version control. Videos and images will eventually end up becoming outdated as new updates keep developing. Instead, make a referral link or a YouTube video. You can add links by using `[Title](www.websitename.com)` in the markdown.
|
|
43
43
|
|
|
44
44
|
**Example**
|
|
45
45
|
|
|
46
46
|
```
|
|
47
|
-
To learn more about hooks,
|
|
47
|
+
To learn more about hooks, see [Fastify hooks](https://www.fastify.io/docs/latest/Hooks).
|
|
48
48
|
```
|
|
49
49
|
|
|
50
50
|
Result:
|
|
51
|
-
>To learn more about hooks,
|
|
51
|
+
>To learn more about hooks, see [Fastify hooks](https://www.fastify.io/docs/latest/Hooks/).
|
|
52
52
|
|
|
53
53
|
|
|
54
54
|
|
|
55
55
|
### Avoid plagiarism
|
|
56
56
|
|
|
57
|
-
Make sure you avoid copying other people's work. Keep it as original as possible. You can learn from what they
|
|
57
|
+
Make sure you avoid copying other people's work. Keep it as original as possible. You can learn from what they have done and reference where it is from if you used a particular quote from their work.
|
|
58
58
|
|
|
59
59
|
|
|
60
60
|
## Word Choice
|
|
61
61
|
|
|
62
|
-
There are a few things you need to use and avoid when writing your documentation to improve readability for readers and
|
|
62
|
+
There are a few things you need to use and avoid when writing your documentation to improve readability for readers and make documentation neat, direct, and clean.
|
|
63
63
|
|
|
64
64
|
|
|
65
65
|
### When to use the second person "you" as the pronoun
|
|
@@ -68,7 +68,7 @@ When writing articles or guides, your content should communicate directly to rea
|
|
|
68
68
|
|
|
69
69
|
**Example**
|
|
70
70
|
|
|
71
|
-
Less
|
|
71
|
+
Less like this: we can use the following plugins.
|
|
72
72
|
|
|
73
73
|
More like this: You can use the following plugins.
|
|
74
74
|
|
|
@@ -80,13 +80,18 @@ One of the main rules of formal writing such as reference documentation, or API
|
|
|
80
80
|
|
|
81
81
|
**Example**
|
|
82
82
|
|
|
83
|
-
Less
|
|
83
|
+
Less like this: You can use the following recommendation as an example.
|
|
84
84
|
|
|
85
85
|
More like this: As an example, the following recommendations should be referenced.
|
|
86
86
|
|
|
87
87
|
To view a live example, refer to the [Decorators.md](/docs/Decorators.md) reference document.
|
|
88
88
|
|
|
89
89
|
|
|
90
|
+
### Avoid using contractions
|
|
91
|
+
|
|
92
|
+
Contractions are the shortened version of written and spoken forms of a word, i.e. using "don't" instead of "do not".
|
|
93
|
+
Avoid contractions to provide a more formal tone.
|
|
94
|
+
|
|
90
95
|
### Avoid using condescending terms
|
|
91
96
|
|
|
92
97
|
Condescending terms are words that include:
|
|
@@ -97,22 +102,22 @@ Condescending terms are words that include:
|
|
|
97
102
|
* Basically
|
|
98
103
|
* Obviously
|
|
99
104
|
|
|
100
|
-
The reader may not find it easy to use
|
|
105
|
+
The reader may not find it easy to use Fastify's framework and plugins; avoid words that make it sound simple, easy, offensive, or insensitive. Not everyone who reads the documentation has the same level of understanding.
|
|
101
106
|
|
|
102
107
|
|
|
103
108
|
### Starting with a verb
|
|
104
109
|
|
|
105
|
-
Mostly start your description with a verb, which makes it simple and precise for the reader to follow. Prefer
|
|
110
|
+
Mostly start your description with a verb, which makes it simple and precise for the reader to follow. Prefer using present tense because it is easier to read and understand than the past or future tense.
|
|
106
111
|
|
|
107
112
|
**Example**
|
|
108
113
|
|
|
109
|
-
Less like this: There is a need for
|
|
114
|
+
Less like this: There is a need for Node.js to be installed before you can be able to use Fastify.
|
|
110
115
|
|
|
111
|
-
More like this: Install
|
|
116
|
+
More like this: Install Node.js to make use of Fastify.
|
|
112
117
|
|
|
113
118
|
### Grammatical moods
|
|
114
119
|
|
|
115
|
-
Grammatical moods
|
|
120
|
+
Grammatical moods are a great way to express your writing. Avoid sounding too bossy while making a direct statement. Know when to switch between indicative, imperative, and subjunctive moods.
|
|
116
121
|
|
|
117
122
|
|
|
118
123
|
**Indicative** - Use when making a factual statement or question.
|
|
@@ -158,7 +163,7 @@ Here is how your hyperlink should look:
|
|
|
158
163
|
```MD
|
|
159
164
|
<!-- More like this -->
|
|
160
165
|
|
|
161
|
-
// Add clear &
|
|
166
|
+
// Add clear & brief description
|
|
162
167
|
[Fastify Plugins] (https://www.fastify.io/docs/latest/Plugins/)
|
|
163
168
|
|
|
164
169
|
<!--Less like this -->
|
|
@@ -172,9 +177,9 @@ Here is how your hyperlink should look:
|
|
|
172
177
|
// Empty title
|
|
173
178
|
[](https://www.fastify.io/docs/latest/Plugins/)
|
|
174
179
|
|
|
175
|
-
// Adding links localhost
|
|
180
|
+
// Adding links localhost URLs instead of using code strings (``)
|
|
176
181
|
[http://localhost:3000/](http://localhost:3000/)
|
|
177
182
|
|
|
178
183
|
```
|
|
179
184
|
|
|
180
|
-
|
|
185
|
+
Include in your documentation as many essential references as possible, but avoid having numerous links when writing for beginners to avoid distractions.
|
package/docs/Testing.md
CHANGED
|
@@ -6,11 +6,11 @@ Testing is one of the most important parts of developing an application. Fastify
|
|
|
6
6
|
|
|
7
7
|
Let's `cd` into a fresh directory called 'testing-example' and type `npm init -y` in our terminal.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Run `npm install fastify && npm install tap pino-pretty --save-dev`
|
|
10
10
|
|
|
11
11
|
### Separating concerns makes testing easy
|
|
12
12
|
|
|
13
|
-
First we
|
|
13
|
+
First we are going to separate our application code from our server code:
|
|
14
14
|
|
|
15
15
|
**app.js**:
|
|
16
16
|
|
|
@@ -55,7 +55,7 @@ server.listen(3000, (err, address) => {
|
|
|
55
55
|
|
|
56
56
|
Fastify comes with built-in support for fake http injection thanks to [`light-my-request`](https://github.com/fastify/light-my-request).
|
|
57
57
|
|
|
58
|
-
Before introducing any tests, we
|
|
58
|
+
Before introducing any tests, we will use the `.inject` method to make a fake request to our route:
|
|
59
59
|
|
|
60
60
|
**app.test.js**:
|
|
61
61
|
|
|
@@ -80,7 +80,7 @@ test()
|
|
|
80
80
|
|
|
81
81
|
First, our code will run inside an asynchronous function, giving us access to async/await.
|
|
82
82
|
|
|
83
|
-
`.inject` insures all registered plugins have booted up and our application is ready to test.
|
|
83
|
+
`.inject` insures 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.
|
|
84
84
|
|
|
85
85
|
|
|
86
86
|
|
|
@@ -120,7 +120,7 @@ test('requests the "/" route', async t => {
|
|
|
120
120
|
})
|
|
121
121
|
```
|
|
122
122
|
|
|
123
|
-
Finally run `npm test` in the terminal and see your test results!
|
|
123
|
+
Finally, run `npm test` in the terminal and see your test results!
|
|
124
124
|
|
|
125
125
|
The `inject` method can do much more than a simple GET request to a URL:
|
|
126
126
|
```js
|