181 lines
2.6 KiB
JavaScript
181 lines
2.6 KiB
JavaScript
// src/api/request.js
|
|
|
|
import { formatApiResponse } from '../utils/response';
|
|
import { oslogApi, apiGo } from './axiosClient';
|
|
|
|
/**
|
|
* Generic Request Helper
|
|
*/
|
|
const request = async ({
|
|
isApiGo = false,
|
|
method = 'get',
|
|
endpoint = '',
|
|
suffix = '',
|
|
data = null,
|
|
customUrl = '',
|
|
axiosConfig = {}
|
|
}) => {
|
|
const url = customUrl || `${endpoint}${suffix}`;
|
|
|
|
try {
|
|
const client = isApiGo ? apiGo : oslogApi;
|
|
|
|
const response = await client({
|
|
method,
|
|
url,
|
|
data,
|
|
|
|
// highest priority
|
|
...axiosConfig
|
|
});
|
|
|
|
return response
|
|
} catch (error) {
|
|
return formatApiResponse(error);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* CREATE NEW
|
|
* POST /endpoint/new
|
|
*/
|
|
export const newRequest = (
|
|
endpoint,
|
|
data = null,
|
|
axiosConfig = {}
|
|
) =>
|
|
request({
|
|
method: 'post',
|
|
endpoint,
|
|
suffix: '/new',
|
|
data,
|
|
axiosConfig
|
|
});
|
|
|
|
/**
|
|
* ADD
|
|
* POST /endpoint/add
|
|
*/
|
|
export const addRequest = (
|
|
endpoint,
|
|
data = null,
|
|
axiosConfig = {}
|
|
) =>
|
|
request({
|
|
method: 'post',
|
|
endpoint,
|
|
suffix: '/add',
|
|
data,
|
|
axiosConfig
|
|
});
|
|
|
|
/**
|
|
* GET BY ID
|
|
* GET /endpoint/:id
|
|
*/
|
|
export const getByIdRequest = (
|
|
id,
|
|
endpoint,
|
|
axiosConfig = {}
|
|
) =>
|
|
request({
|
|
method: 'get',
|
|
endpoint,
|
|
suffix: `/${id}`,
|
|
axiosConfig
|
|
});
|
|
|
|
/**
|
|
* EDIT
|
|
* PUT /endpoint/edit/:id
|
|
*/
|
|
export const editRequest = (
|
|
id,
|
|
endpoint,
|
|
data = null,
|
|
axiosConfig = {}
|
|
) =>
|
|
request({
|
|
method: 'put',
|
|
endpoint,
|
|
suffix: `/edit/${id}`,
|
|
data,
|
|
axiosConfig
|
|
});
|
|
|
|
/**
|
|
* DELETE
|
|
* DELETE /endpoint/delete/:id
|
|
*/
|
|
export const deleteRequest = (
|
|
id,
|
|
endpoint,
|
|
data = null,
|
|
axiosConfig = {}
|
|
) =>
|
|
request({
|
|
method: 'delete',
|
|
endpoint,
|
|
suffix: `/delete/${id}`,
|
|
data,
|
|
axiosConfig
|
|
});
|
|
|
|
/**
|
|
* SEARCH
|
|
* POST /endpoint/search
|
|
*/
|
|
export const searchRequest = (
|
|
endpoint,
|
|
data = null,
|
|
axiosConfig = {}
|
|
) => {
|
|
const timezone = -new Date().getTimezoneOffset() / 60;
|
|
|
|
return request({
|
|
method: 'post',
|
|
endpoint,
|
|
suffix: '/search',
|
|
data,
|
|
|
|
axiosConfig: {
|
|
...axiosConfig,
|
|
|
|
params: {
|
|
tz: timezone,
|
|
...(axiosConfig.params || {})
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
/**
|
|
* CUSTOM REQUEST
|
|
*/
|
|
export const customRequest = ({
|
|
method = 'get',
|
|
url = '',
|
|
data = null,
|
|
isApiGo = false,
|
|
axiosConfig = {}
|
|
}) =>
|
|
request({
|
|
method,
|
|
customUrl: url,
|
|
data,
|
|
isApiGo,
|
|
axiosConfig
|
|
});
|
|
|
|
/**
|
|
* EXPORT OBJECT
|
|
*/
|
|
export const apiRequest = {
|
|
new: newRequest,
|
|
add: addRequest,
|
|
getById: getByIdRequest,
|
|
edit: editRequest,
|
|
delete: deleteRequest,
|
|
search: searchRequest,
|
|
custom: customRequest
|
|
}; |