feat: make common http independent
这个提交包含在:
父节点
5bddc9b167
当前提交
77feb6ecd9
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@xuqm/rn-common",
|
"name": "@xuqm/rn-common",
|
||||||
"version": "0.2.2",
|
"version": "0.2.3",
|
||||||
"description": "XuqmGroup RN SDK — core: init, network, token management",
|
"description": "XuqmGroup RN SDK — core: init, network, token management",
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
|
|||||||
@ -1,7 +1,18 @@
|
|||||||
import AsyncStorage from '@react-native-async-storage/async-storage'
|
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'
|
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> {
|
export async function _getToken(): Promise<string | null> {
|
||||||
return AsyncStorage.getItem(TOKEN_KEY)
|
return AsyncStorage.getItem(TOKEN_KEY)
|
||||||
@ -24,9 +35,9 @@ export async function apiRequest<T>(
|
|||||||
skipAuth?: boolean
|
skipAuth?: boolean
|
||||||
} = {},
|
} = {},
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
const config = getConfig()
|
let url = path.startsWith('http://') || path.startsWith('https://')
|
||||||
|
? path
|
||||||
let url = config.apiUrl + path
|
: `${_baseUrl}${path}`
|
||||||
if (options.params) {
|
if (options.params) {
|
||||||
const qs = new URLSearchParams(options.params).toString()
|
const qs = new URLSearchParams(options.params).toString()
|
||||||
url += (url.includes('?') ? '&' : '?') + qs
|
url += (url.includes('?') ? '&' : '?') + qs
|
||||||
@ -42,7 +53,7 @@ export async function apiRequest<T>(
|
|||||||
if (token) headers['Authorization'] = `Bearer ${token}`
|
if (token) headers['Authorization'] = `Bearer ${token}`
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.debug) {
|
if (_debugHttp) {
|
||||||
console.debug('[XuqmSDK][HTTP] request', {
|
console.debug('[XuqmSDK][HTTP] request', {
|
||||||
method: options.method ?? 'GET',
|
method: options.method ?? 'GET',
|
||||||
url,
|
url,
|
||||||
@ -58,7 +69,7 @@ export async function apiRequest<T>(
|
|||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const err = await res.json().catch(() => ({ message: res.statusText }))
|
const err = await res.json().catch(() => ({ message: res.statusText }))
|
||||||
if (config.debug) {
|
if (_debugHttp) {
|
||||||
console.debug('[XuqmSDK][HTTP] response', {
|
console.debug('[XuqmSDK][HTTP] response', {
|
||||||
url,
|
url,
|
||||||
status: res.status,
|
status: res.status,
|
||||||
@ -70,7 +81,7 @@ export async function apiRequest<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const json = await res.json()
|
const json = await res.json()
|
||||||
if (config.debug) {
|
if (_debugHttp) {
|
||||||
console.debug('[XuqmSDK][HTTP] response', {
|
console.debug('[XuqmSDK][HTTP] response', {
|
||||||
url,
|
url,
|
||||||
status: res.status,
|
status: res.status,
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
export { XuqmSDK } from './sdk'
|
export { XuqmSDK } from './sdk'
|
||||||
export type { XuqmInitOptions, XuqmConfig } from './config'
|
export type { XuqmInitOptions, XuqmConfig } from './config'
|
||||||
export { getConfig, isInitialized, setUserId, getUserId } 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 { DEFAULT_TENANT_PLATFORM_URL, DEFAULT_IM_WS_URL } from './constants'
|
||||||
export { getDeviceId, getDeviceInfo, detectPushVendor } from './device'
|
export { getDeviceId, getDeviceInfo, detectPushVendor } from './device'
|
||||||
export type { DeviceInfo, PushVendor } 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 { initConfigFromRemote, isInitialized, type XuqmInitOptions, setUserId as setCommonUserId, getUserId as getCommonUserId } from './config'
|
||||||
import { DEFAULT_IM_WS_URL, DEFAULT_TENANT_PLATFORM_URL } from './constants'
|
import { DEFAULT_IM_WS_URL, DEFAULT_TENANT_PLATFORM_URL } from './constants'
|
||||||
|
import { configureHttp } from './http'
|
||||||
|
|
||||||
export const XuqmSDK = {
|
export const XuqmSDK = {
|
||||||
/**
|
/**
|
||||||
@ -18,6 +19,10 @@ export const XuqmSDK = {
|
|||||||
fileServiceUrl: remote.fileServiceUrl,
|
fileServiceUrl: remote.fileServiceUrl,
|
||||||
apiUrl: remote.imApiUrl ?? DEFAULT_TENANT_PLATFORM_URL,
|
apiUrl: remote.imApiUrl ?? DEFAULT_TENANT_PLATFORM_URL,
|
||||||
})
|
})
|
||||||
|
configureHttp({
|
||||||
|
baseUrl: remote.imApiUrl ?? DEFAULT_TENANT_PLATFORM_URL,
|
||||||
|
debug: options.debug,
|
||||||
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Fallback: construct URLs from the built-in platform endpoint
|
// Fallback: construct URLs from the built-in platform endpoint
|
||||||
initConfigFromRemote(options, {
|
initConfigFromRemote(options, {
|
||||||
@ -25,6 +30,10 @@ export const XuqmSDK = {
|
|||||||
fileServiceUrl: DEFAULT_TENANT_PLATFORM_URL,
|
fileServiceUrl: DEFAULT_TENANT_PLATFORM_URL,
|
||||||
apiUrl: 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)
|
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,
|
fileServiceUrl: DEFAULT_TENANT_PLATFORM_URL,
|
||||||
apiUrl: DEFAULT_TENANT_PLATFORM_URL,
|
apiUrl: DEFAULT_TENANT_PLATFORM_URL,
|
||||||
})
|
})
|
||||||
|
configureHttp({
|
||||||
|
baseUrl: DEFAULT_TENANT_PLATFORM_URL,
|
||||||
|
debug: options.debug,
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
setUserId(userId: string | null): void {
|
setUserId(userId: string | null): void {
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户