66 行
2.2 KiB
TypeScript
66 行
2.2 KiB
TypeScript
import { initConfigFromRemote, isInitialized, type XuqmInitOptions, setUserId as setCommonUserId, getUserId as getCommonUserId } from './config'
|
|
import { DEFAULT_IM_WS_URL, DEFAULT_TENANT_PLATFORM_URL } from './constants'
|
|
import { configureHttp } from './http'
|
|
|
|
export const XuqmSDK = {
|
|
/**
|
|
* @param options.appKey - Your application key (from the tenant platform)
|
|
* @param options.debug - Enable verbose logging
|
|
*/
|
|
async initialize(options: XuqmInitOptions): Promise<void> {
|
|
if (isInitialized()) return
|
|
const configUrl = `${DEFAULT_TENANT_PLATFORM_URL}/api/sdk/config?appKey=${options.appKey}`
|
|
try {
|
|
const res = await fetch(configUrl)
|
|
const json = await res.json()
|
|
const remote = json.data ?? json
|
|
initConfigFromRemote(options, {
|
|
imWsUrl: remote.imWsUrl,
|
|
fileServiceUrl: remote.fileServiceUrl,
|
|
apiUrl: remote.imApiUrl ?? DEFAULT_TENANT_PLATFORM_URL,
|
|
})
|
|
configureHttp({
|
|
baseUrl: remote.imApiUrl ?? DEFAULT_TENANT_PLATFORM_URL,
|
|
debug: options.debug,
|
|
})
|
|
} catch (e) {
|
|
// Fallback: construct URLs from the built-in platform endpoint
|
|
initConfigFromRemote(options, {
|
|
imWsUrl: DEFAULT_IM_WS_URL,
|
|
fileServiceUrl: DEFAULT_TENANT_PLATFORM_URL,
|
|
apiUrl: DEFAULT_TENANT_PLATFORM_URL,
|
|
})
|
|
configureHttp({
|
|
baseUrl: DEFAULT_TENANT_PLATFORM_URL,
|
|
debug: options.debug,
|
|
})
|
|
if (options.debug) console.warn('[XuqmSDK] Config fetch failed, using fallback URLs', e)
|
|
}
|
|
},
|
|
|
|
/**
|
|
* @param options.appKey - Your application key (from the tenant platform)
|
|
* @param options.debug - Enable verbose logging
|
|
*/
|
|
init(options: XuqmInitOptions): void {
|
|
if (isInitialized()) return
|
|
initConfigFromRemote(options, {
|
|
imWsUrl: DEFAULT_IM_WS_URL,
|
|
fileServiceUrl: DEFAULT_TENANT_PLATFORM_URL,
|
|
apiUrl: DEFAULT_TENANT_PLATFORM_URL,
|
|
})
|
|
configureHttp({
|
|
baseUrl: DEFAULT_TENANT_PLATFORM_URL,
|
|
debug: options.debug,
|
|
})
|
|
},
|
|
|
|
setUserId(userId: string | null): void {
|
|
setCommonUserId(userId)
|
|
},
|
|
|
|
getUserId(): string | null {
|
|
return getCommonUserId()
|
|
},
|
|
}
|