Commit 43a4720a by G

add generic to the function for ts type inference of response

parent 2ef5f12d
import axios, { AxiosError, AxiosResponse } from 'axios';
const baseUrl = process.env.EXPO_PUBLIC_API_URL;
const client = axios.create({baseURL : baseUrl});
const axiosRequest = async<T> ({...options}) : Promise<T> => {
// console.log("base Url", baseUrl);
client.defaults.headers.common.Authorization = `Bearer ${""}`;
// client.defaults.headers.common['Content-Type'] = 'application/json';
// console.log("client default", client.defaults);
// console.log("client datas", client.defaults.data);
console.log("options", options);
const onSuccess = (response : T) => response;
const onError = (error : AxiosError) => {
console.log('request error', error);
throw error;
};
try {
const response : AxiosResponse<T> = await client({ ...options, method: "POST" });
return onSuccess(response.data);
} catch (error : any) {
console.error("Axios Request Error :: ", error.response.data);
return onError(error);
}
}
export default axiosRequest;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment