Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Standard error
# DevKit

![release workflow](https://github.com/Nan0416/dev-kit/actions/workflows/release.yml/badge.svg)

Expand Down
27 changes: 27 additions & 0 deletions src/getenv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export function getenv<T extends string>(name: string, enums: ReadonlyArray<T>, default_?: T): T;
export function getenv(name: string, default_?: string): string;

export function getenv<T extends string>(name: string, v2?: ReadonlyArray<T> | string, v3?: T): T {
const value = process.env[name];
if (typeof value === 'string') {
if (v2 === undefined || typeof v2 === 'string') {
return value as T;
} else if (Array.isArray(v2)) {
if (v2.includes(value)) {
return value as T;
} else {
throw new Error(`Environment variable ${name}'s value ${value} is not a valid value.`);
}
} else {
throw new Error('Invalid argument.');
}
} else {
if (typeof v2 === 'string') {
return v2 as T;
} else if (typeof v3 === 'string') {
return v3 as T;
} else {
throw new Error(`Environment variable ${name} doesn't exist.`);
}
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './metrics';
export * from './helper-functions';
export * from './gc';
export * from './http-client';
export * from './getenv';
54 changes: 54 additions & 0 deletions tests/getenv.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { getenv } from '../src';

describe('getenv', () => {
const ORIGINAL_ENV = process.env;

beforeEach(() => {
process.env = { ...ORIGINAL_ENV }; // clone env
});

afterAll(() => {
process.env = ORIGINAL_ENV; // restore
});

test('returns existing string env var', () => {
process.env.FOO = 'bar';
expect(getenv('FOO')).toBe('bar');
});

test('returns existing env var when default provided', () => {
process.env.FOO = 'bar';
expect(getenv('FOO', 'default')).toBe('bar');
});

test('returns default when env var missing', () => {
expect(getenv('FOO', 'default')).toBe('default');
});

test('throws error when missing and no default provided', () => {
expect(() => getenv('FOO')).toThrow("Environment variable FOO doesn't exist.");
});

test('returns value if included in allowed enum list', () => {
process.env.MODE = 'prod';
const allowed = ['dev', 'prod'] as const;
expect(getenv('MODE', allowed)).toBe('prod');
});

test('throws error if value not in allowed enum list', () => {
process.env.MODE = 'staging';
const allowed = ['dev', 'prod'] as const;
expect(() => getenv('MODE', allowed)).toThrow("Environment variable MODE's value staging is not a valid value.");
});

test('returns default enum if env missing', () => {
const allowed = ['dev', 'prod'] as const;
expect(getenv('MODE', allowed, 'prod')).toBe('prod');
});

test('throws if invalid overload argument type encountered', () => {
process.env.FOO = 'bar';
// @ts-expect-error invalid argument type
expect(() => getenv('FOO', 123)).toThrow('Invalid argument.');
});
});