From 77feb6ecd9a831c50ea96e52c7a22cbcb86e004b Mon Sep 17 00:00:00 2001 From: XuqmGroup Date: Fri, 8 May 2026 09:27:38 +0800 Subject: [PATCH] feat: make common http independent --- packages/common/package.json | 2 +- packages/common/src/http.ts | 25 ++++++++++++++++++------- packages/common/src/index.ts | 2 +- packages/common/src/sdk.ts | 13 +++++++++++++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/common/package.json b/packages/common/package.json index 033f30e..8a048f6 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -1,6 +1,6 @@ { "name": "@xuqm/rn-common", - "version": "0.2.2", + "version": "0.2.3", "description": "XuqmGroup RN SDK — core: init, network, token management", "license": "UNLICENSED", "main": "src/index.ts", diff --git a/packages/common/src/http.ts b/packages/common/src/http.ts index 64b906b..7381ca1 100644 --- a/packages/common/src/http.ts +++ b/packages/common/src/http.ts @@ -1,7 +1,18 @@ import AsyncStorage from '@react-native-async-storage/async-storage' -import { getConfig } from './config' +import { DEFAULT_TENANT_PLATFORM_URL } from './constants' const TOKEN_KEY = '@xuqm:token' +let _baseUrl = DEFAULT_TENANT_PLATFORM_URL +let _debugHttp = false + +export function configureHttp(options: { baseUrl?: string; debug?: boolean } = {}) { + if (options.baseUrl) { + _baseUrl = options.baseUrl.replace(/\/$/, '') + } + if (typeof options.debug === 'boolean') { + _debugHttp = options.debug + } +} export async function _getToken(): Promise { return AsyncStorage.getItem(TOKEN_KEY) @@ -24,9 +35,9 @@ export async function apiRequest( skipAuth?: boolean } = {}, ): Promise { - const config = getConfig() - - let url = config.apiUrl + path + let url = path.startsWith('http://') || path.startsWith('https://') + ? path + : `${_baseUrl}${path}` if (options.params) { const qs = new URLSearchParams(options.params).toString() url += (url.includes('?') ? '&' : '?') + qs @@ -42,7 +53,7 @@ export async function apiRequest( if (token) headers['Authorization'] = `Bearer ${token}` } - if (config.debug) { + if (_debugHttp) { console.debug('[XuqmSDK][HTTP] request', { method: options.method ?? 'GET', url, @@ -58,7 +69,7 @@ export async function apiRequest( if (!res.ok) { const err = await res.json().catch(() => ({ message: res.statusText })) - if (config.debug) { + if (_debugHttp) { console.debug('[XuqmSDK][HTTP] response', { url, status: res.status, @@ -70,7 +81,7 @@ export async function apiRequest( } const json = await res.json() - if (config.debug) { + if (_debugHttp) { console.debug('[XuqmSDK][HTTP] response', { url, status: res.status, diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 57d3a22..430734b 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -1,7 +1,7 @@ export { XuqmSDK } from './sdk' export type { XuqmInitOptions, XuqmConfig } from './config' export { getConfig, isInitialized, setUserId, getUserId } from './config' -export { apiRequest, _getToken, _saveToken, _clearToken } from './http' +export { apiRequest, configureHttp, _getToken, _saveToken, _clearToken } from './http' export { DEFAULT_TENANT_PLATFORM_URL, DEFAULT_IM_WS_URL } from './constants' export { getDeviceId, getDeviceInfo, detectPushVendor } from './device' export type { DeviceInfo, PushVendor } from './device' diff --git a/packages/common/src/sdk.ts b/packages/common/src/sdk.ts index 9c1fdc7..76779ec 100644 --- a/packages/common/src/sdk.ts +++ b/packages/common/src/sdk.ts @@ -1,5 +1,6 @@ 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 = { /** @@ -18,6 +19,10 @@ export const XuqmSDK = { 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, { @@ -25,6 +30,10 @@ export const XuqmSDK = { 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) } }, @@ -40,6 +49,10 @@ export const XuqmSDK = { fileServiceUrl: DEFAULT_TENANT_PLATFORM_URL, apiUrl: DEFAULT_TENANT_PLATFORM_URL, }) + configureHttp({ + baseUrl: DEFAULT_TENANT_PLATFORM_URL, + debug: options.debug, + }) }, setUserId(userId: string | null): void {