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

110 行
3.5 KiB
TypeScript

export interface XuqmInitOptions {
appKey: string
platformUrl?: string // 不传则使用内置默认公有平台地址
debug?: boolean
}
export interface XuqmConfig {
appKey: string
apiUrl: string
imWsUrl: string
fileServiceUrl: string
licenseUrl: string
debug: boolean
// 服务开通状态(由平台远程配置决定)
imEnabled: boolean
pushEnabled: boolean
licenseEnabled: boolean
// 崩溃采集服务rn-bugcollect 使用)
bugCollectApiUrl: string
bugCollectEnabled: boolean
}
export interface XuqmUserInfo {
userId: string
userSig?: string // IM 登录凭证;未开通 IM 时可不传
name?: string
email?: string
phone?: string
avatar?: string
}
// ─── Internal state ────────────────────────────────────────────────────────────
let _config: XuqmConfig | null = null
let _userId: string | null = null
let _userInfo: XuqmUserInfo | null = null
const _userInfoHandlers: Array<(info: XuqmUserInfo | null) => void> = []
// ─── Config ────────────────────────────────────────────────────────────────────
export interface XuqmRemoteConfig {
imApiUrl?: string
apiUrl?: string
imWsUrl?: string
fileServiceUrl?: string
licenseUrl?: string
imEnabled?: boolean
pushEnabled?: boolean
licenseEnabled?: boolean
bugCollectApiUrl?: string
bugCollectEnabled?: boolean
}
export function initConfigFromRemote(
options: XuqmInitOptions,
remote: XuqmRemoteConfig,
): void {
const apiUrl = remote.imApiUrl ?? remote.apiUrl ?? ''
_config = {
appKey: options.appKey,
apiUrl,
imWsUrl: remote.imWsUrl ?? '',
fileServiceUrl: remote.fileServiceUrl ?? apiUrl,
licenseUrl: remote.licenseUrl ?? apiUrl,
debug: options.debug ?? false,
imEnabled: remote.imEnabled ?? !!remote.imWsUrl,
pushEnabled: remote.pushEnabled ?? true,
licenseEnabled: remote.licenseEnabled ?? false,
bugCollectApiUrl: remote.bugCollectApiUrl ?? '',
bugCollectEnabled: remote.bugCollectEnabled ?? false,
}
}
export function getConfig(): XuqmConfig {
if (!_config) throw new Error('[XuqmSDK] Not initialized — call XuqmSDK.initialize() first.')
return _config
}
export function isInitialized(): boolean {
return _config !== null
}
// ─── UserId ────────────────────────────────────────────────────────────────────
export function setUserId(userId: string | null): void {
_userId = userId
}
export function getUserId(): string | null {
return _userId
}
// ─── UserInfo + subscribers ────────────────────────────────────────────────────
export function _registerUserInfoHandler(handler: (info: XuqmUserInfo | null) => void): void {
_userInfoHandlers.push(handler)
}
export function setUserInfo(info: XuqmUserInfo | null): void {
_userInfo = info
_userId = info?.userId ?? null
for (const handler of _userInfoHandlers) {
try { handler(info) } catch { /* sub-SDK errors must not break the chain */ }
}
}
export function getUserInfo(): XuqmUserInfo | null {
return _userInfo
}