2023-10-14 10:58:48 -05:00
|
|
|
export type BlitzResponse<Data extends object> =
|
2023-06-27 02:13:57 +00:00
|
|
|
| {
|
|
|
|
|
status: 'error';
|
|
|
|
|
error: { field: string; message: string; code: 402; value: null };
|
|
|
|
|
}
|
|
|
|
|
| {
|
|
|
|
|
status: 'ok';
|
|
|
|
|
data: Data;
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-14 10:58:48 -05:00
|
|
|
export default async function fetchBlitz<Data extends object>(url: string) {
|
2023-09-23 23:30:14 +00:00
|
|
|
const data = (await fetch(url).then((response) =>
|
|
|
|
|
response.json(),
|
2023-10-14 10:58:48 -05:00
|
|
|
)) as BlitzResponse<Data>;
|
2023-04-27 01:08:19 +00:00
|
|
|
|
2023-09-23 23:30:14 +00:00
|
|
|
if (data.status === 'ok') {
|
|
|
|
|
return data.data;
|
2023-05-08 01:45:57 +00:00
|
|
|
} else {
|
2023-10-17 20:58:36 -05:00
|
|
|
throw new Error(`Wargaming response error status:"${data.status}"`, {
|
|
|
|
|
cause: `Message: "${data.error.message}"\nURL: "${url}"`,
|
|
|
|
|
});
|
2023-05-08 01:45:57 +00:00
|
|
|
}
|
2023-04-27 00:21:39 +00:00
|
|
|
}
|