Skip to content

Commit

Permalink
feat(): add asProvider helper method to config namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Oct 28, 2021
1 parent a3d48e0 commit e76d212
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
4 changes: 4 additions & 0 deletions .npmignore
Expand Up @@ -6,11 +6,15 @@ package-lock.json
tslint.json
tsconfig.json
.prettierrc
.commitlintrc.json
.eslintignore
.eslintrc.js

# github
.github
CONTRIBUTING.md
renovate.json
.release-it.json

# ci
.circleci
1 change: 1 addition & 0 deletions lib/config.constants.ts
Expand Up @@ -9,3 +9,4 @@ export const VALIDATED_ENV_LOADER = 'VALIDATED_ENV_LOADER';
export const PARTIAL_CONFIGURATION_KEY = 'PARTIAL_CONFIGURATION_KEY';
export const PARTIAL_CONFIGURATION_PROPNAME = 'KEY';
export const VALIDATED_ENV_PROPNAME = '_PROCESS_ENV_VALIDATED';
export const AS_PROVIDER_METHOD_KEY = 'asProvider';
47 changes: 31 additions & 16 deletions lib/utils/register-as.util.ts
@@ -1,32 +1,47 @@
import { ConfigModule } from '..';
import {
AS_PROVIDER_METHOD_KEY,
PARTIAL_CONFIGURATION_KEY,
PARTIAL_CONFIGURATION_PROPNAME,
} from '../config.constants';
import { ConfigFactory } from '../interfaces';
import { ConfigObject } from '../types';
import { getConfigToken } from './get-config-token.util';

export type ConfigFactoryKeyHost = { KEY: string };
export interface ConfigFactoryKeyHost<T = unknown> {
KEY: string;
asProvider(): {
imports: [ReturnType<typeof ConfigModule.forFeature>];
useFactory: (config: T) => T;
inject: [string];
};
}

/**
* Registers the configuration object behind a specified token.
*/
export function registerAs<
TConfig extends ConfigObject,
TFactory extends ConfigFactory = ConfigFactory<TConfig>
>(token: string, configFactory: TFactory): TFactory & ConfigFactoryKeyHost {
Object.defineProperty(configFactory, PARTIAL_CONFIGURATION_KEY, {
configurable: false,
enumerable: false,
value: token,
writable: false,
});
Object.defineProperty(configFactory, PARTIAL_CONFIGURATION_PROPNAME, {
configurable: false,
enumerable: false,
value: getConfigToken(token),
writable: false,
});
TFactory extends ConfigFactory = ConfigFactory<TConfig>,
>(
token: string,
configFactory: TFactory,
): TFactory & ConfigFactoryKeyHost<ReturnType<TFactory>> {
const defineProperty = (key: string, value: unknown) => {
Object.defineProperty(configFactory, key, {
configurable: false,
enumerable: false,
value,
writable: false,
});
};

return configFactory as TFactory & ConfigFactoryKeyHost;
defineProperty(PARTIAL_CONFIGURATION_KEY, token);
defineProperty(PARTIAL_CONFIGURATION_PROPNAME, getConfigToken(token));
defineProperty(AS_PROVIDER_METHOD_KEY, () => ({
imports: [ConfigModule.forFeature(configFactory)],
useFactory: (config: unknown) => config,
inject: [getConfigToken(token)],
}));
return configFactory as TFactory & ConfigFactoryKeyHost<ReturnType<TFactory>>;
}
2 changes: 1 addition & 1 deletion tests/src/app.module.ts
@@ -1,5 +1,5 @@
import Joi from 'joi';
import { DynamicModule, Inject, Module, Optional } from '@nestjs/common';
import Joi from 'joi';
import { join } from 'path';
import { ConfigType } from '../../lib';
import { ConfigModule } from '../../lib/config.module';
Expand Down

0 comments on commit e76d212

Please sign in to comment.