From ece9bd8edac866c7e7f2c5f131cc077a3f759027 Mon Sep 17 00:00:00 2001 From: philsch Date: Fri, 16 Sep 2022 17:49:10 +0200 Subject: [PATCH 1/5] docs: onRoute hooks in plugins --- docs/Guides/Plugins-Guide.md | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/docs/Guides/Plugins-Guide.md b/docs/Guides/Plugins-Guide.md index 6f1d543525..8e1fb78513 100644 --- a/docs/Guides/Plugins-Guide.md +++ b/docs/Guides/Plugins-Guide.md @@ -308,6 +308,49 @@ fastify.get('/plugin2', (request, reply) => { ``` Now your hook will run just for the first route! +An alternative approach is to make use of the [onRoute hook](https://www.fastify.io/docs/latest/Reference/Hooks/#onroute) +to customize application routes dynamically from inside the plugin. Every time a new route gets registered, you can read +and modify the route options, for example +based on a [route config option](https://www.fastify.io/docs/latest/Reference/Routes/#routes-options): + +```js +fastify.register((instance, opts, done) => { + instance.decorate('util', (request, key, value) => { request[key] = value }) + + function handler(request, reply, done) { + instance.util(request, 'timestamp', new Date()) + done() + } + + instance.addHook('onRoute', (routeOptions) => { + if (routeOptions.config && routeOptions.config.useUtil === true) { + // set or add our handler to the route preHandler hook + if (!routeOptions.preHandler) { + routeOptions.preHandler = [handler] + return + } + if (Array.isArray(routeOptions.preHandler)) { + routeOptions.preHandler.push(handler) + return + } + routeOptions.preHandler = [routeOptions.preHandler, handler] + } + }) + + fastify.get('/plugin1', {config: {useUtil: true}}, (request, reply) => { + reply.send(request) + }) + + fastify.get('/plugin2', (request, reply) => { + reply.send(request) + }) + + done() +}) +``` + +This variant becomes extremely useful if you plan to distribute your plugin, as described in the next section. + As you probably noticed by now, `request` and `reply` are not the standard Nodejs *request* and *response* objects, but Fastify's objects. From 723e0d223cca1bec520086ae1ae0b75fecb27098 Mon Sep 17 00:00:00 2001 From: philsch Date: Fri, 16 Sep 2022 18:18:10 +0200 Subject: [PATCH 2/5] relative links --- docs/Guides/Plugins-Guide.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/Guides/Plugins-Guide.md b/docs/Guides/Plugins-Guide.md index 8e1fb78513..8f642688db 100644 --- a/docs/Guides/Plugins-Guide.md +++ b/docs/Guides/Plugins-Guide.md @@ -308,10 +308,10 @@ fastify.get('/plugin2', (request, reply) => { ``` Now your hook will run just for the first route! -An alternative approach is to make use of the [onRoute hook](https://www.fastify.io/docs/latest/Reference/Hooks/#onroute) +An alternative approach is to make use of the [onRoute hook](../Reference/Hooks.md#onroute) to customize application routes dynamically from inside the plugin. Every time a new route gets registered, you can read and modify the route options, for example -based on a [route config option](https://www.fastify.io/docs/latest/Reference/Routes/#routes-options): +based on a [route config option](../Reference/Routes.md#routes-options): ```js fastify.register((instance, opts, done) => { From b040d76944efe4fb685b7ef13b4c947025478131 Mon Sep 17 00:00:00 2001 From: philsch Date: Fri, 16 Sep 2022 18:22:13 +0200 Subject: [PATCH 3/5] formatting --- docs/Guides/Plugins-Guide.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/Guides/Plugins-Guide.md b/docs/Guides/Plugins-Guide.md index 8f642688db..f3e852c096 100644 --- a/docs/Guides/Plugins-Guide.md +++ b/docs/Guides/Plugins-Guide.md @@ -309,8 +309,8 @@ fastify.get('/plugin2', (request, reply) => { Now your hook will run just for the first route! An alternative approach is to make use of the [onRoute hook](../Reference/Hooks.md#onroute) -to customize application routes dynamically from inside the plugin. Every time a new route gets registered, you can read -and modify the route options, for example +to customize application routes dynamically from inside the plugin. Every time a new route gets registered, you can read +and modify the route options, for example based on a [route config option](../Reference/Routes.md#routes-options): ```js From 83df6f788a7589dce148641dc5b1a2869c36f02d Mon Sep 17 00:00:00 2001 From: philsch Date: Sat, 17 Sep 2022 08:18:14 +0200 Subject: [PATCH 4/5] linting --- docs/Guides/Plugins-Guide.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/Guides/Plugins-Guide.md b/docs/Guides/Plugins-Guide.md index f3e852c096..dcfbd8dfb5 100644 --- a/docs/Guides/Plugins-Guide.md +++ b/docs/Guides/Plugins-Guide.md @@ -309,8 +309,8 @@ fastify.get('/plugin2', (request, reply) => { Now your hook will run just for the first route! An alternative approach is to make use of the [onRoute hook](../Reference/Hooks.md#onroute) -to customize application routes dynamically from inside the plugin. Every time a new route gets registered, you can read -and modify the route options, for example +to customize application routes dynamically from inside the plugin. Every time +a new route gets registered, you can read and modify the route options, for example based on a [route config option](../Reference/Routes.md#routes-options): ```js @@ -349,7 +349,8 @@ fastify.register((instance, opts, done) => { }) ``` -This variant becomes extremely useful if you plan to distribute your plugin, as described in the next section. +This variant becomes extremely useful if you plan to distribute your plugin, as +described in the next section. As you probably noticed by now, `request` and `reply` are not the standard Nodejs *request* and *response* objects, but Fastify's objects. From 07f5fa8aee1422cf29f782bd4f9ca6b72afda9c1 Mon Sep 17 00:00:00 2001 From: Philipp Schmiedel Date: Tue, 20 Sep 2022 09:37:20 +0200 Subject: [PATCH 5/5] Update docs/Guides/Plugins-Guide.md Co-authored-by: Frazer Smith --- docs/Guides/Plugins-Guide.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Guides/Plugins-Guide.md b/docs/Guides/Plugins-Guide.md index dcfbd8dfb5..e46fb1f980 100644 --- a/docs/Guides/Plugins-Guide.md +++ b/docs/Guides/Plugins-Guide.md @@ -310,7 +310,7 @@ Now your hook will run just for the first route! An alternative approach is to make use of the [onRoute hook](../Reference/Hooks.md#onroute) to customize application routes dynamically from inside the plugin. Every time -a new route gets registered, you can read and modify the route options, for example +a new route is registered, you can read and modify the route options. For example, based on a [route config option](../Reference/Routes.md#routes-options): ```js