mirror of
https://github.com/zenstackhq/zenstack
synced 2026-05-24 10:08:55 +00:00
36 lines
885 B
TypeScript
36 lines
885 B
TypeScript
/**
|
|
* The default query endpoint.
|
|
*/
|
|
export const DEFAULT_QUERY_ENDPOINT = '/api/model';
|
|
|
|
/**
|
|
* Prefix for react-query keys.
|
|
*/
|
|
export const QUERY_KEY_PREFIX = 'zenstack:';
|
|
|
|
/**
|
|
* Context type for configuring the hooks.
|
|
*/
|
|
export type RequestHandlerContext = {
|
|
endpoint: string;
|
|
};
|
|
|
|
async function fetcher<R>(url: string, options?: RequestInit) {
|
|
const res = await fetch(url, options);
|
|
if (!res.ok) {
|
|
const error: Error & { info?: unknown; status?: number } = new Error(
|
|
'An error occurred while fetching the data.'
|
|
);
|
|
error.info = unmarshal(await res.text());
|
|
error.status = res.status;
|
|
throw error;
|
|
}
|
|
|
|
const textResult = await res.text();
|
|
try {
|
|
return unmarshal(textResult) as R;
|
|
} catch (err) {
|
|
console.error(`Unable to deserialize data:`, textResult);
|
|
throw err;
|
|
}
|
|
}
|