53 lines
1.7 KiB
JavaScript
53 lines
1.7 KiB
JavaScript
import { callLogs } from '../../utils/logs.js';
|
|
import EP from './endpoints.js';
|
|
import axios from 'axios';
|
|
|
|
export default async function API(endpoint, method, headers, params, body, onSuccess, onError, subject, action) {
|
|
try {
|
|
if(method === "post") {
|
|
let url = EP[endpoint];
|
|
let head = {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `${headers.token}`
|
|
}
|
|
}
|
|
|
|
let request = await axios.post(url, body, head);
|
|
|
|
if(request) {
|
|
if(request?.data?.status === 0) {
|
|
callLogs(request.data, "error", "frontend", subject, action)
|
|
} else {
|
|
callLogs(request.data, "success", "frontend", subject, action)
|
|
}
|
|
|
|
onSuccess(request.data);
|
|
}
|
|
} else if(method === "get") {
|
|
let url = EP[endpoint];
|
|
let head = {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `${headers.token}`
|
|
}
|
|
}
|
|
|
|
let request = await axios.get(url, head);
|
|
|
|
if(request) {
|
|
if(request?.data?.status === 0) {
|
|
callLogs(request.data, "error", "frontend", subject, action);
|
|
} else {
|
|
console.log(request.data);
|
|
callLogs(request.data, "success", "frontend", subject, action)
|
|
}
|
|
|
|
onSuccess(request.data);
|
|
}
|
|
}
|
|
}catch(err) {
|
|
callLogs(err, "error", "frontend", subject, action);
|
|
onError(err)
|
|
}
|
|
}
|