commerce/lib/shopify/ajax.ts
Kristian Duda 0f2485a010 wip
2024-06-28 12:23:21 +02:00

36 lines
874 B
TypeScript

interface ErrorMessage {
message: string;
}
export class AjaxError extends Error {
statusCode: number;
errors: ErrorMessage[];
constructor(statusCode: number, message: string, errors: ErrorMessage[] = []) {
super(message);
this.errors = errors;
this.statusCode = statusCode;
}
}
type AjaxMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
export const ajax = async <T>(method: AjaxMethod, url: string, data?: object): Promise<T> => {
const response = await fetch(url, {
method,
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
},
body: data ? JSON.stringify(data) : undefined
});
const body = await response.json();
if (response.ok) {
return body as T;
}
throw new AjaxError(response.status, body.message ?? response.statusText, body.errors);
};