Some checks failed
Build and Deploy Front / deploy (push) Has been cancelled
Dockerfile 误用 production 构建会把 VITE_API_ORIGIN 打成 api.ideship.com;运行时按 hostname 纠正,并支持 FRONT_BUILD_SCRIPT=build:test。 Co-authored-by: Cursor <cursoragent@cursor.com>
80 lines
2.2 KiB
TypeScript
80 lines
2.2 KiB
TypeScript
/**
|
||
* Admin API 地址(方案 A)
|
||
* - 开发:相对路径,Vite 代理到 Admin
|
||
* - 生产/测试:{apiOrigin}/admin(可用 VITE_API_ADMIN_URL 覆盖)
|
||
* - 浏览器在 test.ideship.com 时强制 api.test.ideship.com/admin
|
||
*/
|
||
|
||
import { isTestFrontHost, prodApiOrigin } from './rewriteBrowserApiPath'
|
||
|
||
const DEFAULT_TEST_ADMIN = 'https://api.test.ideship.com/admin'
|
||
|
||
function normalizeBase(base: string): string {
|
||
return base.trim().replace(/\/$/, '')
|
||
}
|
||
|
||
function ensureLeadingSlash(path: string): string {
|
||
return path.startsWith('/') ? path : `/${path}`
|
||
}
|
||
|
||
function isBrowserOnTestFront(): boolean {
|
||
return typeof window !== 'undefined' && isTestFrontHost(window.location.hostname)
|
||
}
|
||
|
||
function prodAdminBase(): string {
|
||
if (isBrowserOnTestFront()) {
|
||
return DEFAULT_TEST_ADMIN
|
||
}
|
||
const override = import.meta.env.VITE_API_ADMIN_URL as string | undefined
|
||
if (override?.trim()) return normalizeBase(override)
|
||
return `${prodApiOrigin()}/admin`
|
||
}
|
||
|
||
/** 浏览器请求 Admin API(OpenAPI.BASE 或 omsUrl/orderUrl 等) */
|
||
export function resolveAdminApiPath(path: string): string {
|
||
const p = ensureLeadingSlash(path)
|
||
if (!import.meta.env.DEV) {
|
||
return `${prodAdminBase()}${p}`
|
||
}
|
||
return p
|
||
}
|
||
|
||
export function resolveOpenApiBase(): string {
|
||
if (!import.meta.env.DEV) {
|
||
return prodAdminBase()
|
||
}
|
||
return ''
|
||
}
|
||
|
||
/** 桌面 Agent 等直连 Admin 网关 */
|
||
export function resolveDirectAdminApiUrl(): string {
|
||
if (isBrowserOnTestFront()) {
|
||
return DEFAULT_TEST_ADMIN
|
||
}
|
||
const override = import.meta.env.VITE_API_ADMIN_URL as string | undefined
|
||
if (override?.trim()) {
|
||
return normalizeBase(override)
|
||
}
|
||
if (import.meta.env.DEV) {
|
||
return DEFAULT_TEST_ADMIN
|
||
}
|
||
return prodAdminBase()
|
||
}
|
||
|
||
/** 静态资源(uploads)在 Admin wwwroot,须带 `/admin`。
|
||
* 生产/测试默认 api 域名;开发空串走 Vite `/uploads` 代理。
|
||
*/
|
||
export function resolveAdminAssetOrigin(): string {
|
||
if (isBrowserOnTestFront()) {
|
||
return DEFAULT_TEST_ADMIN
|
||
}
|
||
const override = import.meta.env.VITE_API_ASSET_ORIGIN as string | undefined
|
||
if (override?.trim()) {
|
||
return normalizeBase(override)
|
||
}
|
||
if (!import.meta.env.DEV) {
|
||
return prodAdminBase()
|
||
}
|
||
return ''
|
||
}
|