Menu

Plugins and encapsulation — Fastify with TypeScript (part 8/18)
📰

Plugins and encapsulation — Fastify with TypeScript (part 8/18)

DEV Community: fastify·Antonio Tripodi·10 days ago
#guMu1vIu
#devtofastify#devto#dev#fastify#plugin
Long article (1217 words) — no AI summary yet.
Reading 0:00
15s threshold

Plugins and encapsulation

Eighth chapter of the "Fastify with TypeScript" series. The plugin system is arguably Fastify's most distinctive feature, and understanding encapsulation is essential to structuring any application well. We have been using a plugin since chapter 5 ( userRoutes is one). In this chapter we write a second one, watch it fail in an instructive way, and fix it.

What a plugin is

A plugin is a function that receives a Fastify instance and adds things to it: routes, hooks, decorators, other plugins. You hand it to app.register() .

The instance a plugin receives is not the root app . It is a child of it, and that is the whole point: what a plugin adds to its instance (hooks, decorators) exists inside that plugin and its own children, and nowhere else. This is encapsulation .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.