Skip to content

Commit

Permalink
fix: improve typing for forRoot and forRootAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikaple committed May 17, 2021
1 parent 881571d commit 32acbbb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
15 changes: 11 additions & 4 deletions lib/interfaces/typed-config-module-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ClassConstructor } from 'class-transformer';
import { ValidatorOptions } from 'class-validator';

export type ConfigLoader = () =>
| Promise<Record<string, any>>
| Record<string, any>;
export type ConfigLoader = () => Record<string, any>;
export type AsyncConfigLoader = () => Promise<Record<string, any>>;

export interface TypedConfigModuleOptions {
/**
Expand All @@ -12,7 +11,7 @@ export interface TypedConfigModuleOptions {
schema: ClassConstructor<any>;

/**
* Function to load configurations, can be sync or async.
* Function(s) to load configurations, must be synchronous.
*/
load: ConfigLoader | ConfigLoader[];

Expand Down Expand Up @@ -46,3 +45,11 @@ export interface TypedConfigModuleOptions {
*/
validationOptions?: ValidatorOptions;
}

export interface TypedConfigModuleAsyncOptions
extends TypedConfigModuleOptions {
/**
* Function(s) to load configurations, can be synchronous or asynchronous.
*/
load: AsyncConfigLoader | AsyncConfigLoader[];
}
16 changes: 9 additions & 7 deletions lib/typed-config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'class-validator';
import merge from 'lodash.merge';
import {
ConfigLoader,
TypedConfigModuleAsyncOptions,
TypedConfigModuleOptions,
} from './interfaces/typed-config-module-options.interface';
import { forEachDeep } from './utils/for-each-deep.util';
Expand All @@ -17,22 +17,22 @@ import { debug } from './utils/debug.util';

@Module({})
export class TypedConfigModule {
static forRoot(options: TypedConfigModuleOptions): DynamicModule {
public static forRoot(options: TypedConfigModuleOptions): DynamicModule {
const rawConfig = this.getRawConfig(options.load);

return this.getDynamicModule(options, rawConfig);
}

static async forRootAsync(
options: TypedConfigModuleOptions,
public static async forRootAsync(
options: TypedConfigModuleAsyncOptions,
): Promise<DynamicModule> {
const rawConfig = await this.getRawConfigAsync(options.load);

return this.getDynamicModule(options, rawConfig);
}

private static getDynamicModule(
options: TypedConfigModuleOptions,
options: TypedConfigModuleOptions | TypedConfigModuleAsyncOptions,
rawConfig: Record<string, any>,
) {
const {
Expand Down Expand Up @@ -60,7 +60,7 @@ export class TypedConfigModule {
};
}

private static getRawConfig(load: ConfigLoader | ConfigLoader[]) {
private static getRawConfig(load: TypedConfigModuleOptions['load']) {
if (Array.isArray(load)) {
const config = {};
for (const fn of load) {
Expand All @@ -76,7 +76,9 @@ export class TypedConfigModule {
return load();
}

private static async getRawConfigAsync(load: ConfigLoader | ConfigLoader[]) {
private static async getRawConfigAsync(
load: TypedConfigModuleAsyncOptions['load'],
) {
if (Array.isArray(load)) {
const config = {};
for (const fn of load) {
Expand Down

0 comments on commit 32acbbb

Please sign in to comment.