Мы создаем моно-репо микросервисов и хотим иметь несколько общих библиотек, которые мы импортируем в различные сервисы.
Сейчас я пытаюсь создать общий модуль, у которого будет поставщик, которому нужен доступ к запросу. Вот пример:
import { Injectable, Scope, Inject } from '@nestjs/common'
import { REQUEST } from '@nestjs/core'
import { Request } from 'express'
import { APILogger } from '@freebird/logger'
import { APIGatewayProxyEvent, Context } from 'aws-lambda'
export interface IAPIGatewayRequest extends Request {
apiGateway?: {
event?: APIGatewayProxyEvent
context?: Context
}
}
@Injectable({ scope: Scope.REQUEST })
export class RequestLogger extends APILogger {
constructor(@Inject(REQUEST) request: IAPIGatewayRequest) {
if (!request.apiGateway || !request.apiGateway.event || !request.apiGateway.context) {
throw new Error(
'You are trying to use the API Gateway logger without having used the aws-serverless-express middleware',
)
}
super(request.apiGateway.event, request.apiGateway.context)
}
}
Я пытался связать это как модуль следующим образом:
import { Module } from '@nestjs/common'
import { RequestLogger } from './logger'
@Module({
providers: [RequestLogger],
exports: [RequestLogger],
})
export class LambdaModule {}
А затем импортируйте его в основной сервисный модуль следующим образом:
import { Module } from '@nestjs/common'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { LambdaModule } from '@freebird/nest-lambda'
@Module({
imports: [LambdaModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Однако, когда я делаю это, я получаю сообщение об ошибке:
Nest не может разрешить зависимости RequestLogger (?). Пожалуйста, сделай
убедитесь, что аргумент в index [0] доступен в AppModule
контекст.
Но когда я подключаю провайдера RequestLogger к сервисному модулю и включаю его вот так, я не получаю ошибок:
import { Module } from '@nestjs/common'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { RequestLogger } from './logger'
@Module({
controllers: [AppController],
providers: [AppService, RequestLogger],
})
export class AppModule {}