XuqmGroup-RNSDK/packages/log/src/interceptor/HttpInterceptor.ts
XuqmGroup 16750b0421 feat: rn-update 进度回调 + rn-xwebview JSBridge + rn-log v0.1.0 新建
Agent 3 — rn-update:
- downloadPlugin/downloadApk 新增 onProgress 进度回调
- checkAppUpdate/checkPluginUpdate 版本缓存(30分钟 TTL)
- 新增 UpdateDownloadProgress 类型导出

Agent 3 — rn-xwebview:
- XWebViewBridge 补全标准 JSBridge handler
- getDeviceInfo/getToken/getUserInfo/openNativePage/closeWebView/showToast

Agent 4 — rn-log v0.1.0:
- XLog 主入口:event/captureError/warn/info/startCapture
- LogQueue:AsyncStorage 本地队列 + 批量上报
- ErrorCapture:JS global error + unhandledRejection
- FunnelTracker:漏斗分析
- fingerprint:SHA-256 指纹去重
- HttpInterceptor:rn-common HTTP 错误自动上报
- NativeLogReporter:TurboModule spec
2026-06-16 12:10:28 +08:00

52 行
1.5 KiB
TypeScript

/**
* HttpInterceptor — wraps the global fetch to automatically report
* HTTP error responses (4xx / 5xx) through XLog.captureError.
*
* Usage:
* HttpInterceptor.start(XLog.captureError.bind(XLog))
*/
type OnError = (error: unknown, meta?: Record<string, unknown>) => void
let _originalFetch: typeof fetch | null = null
export const HttpInterceptor = {
start(onError: OnError): void {
if (_originalFetch) return // already installed
_originalFetch = global.fetch
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(global as any).fetch = async (
input: RequestInfo | URL,
init?: RequestInit,
): Promise<Response> => {
const original = _originalFetch!
try {
const res = await original(input, init)
if (!res.ok) {
const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url
onError(new Error(`HTTP ${res.status} ${res.statusText}`), {
type: 'api_error',
url,
status: res.status,
})
}
return res
} catch (e) {
const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url
onError(e, { type: 'api_error', url })
throw e
}
}
},
stop(): void {
if (_originalFetch) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(global as any).fetch = _originalFetch
_originalFetch = null
}
},
}