15710d294f
- Introduced `readKefuChatRuntimeConfig` and `setKefuChatRuntimeConfig` functions to manage runtime configuration for the Kefu Chat SDK. - Updated `useKefuChatStore` to utilize the new runtime configuration functions instead of the previous widget config methods. - Modified the SDK build process to compile TypeScript instead of JavaScript, enhancing type safety and maintainability. - Updated the minified SDK file to reflect the changes in configuration handling and TypeScript compilation.
55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import type { KefuChatRuntimeConfig } from "@/lib/sdk/config-types"
|
|
|
|
export function readKefuChatRuntimeConfig(): KefuChatRuntimeConfig {
|
|
if (typeof window === "undefined") {
|
|
return {
|
|
channelId: "",
|
|
baseUrl: "",
|
|
apiBaseUrl: "",
|
|
}
|
|
}
|
|
|
|
const query = new URLSearchParams(window.location.search)
|
|
const fallback: KefuChatRuntimeConfig = {
|
|
channelId:
|
|
query.get("channelId") ??
|
|
process.env.NEXT_PUBLIC_OPEN_IM_CHANNEL_ID?.trim() ??
|
|
"",
|
|
baseUrl:
|
|
query.get("baseUrl") ??
|
|
process.env.NEXT_PUBLIC_API_BASE_URL?.trim() ??
|
|
window.location.origin,
|
|
apiBaseUrl:
|
|
query.get("apiBaseUrl") ??
|
|
process.env.NEXT_PUBLIC_API_BASE_URL?.trim() ??
|
|
undefined,
|
|
externalId: query.get("externalId") ?? undefined,
|
|
externalName: query.get("externalName") ?? undefined,
|
|
userToken: query.get("userToken") ?? undefined,
|
|
title: query.get("title") ?? undefined,
|
|
subtitle: query.get("subtitle") ?? undefined,
|
|
position: (query.get("position") as "left" | "right" | null) ?? undefined,
|
|
themeColor: query.get("themeColor") ?? undefined,
|
|
width: query.get("width") ?? undefined,
|
|
}
|
|
|
|
if (window.__CS_AGENT_WIDGET_CONFIG__) {
|
|
return window.__CS_AGENT_WIDGET_CONFIG__
|
|
}
|
|
if (window.CSAgentConfig) {
|
|
const { getUserToken: _getUserToken, ...hostConfig } = window.CSAgentConfig
|
|
return {
|
|
...fallback,
|
|
...hostConfig,
|
|
}
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
export function setKefuChatRuntimeConfig(config: KefuChatRuntimeConfig) {
|
|
if (typeof window === "undefined") {
|
|
return
|
|
}
|
|
window.__CS_AGENT_WIDGET_CONFIG__ = config
|
|
}
|