zenstack/packages/plugins/tanstack-query/res/shared.ts

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;
}
}