axios
axios copied to clipboard
Types are still broken with 1.2.4 for interceptors
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
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);
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?
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);
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.
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.
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.
@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>>
Having same issues. Any solution at the moment?
Until this is fixed, I added as any
where needed...
Until this is fixed, I added
as any
where needed...
some scheme
Can confirm after 1.2.2 is breaking change for typescript users.
Still broken version axios version 1.3.3
Try this
export function authorizationInterceptor(internalAxiosRequestConfig: InternalAxiosRequestConfig) {
internalAxiosRequestConfig.headers.set( "your-header-key-here", "yourheader-value-here");
return internalAxiosRequestConfig;
}
Still broken version axios version 1.3.4
Can confirm after 1.2.2 is breaking change for typescript users.
thank you, use 1.2.1, it works
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
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
Can confirm after 1.2.2 is breaking change for typescript users.
Still not working with TS after v1.4.0
Also not working with Axios v1.5.0 I am afraid.
use 'any' can work it !!!!!!!
Try this
export function authorizationInterceptor(internalAxiosRequestConfig: InternalAxiosRequestConfig) { internalAxiosRequestConfig.headers.set( "your-header-key-here", "yourheader-value-here"); return internalAxiosRequestConfig; }
This worked for me, thanks!
you can just downgrade your Axios version = "axios": "^0.24.0" ... it's working fine