Initial GeoIntel V1 foundation
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? "";
|
||||
|
||||
export function apiUrl(path: string): string {
|
||||
return `${API_BASE_URL}${path}`;
|
||||
}
|
||||
|
||||
export class ApiHttpError extends Error {
|
||||
readonly code: string;
|
||||
readonly details?: unknown;
|
||||
|
||||
constructor(message: string, code = "REQUEST_ERROR", details?: unknown) {
|
||||
super(message);
|
||||
this.name = "ApiHttpError";
|
||||
this.code = code;
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
|
||||
async function parseResponse<T>(response: Response): Promise<T> {
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
const code = payload?.error?.code ?? "REQUEST_ERROR";
|
||||
const message = payload?.error?.message ?? `Request failed (${response.status})`;
|
||||
const details = payload?.error?.details;
|
||||
throw new ApiHttpError(message, code, details);
|
||||
}
|
||||
return payload.data as T;
|
||||
}
|
||||
|
||||
export async function apiGet<T>(path: string): Promise<T> {
|
||||
const response = await fetch(apiUrl(path));
|
||||
return parseResponse<T>(response);
|
||||
}
|
||||
|
||||
export async function apiPost<T>(path: string, body?: object): Promise<T> {
|
||||
const response = await fetch(apiUrl(path), {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
return parseResponse<T>(response);
|
||||
}
|
||||
|
||||
export async function apiPatch<T>(path: string, body?: object): Promise<T> {
|
||||
const response = await fetch(apiUrl(path), {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
return parseResponse<T>(response);
|
||||
}
|
||||
|
||||
export async function apiDelete<T>(path: string): Promise<T> {
|
||||
const response = await fetch(apiUrl(path), {
|
||||
method: "DELETE",
|
||||
});
|
||||
return parseResponse<T>(response);
|
||||
}
|
||||
|
||||
export async function apiMultipart<T>(path: string, form: FormData): Promise<T> {
|
||||
const response = await fetch(apiUrl(path), {
|
||||
method: "POST",
|
||||
body: form,
|
||||
});
|
||||
return parseResponse<T>(response);
|
||||
}
|
||||
Reference in New Issue
Block a user