Files
alrahma_web_client/src/lib/apiOrigin.ts
T
2026-06-07 20:01:56 -04:00

32 lines
862 B
TypeScript

const PROD_API_ORIGIN = 'https://api.alrahmaisgl.org'
/**
* Laravel API base URL.
*
* Dev defaults to same-origin `/api/...` so Vite can proxy requests to Laravel.
* Set `VITE_API_ORIGIN` when you want the browser to call Laravel directly, e.g.
* `VITE_API_ORIGIN=http://localhost:8000 npm run dev`.
*
* If `VITE_API_ORIGIN` is not set in dev, `vite.config.ts` proxies `/api` to
* `VITE_PROXY_API` or `http://localhost:8000` by default.
*/
export function apiOrigin(): string {
const v = import.meta.env.VITE_API_ORIGIN
if (typeof v === 'string' && v.trim()) {
return v.replace(/\/$/, '')
}
if (import.meta.env.DEV) {
return ''
}
return PROD_API_ORIGIN
}
export function apiUrl(path: string): string {
const base = apiOrigin()
const p = path.startsWith('/') ? path : `/${path}`
if (!base) return p
return `${base}${p}`
}