diff --git a/README.md b/README.md index 8991a18..a6ebdc7 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/__tests__/index.ts b/src/__tests__/index.ts new file mode 100644 index 0000000..2a7463f --- /dev/null +++ b/src/__tests__/index.ts @@ -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); + }); +}); diff --git a/src/index.ts b/src/index.ts index c4e85a9..61df3fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -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_']; + } +}