first commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
type ApiError = {
|
||||
status: number;
|
||||
bodyText: string;
|
||||
};
|
||||
|
||||
function buildUrl(path: string) {
|
||||
const base = (import.meta.env.VITE_API_BASE_URL as string | undefined) ?? "/api";
|
||||
if (path.startsWith("http://") || path.startsWith("https://")) return path;
|
||||
if (path.startsWith("/")) return `${base}${path}`;
|
||||
return `${base}/${path}`;
|
||||
}
|
||||
|
||||
export async function apiJson<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const url = buildUrl(path);
|
||||
const res = await fetch(url, {
|
||||
...init,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(init?.headers ?? {}),
|
||||
},
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const bodyText = await res.text().catch(() => "");
|
||||
const err: ApiError = { status: res.status, bodyText };
|
||||
throw err;
|
||||
}
|
||||
|
||||
return (await res.json()) as T;
|
||||
}
|
||||
|
||||
export function getMediamtxWebrtcBaseUrl(configUrl?: string) {
|
||||
const env = import.meta.env.VITE_MEDIAMTX_WEBRTC_URL as string | undefined;
|
||||
return env ?? configUrl ?? "http://localhost:8889";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user