apisauce
apisauce copied to clipboard
In addRequestTransform's callback, request.baseURL has no value
Below is my test code, as I run it in a react native project, the console log shows something like "baseURL: undefine requestURL:/xx/xxxx/xxxxx"
request.baseURL has no value, but I have set it when I create the api instance.
const api = create({
baseURL: 'https://www.xxxxx.com',
});
api.addRequestTransform(request => {
console.log('baseURL:' + request.baseURL + ' requestURL:' + request.url);
})
The transform functions has the axiosRequestConfig as param which means you can basically access data, url, method destructured
Example:
api.addRequestTransform(({data, url, method}) => {
// .. do something
})
So even though you have base url, you cannot access it
@chakrihacker Thanks for you reply.
-
If we can not access the baseURL of AxiosRequestConfig param, it should not have baseURL as a property shown.
-
I tested with axios's
axiosInstance.interceptors.request.use()method, in therequestInterceptorOnFulfilledcallback, its AxiosRequestConfig param indeed have the baseURL value. -
I have checked apisauce's source code, the AxiosRequestConfig param passed to addRequestTransform is basically the same config from get/post/... methods, maybe we should merge this config with api instance's config?
Feel free to raise a pr
is there a workaround for this?
Found it:
const api = create({
baseURL: 'https://www.xxxxx.com',
});
api.addRequestTransform(request => {
console.log('baseURL:' + api.getBaseURL() + ' requestURL:' + request.url);
})