opticedge-cloud-utils 1.1.23 → 1.1.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/env.d.ts +1 -1
- package/dist/env.js +7 -7
- package/package.json +1 -1
- package/src/env.ts +9 -7
- package/tests/env.test.ts +59 -12
package/dist/env.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function
|
|
1
|
+
export declare function requireEnv(name: string, defaultValue?: string): string;
|
package/dist/env.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.
|
|
5
|
-
function
|
|
3
|
+
exports.requireEnv = requireEnv;
|
|
4
|
+
function requireEnv(name, defaultValue) {
|
|
6
5
|
const value = process.env[name];
|
|
7
|
-
if (
|
|
8
|
-
if (
|
|
9
|
-
return
|
|
10
|
-
|
|
6
|
+
if (value === undefined || value === '') {
|
|
7
|
+
if (defaultValue !== undefined) {
|
|
8
|
+
return defaultValue;
|
|
9
|
+
}
|
|
10
|
+
throw new Error(`${name} env var must be set`);
|
|
11
11
|
}
|
|
12
12
|
return value;
|
|
13
13
|
}
|
package/package.json
CHANGED
package/src/env.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export function getEnv(name: string, fallback?: any): any {
|
|
1
|
+
export function requireEnv(name: string, defaultValue?: string): string {
|
|
4
2
|
const value = process.env[name]
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
|
|
4
|
+
if (value === undefined || value === '') {
|
|
5
|
+
if (defaultValue !== undefined) {
|
|
6
|
+
return defaultValue
|
|
7
|
+
}
|
|
8
|
+
throw new Error(`${name} env var must be set`)
|
|
8
9
|
}
|
|
10
|
+
|
|
9
11
|
return value
|
|
10
|
-
}
|
|
12
|
+
}
|
package/tests/env.test.ts
CHANGED
|
@@ -1,18 +1,65 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { requireEnv } from '../src/env'
|
|
2
2
|
|
|
3
|
-
describe('
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
describe('requireEnv', () => {
|
|
4
|
+
const ORIGINAL_ENV = process.env
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
jest.resetModules()
|
|
8
|
+
process.env = { ...ORIGINAL_ENV }
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
afterAll(() => {
|
|
12
|
+
process.env = ORIGINAL_ENV
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
// ============================================================
|
|
16
|
+
// Success cases
|
|
17
|
+
// ============================================================
|
|
18
|
+
|
|
19
|
+
it('returns env value when set', () => {
|
|
20
|
+
process.env.TEST_VAR = 'hello'
|
|
21
|
+
expect(requireEnv('TEST_VAR')).toBe('hello')
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('returns "0" correctly (should not treat as missing)', () => {
|
|
25
|
+
process.env.TEST_VAR = '0'
|
|
26
|
+
expect(requireEnv('TEST_VAR')).toBe('0')
|
|
7
27
|
})
|
|
8
28
|
|
|
9
|
-
it('returns
|
|
10
|
-
|
|
11
|
-
expect(
|
|
29
|
+
it('returns "false" correctly (should not treat as missing)', () => {
|
|
30
|
+
process.env.TEST_VAR = 'false'
|
|
31
|
+
expect(requireEnv('TEST_VAR')).toBe('false')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
// ============================================================
|
|
35
|
+
// Default handling
|
|
36
|
+
// ============================================================
|
|
37
|
+
|
|
38
|
+
it('returns default when env is undefined', () => {
|
|
39
|
+
delete process.env.TEST_VAR
|
|
40
|
+
expect(requireEnv('TEST_VAR', 'fallback')).toBe('fallback')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('returns default when env is empty string', () => {
|
|
44
|
+
process.env.TEST_VAR = ''
|
|
45
|
+
expect(requireEnv('TEST_VAR', 'fallback')).toBe('fallback')
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
// ============================================================
|
|
49
|
+
// Error cases
|
|
50
|
+
// ============================================================
|
|
51
|
+
|
|
52
|
+
it('throws when env is undefined and no default provided', () => {
|
|
53
|
+
delete process.env.TEST_VAR
|
|
54
|
+
expect(() => requireEnv('TEST_VAR')).toThrow(
|
|
55
|
+
'TEST_VAR env var must be set'
|
|
56
|
+
)
|
|
12
57
|
})
|
|
13
58
|
|
|
14
|
-
it('throws
|
|
15
|
-
|
|
16
|
-
expect(() =>
|
|
59
|
+
it('throws when env is empty string and no default provided', () => {
|
|
60
|
+
process.env.TEST_VAR = ''
|
|
61
|
+
expect(() => requireEnv('TEST_VAR')).toThrow(
|
|
62
|
+
'TEST_VAR env var must be set'
|
|
63
|
+
)
|
|
17
64
|
})
|
|
18
|
-
})
|
|
65
|
+
})
|