daddy/frontend/src/util/daddy.ts

22 lines
503 B
TypeScript

export class UnauthorizedError extends Error {
constructor(...args: any[]) {
super(...args)
Object.setPrototypeOf(this, UnauthorizedError.prototype);
}
}
export class DaddyClient {
assertOk(res: any) {
if (!res.ok) return Promise.reject(new Error('Request failed'));
return res;
}
assertAuthorization(res: any) {
if (res.status === 401 || res.status === 404) return Promise.reject(new UnauthorizedError());
return res;
}
}
export const daddy = new DaddyClient();