Refactorings, error handling and logout

This commit is contained in:
Dries Augustyns
2025-12-03 20:39:11 +01:00
parent 113b94cf2c
commit 382e97e5b2
12 changed files with 306 additions and 189 deletions
+16 -4
View File
@@ -11,6 +11,17 @@ interface TypedSchema extends ZodSchema {
_type: unknown;
}
interface ApiResponse {
message?: string;
error?: {
message?: string;
code?: string;
[key: string]: unknown;
};
[key: string]: unknown;
}
export class network {
/**
* Fetcher function that includes toast support
@@ -31,13 +42,14 @@ export class network {
credentials: 'include',
});
const res = await response.json();
const res = (await response.json()) as ApiResponse;
if (response.status >= 400) {
throw new Error(res?.message ?? 'Something went wrong!');
// Extract error message from standardized error response or fall back to direct message property
const errorMessage = res.error?.message ?? res.message ?? 'Something went wrong!';
throw new Error(errorMessage);
}
return res;
return res as T;
}
}