apisauce icon indicating copy to clipboard operation
apisauce copied to clipboard

In addRequestTransform's callback, request.baseURL has no value

Open zhima opened this issue 4 years ago • 5 comments
trafficstars

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);
})

zhima avatar May 10 '21 12:05 zhima

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 avatar May 10 '21 12:05 chakrihacker

@chakrihacker Thanks for you reply.

  1. If we can not access the baseURL of AxiosRequestConfig param, it should not have baseURL as a property shown.

  2. I tested with axios's axiosInstance.interceptors.request.use() method, in the requestInterceptorOnFulfilled callback, its AxiosRequestConfig param indeed have the baseURL value.

  3. 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?

zhima avatar May 11 '21 00:05 zhima

Feel free to raise a pr

chakrihacker avatar May 11 '21 02:05 chakrihacker

is there a workaround for this?

jjercx avatar Oct 14 '21 02:10 jjercx

Found it:

const api = create({
  baseURL: 'https://www.xxxxx.com',
});

api.addRequestTransform(request => {
  console.log('baseURL:' + api.getBaseURL() + ' requestURL:' + request.url);
})

jjercx avatar Oct 14 '21 02:10 jjercx