Skip to content

Commit

Permalink
refactor: use globalThis instead of custom method
Browse files Browse the repository at this point in the history
  • Loading branch information
Hebilicious committed Jul 8, 2023
1 parent 3ac2dfe commit 695c678
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/runtime/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ const ENV_PREFIX_ALT =
_inlineRuntimeConfig.nitro.envPrefix ?? process.env.NITRO_ENV_PREFIX ?? "_";

// Runtime config
let appliedRuntimeConfig = {};
const _sharedRuntimeConfig = _deepFreeze(
_applyEnv(klona(_inlineRuntimeConfig))
);
const getRuntimeConfig = () => ({
..._sharedRuntimeConfig,
...appliedRuntimeConfig,
...envToRuntimeObject(globalThis.__env__ ?? {}),
});

export function useRuntimeConfig<
Expand All @@ -39,8 +38,8 @@ export function useRuntimeConfig<
return runtimeConfig;
}

export function applyEnvToRuntimeConfig(
env: Record<string, any>,
export function envToRuntimeObject(
env: Record<string, unknown>,
prefixes = [ENV_PREFIX, "NUXT_", ENV_PREFIX_ALT]
) {
const safeEnv = Object.fromEntries(
Expand All @@ -50,8 +49,7 @@ export function applyEnvToRuntimeConfig(
)
.map(([key, value]) => [formatKey(key), value])
);
appliedRuntimeConfig = safeEnv;
return useRuntimeConfig();
return safeEnv;
}

const formatKey = (key: string) =>
Expand Down
2 changes: 0 additions & 2 deletions src/runtime/entries/cloudflare-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
// @ts-ignore Bundled by Wrangler
// See https://github.com/cloudflare/kv-asset-handler#asset_manifest-required-for-es-modules
import manifest from "__STATIC_CONTENT_MANIFEST";
import { applyEnvToRuntimeConfig } from "../config";
import { requestHasBody } from "../utils";
import { nitroApp } from "#internal/nitro/app";
import { useRuntimeConfig } from "#internal/nitro";
Expand Down Expand Up @@ -52,7 +51,6 @@ export default {

// Expose latest env to the global context
globalThis.__env__ = env;
applyEnvToRuntimeConfig(env);
return nitroApp.localFetch(url.pathname + url.search, {
context: {
cf: (request as any).cf,
Expand Down
2 changes: 0 additions & 2 deletions src/runtime/entries/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type {
Request as CFRequest,
EventContext,
} from "@cloudflare/workers-types";
import { applyEnvToRuntimeConfig } from "../config";
import { requestHasBody } from "#internal/nitro/utils";
import { nitroApp } from "#internal/nitro/app";
import { isPublicAssetURL } from "#internal/nitro/virtual/public-assets";
Expand Down Expand Up @@ -39,7 +38,6 @@ export default {

// Expose latest env to the global context
globalThis.__env__ = env;
applyEnvToRuntimeConfig(env);
return nitroApp.localFetch(url.pathname + url.search, {
context: {
cf: request.cf,
Expand Down
2 changes: 2 additions & 0 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ export function testNitro(
const { data } = await callHandler({
url: "/config",
});
console.log(data.sharedRuntimeConfig);
console.log(data.runtimeConfig);
expect(data.runtimeConfig.hello).toBe("world");
expect(data.runtimeConfig.helloThere).toBe("general");
expect(data.runtimeConfig.secret).toBeUndefined();
Expand Down

0 comments on commit 695c678

Please sign in to comment.