Files
life-echo/app-expo/src/core/api/media-url.ts

15 lines
504 B
TypeScript
Raw Normal View History

import { config } from '@/core/config';
/** 将 API 返回的相对路径(如 `/api/auth/avatars/x.jpg`)转为可请求的绝对 URL。 */
export function resolveApiMediaUrl(
pathOrUrl: string | null | undefined,
): string | null {
if (pathOrUrl == null || pathOrUrl === '') return null;
if (/^https?:\/\//i.test(pathOrUrl)) return pathOrUrl;
if (pathOrUrl.startsWith('/')) {
const base = config.apiBaseUrl.replace(/\/$/, '');
return `${base}${pathOrUrl}`;
}
return pathOrUrl;
}