From 5fe23a302df1550d04ef30ccfa953ea8a9d21b79 Mon Sep 17 00:00:00 2001 From: Leandro Andrade Date: Sun, 16 Oct 2022 11:20:14 -0300 Subject: [PATCH 1/3] docs: example how decorators dependencies works --- docs/Reference/Decorators.md | 40 ++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/docs/Reference/Decorators.md b/docs/Reference/Decorators.md index 9a051e6305..2b5ba99819 100644 --- a/docs/Reference/Decorators.md +++ b/docs/Reference/Decorators.md @@ -114,10 +114,46 @@ fastify.get('/', async function (request, reply) { The `dependencies` parameter is an optional list of decorators that the decorator being defined relies upon. This list is simply a list of string names of other decorators. In the following example, the "utility" decorator depends -upon "greet" and "log" decorators: +upon "greet" and "hi" decorators: ```js -fastify.decorate('utility', fn, ['greet', 'log']) +'use strict' + +const fastify = require('../fastify')({ + logger: false +}) +const fp = require('fastify-plugin') + +async function greetDecorator (fastify, opts) { + fastify.decorate('greet', () => { + return 'greet message' + }) +} + +async function hiDecorator (fastify, opts) { + fastify.decorate('hi', () => { + return 'hi message' + }) +} + +async function utilityDecorator (fastify, opts) { + fastify.decorate('utility', () => { + return `${fastify.greet()} | ${fastify.hi()}` + }) +} + +fastify.register(fp(greetDecorator, { name: 'greet' })) +fastify.register(fp(hiDecorator, { name: 'hi' })) +fastify.register(fp(utilityDecorator, { dependencies: ['greet', 'hi'] })) + +fastify.get('/', function (req, reply) { + // Response: {"hello":"greet message | hi message"} + reply.send({ hello: fastify.utility() }) +}) + +fastify.listen({ port: 3000 }, (err, address) => { + if (err) throw err +}) ``` Note: using an arrow function will break the binding of `this` to the From 927813cef93ba55dd3ed72bf832e47959feb0eff Mon Sep 17 00:00:00 2001 From: Leandro Andrade Date: Mon, 17 Oct 2022 08:23:25 -0300 Subject: [PATCH 2/3] docs: remove unnecessary example definitions --- docs/Reference/Decorators.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/docs/Reference/Decorators.md b/docs/Reference/Decorators.md index 2b5ba99819..f14ebbaeb4 100644 --- a/docs/Reference/Decorators.md +++ b/docs/Reference/Decorators.md @@ -117,13 +117,6 @@ of other decorators. In the following example, the "utility" decorator depends upon "greet" and "hi" decorators: ```js -'use strict' - -const fastify = require('../fastify')({ - logger: false -}) -const fp = require('fastify-plugin') - async function greetDecorator (fastify, opts) { fastify.decorate('greet', () => { return 'greet message' From 30f1a68ad8ce6a1558c8538bf06d7a4bc0088312 Mon Sep 17 00:00:00 2001 From: Leandro Andrade Date: Mon, 17 Oct 2022 09:09:48 -0300 Subject: [PATCH 3/3] docs: change fp to fastifyPlugin to improve readability --- docs/Reference/Decorators.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/Reference/Decorators.md b/docs/Reference/Decorators.md index f14ebbaeb4..b2bf0a9985 100644 --- a/docs/Reference/Decorators.md +++ b/docs/Reference/Decorators.md @@ -135,9 +135,9 @@ async function utilityDecorator (fastify, opts) { }) } -fastify.register(fp(greetDecorator, { name: 'greet' })) -fastify.register(fp(hiDecorator, { name: 'hi' })) -fastify.register(fp(utilityDecorator, { dependencies: ['greet', 'hi'] })) +fastify.register(fastifyPlugin(greetDecorator, { name: 'greet' })) +fastify.register(fastifyPlugin(hiDecorator, { name: 'hi' })) +fastify.register(fastifyPlugin(utilityDecorator, { dependencies: ['greet', 'hi'] })) fastify.get('/', function (req, reply) { // Response: {"hello":"greet message | hi message"}