Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the exact use of fastify-plugin

I new to fastify framework for node.js, and I'm asking for what is the exact use of fastify-plugin because I can't get the the idea behind it. I tried some code with or without the plugin and I can't notice the difference. except for some behavior like :

1- I can override the decorator I initiate and wrapped it with fastify-plugin.

2- I can use and share the decorator with other registered plugins.

like image 903
Momen Nano Avatar asked Apr 03 '20 21:04

Momen Nano


1 Answers

The concept is this one:

  • every register call will create an encapsulated context
  • every register + fastify-plugin will not create an encapsulated context: you will stay in the same context where the register was called

An encapsulated context you will use:

  • all the hooks in the context and in its parent
  • all the decorators in the context and in its parent

Here a visualization:

encapsulation

so, if you add an onRequest hook in the root node (the blue one), all the routes will execute that hook.

if you add the onRequest hook in the green one instead, ONLY the routes defined in that context will use that hook.

like image 154
Manuel Spigolon Avatar answered Nov 15 '22 04:11

Manuel Spigolon