2022-05-05 16:31:10 +03:00
|
|
|
import { getItem } from "./storage";
|
2022-04-14 02:12:38 +03:00
|
|
|
|
|
|
|
export default async function(method, endpoint, auth=true, body=null) {
|
|
|
|
const options = {
|
|
|
|
method,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (body) {
|
2022-04-20 03:14:28 +03:00
|
|
|
options.body = JSON.stringify(body);
|
2022-04-14 02:12:38 +03:00
|
|
|
options.headers = {
|
|
|
|
...options.headers || {},
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auth) {
|
2022-05-05 21:30:21 +03:00
|
|
|
const token = getItem("auth:token");
|
2022-04-14 02:12:38 +03:00
|
|
|
if (token) {
|
|
|
|
options.headers = {
|
|
|
|
...options.headers || {},
|
|
|
|
"Authorization": `Bearer ${token}`
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await fetch(endpoint, options);
|
|
|
|
return {
|
|
|
|
success: true,
|
2022-04-26 23:57:06 +03:00
|
|
|
json: res.status === 204 ? null : await res.json(),
|
2022-04-14 02:12:38 +03:00
|
|
|
ok: res.ok,
|
|
|
|
status: res.status
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
json: null,
|
2022-04-19 21:21:55 +03:00
|
|
|
ok: false,
|
|
|
|
status: null
|
2022-04-14 02:12:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|