zappyzep/dashboard/src/api.ts

69 lines
1.7 KiB
TypeScript
Raw Normal View History

import { RootStore } from "./store";
const apiUrl = process.env.API_URL;
type QueryParamObject = { [key: string]: string | null };
export class ApiError extends Error {
public body: any;
public status: number;
public res: Response;
constructor(message: string, body: object, status: number, res: Response) {
super(message);
this.body = body;
this.status = status;
this.res = res;
}
}
function buildQueryString(params: QueryParamObject) {
if (Object.keys(params).length === 0) return "";
return (
"?" +
Array.from(Object.entries(params))
2021-09-11 19:06:51 +03:00
.map((pair) => `${encodeURIComponent(pair[0])}=${encodeURIComponent(pair[1] || "")}`)
.join("&")
);
}
export function request(resource, fetchOpts: RequestInit = {}) {
2021-09-11 19:06:51 +03:00
return fetch(`${apiUrl}/${resource}`, fetchOpts).then(async (res) => {
if (!res.ok) {
if (res.status === 401) {
RootStore.dispatch("auth/expiredLogin");
return;
}
const body = await res.json();
throw new ApiError(res.statusText, body, res.status, res);
}
return res.json();
});
}
export function get(resource: string, params: QueryParamObject = {}) {
const headers: Record<string, string> = RootStore.state.auth.apiKey
? { "X-Api-Key": RootStore.state.auth.apiKey }
: {};
return request(resource + buildQueryString(params), {
method: "GET",
headers,
});
}
export function post(resource: string, params: QueryParamObject = {}) {
const headers: Record<string, string> = RootStore.state.auth.apiKey
? { "X-Api-Key": RootStore.state.auth.apiKey }
: {};
return request(resource, {
method: "POST",
body: JSON.stringify(params),
headers: {
...headers,
"Content-Type": "application/json",
},
});
}