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.

transformResponse docs misleading

See original GitHub issue

I’ve recently used transformRequest to get rid of the Authorization header. It took me a while to get the correct way to pass on the data. Documentation says:

 // `transformResponse` allows changes to the response data to be made before
 // it is passed to then/catch
 transformResponse: [function (data) {
    // Do whatever you want to transform the data
    return data;
 }],

but if you use this exact code the data are passed in a wrong format (a list with an object). At last I used:

transformResponse: [function (data) {
      delete headers.common.Authorization
      return JSON.stringify(data)
      return data;
 }],

I’m definitely a beginner in this specific field but I found the documentation misleading and if what I ended up doing is correct I’d appreciate the documentation to be updated.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

32reactions
mspidervcommented, Jun 4, 2019

@sandroden The following code overrides default axios transformResponse array of functions.

transformResponse: [function (data) {
  // something here ...
}],

If you want to add your own transformResponse function, then do not override the whole array. Just preppend your function to that array using this code:

transformRequest: [(data, headers) => {
  // Do whatever you want here, for example headers.Authorization = `Bearer ${jwt}`
  return data
}, ...axios.defaults.transformRequest]

The whole magic is in ...axios.defaults.transformRequest

10reactions
stefanozaninicommented, Feb 24, 2020

@sandroden The following code overrides default axios transformResponse array of functions.

transformResponse: [function (data) {
  // something here ...
}],

If you want to add your own transformResponse function, then do not override the whole array. Just preppend your function to that array using this code:

transformRequest: [(data, headers) => {
  // Do whatever you want here, for example headers.Authorization = `Bearer ${jwt}`
  return data
}, ...axios.defaults.transformRequest]

The whole magic is in ...axios.defaults.transformRequest

How can one do this in TypeScript? I get the following error:

Type 'AxiosTransformer | AxiosTransformer[] | undefined' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.ts(2569)

According to Axios types it doesn’t have to be an array.

Not the perfect solution, but i managed to bypass the error doing something like this:

transformRequest: [(data, headers) => {
  // Do whatever you want here, for example headers.Authorization = `Bearer ${jwt}`
  return data
}, ...(axios.defaults.transformRequest as AxiosTransformer[])]
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Toolkit Query - How to use transformResponse props ...
I have a root API where I am injecting endpoints. But when I try to add a transform function it doesn't work. From...
Read more >
draft-ietf-httpbis-cache-17
This document may contain material from IETF Documents or IETF Contributions ... The "no-transform" response directive indicates that an ...
Read more >
Deprecated List (Atlassian Confluence 7.3.5 API)
since 7.0 This query is redundant, misleading. ... since 6.10.0. Use PageResponseImpl.transformResponse(PageResponse, Function) instead.
Read more >
emmeans: Estimated Marginal Means, aka Least-Squares ...
CLDs are misleading because they visually group means with comparisons P > alpha as though they are equal, when in fact we have...
Read more >
What's New - Product Documentation Center
Dropping Support for SuSe Linux 12 SP2 ◾ Python 3.8 Syntax ◾ Required Upgrade to MSC Licensing Helium as of Adams 2020.0 ◾...
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