npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@tganzhorn/fastify-modular

v0.1.16

Published

Fastify Modular is a lightweight architectural layer for Fastify that introduces a clean, structured way to organize applications using controllers and services — inspired by frameworks like NestJS, but without the overhead.

Readme

📦 Fastify Modular

Fastify Modular is a lightweight architectural layer for Fastify that introduces a clean, structured way to organize applications using controllers and services — inspired by frameworks like NestJS, but without the overhead.

It enables developers to build scalable and testable applications by separating responsibilities:

Controllers handle routing and HTTP logic.

Services encapsulate business logic and reusable operations.


✨ Features

  • ✅ HTTP method decorators: @Get, @Post, @Put, @Patch, @Delete, @All
  • 🧠 Dependency Injection via @Service
  • 🧵 Per-request scoped services via RequestStore<T>
  • 🧹 Parameter decorators: @Body, @Query, @Parameter, @Headers, etc.
  • 📜 Schema validation with @Schema
  • 🔄 Lifecycle hooks with OnServiceInit
  • ⏱️ Background workers via @JobScheduler + @Worker
  • 📱 (Planned) SSE support via @Sse
  • ⚡ Easy controller registration

🚀 Getting Started

1. Install

npm install @tganzhorn/fastify-modular

2. Define a Service

import { Service } from "@tganzhorn/fastify-modular";

@Service([])
class CounterService {
  private counter = 0;
  increment() {
    return ++this.counter;
  }
}

Per-request scoped service:

import { Service, RequestStore } from "@tganzhorn/fastify-modular";

@Service([])
class RequestScopedService extends RequestStore<{ counter: number }> {
  constructor() {
    super({ counter: 0 });
  }

  increment() {
    return ++this.requestStore.counter;
  }
}

3. Define a Controller

import {
  Controller,
  Get,
  Post,
  Body,
  Parameter,
} from "@tganzhorn/fastify-modular";

@Controller("/counter", [CounterService])
class CounterController {
  constructor(private counterService: CounterService) {}

  @Get("/increment")
  increment() {
    return this.counterService.increment();
  }

  @Post("/echo")
  echo(@Body() body: any) {
    return body;
  }

  @Get("/params/:id")
  getById(@Parameter("id") id: string) {
    return id;
  }
}

4. Register Controllers

import Fastify from "fastify";
import { registerControllers } from "@tganzhorn/fastify-modular";

const app = Fastify();

await registerControllers(app, {
  controllers: [CounterController],
  bullMqConnection: { host: "localhost", port: 6379 },
});

await app.listen({ port: 3000 });

📌 Decorators

🔧 Route Handlers

| Decorator | Description | | --------------- | ---------------------- | | @Get(path) | Handles HTTP GET | | @Post(path) | Handles HTTP POST | | @Put(path) | Handles HTTP PUT | | @Patch(path) | Handles HTTP PATCH | | @Delete(path) | Handles HTTP DELETE | | @All(path) | Handles any method | | @Sse(path) | Handles HTTP GET (SSE) |


📅 Parameter Injection

| Decorator | Injects From | | ---------------- | ------------------- | | @Body() | Request body | | @Headers() | Request headers | | @Parameter() | URL path parameters | | @Query() | Query string | | @Req() | Raw FastifyRequest | | @Rep() | Raw FastifyReply | | @Raw() | { req, res } pair | | @Ctx() | Context data | | @Job() | BullMq job | | @InjectQueue() | BullMq queue |


📜 Schema Validation

@Schema({
  body: {
    type: "object",
    properties: {
      test: { type: "string" },
    },
    required: ["test"],
  },
})
@Post("/validate")
validate(@Body() body: { test: string }) {
  return body;
}

⚙️ Service Lifecycle

Implement OnServiceInit for async setup logic:

import { OnServiceInit } from "@tganzhorn/fastify-modular";

class InitService implements OnServiceInit {
  initialized = false;

  async onServiceInit() {
    await someAsyncSetup();
    this.initialized = true;
  }
}

[!CAUTION] onServiceInit is never awaited so don't except it to be initialized immediately!


⏱️ Background Jobs

class WorkerController {
  @JobScheduler("test-scheduler", { every: 1000 }) // every 1s
  @Worker("test")
  async runJob() {
    // background task logic
  }
}

Requires BullMQ and a Redis connection configured via registerControllers.


📦 API

registerControllers(fastify, options)

Registers all provided controllers and their workers.

Options:

  • controllers: Class[] – list of controllers to register
  • bullMqConnection: { host: string; port: number } – Redis config for BullMQ

Code generation

Usage

fastify-modular create <component> <name>
  • <component>: Type of component to create. Must be either service or controller.
  • <name>: The name of the component (e.g., user, auth).

Example:

fastify-modular create controller user
fastify-modular create service auth

This will generate files like:

src/user/user.controller.ts

src/auth/auth.service.ts

Configuration

You can customize the output folder and folder structure by creating a .fastify-modular.rc.json file in your project root:

{
  "$schema": "https://raw.githubusercontent.com/tganzhorn/fastify-opinionated/refs/heads/main/fastify-modular.rc.schema.json",
  "root": "/src",
  "createSubFolders": true
}

📄 License

MIT – feel free to use, contribute, or fork!