-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow oveeridng nested runtime config with env (#1831)
* chore(playground): Add GET config endpoint * fix(runtimeConfig): Replace nested obj value with primitive defined env variables * chore(playground): Revert changes * chore: apply automated fixes * add unit tests * fix test / restore env --------- Co-authored-by: Pooya Parsa <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Alexander Lichter <[email protected]> Co-authored-by: Pooya Parsa <[email protected]>
- Loading branch information
1 parent
06e1940
commit 1d093d6
Showing
4 changed files
with
95 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export default defineNitroConfig({}); | ||
export default defineNitroConfig({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import destr from "destr"; | ||
import { snakeCase } from "scule"; | ||
|
||
export type EnvOptions = { | ||
prefix?: string; | ||
altPrefix?: string; | ||
}; | ||
|
||
export function getEnv(key: string, opts: EnvOptions) { | ||
const envKey = snakeCase(key).toUpperCase(); | ||
return destr( | ||
process.env[opts.prefix + envKey] ?? process.env[opts.altPrefix + envKey] | ||
); | ||
} | ||
|
||
function _isObject(input: unknown) { | ||
return typeof input === "object" && !Array.isArray(input); | ||
} | ||
|
||
export function applyEnv(obj: object, opts: EnvOptions, parentKey = "") { | ||
for (const key in obj) { | ||
const subKey = parentKey ? `${parentKey}_${key}` : key; | ||
const envValue = getEnv(subKey, opts); | ||
if (_isObject(obj[key])) { | ||
// Same as before | ||
if (_isObject(envValue)) { | ||
obj[key] = { ...obj[key], ...(envValue as object) }; | ||
applyEnv(obj[key], opts, subKey); | ||
} | ||
// If envValue is undefined | ||
// Then proceed to nested properties | ||
else if (envValue === undefined) { | ||
applyEnv(obj[key], opts, subKey); | ||
} | ||
// If envValue is a primitive other than undefined | ||
// Then set objValue and ignore the nested properties | ||
else { | ||
obj[key] = envValue ?? obj[key]; | ||
} | ||
} else { | ||
obj[key] = envValue ?? obj[key]; | ||
} | ||
} | ||
return obj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { applyEnv } from "../../src/runtime/utils.env"; | ||
|
||
describe("env utils", () => { | ||
describe("applyEnv", () => { | ||
const tests = [ | ||
{ | ||
config: { a: 1, b: 2 }, | ||
env: { NITRO_A: "123" }, | ||
expected: { a: 123, b: 2 }, | ||
}, | ||
{ | ||
config: { feature: { options: { optionA: true, optionB: true } } }, | ||
env: { NITRO_FEATURE: false }, | ||
expected: { feature: false }, | ||
}, | ||
{ | ||
config: { feature: { options: { optionA: true, optionB: true } } }, | ||
env: { NITRO_FEATURE_OPTIONS: false }, | ||
expected: { feature: { options: false } }, | ||
}, | ||
]; | ||
for (const test of tests) { | ||
it(`Config: ${JSON.stringify(test.config)} Env: { ${Object.entries( | ||
test.env | ||
) | ||
.map(([key, value]) => `${key}=${JSON.stringify(value)}`) | ||
.join(" ")} }`, () => { | ||
for (const key in test.env) { | ||
process.env[key] = test.env[key]; | ||
} | ||
expect(applyEnv(test.config, { prefix: "NITRO_" })).toEqual( | ||
test.expected | ||
); | ||
for (const key in test.env) { | ||
delete process.env[key]; | ||
} | ||
}); | ||
} | ||
}); | ||
}); |