fix(common): XuqmSDK.initialize() must not fall back to defaults on failure
Remove the silent fallback that used DEFAULT_TENANT_PLATFORM_URL on fetch errors. Failure now propagates: rejects awaitInitialization() callers and throws to the caller of initialize(). State is reset so initialize() can be retried. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
这个提交包含在:
父节点
07b08a4f5a
当前提交
90ba1f9de3
@ -5,10 +5,14 @@ import { decryptConfigFile } from './configCrypto'
|
|||||||
|
|
||||||
let _initPromise: Promise<void> | null = null
|
let _initPromise: Promise<void> | null = null
|
||||||
let _initResolve: (() => void) | null = null
|
let _initResolve: (() => void) | null = null
|
||||||
|
let _initReject: ((e: unknown) => void) | null = null
|
||||||
|
|
||||||
function ensureInitPromise(): Promise<void> {
|
function ensureInitPromise(): Promise<void> {
|
||||||
if (!_initPromise) {
|
if (!_initPromise) {
|
||||||
_initPromise = new Promise((resolve) => { _initResolve = resolve })
|
_initPromise = new Promise((resolve, reject) => {
|
||||||
|
_initResolve = resolve
|
||||||
|
_initReject = reject
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return _initPromise
|
return _initPromise
|
||||||
}
|
}
|
||||||
@ -17,9 +21,19 @@ function markInitialized(): void {
|
|||||||
if (_initResolve) {
|
if (_initResolve) {
|
||||||
_initResolve()
|
_initResolve()
|
||||||
_initResolve = null
|
_initResolve = null
|
||||||
|
_initReject = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function markInitializationFailed(e: unknown): void {
|
||||||
|
const reject = _initReject
|
||||||
|
// Reset so a retry can call initialize() again
|
||||||
|
_initPromise = null
|
||||||
|
_initResolve = null
|
||||||
|
_initReject = null
|
||||||
|
reject?.(e)
|
||||||
|
}
|
||||||
|
|
||||||
export const XuqmSDK = {
|
export const XuqmSDK = {
|
||||||
/**
|
/**
|
||||||
* @param options.appKey - Your application key (from the tenant platform)
|
* @param options.appKey - Your application key (from the tenant platform)
|
||||||
@ -27,6 +41,7 @@ export const XuqmSDK = {
|
|||||||
*/
|
*/
|
||||||
async initialize(options: XuqmInitOptions): Promise<void> {
|
async initialize(options: XuqmInitOptions): Promise<void> {
|
||||||
if (isInitialized()) return
|
if (isInitialized()) return
|
||||||
|
ensureInitPromise()
|
||||||
const configUrl = `${DEFAULT_TENANT_PLATFORM_URL}/api/sdk/config?appKey=${options.appKey}`
|
const configUrl = `${DEFAULT_TENANT_PLATFORM_URL}/api/sdk/config?appKey=${options.appKey}`
|
||||||
try {
|
try {
|
||||||
const res = await fetch(configUrl)
|
const res = await fetch(configUrl)
|
||||||
@ -41,20 +56,11 @@ export const XuqmSDK = {
|
|||||||
baseUrl: remote.imApiUrl ?? DEFAULT_TENANT_PLATFORM_URL,
|
baseUrl: remote.imApiUrl ?? DEFAULT_TENANT_PLATFORM_URL,
|
||||||
debug: options.debug,
|
debug: options.debug,
|
||||||
})
|
})
|
||||||
|
markInitialized()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Fallback: construct URLs from the built-in platform endpoint
|
markInitializationFailed(e)
|
||||||
initConfigFromRemote(options, {
|
throw e
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
markInitialized()
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户