@poolsea-nestjs/middleware
v1.0.0
Published
NestJS Middleware Module for Lido Finance projects. Part of [Lido NestJS Modules](https://github.com/lidofinance/lido-nestjs-modules/#readme)
Downloads
3
Maintainers
Readme
Middleware
NestJS Middleware Module for Lido Finance projects. Part of Lido NestJS Modules
Install
yarn add @poolsea-nestjs/middleware
Usage
Basic usage
// Import
import { Module } from '@nestjs/common';
import { MiddlewareModule } from '@poolsea-nestjs/middleware';
import { MyService } from './my.service';
@Module({
imports: [
MiddlewareModule.forFeature({
middlewares: [
(next) => {
console.log(1);
next();
console.log(3);
},
],
}),
],
providers: [MyService],
exports: [MyService],
})
export class MyModule {}
// Usage
import { MiddlewareService } from '@poolsea-nestjs/middleware';
export class MyService {
constructor(private middlewareService: MiddlewareService) {}
async myMethod() {
return await this.middlewareService.go(() => {
console.log(2);
});
}
}
Async usage
// Import
import { Module } from '@nestjs/common';
import { LoggerModule } from '@poolsea-nestjs/logger';
import { MiddlewareModule } from '@poolsea-nestjs/middleware';
import { MyService } from './my.service';
@Module({
imports: [
LoggerModule,
MiddlewareModule.forFeatureAsync({
imports: [LoggerModule],
async useFactory(loggerService: LoggerService) {
return {
middlewares: [
(next) => {
loggerService.log(1);
next();
},
],
};
},
inject: [LoggerService],
}),
],
providers: [MyService],
exports: [MyService],
})
export class MyModule {}
// Usage
import { MiddlewareService } from '@poolsea-nestjs/middleware';
export class MyService {
constructor(
private middlewareService: MiddlewareService,
private loggerService: LoggerService,
) {}
async myMethod() {
return await this.middlewareService.go(() => {
loggerService.log(2);
});
}
}