This commit is contained in:
2026-07-16 11:14:58 +03:30
parent 51e3c3f0cb
commit 5d390fb225
15 changed files with 618 additions and 62 deletions
+48 -2
View File
@@ -4,10 +4,56 @@ type RawAppConfig = AppConfig & {
user?: Record<string, any>
}
const EMPTY_STRING_CONFIG_KEYS = new Set([
'color',
'secondary_color',
'background_color',
'theme_config',
])
function emptyStringToNull(value: unknown) {
if (typeof value === 'string' && value.trim() === '') {
return null
}
return value
}
function normalizeUserConfig(user?: Record<string, any>) {
if (!user || typeof user !== 'object') {
return user
}
const normalized: Record<string, any> = { ...user }
for (const key of EMPTY_STRING_CONFIG_KEYS) {
if (key in normalized) {
normalized[key] = emptyStringToNull(normalized[key])
}
}
return normalized
}
export function normalizeAppConfig(config: RawAppConfig): RawAppConfig {
if (!config || typeof config !== 'object') {
return {}
}
if (!config.user || typeof config.user !== 'object') {
return config
}
return {
...config,
user: normalizeUserConfig(config.user),
}
}
let appConfig: RawAppConfig | null = null
export function setAppConfig(config: RawAppConfig) {
appConfig = config || {}
appConfig = normalizeAppConfig(config || {})
window.__APP_CONFIG__ = appConfig
}
@@ -31,7 +77,7 @@ export function getTenantConfig() {
export function getConfig<T = any>(key: string, fallback?: T): T {
const config = getTenantConfig()
const value = config?.[key]
const value = emptyStringToNull(config?.[key])
return (value ?? fallback ?? null) as T
}
+3 -2
View File
@@ -1,4 +1,5 @@
import type { AppConfig } from '@/config/types'
import { normalizeAppConfig } from '@/config/app-config'
import { readCachedAppConfig, writeCachedAppConfig } from '@/config/config-cache'
const CONFIG_SUCCESS_URL_KEY = 'CONFIG_Success_URL'
@@ -60,7 +61,7 @@ async function fetchConfig(url: string, timeout = 3000): Promise<AppConfig> {
throw new Error('Config response is not JSON')
}
return await response.json()
return normalizeAppConfig(await response.json())
} finally {
clearTimeout(timeoutId)
}
@@ -95,7 +96,7 @@ export async function loadAppConfig(): Promise<AppConfig> {
const cachedConfig = readCachedAppConfig()
if (cachedConfig) {
return cachedConfig
return normalizeAppConfig(cachedConfig)
}
return await fetchFreshAppConfig()