feat: make common http independent
这个提交包含在:
父节点
5bddc9b167
当前提交
77feb6ecd9
@ -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",
|
||||
|
||||
@ -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<string | null> {
|
||||
return AsyncStorage.getItem(TOKEN_KEY)
|
||||
@ -24,9 +35,9 @@ export async function apiRequest<T>(
|
||||
skipAuth?: boolean
|
||||
} = {},
|
||||
): Promise<T> {
|
||||
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<T>(
|
||||
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<T>(
|
||||
|
||||
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<T>(
|
||||
}
|
||||
|
||||
const json = await res.json()
|
||||
if (config.debug) {
|
||||
if (_debugHttp) {
|
||||
console.debug('[XuqmSDK][HTTP] response', {
|
||||
url,
|
||||
status: res.status,
|
||||
|
||||
@ -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'
|
||||
|
||||
@ -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 {
|
||||
|
||||
正在加载...
在新工单中引用
屏蔽一个用户