2026-04-25 16:41:19 +08:00
|
|
|
import { initConfigFromRemote, isInitialized, type XuqmInitOptions } from './config'
|
2026-04-24 16:16:31 +08:00
|
|
|
|
|
|
|
|
export const XuqmSDK = {
|
|
|
|
|
/**
|
2026-04-25 16:41:19 +08:00
|
|
|
* Async initialize — fetches SDK config from the tenant platform.
|
|
|
|
|
* Recommended for production use.
|
2026-04-24 16:16:31 +08:00
|
|
|
*
|
2026-04-25 16:41:19 +08:00
|
|
|
* @param options.appId - Your application ID (from the tenant platform)
|
|
|
|
|
* @param options.serverUrl - Base URL of the tenant platform, e.g. "https://sentry.xuqinmin.com"
|
|
|
|
|
* @param options.appKey - Optional; defaults to appId
|
|
|
|
|
* @param options.debug - Enable verbose logging
|
|
|
|
|
*/
|
|
|
|
|
async initialize(options: XuqmInitOptions): Promise<void> {
|
|
|
|
|
if (isInitialized()) return
|
|
|
|
|
const configUrl = `${options.serverUrl}/api/sdk/config?appId=${options.appId}`
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch(configUrl)
|
|
|
|
|
const json = await res.json()
|
|
|
|
|
const remote = json.data ?? json
|
|
|
|
|
initConfigFromRemote(options, {
|
|
|
|
|
imWsUrl: remote.imWsUrl,
|
|
|
|
|
fileServiceUrl: remote.fileServiceUrl,
|
|
|
|
|
apiBaseUrl: remote.imApiUrl ?? options.serverUrl,
|
|
|
|
|
})
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Fallback: construct URLs from serverUrl
|
|
|
|
|
initConfigFromRemote(options, {
|
|
|
|
|
imWsUrl: options.serverUrl.replace('https://', 'wss://').replace('http://', 'ws://') + '/ws/im',
|
|
|
|
|
fileServiceUrl: options.serverUrl,
|
|
|
|
|
apiBaseUrl: options.serverUrl,
|
|
|
|
|
})
|
|
|
|
|
if (options.debug) console.warn('[XuqmSDK] Config fetch failed, using fallback URLs', e)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sync initialize — uses fallback URL construction from serverUrl.
|
|
|
|
|
* Kept for backward compatibility; prefer initialize() for production use.
|
|
|
|
|
*
|
|
|
|
|
* @param options.appId - Your application ID (from the tenant platform)
|
|
|
|
|
* @param options.serverUrl - Base URL of the tenant platform, e.g. "https://sentry.xuqinmin.com"
|
|
|
|
|
* @param options.appKey - Optional; defaults to appId
|
|
|
|
|
* @param options.debug - Enable verbose logging
|
2026-04-24 16:16:31 +08:00
|
|
|
*/
|
|
|
|
|
init(options: XuqmInitOptions): void {
|
|
|
|
|
if (isInitialized()) return
|
2026-04-25 16:41:19 +08:00
|
|
|
initConfigFromRemote(options, {
|
|
|
|
|
imWsUrl: options.serverUrl.replace('https://', 'wss://').replace('http://', 'ws://') + '/ws/im',
|
|
|
|
|
fileServiceUrl: options.serverUrl,
|
|
|
|
|
apiBaseUrl: options.serverUrl,
|
|
|
|
|
})
|
2026-04-24 16:16:31 +08:00
|
|
|
},
|
|
|
|
|
}
|