Dependency Injection

Make classes injectable and mark pipeline classes

@Injectable makes a class available to the dependency injection container. The other decorators here mark classes as pipeline components, so NitroStack can create them with their dependencies.

@Injectable

Marks a class for dependency injection.

import { Injectable } from '@nitrostack/core';

@Injectable()
@injectable(deps=[ServiceA, ServiceB])

Example:

@Injectable()
export class UserService {
  constructor(
    private db: DatabaseService,
    private cache: CacheService
  ) {}

  async findById(id: string): Promise<User | null> {
    return this.db.query('SELECT * FROM users WHERE id = $1', [id]);
  }
}

@Middleware

Marks a class as middleware.

import { Middleware, MiddlewareInterface } from '@nitrostack/core';

@Middleware()
export class LoggingMiddleware implements MiddlewareInterface {
  async use(context: ExecutionContext, next: () => Promise<any>): Promise<any> {
    // Implementation
  }
}

@Interceptor

Marks a class as an interceptor.

import { Interceptor, InterceptorInterface } from '@nitrostack/core';

@Interceptor()
export class TransformInterceptor implements InterceptorInterface {
  async intercept(context: ExecutionContext, next: () => Promise<any>): Promise<any> {
    // Implementation
  }
}

@Pipe

Marks a class as a pipe for argument transformation and validation.

import { Pipe, PipeInterface, ArgumentMetadata } from '@nitrostack/core';

@Pipe()
export class ValidationPipe implements PipeInterface {
  transform(value: unknown, metadata: ArgumentMetadata): unknown {
    // Validation or transformation logic
    return value;
  }
}

@ExceptionFilter

Marks a class as an exception filter.

import { ExceptionFilter, ExceptionFilterInterface } from '@nitrostack/core';

@ExceptionFilter()
export class HttpExceptionFilter implements ExceptionFilterInterface {
  catch(exception: unknown, context: ExecutionContext): any {
    // Implementation
  }
}

On this page