@albertoielpo/ielpify
v0.3.0
Published
A simplified way to build Fastify applications using decorators, inspired by NestJS
Maintainers
Readme
@albertoielpo/ielpify
TypeScript decorators for Fastify: controllers, routing, scheduling, and simple dependency injection.
Installation
npm install @albertoielpo/ielpify
npm install reflect-metadata fastify # peer dependenciesDecorators
Routing
@Controller(prefix?)- Define a controller with optional route prefix@Get(path?),@Post(path?),@Put(path?),@Delete(path?),@Patch(path?)- HTTP method decorators
Dependency Injection
@Injectable()- Mark a class as a singleton service@Inject(ServiceClass)- Inject a singleton dependency into a constructor parameter
Scheduling
@Timeout(options)- Schedule a method to run after a delay, optionally repeating
API Reference
Routing functions
registerController(fastify, ControllerClass)
Registers a controller with a Fastify instance. Creates the controller instance (with DI) and sets up all routes.
registerController(fastify, HomeController);getControllerInstances()
Returns all registered controller instances. Useful for applying lifecycle operations such as startTimeouts.
const instances = getControllerInstances();Dependency Injection functions
resolveService(ServiceClass)
Resolves a singleton service from the DI container. Creates the instance on first call, then returns the same instance on subsequent calls. Throws if the class is not decorated with @Injectable().
const service = resolveService(MyService);registerService(ServiceClass)
Manually registers a service in the DI container (alias for resolveService that discards the return value).
registerService(MyService);createWithInjection(TargetClass)
Creates an instance of a class with all @Inject()-decorated constructor parameters resolved automatically.
const instance = createWithInjection(MyClass);isInjectable(TargetClass)
Returns true if the class is decorated with @Injectable().
if (isInjectable(MyService)) { ... }getContainerInstances()
Returns all service instances currently stored in the DI container. Useful for applying lifecycle operations such as startTimeouts.
const instances = getContainerInstances();Scheduling functions
startTimeouts(instance)
Starts all @Timeout-decorated methods on a given instance. Call this after all controllers and services are registered.
[...getControllerInstances(), ...getContainerInstances()].forEach(startTimeouts);TimeoutOptions
| Property | Type | Default | Description |
|----------|----------|---------|--------------------------------------------------|
| ms | number | — | Delay in milliseconds between each execution |
| times | number | 1 | Number of executions. Use -1 for infinite loop |
Example
import { Injectable, Injectable, Inject, Timeout } from "@albertoielpo/ielpify";
@Injectable()
class MyService {
@Timeout({ ms: 5000, times: 3 })
poll() {
console.log("polling...");
}
}
@Controller("hello")
class HelloController {
constructor(
@Inject(MyService) private myService: MyService
) {}
@Get("world")
helloWorld(req: FastifyRequest, res: FastifyReply) {
return res.send({ message: "Hello, world!" });
}
}
const fastify = Fastify();
registerController(fastify, HelloController);
[...getControllerInstances(), ...getContainerInstances()].forEach(startTimeouts);
fastify.listen({ port: 3000 });See the example/ folder for a complete working demo.
cd example
npm install
npm run devDevelopment
To test changes locally without publishing:
npm run build
mkdir -p example/node_modules/@albertoielpo/ielpify
cp -R dist example/node_modules/@albertoielpo/ielpify/
cp package.json example/node_modules/@albertoielpo/ielpify/License
MIT
