Skip to content

Commit 99ebb51

Browse files
NamesMTdaniel-lxs
authored andcommitted
chore: better describe the accepted config type and more extensive test
1 parent cdfd0d8 commit 99ebb51

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/utils/__tests__/config.spec.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,44 @@ describe("injectEnv", () => {
3131
key: "${env:API_KEY}",
3232
url: "${env:ENDPOINT}",
3333
nested: {
34-
value: "Keep this ${env:API_KEY}",
34+
string: "Keep this ${env:API_KEY}",
35+
number: 123,
36+
boolean: true,
37+
stringArr: ["${env:API_KEY}", "${env:ENDPOINT}"],
38+
numberArr: [123, 456],
39+
booleanArr: [true, false],
40+
},
41+
deeply: {
42+
nested: {
43+
string: "Keep this ${env:API_KEY}",
44+
number: 123,
45+
boolean: true,
46+
stringArr: ["${env:API_KEY}", "${env:ENDPOINT}"],
47+
numberArr: [123, 456],
48+
booleanArr: [true, false],
49+
},
3550
},
3651
}
3752
const expectedObject = {
3853
key: "12345",
3954
url: "https://example.com",
4055
nested: {
41-
value: "Keep this 12345",
56+
string: "Keep this 12345",
57+
number: 123,
58+
boolean: true,
59+
stringArr: ["12345", "https://example.com"],
60+
numberArr: [123, 456],
61+
booleanArr: [true, false],
62+
},
63+
deeply: {
64+
nested: {
65+
string: "Keep this 12345",
66+
number: 123,
67+
boolean: true,
68+
stringArr: ["12345", "https://example.com"],
69+
numberArr: [123, 456],
70+
booleanArr: [true, false],
71+
},
4272
},
4373
}
4474
const result = await injectEnv(configObject)

src/utils/config.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
export type InjectableConfigType =
2+
| string
3+
| {
4+
[key: string]:
5+
| undefined
6+
| null
7+
| boolean
8+
| number
9+
| InjectableConfigType
10+
| Array<undefined | null | boolean | number | InjectableConfigType>
11+
}
12+
113
/**
214
* Deeply injects environment variables into a configuration object/string/json
315
*
416
* Uses VSCode env:name pattern: https://code.visualstudio.com/docs/reference/variables-reference#_environment-variables
517
*
618
* Does not mutate original object
719
*/
8-
export async function injectEnv<C extends string | Record<PropertyKey, any>>(config: C, notFoundValue: any = "") {
20+
export async function injectEnv<C extends InjectableConfigType>(config: C, notFoundValue: any = "") {
921
return injectVariables(config, { env: process.env }, notFoundValue)
1022
}
1123

@@ -20,8 +32,8 @@ export async function injectEnv<C extends string | Record<PropertyKey, any>>(con
2032
*
2133
* Matched keys that have `null` | `undefined` values are treated as not found.
2234
*/
23-
export async function injectVariables<C extends string | Record<PropertyKey, any>>(
24-
config: string | Record<PropertyKey, any>,
35+
export async function injectVariables<C extends InjectableConfigType>(
36+
config: C,
2537
variables: Record<string, undefined | null | string | Record<string, undefined | null | string>>,
2638
propNotFoundValue?: any,
2739
) {

0 commit comments

Comments
 (0)