Skip to content

chore: added support for forFeatureAsync #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
42 changes: 41 additions & 1 deletion src/http/http.module.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -83,4 +89,38 @@ export class HttpModule {
provider,
};
}

static forFeatureAsync(options: {
serviceName: string;
imports?: ModuleMetadata["imports"];
inject?: FactoryProvider["inject"];
useFactory: (
...args: any[]
) =>
| Promise<Omit<IHttpModuleOptions, "serviceName">>
| Omit<IHttpModuleOptions, "serviceName">;
}): 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],
};
}
}
20 changes: 20 additions & 0 deletions src/test/HttpModule.spec.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<HttpService>("AsyncService");

expect(asyncHttpService).toBeInstanceOf(HttpService);
});
});