fastify 4.19.0 → 4.19.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.
@@ -28,7 +28,7 @@ snippet of code.
28
28
  - [Google Cloud Functions](#google-cloud-functions)
29
29
  - [Google Cloud Run](#google-cloud-run)
30
30
  - [Netlify Lambda](#netlify-lambda)
31
- - [Platformatic Cloud](#platformatic)
31
+ - [Platformatic Cloud](#platformatic-cloud)
32
32
  - [Vercel](#vercel)
33
33
 
34
34
  ## AWS
@@ -1,12 +1,15 @@
1
- <h1 align="center">Fastify</h1>
1
+ <h1 style="text-align: center;">Fastify</h1>
2
2
 
3
- ## Testing
3
+ # Testing
4
+ <a id="testing"></a>
4
5
 
5
6
  Testing is one of the most important parts of developing an application. Fastify
6
7
  is very flexible when it comes to testing and is compatible with most testing
7
8
  frameworks (such as [Tap](https://www.npmjs.com/package/tap), which is used in
8
9
  the examples below).
9
10
 
11
+ ## Application
12
+
10
13
  Let's `cd` into a fresh directory called 'testing-example' and type `npm init
11
14
  -y` in our terminal.
12
15
 
@@ -345,3 +348,133 @@ test('should ...', {only: true}, t => ...)
345
348
 
346
349
  Now you should be able to step through your test file (and the rest of
347
350
  `Fastify`) in your code editor.
351
+
352
+
353
+
354
+ ## Plugins
355
+ Let's `cd` into a fresh directory called 'testing-plugin-example' and type `npm init
356
+ -y` in our terminal.
357
+
358
+ Run `npm i fastify fastify-plugin && npm i tap -D`
359
+
360
+ **plugin/myFirstPlugin.js**:
361
+
362
+ ```js
363
+ const fP = require("fastify-plugin")
364
+
365
+ async function myPlugin(fastify, options) {
366
+ fastify.decorateRequest("helloRequest", "Hello World")
367
+ fastify.decorate("helloInstance", "Hello Fastify Instance")
368
+ }
369
+
370
+ module.exports = fP(myPlugin)
371
+ ```
372
+
373
+ A basic example of a Plugin. See [Plugin Guide](./Plugins-Guide.md)
374
+
375
+ **test/myFirstPlugin.test.js**:
376
+
377
+ ```js
378
+ const Fastify = require("fastify");
379
+ const tap = require("tap");
380
+ const myPlugin = require("../plugin/myFirstPlugin");
381
+
382
+ tap.test("Test the Plugin Route", async t => {
383
+ // Create a mock fastify application to test the plugin
384
+ const fastify = Fastify()
385
+
386
+ fastify.register(myPlugin)
387
+
388
+ // Add an endpoint of your choice
389
+ fastify.get("/", async (request, reply) => {
390
+ return ({ message: request.helloRequest })
391
+ })
392
+
393
+ // Use fastify.inject to fake a HTTP Request
394
+ const fastifyResponse = await fastify.inject({
395
+ method: "GET",
396
+ url: "/"
397
+ })
398
+
399
+ console.log('status code: ', fastifyResponse.statusCode)
400
+ console.log('body: ', fastifyResponse.body)
401
+ })
402
+ ```
403
+ Learn more about [```fastify.inject()```](#benefits-of-using-fastifyinject).
404
+ Run the test file in your terminal `node test/myFirstPlugin.test.js`
405
+
406
+ ```sh
407
+ status code: 200
408
+ body: {"message":"Hello World"}
409
+ ```
410
+
411
+ Now we can replace our `console.log` calls with actual tests!
412
+
413
+ In your `package.json` change the "test" script to:
414
+
415
+ `"test": "tap --reporter=list --watch"`
416
+
417
+ Create the tap test for the endpoint.
418
+
419
+ **test/myFirstPlugin.test.js**:
420
+
421
+ ```js
422
+ const Fastify = require("fastify");
423
+ const tap = require("tap");
424
+ const myPlugin = require("../plugin/myFirstPlugin");
425
+
426
+ tap.test("Test the Plugin Route", async t => {
427
+ // Specifies the number of test
428
+ t.plan(2)
429
+
430
+ const fastify = Fastify()
431
+
432
+ fastify.register(myPlugin)
433
+
434
+ fastify.get("/", async (request, reply) => {
435
+ return ({ message: request.helloRequest })
436
+ })
437
+
438
+ const fastifyResponse = await fastify.inject({
439
+ method: "GET",
440
+ url: "/"
441
+ })
442
+
443
+ t.equal(fastifyResponse.statusCode, 200)
444
+ t.same(JSON.parse(fastifyResponse.body), { message: "Hello World" })
445
+ })
446
+ ```
447
+
448
+ Finally, run `npm test` in the terminal and see your test results!
449
+
450
+ Test the ```.decorate()``` and ```.decorateRequest()```.
451
+
452
+ **test/myFirstPlugin.test.js**:
453
+
454
+ ```js
455
+ const Fastify = require("fastify");
456
+ const tap = require("tap");
457
+ const myPlugin = require("../plugin/myFirstPlugin");
458
+
459
+ tap.test("Test the Plugin Route", async t => {
460
+ t.plan(5)
461
+ const fastify = Fastify()
462
+
463
+ fastify.register(myPlugin)
464
+
465
+ fastify.get("/", async (request, reply) => {
466
+ // Testing the fastify decorators
467
+ t.not(request.helloRequest, null)
468
+ t.ok(request.helloRequest, "Hello World")
469
+ t.ok(fastify.helloInstance, "Hello Fastify Instance")
470
+ return ({ message: request.helloRequest })
471
+ })
472
+
473
+ const fastifyResponse = await fastify.inject({
474
+ method: "GET",
475
+ url: "/"
476
+ })
477
+ t.equal(fastifyResponse.statusCode, 200)
478
+ t.same(JSON.parse(fastifyResponse.body), { message: "Hello World" })
479
+ })
480
+ ```
@@ -1,4 +1,4 @@
1
- <h1 align="center">Fastify</h1>
1
+ <h1 style="text-align: center;">Fastify</h1>
2
2
 
3
3
  # How to write a good plugin
4
4
  First, thank you for deciding to write a plugin for Fastify. Fastify is a
@@ -63,6 +63,8 @@ among different versions of its dependencies.
63
63
  We do not enforce any testing library. We use [`tap`](https://www.node-tap.org/)
64
64
  since it offers out-of-the-box parallel testing and code coverage, but it is up
65
65
  to you to choose your library of preference.
66
+ We highly recommend you read the [Plugin Testing](./Testing.md#plugins) to
67
+ learn about how to test your plugins.
66
68
 
67
69
  ## Code Linter
68
70
  It is not mandatory, but we highly recommend you use a code linter in your
@@ -508,7 +508,7 @@ Fastify Plugin in a TypeScript Project.
508
508
  `"compilerOptions"` object.
509
509
  ```json
510
510
  {
511
- "compileOptions": {
511
+ "compilerOptions": {
512
512
  "declaration": true
513
513
  }
514
514
  }
package/fastify.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const VERSION = '4.19.0'
3
+ const VERSION = '4.19.1'
4
4
 
5
5
  const Avvio = require('avvio')
6
6
  const http = require('http')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify",
3
- "version": "4.19.0",
3
+ "version": "4.19.1",
4
4
  "description": "Fast and low overhead web framework, for Node.js",
5
5
  "main": "fastify.js",
6
6
  "type": "commonjs",
@@ -18,6 +18,7 @@ const getHandler: RouteHandlerMethod = function (_request, reply) {
18
18
  expectType<FastifyRequest<RouteGenericInterface, RawServerDefault, RawRequestDefaultExpression>>(reply.request)
19
19
  expectType<<Code extends number>(statusCode: Code) => DefaultFastifyReplyWithCode<Code>>(reply.code)
20
20
  expectType<<Code extends number>(statusCode: Code) => DefaultFastifyReplyWithCode<Code>>(reply.status)
21
+ expectType<(payload?: unknown) => FastifyReply>(reply.code(100 as number).send)
21
22
  expectType<number>(reply.statusCode)
22
23
  expectType<boolean>(reply.sent)
23
24
  expectType<((payload?: unknown) => FastifyReply)>(reply.send)
package/types/reply.d.ts CHANGED
@@ -12,10 +12,11 @@ export interface ReplyGenericInterface {
12
12
  Reply?: ReplyDefault;
13
13
  }
14
14
 
15
- export type ReplyTypeConstrainer<RouteGenericReply, Code extends ReplyKeysToCodes<keyof RouteGenericReply>> =
15
+ export type ReplyTypeConstrainer<RouteGenericReply, Code extends ReplyKeysToCodes<keyof RouteGenericReply>, ReplyKey = CodeToReplyKey<Code>> =
16
16
  Code extends keyof RouteGenericReply ? RouteGenericReply[Code] :
17
- CodeToReplyKey<Code> extends keyof RouteGenericReply ? RouteGenericReply[CodeToReplyKey<Code>] :
18
- unknown;
17
+ [ReplyKey] extends [never] ? unknown :
18
+ ReplyKey extends keyof RouteGenericReply ? RouteGenericReply[ReplyKey] :
19
+ unknown;
19
20
  /**
20
21
  * FastifyReply is an instance of the standard http or http2 reply types.
21
22
  * It defaults to http.ServerResponse, and it also extends the relative reply object.