axios icon indicating copy to clipboard operation
axios copied to clipboard

Types are still broken with 1.2.4 for interceptors

Open orgads opened this issue 1 year ago • 22 comments

Describe the bug

Assigning custom interceptors that accept and return AxiosRequestConfig works with 1.2.2, but breaks with 1.2.4.

This is used by e.g. https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/axios-token-interceptor, and also in user code.

Code snippet

axios.interceptors.request.use((config: AxiosRequestConfig) => Promise.resolve(config));

Error message

error TS2345: Argument of type '(config: AxiosRequestConfig) => Promise<AxiosRequestConfig<any>>' is not assignable to parameter of type '(value: InternalAxiosRequestConfig<any>) => InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>'.
  Type 'Promise<AxiosRequestConfig<any>>' is not assignable to type 'InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>'.

150         axios.interceptors.request.use((config: AxiosRequestConfig) => Promise.resolve(config));
                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


### Expected behavior

It should not break, this is a minor upgrade.

### Axios Version

1.2.4

### Adapter Version

_No response_

### Browser

_No response_

### Browser Version

_No response_

### Node.js Version

18.12.1

### OS

_No response_

### Additional Library Versions

```bash
Typescript

Additional context/Screenshots

No response

orgads avatar Jan 24 '23 20:01 orgads

Config inside interceptors has a different interface. If you explicitly specify the config type, use InternalAxiosRequestConfig instead of AxiosRequestConfig. AxiosRequestConfig is the external/raw config interface.

axios.interceptors.request.use(async (config: InternalAxiosRequestConfig) => {

});

const config: AxiosRequestConfig = {};

axios(config);

DigitalBrainJS avatar Jan 24 '23 21:01 DigitalBrainJS

Can you suggest a fix for https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/axios-token-interceptor too? Is it possible to make it compatible with both versions?

orgads avatar Jan 24 '23 21:01 orgads

Not only the types, the implementation is here. Will it work with latest axios? Is it still possible to assign a header like it is done there?

requestConfig.headers[header] = headerFormatter(token);

orgads avatar Jan 24 '23 21:01 orgads

Most likely though this package is deprecated as it depends on Axios 0.x.

"axios": "^0.19.2",

The incompatibility is only in the TypeScript types.

DigitalBrainJS avatar Jan 24 '23 21:01 DigitalBrainJS

This is in devDependencies. We use it and it works fine with axios v1.

I'll propose an update to the types package tomorrow. Thank you.

orgads avatar Jan 24 '23 21:01 orgads

I already mentioned it in #5476 for 1.2.3 (see here) and the problem still persists in version 1.2.4: the headers of the config in AxiosResponse are not optional anymore (as it was in 1.2.1). In 1.2.4, the config has the type InternalAxiosRequestConfig (in code) which is still a breaking change. It affects then also the interceptors as described in the first post.

VGalata avatar Jan 25 '23 08:01 VGalata

@DigitalBrainJS it's harder than I expected. Pushed my changes to https://github.com/DefinitelyTyped/DefinitelyTyped/pull/64075, but the test fails.

This is the test:

const provider = tokenProvider(validOptions1); // $ExpectType TokenProvider
const oldConfig: AxiosRequestConfig = {
    headers: {}
};
const newConfig: InternalAxiosRequestConfig = {
    headers: new AxiosHeaders()
};
provider(oldConfig); // $ExpectType Promise<AxiosRequestConfig<any>>
provider(newConfig); // $ExpectType Promise<InternalAxiosRequestConfig<any>>

and this is the error:

ERROR: 32:1  expect  [email protected] expected type to be:
  Promise<InternalAxiosRequestConfig>
got:
  Promise<AxiosRequestConfig<any>>

orgads avatar Jan 25 '23 09:01 orgads

Having same issues. Any solution at the moment?

crypt096 avatar Feb 01 '23 11:02 crypt096

Until this is fixed, I added as any where needed...

orgads avatar Feb 01 '23 12:02 orgads

Until this is fixed, I added as any where needed...

some scheme

AgoniNemo avatar Feb 06 '23 08:02 AgoniNemo

Can confirm after 1.2.2 is breaking change for typescript users.

PerfectionVR avatar Feb 06 '23 14:02 PerfectionVR

Still broken version axios version 1.3.3

Xefexium avatar Feb 22 '23 01:02 Xefexium

Try this

export function authorizationInterceptor(internalAxiosRequestConfig: InternalAxiosRequestConfig) {
    internalAxiosRequestConfig.headers.set( "your-header-key-here", "yourheader-value-here");
    return internalAxiosRequestConfig;
}

isawk avatar Feb 27 '23 02:02 isawk

Still broken version axios version 1.3.4

CharleeWa avatar Mar 08 '23 02:03 CharleeWa

Can confirm after 1.2.2 is breaking change for typescript users.

thank you, use 1.2.1, it works

YR2368044 avatar Mar 21 '23 11:03 YR2368044

The interceptor signature seems to have changed, fix by using header.set( .. , .. ) works

On Tue, 21 Mar 2023 at 13:36, YR @.***> wrote:

Can confirm after 1.2.2 is breaking change for typescript users.

thank you, use 1.2.1, it works

— Reply to this email directly, view it on GitHub https://github.com/axios/axios/issues/5494#issuecomment-1477682764, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFANXKKJVRO7NHY5U7QMMP3W5GHC5ANCNFSM6AAAAAAUFRTVYU . You are receiving this because you commented.Message ID: @.***>

-- Kwasi Owusu Gyasi-Agyei @.*** \ +27 (81) 466 - 4488

isawk avatar Mar 22 '23 06:03 isawk

Can confirm after 1.2.2 is breaking change for typescript users.

thank you, use 1.2.1, it works

for me works version 1.2.1 does not work only the 0.19.2 works see here

andreas-kondylis-eli avatar Mar 29 '23 23:03 andreas-kondylis-eli

Can confirm after 1.2.2 is breaking change for typescript users.

Still not working with TS after v1.4.0

AnnaMadsenKnowit avatar Jul 13 '23 13:07 AnnaMadsenKnowit

Also not working with Axios v1.5.0 I am afraid.

JanMosigItemis avatar Aug 28 '23 08:08 JanMosigItemis

use 'any' can work it !!!!!!!

yerennpm avatar Sep 05 '23 08:09 yerennpm

Try this

export function authorizationInterceptor(internalAxiosRequestConfig: InternalAxiosRequestConfig) {
    internalAxiosRequestConfig.headers.set( "your-header-key-here", "yourheader-value-here");
    return internalAxiosRequestConfig;
}

This worked for me, thanks!

NickAlvesX avatar Oct 11 '23 23:10 NickAlvesX

you can just downgrade your Axios version = "axios": "^0.24.0" ... it's working fine

ahmedkhaled4d avatar Feb 06 '24 16:02 ahmedkhaled4d