import { BuildConfig, getBuildConfig } from "./build"; export function getClientConfig() { if (typeof document !== "undefined") { // client side return JSON.parse(queryMeta("config")) as BuildConfig; } if (typeof process !== "undefined") { // server side return getBuildConfig(); } } /** * 适配basePath不为空的情况 * @param api */ export function getFullApi(api: string) { const base = getClientConfig()?.baseApi ?? ""; if (api.startsWith("/") && base.endsWith("/")) { return base + api.substring(1); } if (api.startsWith("/") || base.endsWith("/")) { return base + api; } return base + "/" + api; } function queryMeta(key: string, defaultValue?: string): string { let ret: string; if (document) { const meta = document.head.querySelector( `meta[name='${key}']`, ) as HTMLMetaElement; ret = meta?.content ?? ""; } else { ret = defaultValue ?? ""; } return ret; }