nestjs-provider-util
v1.0.3
Published
Simple provider util
Downloads
30
Readme
Description
Provider util for Nest.
Installation
$ npm i --save nestjs-provider-util
Usage
Register provider
The goal of register provider util its give separation between provider and implementation of this provider
define service interface
//modules/app/interfaces/app-service.interface.ts
export interface AppServiceInterface {
ping(): { message: string };
}
implement service interface
//modules/app/services/app/app.service.ts
import { Injectable } from '@nestjs/common';
import { AppServiceInterface } from '../../interfaces/app-service.interface';
@Injectable()
export class AppService implements AppServiceInterface {
public ping() {
return { message: 'pong' };
}
}
register service as provider
//modules/app/services/app/index.ts
import { registerProvider } from 'nestjs-provider-util';
import { AppService } from '../../interfaces/app-service.interface';
export default registerProvider<AppService>('APP_SERVICE', AppService);
provide service
//modules/app/app.module.ts
import { Module } from '@nestjs/common';
import AppService from './services/app';
@Module({
providers: [AppService.PROVIDER],
exports: [AppService.PROVIDER],
})
export class AppModule {}
inject service
//modules/app/controllers/app.controller.ts
import { Controller, Inject, Get } from '@nestjs/common';
import AppService from '../services/app';
import { AppServiceInterface } from '@modules/app/common/interfaces/app-service.interface';
@Controller({ version: '1' })
export class AppController {
constructor(
@Inject(AppService.TOKEN)
private readonly appService: AppServiceInterface,
) {}
@Get('/ping')
public ping() {
return this.appService.ping();
}
}