Skip to content

Commit e66d72f

Browse files
feature(common) add async support (HttpModule) and extraProviders (cache/http)
1 parent c6ec887 commit e66d72f

6 files changed

Lines changed: 91 additions & 8 deletions

File tree

packages/common/cache/cache.module.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ export class CacheModule {
2424
return {
2525
module: CacheModule,
2626
imports: options.imports,
27-
providers: this.createAsyncProviders(options),
27+
providers: [
28+
...this.createAsyncProviders(options),
29+
...(options.extraProviders || []),
30+
],
2831
};
2932
}
3033

@@ -55,7 +58,8 @@ export class CacheModule {
5558
}
5659
return {
5760
provide: CACHE_MODULE_OPTIONS,
58-
useFactory: async (optionsFactory: CacheOptionsFactory) => optionsFactory.createCacheOptions(),
61+
useFactory: async (optionsFactory: CacheOptionsFactory) =>
62+
optionsFactory.createCacheOptions(),
5963
inject: [options.useExisting || options.useClass],
6064
};
6165
}

packages/common/cache/interfaces/cache-module.interface.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ModuleMetadata, Type } from '../../interfaces';
1+
import { ModuleMetadata, Provider, Type } from '../../interfaces';
22
import { CacheManagerOptions } from './cache-manager.interface';
33

44
export interface CacheModuleOptions extends CacheManagerOptions {
@@ -14,7 +14,8 @@ export interface CacheModuleAsyncOptions
1414
useExisting?: Type<CacheOptionsFactory>;
1515
useClass?: Type<CacheOptionsFactory>;
1616
useFactory?: (
17-
...args: any[],
17+
...args: any[]
1818
) => Promise<CacheModuleOptions> | CacheModuleOptions;
1919
inject?: any[];
20+
extraProviders?: Provider[];
2021
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const AXIOS_INSTANCE_TOKEN = 'AXIOS_INSTANCE_TOKEN';
22
export const HTTP_MODULE_ID = 'HTTP_MODULE_ID';
3+
export const HTTP_MODULE_OPTIONS = 'HTTP_MODULE_OPTIONS';

packages/common/http/http.module.ts

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import Axios, { AxiosRequestConfig } from 'axios';
1+
import Axios from 'axios';
22
import { Module } from '../decorators/modules/module.decorator';
3-
import { DynamicModule } from '../interfaces';
3+
import { DynamicModule, Provider } from '../interfaces';
44
import { randomStringGenerator } from '../utils/random-string-generator.util';
5-
import { AXIOS_INSTANCE_TOKEN, HTTP_MODULE_ID } from './http.constants';
5+
import {
6+
AXIOS_INSTANCE_TOKEN,
7+
HTTP_MODULE_ID,
8+
HTTP_MODULE_OPTIONS,
9+
} from './http.constants';
610
import { HttpService } from './http.service';
11+
import {
12+
HttpModuleAsyncOptions,
13+
HttpModuleOptions,
14+
HttpModuleOptionsFactory,
15+
} from './interfaces';
716

817
@Module({
918
providers: [
@@ -16,7 +25,7 @@ import { HttpService } from './http.service';
1625
exports: [HttpService],
1726
})
1827
export class HttpModule {
19-
static register(config: AxiosRequestConfig): DynamicModule {
28+
static register(config: HttpModuleOptions): DynamicModule {
2029
return {
2130
module: HttpModule,
2231
providers: [
@@ -31,4 +40,52 @@ export class HttpModule {
3140
],
3241
};
3342
}
43+
44+
static registerAsync(options: HttpModuleAsyncOptions): DynamicModule {
45+
return {
46+
module: HttpModule,
47+
imports: options.imports,
48+
providers: [
49+
...this.createAsyncProviders(options),
50+
{
51+
provide: HTTP_MODULE_ID,
52+
useValue: randomStringGenerator(),
53+
},
54+
...(options.extraProviders || []),
55+
],
56+
};
57+
}
58+
59+
private static createAsyncProviders(
60+
options: HttpModuleAsyncOptions,
61+
): Provider[] {
62+
if (options.useExisting || options.useFactory) {
63+
return [this.createAsyncOptionsProvider(options)];
64+
}
65+
return [
66+
this.createAsyncOptionsProvider(options),
67+
{
68+
provide: options.useClass,
69+
useClass: options.useClass,
70+
},
71+
];
72+
}
73+
74+
private static createAsyncOptionsProvider(
75+
options: HttpModuleAsyncOptions,
76+
): Provider {
77+
if (options.useFactory) {
78+
return {
79+
provide: HTTP_MODULE_OPTIONS,
80+
useFactory: options.useFactory,
81+
inject: options.inject || [],
82+
};
83+
}
84+
return {
85+
provide: HTTP_MODULE_OPTIONS,
86+
useFactory: async (optionsFactory: HttpModuleOptionsFactory) =>
87+
optionsFactory.createHttpOptions(),
88+
inject: [options.useExisting || options.useClass],
89+
};
90+
}
3491
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { AxiosRequestConfig } from 'axios';
2+
import { ModuleMetadata, Provider, Type } from '../../interfaces';
3+
4+
export interface HttpModuleOptions extends AxiosRequestConfig {}
5+
6+
export interface HttpModuleOptionsFactory {
7+
createHttpOptions(): Promise<HttpModuleOptions> | HttpModuleOptions;
8+
}
9+
10+
export interface HttpModuleAsyncOptions
11+
extends Pick<ModuleMetadata, 'imports'> {
12+
useExisting?: Type<HttpModuleOptions>;
13+
useClass?: Type<HttpModuleOptions>;
14+
useFactory?: (
15+
...args: any[]
16+
) => Promise<HttpModuleOptions> | HttpModuleOptions;
17+
inject?: any[];
18+
extraProviders?: Provider[];
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './http-module.interface';

0 commit comments

Comments
 (0)