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.
36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import { createWebSocketBaseUrl } from "@/lib/api/websocket"
|
|
import { getCustomerSessionToken, type ImMessage } from "@/lib/api/im"
|
|
import { readKefuChatRuntimeConfig } from "@/lib/sdk/runtime-config"
|
|
import type {
|
|
RealtimeConversationPatch,
|
|
RealtimeMessageCreatedPayload,
|
|
} from "@/lib/im-realtime-state"
|
|
|
|
export type ImRealtimeEnvelope = {
|
|
type: string
|
|
topic?: string
|
|
data?: RealtimeMessageCreatedPayload<ImMessage> &
|
|
RealtimeConversationPatch & {
|
|
customerSessionToken?: string
|
|
expiresAt?: string
|
|
}
|
|
payload?: RealtimeMessageCreatedPayload<ImMessage> &
|
|
RealtimeConversationPatch & {
|
|
customerSessionToken?: string
|
|
expiresAt?: string
|
|
}
|
|
}
|
|
|
|
export function createImRealtimeConnection() {
|
|
const config = readKefuChatRuntimeConfig()
|
|
const apiBaseUrl = (config.apiBaseUrl || "").trim()
|
|
const baseUrl = apiBaseUrl
|
|
? apiBaseUrl.replace(/^http/, "ws").replace(/\/$/, "")
|
|
: createWebSocketBaseUrl()
|
|
const channelId = encodeURIComponent(config.channelId || "")
|
|
const customerSessionToken = getCustomerSessionToken()
|
|
return new WebSocket(
|
|
`${baseUrl}/api/ws/open?channelId=${channelId}&customerSessionToken=${encodeURIComponent(customerSessionToken)}`
|
|
)
|
|
}
|