From 20c1d2cf41a3b6f5c4dbab4da2b3897f6023f327 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 3 Feb 2023 23:19:57 +0000 Subject: [PATCH] fix: provide fallback values for undefined runtime config --- src/options.ts | 4 +++- src/utils/index.ts | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/options.ts b/src/options.ts index e0bd8bd203..35dd854121 100644 --- a/src/options.ts +++ b/src/options.ts @@ -8,7 +8,7 @@ import escapeRE from "escape-string-regexp"; import { withLeadingSlash, withoutTrailingSlash, withTrailingSlash } from "ufo"; import { isTest, isDebug } from "std-env"; import { findWorkspaceDir } from "pkg-types"; -import { resolvePath, detectTarget } from "./utils"; +import { resolvePath, detectTarget, provideFallbackValues } from "./utils"; import type { NitroConfig, NitroOptions, @@ -290,6 +290,8 @@ export async function loadOptions( options.routeRules = normalizedRules; options.baseURL = withLeadingSlash(withTrailingSlash(options.baseURL)); + + provideFallbackValues(options.runtimeConfig); options.runtimeConfig = defu(options.runtimeConfig, { app: { baseURL: options.baseURL, diff --git a/src/utils/index.ts b/src/utils/index.ts index 1d973830d3..58147c622c 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -189,3 +189,13 @@ export async function retry(fn: () => Promise, retries: number) { } throw error; } + +export function provideFallbackValues(obj: Record) { + for (const key in obj) { + if (typeof obj[key] === "undefined" || obj[key] === null) { + obj[key] = ""; + } else if (typeof obj[key] === "object") { + provideFallbackValues(obj[key]); + } + } +}