Skip to content
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ Default: `config.getAll(onlyParsed=false): {[key: string]: string}`

Return all defined keys and values as a key value object (from process.env and .env merged), or only from .env if onlyParsed is set to true.

#### getAllPublic
#### getPublic

Default: `config.getAllPublic(onlyParsed=false): {[key: string]: string}`
Default: `config.getPublic(onlyParsed=false): {[key: string]: string}`

Return all defined public (envs. vars that the prefix `PUBLIC_`) keys and values as a key value object (from process.env and .env merged), or only from .env if onlyParsed is set to true.

Expand Down
78 changes: 78 additions & 0 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const path = require('path');

process.env.ENV_CONFIG_PATH = path.join(__dirname, '../../fixtures/.env');

import { getAll, get, has, getPublic } from '../index';

describe('getAll', () => {
it('confirm that all keys are returned', () => {
const all = getAll();
expect(Object.keys(all)).toHaveLength(19);
expect(all.EMPTY).toBe(null);
});
});

describe('getPublic', () => {
it('confirm that all safe keys are returned', () => {
const all = getPublic();
expect(Object.keys(all)).toHaveLength(1);
expect(all.PUBLIC_APP_NAME_NOT_IMPORTANT).toBe('Bedrock Config');
});
});

describe('get', () => {
it('get value', () => {
expect(get('BASIC')).toBe('basic');
expect(get('NUMBER')).toBe('1');
});

it('get empty', () => {
expect(get('EMPTY')).toBe(null);
});

it('get empty with quotes', () => {
expect(get('EMPTY_WITH_QUOTES')).toBe(null);
});

it('not defined key', () => {
expect(() => get('BAD_VALUE')).toThrow(
'Configuration variable "BAD_VALUE" is not exposed as environment variable nor was a default provided in `.env`'
);
});

it('get value as number', () => {
expect(get('NUMBER', 'integer')).toBe(1);
});

it('get value as float', () => {
expect(get('NUMBER', 'float')).toBe(1);
});

it('get value as boolean', () => {
expect(get('NUMBER', 'boolean')).toBe(true);
});

it('get value as date', () => {
expect(get('DATE', 'date').valueOf()).toBe(1548794502645);
});

it('get value as json', () => {
expect(get('RETAIN_INNER_QUOTES', 'json')).toEqual({
foo: 'bar',
});
});
});

describe('has', () => {
it('with value', () => {
expect(has('BASIC')).toBe(true);
});

it('without value', () => {
expect(has('EMPTY')).toBe(false);
});

it('not defined key', () => {
expect(has('BAD_VALUE')).toBe(false);
});
});
28 changes: 25 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,24 @@ export function getAll(onlyParsed = false): { [key: string]: string } {
return result;
}

export function getAllPublic(onlyParsed = false): { [key: string]: string } {
export function getPublic(onlyParsed = false): { [key: string]: string } {
const result: { [key: string]: string } = {};

const publicPrefixes = resolvePublicPrefixes();

const keys = parsed.keys();

for (const key of keys) {
if (!key.startsWith('PUBLIC_')) continue;
const isPublic = publicPrefixes.some((prefix) => {
return key.startsWith(prefix);
});

if (isPublic) {
continue;
}

const value = onlyParsed ? parsed.get(key) : process.env[key] || parsed.get(key);

result[key] = value;
}
return result;
Expand All @@ -103,6 +115,16 @@ export function has(variable: string): boolean {
export default {
get,
getAll,
getAllPublic,
getPublic,
has,
};

function resolvePublicPrefixes() {
const { ENV_PUBLIC_PREFIXES } = process.env;

if (ENV_PUBLIC_PREFIXES) {
return ENV_PUBLIC_PREFIXES.split(',');
} else {
return ['APP_', 'PUBLIC_'];
}
}