diff --git a/README.md b/README.md index f3836b6..7322c12 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,31 @@ import { HttpModule } from "@nodeflip/nest-axios-http"; }) export class AppModule {} +``` + +### Inject other modules and use it + +```typescript + +import { Module } from "@nestjs/common"; +import { HttpModule } from "@nodeflip/nest-axios-http"; + +@Module({ + imports: [ + HttpModule.forFeatureAsync({ + logger: customLogger, // Custom logger instance + serviceName: 'MyService', // Service name for logging + inject: [ConfigService], + useFactory: (configService: ConfigService) => { + return { + baseURL: configService.get('API_BASE_URL'), + enableLogging: configService.get('ENABLE_LOGGING'), + } + } + }), + ], +}) + ``` ## License diff --git a/package.json b/package.json index 4b1ca38..5fb5a18 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nodeflip/nest-axios-http", - "version": "1.0.1", + "version": "1.0.3", "description": "A NestJS module for simplified HTTP requests using Axios with dynamic configuration, logging, and interceptor support.", "main": "dist/index.js", "scripts": { diff --git a/src/http/http.module.ts b/src/http/http.module.ts index 627f392..485d7ce 100644 --- a/src/http/http.module.ts +++ b/src/http/http.module.ts @@ -1,4 +1,10 @@ -import { DynamicModule, Module, Provider } from "@nestjs/common"; +import { + DynamicModule, + FactoryProvider, + Module, + ModuleMetadata, + Provider, +} from "@nestjs/common"; import { HttpModule as NestHttpModule } from "@nestjs/axios"; import { HttpService } from "./http.service"; @@ -83,4 +89,38 @@ export class HttpModule { provider, }; } + + static forFeatureAsync(options: { + serviceName: string; + imports?: ModuleMetadata["imports"]; + inject?: FactoryProvider["inject"]; + useFactory: ( + ...args: any[] + ) => + | Promise> + | Omit; + }): DynamicModule { + const { + serviceName = "HttpService", + imports, + inject, + useFactory, + } = options; + + const provider = { + provide: serviceName, + useFactory: async (...args: any[]) => { + const config = await useFactory(...args); + return new HttpService(config); + }, + inject: inject || [], + }; + + return { + module: HttpModule, + imports: imports || [], + providers: [provider], + exports: [provider], + }; + } } diff --git a/src/test/HttpModule.spec.ts b/src/test/HttpModule.spec.ts index 4779ea8..4749e9a 100644 --- a/src/test/HttpModule.spec.ts +++ b/src/test/HttpModule.spec.ts @@ -1,4 +1,5 @@ import { Test, TestingModule } from "@nestjs/testing"; +import { DynamicModule, FactoryProvider, ModuleMetadata } from "@nestjs/common"; import { HttpModule } from "../http/http.module"; // Adjust the import path as necessary import { HttpService } from "../http/http.service"; @@ -62,4 +63,23 @@ describe("HttpModule", () => { expect(serviceOne).toBeInstanceOf(HttpService); expect(serviceTwo).toBeInstanceOf(HttpService); }); + + it("should handle asynchronous dynamic configuration", async () => { + const asyncConfig = { + imports: [] as ModuleMetadata["imports"], + inject: [] as FactoryProvider["inject"], + useFactory: async () => ({ + config: { baseURL: "http://async-config.com" }, + }), + serviceName: "AsyncService", + }; + + const asyncModule = HttpModule.forFeatureAsync(asyncConfig); + const featureModule = await Test.createTestingModule({ + imports: [asyncModule], + }).compile(); + const asyncHttpService = featureModule.get("AsyncService"); + + expect(asyncHttpService).toBeInstanceOf(HttpService); + }); });