Menu

Decorators — Fastify with TypeScript (part 11/18)
📰

Decorators — Fastify with TypeScript (part 11/18)

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

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.…

Continue reading — create a free account

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