inngest-nestjs
v1.0.2
Published
NestJS integration library for Inngest with decorators and type-safe helpers
Downloads
22
Maintainers
Readme
inngest-nestjs
A modern, type-safe NestJS integration for Inngest. This library provides a seamless way to define, discover, and serve Inngest functions within your NestJS application using decorators and automatic middleware configuration.
Features
- 🎯 Typed Decorators - Create decorators bound to your specific Inngest client for full event name autocompletion.
- � Automatic Discovery - Automatically finds and registers Inngest functions from your providers and controllers.
- � Middleware Integration - Automatically sets up the Inngest serve endpoint using NestJS Middleware.
- 🛠️ Full NestJS Integration - Works with dependency injection, allowing you to use your services inside Inngest functions.
Installation
npm install inngest-nestjsQuick Start
1. Define your Inngest Client and Decorators
Create a file (e.g., src/inngest/client.ts) to define your client with schemas and instantiate the typed decorators.
import { Inngest, EventSchemas } from 'inngest';
import { createInngestDecorators } from 'inngest-nestjs';
// 1. Define your event types
type Events = {
"app/user.created": { data: { userId: string } };
"job/hello.world": { data: { name: string } };
};
// 2. Create the Inngest client
export const inngest = new Inngest({
id: 'my-nestjs-app',
schemas: new EventSchemas().fromRecord<Events>(),
});
// 3. Export typed decorators
export const { Function, Trigger } = createInngestDecorators(inngest);2. Register the Inngest Module
In your AppModule, import InngestModule and provide the client.
import { Module } from '@nestjs/common';
import { InngestModule } from 'inngest-nestjs';
import { inngest } from './inngest/client';
@Module({
imports: [
InngestModule.forRoot({
client: inngest,
path: '/api/inngest', // Optional: defaults to /api/inngest
}),
],
})
export class AppModule {}3. Create your first Inngest Function
Use the decorators you exported in step 1. Note that you can use standard NestJS dependency injection in your class.
import { Injectable } from '@nestjs/common';
import { Function, Trigger } from '../inngest/client';
@Injectable()
export class UserService {
constructor(private readonly logger: MyLoggerService) {}
@Function({ id: 'send-welcome-email' })
@Trigger({ event: 'app/user.created' }) // Full autocompletion here!
async sendWelcomeEmail(ctx: GetFunctionInput<typeof inngest, 'app/user.created'>) {
const { userId } = ctx.event.data;
await ctx.step.run('fetch-user-details', async () => {
// Logic here
this.logger.log(`Processing user ${userId}`);
});
}
}4. Trigger Events
Inject the client elsewhere in your application to send events.
import { Injectable, Inject } from '@nestjs/common';
import { INNGEST_CLIENT } from 'inngest-nestjs';
import { Inngest } from 'inngest';
@Injectable()
export class AuthService {
constructor(@Inject(INNGEST_CLIENT) private client: Inngest) {}
async signup(email: string) {
// ... logic
await this.client.send({
name: 'app/user.created',
data: { userId: '123' },
});
}
}Advanced Configuration
Module Options
| Option | Type | Description |
| --- | --- | --- |
| client | Inngest | Required. Your Inngest client instance. |
| path | string | Optional. The path where the Inngest endpoint will be served. Defaults to /api/inngest. |
Serving via Middleware
The library automatically registers the serve handler from inngest/express as a middleware. This means:
- You don't need to manually create a controller for Inngest.
- You don't need to call
app.register()orapp.use()in yourmain.tsfor Inngest specifically. - It works seamlessly with both Express and Fastify (via the compatibility layer).
Authors
- Carlos Ivan Soto (carlosivansotop)
License
MIT License
