example-nested-module.ts
import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ExampleCommunicationService } from '@example-integration/modules/xample-communication/providers/services';
import { ExampleHttpConfiguration } from '@example-integration/utils/config';
@Module({
imports: [HttpModule.registerAsync({
imports: [ConfigModule],
useClass: ExampleHttpConfiguration
}),
ConfigModule
],
providers: [ExampleCommunicationService],
exports: [ExampleCommunicationService]
})
export class ExampleCommunicationModule {}
example-http-service-configuration.ts
import { HttpModuleOptions, HttpModuleOptionsFactory } from "@nestjs/axios";
import { Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { ExampleConfiguration } from "@example-integration/data";
@Injectable()
export class ExampleHttpConfiguration implements HttpModuleOptionsFactory {
private readonly apiKey: string;
private readonly baseUrl: string;
private readonly contentType: string = 'application/xml';
constructor(private readonly configService: ConfigService) {
this.apiKey = this.configService.getOrThrow<string>(ExampleConfiguration.ApiKey);
this.baseUrl = this.configService.getOrThrow<string>(ExampleConfiguration.Url);
}
createHttpOptions(): HttpModuleOptions|Promise<HttpModuleOptions> {
return {
baseURL: `https://${this.baseUrl}`,
headers: {
"Content-Type": this.contentType,
"X-API-Key": this.apiKey,
"Accept": this.contentType,
"Host": this.baseUrl
}
}
}
};
example-communication.service.ts
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { firstValueFrom } from 'rxjs';
import { ExampleXMLDocument, ExampleResponse } from '@example-integration/data';
@Injectable()
export class ExampleCommunicationService {
private readonly resourcePath: string = '/example';
constructor(private readonly exampleHttpService: HttpService) {}
public async sendXml(xmlRequestData: ExampleXMLDocument): Promise<ExampleResponse> {
return await firstValueFrom(this.exampleHttpService.post(this.resourcePath, xmlRequestData));
}
}