-
-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(types): allow to specify partial default headers for instance cre… #4185
Conversation
I think the type RequestHeaders = Record<string, string> | {
common?: Record<string, string>;
delete?: Record<string, string>;
get?: Record<string, string>;
head?: Record<string, string>;
link?: Record<string, string>;
options?: Record<string, string>;
patch?: Record<string, string>;
post?: Record<string, string>;
purge?: Record<string, string>;
put?: Record<string, string>;
unlink?: Record<string, string>;
} Then I just tried this and it appears to correctly reflect the runtime behaviour regardless if the headers are set as |
I'm not sure |
I see what you’re saying. This is the exact reason I made the default headers optional for some methods, and required for others initially. Axios still works fine after deleting those though. I also feel like the intent should be that I.e. this works fine: const axios = require('axios');
delete axios.defaults.headers.common;
delete axios.defaults.headers.get;
axios.get('https://example.com').then((response) => {
console.log(response.data);
}); |
Yeah you're right. Though I'm guessing undefined or empty object will produce same result on request headers. ... There is also a question whether this is desirable to do so or design flaw. You could probably wipe lots of stuff on axios object. These could also be sealed so they can be modified but no removed. |
In the readme modifying defaults is a documented feature, so I guess being able to delete header defaults is a feature as well. |
I meant the internal implementation. If |
I don’t know much about the internal implementations or the choices made there. I don’t really have strong opnions about making the distinction between the header types for request configurations and axios defaults just to make the distinction between whether or not headers are optional. However, I do feel the types should reflect type correctness, which is what we’re both trying to solve here. :) If you would like to make this distinction, I guess a second type needs to be added: type DefaultsRequestHeaders = Record<string, string> | {
common: Record<string, string>;
delete: Record<string, string>;
get: Record<string, string>;
head: Record<string, string>;
link?: Record<string, string>;
options?: Record<string, string>;
patch: Record<string, string>;
post: Record<string, string>;
purge?: Record<string, string>;
put: Record<string, string>;
unlink?: Record<string, string>;
} |
Yeah, sounds alright. It's slightly off topic anyway. It's probably worth separate PR as instance creation headers should be optional regardless the decision regarding the default headers. If your suggestion gets accepted by axios maintainers, this can be closed. If not, this should still be considered. |
Have created similar PR #4191 although things looks messy
`instance1.defaults.headers -> common: {Accept: 'application/json, text/plain, /', common-header-from-common: '1'} so if some common header gets put directly inside header object (rather that common) it does not goes to common but stays at headers level (although both are sent in request I think). So |
yeah, @remcohaszing already pointed that out at the beginning of the thread. Juts haven't committed it yet. |
@jasonsaayman any chance this could be looked at? It's been present in codebase for quite a while. And you can't create axios instances using factory function unless you ts-ignore it for now. I've squashed and rebased it on top of the master and added ts tests. Happy to make further changes if needed. |
For sure thanks, I am going to do everything relating to typescript in one go, there is a lot of it and the last time I made a large change a lot of people got super upset. |
Although PR #4140 mentioned it solved the #4117 PR, it didn't include changes to axios instance creation.
E.g. you can't still do the following as it still expects to provide full provide full
HeadersDefaults
interface rather a partial one.It should be possible to just provide partial one (
Partial<HeadersDefaults>
) as it's merged with defaults anyway.