From 67bee117a7c5c6dad503f656204e49cf329a37af Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Tue, 30 Aug 2022 13:57:04 +0200 Subject: [PATCH] docs: improved migration guide for onRoute (#4233) * docs: improved migration guide. * docs: have consistent examples. --- docs/Guides/Migration-Guide-V4.md | 68 +++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 18 deletions(-) diff --git a/docs/Guides/Migration-Guide-V4.md b/docs/Guides/Migration-Guide-V4.md index 9968d49709..83fe25bd83 100644 --- a/docs/Guides/Migration-Guide-V4.md +++ b/docs/Guides/Migration-Guide-V4.md @@ -71,28 +71,60 @@ The route registration has been made synchronous from v4. This change was done to provide better error reporting for route definition. As a result if you specify an `onRoute` hook in a plugin you should either: * wrap your routes in a plugin (recommended) + + For example refactor this: + + ``` + fastify.register((instance, opts, done) => { + instance.addHook('onRoute', (routeOptions) => { + const { path, method } = routeOptions; + console.log({ path, method }); + done(); + }); + }); + + fastify.get('/', () => 'hello'); + ``` + + Into this: + + ``` + fastify.register((instance, opts, done) => { + instance.addHook('onRoute', (routeOptions) => { + const { path, method } = routeOptions; + console.log({ path, method }); + done(); + }); + }); + + fastify.register((instance, opts, done) => { + instance.get('/', () => 'hello'); + done(); + }); + ``` + * use `await register(...)` -For example refactor this: -``` -fastify.register((instance, opts, done) => { - instance.addHook('onRoute', (routeOptions) => { - const { path, method } = routeOptions; - console.log({ path, method }); + For example refactor this: + ``` + fastify.register((instance, opts, done) => { + instance.addHook('onRoute', (routeOptions) => { + const { path, method } = routeOptions; + console.log({ path, method }); + }); + done(); }); - done(); -}); -``` -Into this: -``` -await fastify.register((instance, opts, done) => { - instance.addHook('onRoute', (routeOptions) => { - const { path, method } = routeOptions; - console.log({ path, method }); + ``` + Into this: + ``` + await fastify.register((instance, opts, done) => { + instance.addHook('onRoute', (routeOptions) => { + const { path, method } = routeOptions; + console.log({ path, method }); + }); + done(); }); - done(); -}); -``` + ``` ## Non Breaking Changes