fix(users): repair create delete and duplicate email handling
This commit is contained in:
parent
155fdbb16a
commit
0bbcaba211
22 changed files with 773 additions and 107 deletions
|
|
@ -118,8 +118,22 @@ export type Paginated<T> = {
|
|||
total: number;
|
||||
page: number;
|
||||
page_size: number;
|
||||
pages?: number;
|
||||
};
|
||||
|
||||
async function readErrorMessage(response: Response, fallback: string) {
|
||||
const text = await response.text();
|
||||
if (!text) {
|
||||
return fallback;
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(text) as { detail?: string; message?: string };
|
||||
return parsed.detail ?? parsed.message ?? text;
|
||||
} catch {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
export async function login(email: string, password: string) {
|
||||
const response = await fetch(`/api/login`, {
|
||||
method: "POST",
|
||||
|
|
@ -155,8 +169,7 @@ export async function apiSend<T>(path: string, token: string, method: "POST" | "
|
|||
credentials: "include"
|
||||
});
|
||||
if (!response.ok) {
|
||||
const detail = await response.text();
|
||||
throw new Error(detail || `API request failed: ${response.status}`);
|
||||
throw new Error(await readErrorMessage(response, `API request failed: ${response.status}`));
|
||||
}
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
|
|
@ -167,7 +180,7 @@ export async function apiDelete(path: string, token: string): Promise<void> {
|
|||
credentials: "include"
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`API request failed: ${response.status}`);
|
||||
throw new Error(await readErrorMessage(response, `API request failed: ${response.status}`));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -178,7 +191,7 @@ export async function apiFetch<T>(path: string, token: string, init?: RequestIni
|
|||
credentials: "include"
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(await response.text());
|
||||
throw new Error(await readErrorMessage(response, `API request failed: ${response.status}`));
|
||||
}
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue