feat input virtual
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import axios from 'axios';
|
||||
import { API_BASES } from '../config/constants';
|
||||
import { formatApiResponse } from '../utils/response';
|
||||
|
||||
const createClient = (baseURL, withBaseFormat = false) => {
|
||||
const client = axios.create({
|
||||
baseURL,
|
||||
headers: { 'Content-Type': 'application/json', Accept: 'application/json' }
|
||||
});
|
||||
|
||||
client.interceptors.request.use((config) => {
|
||||
const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null;
|
||||
if (token) {
|
||||
config.headers = config.headers ?? {};
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
return config;
|
||||
});
|
||||
|
||||
if (withBaseFormat) {
|
||||
client.interceptors.response.use(
|
||||
(response) =>
|
||||
formatApiResponse({
|
||||
status: response.status,
|
||||
data: response.data,
|
||||
message: response.data?.message ?? response.statusText
|
||||
}),
|
||||
(error) => Promise.resolve(formatApiResponse(error))
|
||||
);
|
||||
}
|
||||
|
||||
return client;
|
||||
};
|
||||
|
||||
export const oslogApi = createClient(API_BASES.OSLOG_API, true);
|
||||
export const apiGo = createClient(API_BASES.API_GO, true);
|
||||
export const apiPhp = createClient(API_BASES.API_PHP);
|
||||
export const apiDashboard = createClient(API_BASES.DASHBOARD_API_OSLOG_PRO);
|
||||
export const apiHowen = createClient(API_BASES.HOWEN_API);
|
||||
export const apiNominatim = createClient(API_BASES.API_NOMINATIM);
|
||||
export const apiVss = createClient(API_BASES.VSS);
|
||||
@@ -0,0 +1,163 @@
|
||||
import { formatApiResponse } from '../utils/response';
|
||||
import { oslogApi, apiGo } from './axiosClient';
|
||||
|
||||
/**
|
||||
* Generic Request Helper
|
||||
*/
|
||||
const request = async ({
|
||||
isApiGo = false,
|
||||
method = 'get',
|
||||
endpoint = '',
|
||||
suffix = '',
|
||||
data = null,
|
||||
params = {},
|
||||
headers = {},
|
||||
customUrl = '',
|
||||
axiosConfig = {},
|
||||
}) => {
|
||||
const url = customUrl || `${endpoint}${suffix}`;
|
||||
|
||||
try {
|
||||
const client = isApiGo ? apiGo : oslogApi;
|
||||
const response = await client({
|
||||
method,
|
||||
url,
|
||||
data,
|
||||
params,
|
||||
headers,
|
||||
...axiosConfig,
|
||||
});
|
||||
|
||||
return formatApiResponse(response);
|
||||
} catch (error) {
|
||||
const responseError = formatApiResponse(error);
|
||||
|
||||
//logError(`at endpoint: ${url} error: `, responseError);
|
||||
|
||||
return responseError;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* CREATE NEW
|
||||
* POST /endpoint/new
|
||||
*/
|
||||
export const newRequest = (endpoint, data = null, config = {}) =>
|
||||
request({
|
||||
method: 'post',
|
||||
endpoint,
|
||||
suffix: '/new',
|
||||
data,
|
||||
...config,
|
||||
});
|
||||
|
||||
/**
|
||||
* ADD
|
||||
* POST /endpoint/add
|
||||
*/
|
||||
export const addRequest = (endpoint, data = null, config = {}) =>
|
||||
request({
|
||||
method: 'post',
|
||||
endpoint,
|
||||
suffix: '/add',
|
||||
data,
|
||||
...config,
|
||||
});
|
||||
|
||||
/**
|
||||
* GET BY ID
|
||||
* GET /endpoint/:id
|
||||
*/
|
||||
export const getByIdRequest = (id, endpoint, params = {}, config = {}) =>
|
||||
request({
|
||||
method: 'get',
|
||||
endpoint,
|
||||
suffix: `/${id}`,
|
||||
params,
|
||||
...config,
|
||||
});
|
||||
|
||||
/**
|
||||
* EDIT
|
||||
* PUT /endpoint/edit/:id
|
||||
*/
|
||||
export const editRequest = (id, endpoint, data = null, config = {}) =>
|
||||
request({
|
||||
method: 'put',
|
||||
endpoint,
|
||||
suffix: `/edit/${id}`,
|
||||
data,
|
||||
...config,
|
||||
});
|
||||
|
||||
/**
|
||||
* DELETE
|
||||
* DELETE /endpoint/delete/:id
|
||||
*/
|
||||
export const deleteRequest = (id, endpoint, data = null, config = {}) =>
|
||||
request({
|
||||
method: 'delete',
|
||||
endpoint,
|
||||
suffix: `/delete/${id}`,
|
||||
data,
|
||||
...config,
|
||||
});
|
||||
|
||||
/**
|
||||
* SEARCH
|
||||
* POST /endpoint/search
|
||||
*/
|
||||
export const searchRequest = (
|
||||
endpoint,
|
||||
data = null,
|
||||
params = {},
|
||||
config = {},
|
||||
) => {
|
||||
const timezone = -new Date().getTimezoneOffset() / 60;
|
||||
|
||||
return request({
|
||||
method: 'post',
|
||||
endpoint,
|
||||
suffix: '/search',
|
||||
data,
|
||||
params: {
|
||||
tz: timezone,
|
||||
...params,
|
||||
},
|
||||
...config,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* CUSTOM REQUEST
|
||||
* untuk endpoint bebas / beda sendiri
|
||||
*/
|
||||
export const customRequest = ({
|
||||
method = 'get',
|
||||
url = '',
|
||||
data = null,
|
||||
params = {},
|
||||
headers = {},
|
||||
axiosConfig = {},
|
||||
}) =>
|
||||
request({
|
||||
method,
|
||||
customUrl: url,
|
||||
data,
|
||||
params,
|
||||
headers,
|
||||
axiosConfig,
|
||||
});
|
||||
|
||||
/**
|
||||
* Optional Export Object
|
||||
*/
|
||||
export const apiRequest = {
|
||||
new: newRequest,
|
||||
add: addRequest,
|
||||
getById: getByIdRequest,
|
||||
edit: editRequest,
|
||||
delete: deleteRequest,
|
||||
search: searchRequest,
|
||||
custom: customRequest,
|
||||
};
|
||||
Reference in New Issue
Block a user