-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconfig.ts
More file actions
85 lines (69 loc) · 2.1 KB
/
config.ts
File metadata and controls
85 lines (69 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import type { PluginClientConfigs, PluginEndpoints } from "shared";
export interface AppKitClientConfig {
appName: string;
queries: Record<string, string>;
endpoints: PluginEndpoints;
plugins: PluginClientConfigs;
}
declare global {
interface Window {
__appkit__?: AppKitClientConfig;
}
}
const APPKIT_CONFIG_SCRIPT_ID = "__appkit__";
const EMPTY_CONFIG: AppKitClientConfig = Object.freeze({
appName: "",
queries: Object.freeze({}),
endpoints: Object.freeze({}),
plugins: Object.freeze({}),
});
function normalizeClientConfig(config: unknown): AppKitClientConfig {
if (!config || typeof config !== "object" || Array.isArray(config)) {
return EMPTY_CONFIG;
}
const normalized = config as Partial<AppKitClientConfig>;
return {
appName: normalized.appName ?? EMPTY_CONFIG.appName,
queries: normalized.queries ?? EMPTY_CONFIG.queries,
endpoints: normalized.endpoints ?? EMPTY_CONFIG.endpoints,
plugins: normalized.plugins ?? EMPTY_CONFIG.plugins,
};
}
function readClientConfigFromDom(): AppKitClientConfig | undefined {
if (typeof document === "undefined") {
return undefined;
}
const configScript = document.getElementById(APPKIT_CONFIG_SCRIPT_ID);
if (!configScript?.textContent) {
return undefined;
}
try {
return normalizeClientConfig(JSON.parse(configScript.textContent));
} catch (error) {
console.warn("[appkit] Failed to parse config from DOM:", error);
return undefined;
}
}
let _cache: AppKitClientConfig | undefined;
/**
* @internal Reset the module-scoped config cache. Test utility only.
*/
export function _resetConfigCache(): void {
_cache = undefined;
}
export function getClientConfig(): AppKitClientConfig {
if (typeof window === "undefined") {
return EMPTY_CONFIG;
}
if (!_cache) {
_cache =
readClientConfigFromDom() ?? normalizeClientConfig(window.__appkit__);
}
return _cache;
}
const EMPTY_PLUGIN_CONFIG = Object.freeze({});
export function getPluginClientConfig<T = Record<string, unknown>>(
pluginName: string,
): T {
return (getClientConfig().plugins[pluginName] ?? EMPTY_PLUGIN_CONFIG) as T;
}