gaxios icon indicating copy to clipboard operation
gaxios copied to clipboard

Interceptors

Open joseglego opened this issue 3 years ago • 1 comments

Is your feature request related to a problem? Please describe. When using gaxios in my project to connect to Google services, but also to third-party services. I'm connecting to APIS which uses different syntaxis for communication (for example, snake_case) and I'm using camelCase in my project. So, every time I do a request I have to decamelize (transform into snake_case) and when receiving the information I have to camelize it (transform into camelCase)

In general, I'd like to extend gaxios to handle all the requests. So, I don't have to have multiple libraries to do the same.

Describe the solution you'd like I have used axios previously, and the solution which I found in that tool was being able to create interceptors to responses, but also to requests:

// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  });

More info: https://github.com/axios/axios#interceptors Describe alternatives you've considered Create one small library/util which will work as a wrap to gaxios, which would process (camelize the data) but also decamelize the response.

Additional context I have found already this issue: https://github.com/googleapis/gaxios/issues/181 and read the implementation: https://github.com/googleapis/gaxios/pull/319/files

but what I noticed is, that adapters are ONLY working for response and not for requests.

joseglego avatar May 10 '22 15:05 joseglego

I have the same necessity

geroale avatar Aug 30 '22 11:08 geroale