feat: ApiContext

This commit is contained in:
2026-06-02 13:17:58 +07:00
parent 25c9bc21b3
commit 1797af7ba9
8 changed files with 4444 additions and 4262 deletions
+56 -38
View File
@@ -1,3 +1,5 @@
// src/api/request.js
import { formatApiResponse } from '../utils/response';
import { oslogApi, apiGo } from './axiosClient';
@@ -10,31 +12,26 @@ const request = async ({
endpoint = '',
suffix = '',
data = null,
params = {},
headers = {},
customUrl = '',
axiosConfig = {},
axiosConfig = {}
}) => {
const url = customUrl || `${endpoint}${suffix}`;
try {
const client = isApiGo ? apiGo : oslogApi;
const response = await client({
method,
url,
data,
params,
headers,
...axiosConfig,
// highest priority
...axiosConfig
});
return formatApiResponse(response);
} catch (error) {
const responseError = formatApiResponse(error);
//logError(`at endpoint: ${url} error: `, responseError);
return responseError;
return formatApiResponse(error);
}
};
@@ -42,65 +39,86 @@ const request = async ({
* CREATE NEW
* POST /endpoint/new
*/
export const newRequest = (endpoint, data = null, config = {}) =>
export const newRequest = (
endpoint,
data = null,
axiosConfig = {}
) =>
request({
method: 'post',
endpoint,
suffix: '/new',
data,
...config,
axiosConfig
});
/**
* ADD
* POST /endpoint/add
*/
export const addRequest = (endpoint, data = null, config = {}) =>
export const addRequest = (
endpoint,
data = null,
axiosConfig = {}
) =>
request({
method: 'post',
endpoint,
suffix: '/add',
data,
...config,
axiosConfig
});
/**
* GET BY ID
* GET /endpoint/:id
*/
export const getByIdRequest = (id, endpoint, params = {}, config = {}) =>
export const getByIdRequest = (
id,
endpoint,
axiosConfig = {}
) =>
request({
method: 'get',
endpoint,
suffix: `/${id}`,
params,
...config,
axiosConfig
});
/**
* EDIT
* PUT /endpoint/edit/:id
*/
export const editRequest = (id, endpoint, data = null, config = {}) =>
export const editRequest = (
id,
endpoint,
data = null,
axiosConfig = {}
) =>
request({
method: 'put',
endpoint,
suffix: `/edit/${id}`,
data,
...config,
axiosConfig
});
/**
* DELETE
* DELETE /endpoint/delete/:id
*/
export const deleteRequest = (id, endpoint, data = null, config = {}) =>
export const deleteRequest = (
id,
endpoint,
data = null,
axiosConfig = {}
) =>
request({
method: 'delete',
endpoint,
suffix: `/delete/${id}`,
data,
...config,
axiosConfig
});
/**
@@ -110,8 +128,7 @@ export const deleteRequest = (id, endpoint, data = null, config = {}) =>
export const searchRequest = (
endpoint,
data = null,
params = {},
config = {},
axiosConfig = {}
) => {
const timezone = -new Date().getTimezoneOffset() / 60;
@@ -120,37 +137,38 @@ export const searchRequest = (
endpoint,
suffix: '/search',
data,
params: {
tz: timezone,
...params,
},
...config,
axiosConfig: {
...axiosConfig,
params: {
tz: timezone,
...(axiosConfig.params || {})
}
}
});
};
/**
* CUSTOM REQUEST
* untuk endpoint bebas / beda sendiri
*/
export const customRequest = ({
method = 'get',
url = '',
data = null,
params = {},
headers = {},
axiosConfig = {},
isApiGo = false,
axiosConfig = {}
}) =>
request({
method,
customUrl: url,
data,
params,
headers,
axiosConfig,
isApiGo,
axiosConfig
});
/**
* Optional Export Object
* EXPORT OBJECT
*/
export const apiRequest = {
new: newRequest,
@@ -159,5 +177,5 @@ export const apiRequest = {
edit: editRequest,
delete: deleteRequest,
search: searchRequest,
custom: customRequest,
custom: customRequest
};