task-2: buildJavaUrl() 统一 URL 构造(query 编码、尾斜杠规范化)
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
/** Java 主后端 URL 构造:常量表值已含 /api 前缀,此处只加 /newApi 网关前缀 */
|
||||
export const JAVA_API_PREFIX = '/newApi/api'
|
||||
|
||||
function buildQueryString(query?: Record<string, unknown>): string {
|
||||
if (!query) return ''
|
||||
const params = new URLSearchParams()
|
||||
for (const [key, value] of Object.entries(query)) {
|
||||
if (value !== undefined && value !== null) {
|
||||
params.append(key, String(value))
|
||||
}
|
||||
}
|
||||
const text = params.toString().replace(/\+/g, '%20')
|
||||
return text ? `?${text}` : ''
|
||||
}
|
||||
|
||||
export function buildJavaUrl(
|
||||
endpoint: string,
|
||||
query?: Record<string, unknown>,
|
||||
): string {
|
||||
const normalized = endpoint.endsWith('/') ? endpoint.slice(0, -1) : endpoint
|
||||
const prefix = normalized.startsWith('/api')
|
||||
? '/newApi'
|
||||
: JAVA_API_PREFIX
|
||||
return `${prefix}${normalized}${buildQueryString(query)}`
|
||||
}
|
||||
Reference in New Issue
Block a user