59 lines
1.8 KiB
JavaScript
59 lines
1.8 KiB
JavaScript
import { klona } from "klona";
|
|
import { appConfig as _inlineAppConfig } from "#nitro-internal-virtual/app-config";
|
|
import { applyEnv } from "./utils.env.mjs";
|
|
const _inlineRuntimeConfig = process.env.RUNTIME_CONFIG;
|
|
const envOptions = {
|
|
prefix: "NITRO_",
|
|
altPrefix: _inlineRuntimeConfig.nitro.envPrefix ?? process.env.NITRO_ENV_PREFIX ?? "_",
|
|
envExpansion: _inlineRuntimeConfig.nitro.envExpansion ?? process.env.NITRO_ENV_EXPANSION ?? false
|
|
};
|
|
const _sharedRuntimeConfig = _deepFreeze(
|
|
applyEnv(klona(_inlineRuntimeConfig), envOptions)
|
|
);
|
|
export function useRuntimeConfig(event) {
|
|
if (!event) {
|
|
return _sharedRuntimeConfig;
|
|
}
|
|
if (event.context.nitro.runtimeConfig) {
|
|
return event.context.nitro.runtimeConfig;
|
|
}
|
|
const runtimeConfig = klona(_inlineRuntimeConfig);
|
|
applyEnv(runtimeConfig, envOptions);
|
|
event.context.nitro.runtimeConfig = runtimeConfig;
|
|
return runtimeConfig;
|
|
}
|
|
const _sharedAppConfig = _deepFreeze(klona(_inlineAppConfig));
|
|
export function useAppConfig(event) {
|
|
if (!event) {
|
|
return _sharedAppConfig;
|
|
}
|
|
if (event.context.nitro.appConfig) {
|
|
return event.context.nitro.appConfig;
|
|
}
|
|
const appConfig = klona(_inlineAppConfig);
|
|
event.context.nitro.appConfig = appConfig;
|
|
return appConfig;
|
|
}
|
|
function _deepFreeze(object) {
|
|
const propNames = Object.getOwnPropertyNames(object);
|
|
for (const name of propNames) {
|
|
const value = object[name];
|
|
if (value && typeof value === "object") {
|
|
_deepFreeze(value);
|
|
}
|
|
}
|
|
return Object.freeze(object);
|
|
}
|
|
export default new Proxy(/* @__PURE__ */ Object.create(null), {
|
|
get: (_, prop) => {
|
|
console.warn(
|
|
"Please use `useRuntimeConfig()` instead of accessing config directly."
|
|
);
|
|
const runtimeConfig = useRuntimeConfig();
|
|
if (prop in runtimeConfig) {
|
|
return runtimeConfig[prop];
|
|
}
|
|
return void 0;
|
|
}
|
|
});
|