@wyrly/fastify
v2.3.1
Published
Request-scoped dependency injection adapter for Fastify 5 and TypeScript
Downloads
244
Maintainers
Readme
@wyrly/fastify
Request-scoped dependency injection for Fastify 5 without reflect-metadata — use diPlugin() and
getDI(request) for one scope per HTTP request.
Japanese: README.ja.md
Install
npm install @wyrly/fastify @wyrly/core fastifyPeer dependency: fastify ^5.0.0. The plugin uses fastify-plugin internally so hooks apply to routes registered on the root instance.
Quick start
import Fastify from "fastify";
import { diPlugin, getDI } from "@wyrly/fastify";
import { createContainer } from "@wyrly/core";
const container = createContainer();
const app = Fastify();
await app.register(diPlugin(container));
app.get("/users/:id", async (request) => {
const di = getDI(request);
// di.resolve(SomeUseCase)
return { ok: true };
});Scopes are disposed on response finish / close (same lifecycle as @wyrly/express). Map HTTP
data to port tokens in a preHandler (see
examples/fastify-api).
GraphQL on Fastify
For GraphQL on Fastify, combine with
@wyrly/apollo or
@wyrly/yoga. Mount the GraphQL
handler on a Fastify route and use the GraphQL adapter for resolver scopes; keep diPlugin for REST
routes. See yoga-graphql and
apollo-express-graphql.
Documentation
Related packages
| Package | npm | Description |
| ---------------- | -------- | --------------------- |
| @wyrly/core | yes | Core DI |
| @wyrly/next | yes | Next.js App Router |
| @wyrly/express | yes | Express 5 middleware |
| @wyrly/hono | yes | Hono middleware |
| @wyrly/fastify | yes | This package |
| @wyrly/fresh | JSR only | Fresh 2.x |
| @wyrly/graphql | yes | GraphQL request scope |
| @wyrly/yoga | yes | GraphQL Yoga 5 plugin |
| @wyrly/apollo | yes | Apollo Server plugin |
License
Apache-2.0 — LICENSE
