2019-06-23 03:40:53 +03:00
|
|
|
import { RootStore } from "./store";
|
2019-06-22 18:52:24 +03:00
|
|
|
const apiUrl = process.env.API_URL;
|
|
|
|
|
|
|
|
type QueryParamObject = { [key: string]: string | null };
|
|
|
|
|
2019-07-11 12:23:57 +03:00
|
|
|
export class ApiError extends Error {
|
2021-04-10 13:23:55 +03:00
|
|
|
public body: any;
|
|
|
|
public status: number;
|
|
|
|
public res: Response;
|
2019-07-11 12:23:57 +03:00
|
|
|
|
|
|
|
constructor(message: string, body: object, status: number, res: Response) {
|
|
|
|
super(message);
|
|
|
|
this.body = body;
|
|
|
|
this.status = status;
|
|
|
|
this.res = res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 18:52:24 +03:00
|
|
|
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] || "")}`)
|
2019-06-22 18:52:24 +03:00
|
|
|
.join("&")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function request(resource, fetchOpts: RequestInit = {}) {
|
2021-09-11 19:06:51 +03:00
|
|
|
return fetch(`${apiUrl}/${resource}`, fetchOpts).then(async (res) => {
|
2019-07-11 12:23:57 +03:00
|
|
|
if (!res.ok) {
|
2021-05-22 21:33:34 +03:00
|
|
|
if (res.status === 401) {
|
|
|
|
RootStore.dispatch("auth/expiredLogin");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-11 12:23:57 +03:00
|
|
|
const body = await res.json();
|
|
|
|
throw new ApiError(res.statusText, body, res.status, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json();
|
|
|
|
});
|
2019-06-22 18:52:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function get(resource: string, params: QueryParamObject = {}) {
|
2019-06-23 03:40:53 +03:00
|
|
|
const headers: Record<string, string> = RootStore.state.auth.apiKey
|
|
|
|
? { "X-Api-Key": RootStore.state.auth.apiKey }
|
|
|
|
: {};
|
2019-06-22 18:52:24 +03:00
|
|
|
return request(resource + buildQueryString(params), {
|
|
|
|
method: "GET",
|
|
|
|
headers,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function post(resource: string, params: QueryParamObject = {}) {
|
2019-06-23 03:40:53 +03:00
|
|
|
const headers: Record<string, string> = RootStore.state.auth.apiKey
|
|
|
|
? { "X-Api-Key": RootStore.state.auth.apiKey }
|
|
|
|
: {};
|
2019-07-11 12:23:57 +03:00
|
|
|
return request(resource, {
|
2019-06-22 18:52:24 +03:00
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(params),
|
|
|
|
headers: {
|
|
|
|
...headers,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-11-03 01:14:41 +02:00
|
|
|
|
|
|
|
type FormPostOpts = {
|
|
|
|
target?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function formPost(resource: string, body: Record<any, any> = {}, opts: FormPostOpts = {}) {
|
|
|
|
body["X-Api-Key"] = RootStore.state.auth.apiKey;
|
|
|
|
const form = document.createElement("form");
|
|
|
|
form.action = `${apiUrl}/${resource}`;
|
|
|
|
form.method = "POST";
|
|
|
|
form.enctype = "multipart/form-data";
|
|
|
|
if (opts.target != null) {
|
|
|
|
form.target = opts.target;
|
|
|
|
}
|
|
|
|
for (const [key, value] of Object.entries(body)) {
|
|
|
|
const input = document.createElement("input");
|
|
|
|
input.type = "hidden";
|
|
|
|
input.name = key;
|
|
|
|
input.value = value;
|
|
|
|
form.appendChild(input);
|
|
|
|
}
|
|
|
|
document.body.appendChild(form);
|
|
|
|
form.submit();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
document.body.removeChild(form);
|
|
|
|
}, 1);
|
|
|
|
}
|