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

114 行
3.5 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'
2026-05-08 09:27:38 +08:00
import { configureHttp } from './http'
let _initPromise: Promise<void> | null = null
let _initResolve: (() => void) | null = null
function ensureInitPromise(): Promise<void> {
if (!_initPromise) {
_initPromise = new Promise((resolve) => { _initResolve = resolve })
}
return _initPromise
}
function markInitialized(): void {
if (_initResolve) {
_initResolve()
_initResolve = null
}
}
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
2026-05-07 19:39:41 +08:00
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,
})
2026-05-08 09:27:38 +08:00
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,
})
2026-05-08 09:27:38 +08:00
configureHttp({
baseUrl: DEFAULT_TENANT_PLATFORM_URL,
debug: options.debug,
})
if (options.debug) console.warn('[XuqmSDK] Config fetch failed, using fallback URLs', e)
}
markInitialized()
},
/**
* @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,
})
2026-05-08 09:27:38 +08:00
configureHttp({
baseUrl: DEFAULT_TENANT_PLATFORM_URL,
debug: options.debug,
})
markInitialized()
},
/**
* Initialize from a decrypted license file object.
* Use @xuqm/rn-license's decryptLicenseFile() to decrypt the raw file content first.
*/
initializeFromLicense(file: { appKey: string; baseUrl?: string; serverUrl?: string }, options?: { debug?: boolean }): void {
if (isInitialized()) return
const serverUrl = file.serverUrl || file.baseUrl || DEFAULT_TENANT_PLATFORM_URL
initConfigFromRemote({ appKey: file.appKey, debug: options?.debug }, {
imWsUrl: DEFAULT_IM_WS_URL,
fileServiceUrl: serverUrl,
apiUrl: serverUrl,
})
configureHttp({ baseUrl: serverUrl, debug: options?.debug })
markInitialized()
},
/**
* Wait for initialization to complete.
*/
async awaitInitialization(): Promise<void> {
if (isInitialized()) return
await ensureInitPromise()
},
setUserId(userId: string | null): void {
setCommonUserId(userId)
},
getUserId(): string | null {
return getCommonUserId()
},
}
export async function awaitInitialization(): Promise<void> {
if (isInitialized()) return
await ensureInitPromise()
}