question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for async transformRequest

See original GitHub issue

Hello, I have a feature request to this project. It would be great if you could add support for async transformRequest functions.

The goal I want to achieve is to add oauth2 token to the authorization header, but this token must be fetched from external service, and sometimes must be refreshed, so the function which manages it is asynchronous.

It’d be great if that code could work:

        const instance = axios.create({
            transformRequest: [async function (data, headers) {
                headers['Authorization'] = 'Bearer ' + await loginService.getToken()
            }],
        });

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:14
  • Comments:5

github_iconTop GitHub Comments

15reactions
baijunjiecommented, Oct 2, 2018

interceptors.request support for async function.

axios.interceptors.request.use(async function (options) {
    options.headers['Authorization'] = await getToken();
    return options;
}, function (error) {
    console.log('Request error: ', error);
    return Promise.reject(error);
});
0reactions
amoshydracommented, Dec 30, 2019

Looks like @baijunjie has already mentioned interceptor. Sorry, I didn’t read the thread properly…

original

Hi, I don’t think the proposed solution work. Not sure why this issue is closed though…

Looking at how transformRequest is used in transformData, returning a Promise inside transformRequest may yield incorrect result. https://github.com/axios/axios/blob/203cbc2da0ef0f68d517ba2a6176df1d48efc2fb/lib/core/transformData.js#L13-L20

I don’t think transformRequest allow async operation out of the box.

Alternative?

However, I think the requirements from both @aartek and @baijunjie can be fulfilled with Axios’ request interceptor?

axios.interceptors.request.use(async (config) => {
  config.headers['Authorization'] = 'Bearer ' + await loginService.getToken()
  return config;
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

$http.transformRequest with results from an asynchronous ...
I have an angular application that uses the $http service to fetch data from a remote server. Under certain circumstances I need to...
Read more >
Request and Response Transforms
Request trailers are not supported because they are not supported by the underlying HttpClient. Adding transforms. Transforms can be added to routes either ......
Read more >
xhr-async - npm
xhr-async supports request cancellation. You can abort an ongoing request, or abort a group of requests. Abort a single request. let xhrReq const ......
Read more >
Using MapLibre GL JS with Amazon Location Service
Building the application: Request transformation. MapLibre GL JS map instances include a transformRequest option, which you use to intercept and modify requests ...
Read more >
async function - JavaScript - MDN Web Docs - Mozilla
The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found