XuqmGroup-RNSDK/packages/common/src/sdk.ts

54 行
2.2 KiB
TypeScript

import { initConfigFromRemote, isInitialized, type XuqmInitOptions } from './config'
export const XuqmSDK = {
/**
* Async initialize fetches SDK config from the tenant platform.
* Recommended 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
*/
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
*/
init(options: XuqmInitOptions): void {
if (isInitialized()) return
initConfigFromRemote(options, {
imWsUrl: options.serverUrl.replace('https://', 'wss://').replace('http://', 'ws://') + '/ws/im',
fileServiceUrl: options.serverUrl,
apiBaseUrl: options.serverUrl,
})
},
}