当前位置:网站首页>Nestjs environment variable configuration to solve the problem of how to inject services into interceptors
Nestjs environment variable configuration to solve the problem of how to inject services into interceptors
2022-06-27 03:27:00 【acgCode】
In fact, this is also a NestJS Process record of environment variable configuration .
During the development process, I have generated such a requirement , I need to start different environments with different script commands .mock Environment , I want to provide virtual data for the front end ;staging Environment , I want to be BFF Provide the front end with data transmitted from the background .
Before returning data , I need to format the data uniformly . So I made an interceptor RespTransformInterceptor.
import {
CallHandler,
ExecutionContext,
NestInterceptor,
} from '@nestjs/common';
import {
Observable } from 'rxjs';
import {
map } from 'rxjs/operators';
@Injectable()
export class RespTransformInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
map((data) => {
return {
code: 0,
result: data,
};
}),
);
}
}
Now I want to process the data in the interceptor , Make sure that no matter what the environment , The data structures I return are unified . Then I have to distinguish the current operating environment .
First , According to official website To configure Custom configuration files .
// configuration.ts
// All with ':mock' At the end of the package.json command , The environment is mock; For the other staging
export type ENV_TYPE = 'mock' | 'staging';
export default () => {
const commandSlices = process.env.npm_lifecycle_event.split(':');
const envType = commandSlices[commandSlices.length - 1] as ENV_TYPE;
return {
node_env: envType === 'mock' ? 'mock' : 'staging',
};
};
then , We import it into AppModule in .
// app.module.ts
import {
Module } from '@nestjs/common';
import {
AppController } from './app.controller';
import {
AppService } from './app.service';
import {
LayoutModule } from './common/layout/layout.module';
import {
ConfigModule, ConfigService } from '@nestjs/config';
import {
APP_INTERCEPTOR } from '@nestjs/core';
import {
RespTransformInterceptor } from './common/interceptor/resp-transform.interceptor';
import configuration from './config/configuration';
@Module({
imports: [
LayoutModule,
ConfigModule.forRoot({
load: [configuration], // The configuration file
isGlobal: true, // Provide to the global
}),
],
controllers: [AppController],
providers: [
AppService,
{
provide: APP_INTERCEPTOR,
useClass: RespTransformInterceptor,
},
// Another way to provide to the global
// ConfigService
],
})
export class AppModule {
}
Last , We need to inject services into interceptors :
// resp-transform.interceptor.ts
constructor(private readonly configService: ConfigService) {
}
We can provide services through get API Get the current environment .
// resp-transform.interceptor.ts
this.configService.get<string>('node_env') // log:mock | staging
summary , We can inject the services that need to be injected provide Into the module , At the same time, we should also inject the services that need to be injected 、 Interceptor 、 Guard, etc provide Into the module . In this way, we can obtain various services through injection .
边栏推荐
- 我是怎样简化开源系统中的接口的开发的?
- 对数器
- Laravel 的 ORM 缓存包
- Paddlepaddle 21 is implemented based on dropout with 4 lines of code droplock
- 语义化版本 2.0.0
- 2021:Graphhopper: Multi-Hop Scene Graph Reasoning for Visual Question Answering
- 剑指Offer || :栈与队列(简单)
- fplan-电源规划
- 2021:AdaVQA: Overcoming Language Priors with Adapted Margin Cosine Loss∗自适应的边缘余弦损失解决语言先验
- JWT certification process and use cases
猜你喜欢

Yiwen teaches you Kali information collection

QIngScan使用

PAT甲级 1020 Tree Traversals

学习太极创客 — MQTT(六)ESP8266 发布 MQTT 消息

fplan-Powerplan实例

Topolvm: kubernetes local persistence scheme based on LVM, capacity aware, dynamically create PV, and easily use local disk

PAT甲级 1024 Palindromic Number

发现一款 JSON 可视化工具神器,太爱了!

resnet152 辣椒病虫害图像识别1.0

Calculation of average wind direction and speed (unit vector method)
随机推荐
Mmdetection valueerror: need at least one array to concatenate solution
【数组】剑指 Offer II 012. 左右两边子数组的和相等 | 剑指 Offer II 013. 二维子矩阵的和
记录unity 自带读取excel的方法和遇到的一些坑的解决办法
2021:Beyond Question-Based Biases:Assessing Multimodal Shortcut Learning in Visual Question Answeri
Getting started with bluecms code auditing
JMeter distributed pressure measurement
Pat class a 1024 palindromic number
我是怎样简化开源系统中的接口的开发的?
学习太极创客 — MQTT(六)ESP8266 发布 MQTT 消息
Stack overflow vulnerability
PAT甲级 1026 Table Tennis
栈溢出漏洞
Pat grade a 1019 general palindromic number
Sword finger offer 𞓜: stack and queue (simple)
Logarithm
PAT甲级 1021 Deepest Root
学习太极创客 — MQTT 第二章(一)QoS 服务质量等级
2016Analyzing the Behavior of Visual Question Answering Models
Method of decoding iPhone certificate file
Pat grade a 1021 deep root