Skip to content

Commit

Permalink
feat(config)!: env and envPrefix as function export
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape committed Jul 28, 2024
1 parent 6b85120 commit cd3b70a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ export enum DotEnv {
}

const log = getLog({ brand: '@dot', name: '\u001b[1D/config' });
const { DEPLOY_ENV, NODE_ENV } = process.env;
const deployEnv = DEPLOY_ENV || NODE_ENV;

export const env = (deployEnv === 'production' ? 'prod' : deployEnv) as DotEnv;
export const envPrefix = `${env}-`;
export const env = () => {
const { DEPLOY_ENV, NODE_ENV } = process.env;
const deployEnv = DEPLOY_ENV || NODE_ENV;
return (deployEnv === 'production' ? 'prod' : deployEnv) as DotEnv;
};

export const envPrefix = () => `${env()}-`;

export const init = <TDefaults, TSecrets, TSsm>({
defaultConfig,
Expand All @@ -47,14 +50,17 @@ export const init = <TDefaults, TSecrets, TSsm>({
}

const envResult = typeof config[key] !== 'undefined' ? config[key] : void 0;
const defaultValues: { [key: string]: unknown } = { ...(defaults as any) };
const defaultResult = typeof defaultValues[key] !== 'undefined' ? defaultValues[key] : void 0;

log.debug('envResult for:', key, '→', envResult);
log.debug('defaultResult for:', key, '→', defaultResult);

const result =
envResult ||
(await getSsmValue((ssmKeys as KeyConfig)[key])) ||
(await getSecretValue((secretKeys as KeyConfig)[key])) ||
(defaults as KeyConfig)[key];
defaultResult;

if (result === void 0)
throw new RangeError(
Expand Down
16 changes: 16 additions & 0 deletions packages/config/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ test('init', async (t) => {
t.truthy(result.put);
});

test('env export', async (t) => {
process.env.DEPLOY_ENV = 'BATCAVE';

const { env } = await import('../src');

t.is(env(), process.env.DEPLOY_ENV as any);
});

test('envPrefix export', async (t) => {
process.env.DEPLOY_ENV = 'BATCAVE';

const { envPrefix } = await import('../src');

t.is(envPrefix(), `${process.env.DEPLOY_ENV}-`);
});

test('get → defaults', async (t) => {
const defaultConfig = {
BATMAN_ADDRESS: 'BATCAVE'
Expand Down

0 comments on commit cd3b70a

Please sign in to comment.