Open selimb opened 1 year ago
I'd be happy to work on a PR.
Already have a proof-of-concept commit: https://github.com/selimb/axios/commit/58a5c71509c59b2acd2465cb5ba9aee29535fa88
Also just noticed docs for AxiosHeaders
has recently been committed. Should probably update those as well.
Currently, to check within an interceptor whether a specific response header is contained within, one has to do this:
axiosInstance.interceptors.response.use((response) => {
if (response.headers instanceof AxiosHeaders && response.headers.has('X-CSRF-Token')) {
//...
}
The instanceof check gets warned with Invalid 'instanceof' check: 'headers' has type that is not related to 'AxiosHeaders'
, but it is working nonetheless. Without this supposedly invalid instanceof check, it is impossible to do in TypeScript.
Describe the bug
AxiosResponse["headers"]
is typed asRawAxiosResponseHeaders | AxiosResponseHeaders
.AxiosResponseHeaders
?AxiosResponseHeaders
(more specifically an instance ofAxiosHeaders
), Typescript won't let me donew AxiosHeaders(response.headers)
, becauseRawAxiosResponseHeaders
is aPartial
.AxiosHeaders.get
claims to returnstring | string[] | number | boolean | AxiosHeaders | null
, forcing callers to deal with any of those types. In reality, isn'tstring | string[] | undefined
all you should expect?To Reproduce
https://codesandbox.io/s/axios-typescript-headers-issue-64vrrl?file=/src/index.ts
Code snippet
Expected behavior
AxiosResponse["headers"]
should always beAxiosHeaders
(fixes (1)), ornew AxiosHeaders(r.headers)
should be possible (fixes (2))AxiosHeaders.get
should be stricter than input types, sinceAxiosHeaders
normalizes inputs. Specifically,AxiosHeaders.get
should returnstring | string[] | undefined
.Axios Version
1.5.1.4
Adapter Version
No response
Browser
N/A
Browser Version
N/A
Node.js Version
16.15.1
OS
N/A
Additional Library Versions
Additional context/Screenshots