mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 11:01:45 +00:00
feat(dados-client): adotar react query em leituras do app
This commit is contained in:
26
src/shared/lib/fetch-json.ts
Normal file
26
src/shared/lib/fetch-json.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
export async function fetchJson<T>(
|
||||
input: RequestInfo | URL,
|
||||
init?: RequestInit,
|
||||
): Promise<T> {
|
||||
const response = await fetch(input, {
|
||||
cache: "no-store",
|
||||
...init,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
let message = `Erro na requisição (${response.status})`;
|
||||
|
||||
try {
|
||||
const payload = (await response.json()) as { error?: string };
|
||||
if (payload.error) {
|
||||
message = payload.error;
|
||||
}
|
||||
} catch {
|
||||
// noop
|
||||
}
|
||||
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
Reference in New Issue
Block a user