Add customer registration and deployment updates
This commit is contained in:
@@ -1,9 +1,56 @@
|
||||
const API_ROOT = import.meta.env.VITE_API_URL ?? "http://localhost:8090/api";
|
||||
import { USER_STORAGE_KEY } from "./storage";
|
||||
|
||||
const API_ROOT = import.meta.env.VITE_API_URL ?? (import.meta.env.DEV ? "http://localhost:8090/api" : "/api");
|
||||
|
||||
type ApiErrorPayload = {
|
||||
message?: string;
|
||||
error?: string;
|
||||
detail?: string;
|
||||
};
|
||||
|
||||
async function readErrorMessage(response: Response): Promise<string> {
|
||||
const contentType = response.headers.get("content-type") ?? "";
|
||||
|
||||
if (contentType.includes("application/json")) {
|
||||
const payload = (await response.json()) as ApiErrorPayload;
|
||||
const message = payload.message?.trim() || payload.detail?.trim();
|
||||
if (message) {
|
||||
return message;
|
||||
}
|
||||
if (payload.error?.trim()) {
|
||||
return `Anfrage fehlgeschlagen: ${payload.error.trim()}`;
|
||||
}
|
||||
}
|
||||
|
||||
const text = (await response.text()).trim();
|
||||
if (text) {
|
||||
return text;
|
||||
}
|
||||
|
||||
return `Anfrage fehlgeschlagen (${response.status})`;
|
||||
}
|
||||
|
||||
function actorHeaders(): Record<string, string> {
|
||||
if (typeof window === "undefined") {
|
||||
return {};
|
||||
}
|
||||
|
||||
const rawUser = window.localStorage.getItem(USER_STORAGE_KEY);
|
||||
if (!rawUser) {
|
||||
return {};
|
||||
}
|
||||
|
||||
try {
|
||||
const user = JSON.parse(rawUser) as { id?: string | null };
|
||||
return user.id ? { "X-MUH-Actor-Id": user.id } : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
throw new Error(text || "Unbekannter API-Fehler");
|
||||
throw new Error(await readErrorMessage(response));
|
||||
}
|
||||
if (response.status === 204) {
|
||||
return undefined as T;
|
||||
@@ -12,14 +59,21 @@ async function handleResponse<T>(response: Response): Promise<T> {
|
||||
}
|
||||
|
||||
export async function apiGet<T>(path: string): Promise<T> {
|
||||
return handleResponse<T>(await fetch(`${API_ROOT}${path}`));
|
||||
return handleResponse<T>(
|
||||
await fetch(`${API_ROOT}${path}`, {
|
||||
headers: actorHeaders(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function apiPost<T>(path: string, body: unknown): Promise<T> {
|
||||
return handleResponse<T>(
|
||||
await fetch(`${API_ROOT}${path}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...actorHeaders(),
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
}),
|
||||
);
|
||||
@@ -29,7 +83,10 @@ export async function apiPut<T>(path: string, body: unknown): Promise<T> {
|
||||
return handleResponse<T>(
|
||||
await fetch(`${API_ROOT}${path}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...actorHeaders(),
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
}),
|
||||
);
|
||||
@@ -39,7 +96,10 @@ export async function apiPatch<T>(path: string, body: unknown): Promise<T> {
|
||||
return handleResponse<T>(
|
||||
await fetch(`${API_ROOT}${path}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...actorHeaders(),
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
}),
|
||||
);
|
||||
@@ -49,6 +109,7 @@ export async function apiDelete(path: string): Promise<void> {
|
||||
await handleResponse<void>(
|
||||
await fetch(`${API_ROOT}${path}`, {
|
||||
method: "DELETE",
|
||||
headers: actorHeaders(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user