Inject any value directly into dto classes using decorators.
npm i nestjs-dto-injector
yarn add nestjs-dto-injector
// test.injector.ts
import { Injectable } from '@nestjs/common';
import { Injector } from 'nestjs-dto-injector';
@Injectable()
export class TestInjector implements Injector<string> {
async apply(dto: any): Promise<string> {
return 'new value';
}
}
// test.dto.ts
import { TestInjector } from './test.injector';
import { ApplyInjection } from 'nestjs-dto-injector';
export class TestDto {
@ApplyInjection(TestInjector)
value: string;
}
// test.controller.ts
import { TestDto } from './test.dto';
import { ApplyInjectionPipe } from 'nestjs-dto-injector';
import { Body, Controller, Post, UsePipes } from '@nestjs/common';
@Controller()
export class TestController {
@Post()
@UsePipes(ApplyInjectionPipe)
async post(@Body() dto: TestDto) {
console.log(dto.value); // new value
}
}
// test.module.ts
import { TestInjector } from 'test.injector';
import { TestController } from './test.controller';
@Module({
controllers: [TestController],
providers: [TestInjector],
})
export class TestModule {}
// test.dto.ts
import { TestInjector } from './test.injector';
import { ApplyInjection } from 'nestjs-dto-injector';
export class TestDto {
@ApplyInjection(TestInjector)
value: string;
}
// test-nested.dto.ts
import { TestDto } from 'test.dto';
import { ApplyInjectionNested } from 'nestjs-dto-injector';
export class TestNestedDto {
@ApplyInjectionNested()
testDto: TestDto;
}