custome_own-package
Version:
> Centralized Core For Frontend Core Modules
85 lines (75 loc) • 2.03 kB
JavaScript
import baseApi from './baseApiInstance';
export function apiServiceErrorHandler(error) {
let customError = {};
if (error instanceof AxiosError) {
customError['data'] = error?.response?.data;
customError['status'] = error?.response?.status;
customError['request-url'] = error?.response?.config?.url;
} else {
customError = error;
}
console.error(`Api Service Failed`, customError);
}
export function apiServiceResponseHandler(res) {
//Logic can be added here to handle api responses, if required!
return res;
}
const get = async (url, config) => {
try {
const res = await baseApi.get(url, config);
return apiServiceResponseHandler(res);
} catch (error) {
apiServiceErrorHandler(error);
throw error;
}
};
const post = async (url, data, config) => {
try {
const res = await baseApi.post(url, data, config);
return apiServiceResponseHandler(res);
} catch (error) {
apiServiceErrorHandler(error);
throw error;
}
};
const uploadFile = async (url, file) => {
try {
const res = await baseApi.post(url, file, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
return apiServiceResponseHandler(res);
} catch (error) {
apiServiceErrorHandler(error);
throw error;
}
};
const put = async (url, data, config) => {
try {
const res = await baseApi.put(url, data, config);
return apiServiceResponseHandler(res);
} catch (error) {
apiServiceErrorHandler(error);
throw error;
}
};
const __delete = async (url, config) => {
try {
const res = await baseApi.delete(url, config);
return apiServiceResponseHandler(res);
} catch (error) {
apiServiceErrorHandler(error);
throw error;
}
};
const patch = async (url, data, config) => {
try {
const res = await baseApi.patch(url, data, config);
return apiServiceResponseHandler(res);
} catch (error) {
apiServiceErrorHandler(error);
throw error;
}
};
export { get, post, put, __delete, uploadFile, patch };