63 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-08-31 14:28:35 +02:00
import axios from "axios";
import { Requests, Responses, UserRole } from "../dto";
2022-08-25 23:42:12 +02:00
export { Requests, Responses, UserRole };
export const post = <T extends Requests.BaseRequest>(url: string, data: T) =>
2022-08-31 14:28:35 +02:00
axios
.post(url, data, {
headers: { "Content-type": "application/json" },
})
.then((res) => res.data)
.catch((err) => err.response.data);
2022-08-25 15:13:44 +02:00
export const post_token = <T extends Requests.BaseRequest>(
2022-08-31 14:28:35 +02:00
url: string,
data: T,
token: string
2022-08-25 15:13:44 +02:00
) =>
2022-08-31 14:28:35 +02:00
axios
.post(url, data, {
headers: {
Authorization: "Bearer " + token,
"Content-type": "application/json",
},
})
.then((res) => res.data)
.catch((err) => err.response.data);
export const post_token_form = (
2022-08-31 14:28:35 +02:00
url: string,
data: FormData,
token: string,
onProgress: (progressEvent: ProgressEvent) => void
) =>
2022-08-31 14:28:35 +02:00
axios
.post(url, data, {
headers: {
Authorization: "Bearer " + token,
"Content-type": "multipart/form-data",
},
onUploadProgress: onProgress,
})
.then((res) => res.data)
.catch((err) => err.response.data);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const get = (url: string) =>
2022-08-31 14:28:35 +02:00
axios
.get(url)
.then((res) => res.data)
.catch((err) => err.response.data);
export const get_token = (url: string, token: string) =>
2022-08-31 14:28:35 +02:00
axios
.get(url, {
headers: { Authorization: "Bearer " + token },
})
.then((res) => res.data)
.catch((err) => err.response.data);
export const isErrorResponse = (
2022-08-31 14:28:35 +02:00
res: Responses.BaseResponse
): res is Responses.ErrorResponse => res.statusCode != 200;