Decorators
Eleventh chapter of the "Fastify with TypeScript" series. Decorators let you add your own properties and methods to the Fastify instance, the request, and the reply, in a fully type-safe way. We've been using one since chapter 4 without naming it: fastify.config . In this chapter we write another, to solve a problem the next chapter would otherwise run into.
The problem: state trapped in a plugin
Our users live in an array declared inside the userRoutes plugin. That was the right place for it (chapter 5), but it means nothing outside that function can reach it. The next chapter adds an auth module with a login route, and login needs to look a user up by email. Two modules, one set of data: it has to live somewhere both can see.
A global variable would do it, and we've already said why we don't want one.…