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
Adding axios headers #2647
Adding axios headers #2647
Conversation
headers.ts
Outdated
@@ -0,0 +1,105 @@ | |||
type LooseObject = object & { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the meaning of object &
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually you assign a value to a key in an object like this:
const obj = {
key: value
}
But if we work with headers, we want a String as key as well. Like this:
const obj = {
'key': value
}
This however gives us an error in typescript. So we need to create a custom type, the LooseObject
. But it has to inherit everything from a normal object, that is why you see the object &
.
With this we can use strings as keys for objects:
const obj: LooseObject = {
'key': value
}
And the AxiosHeaders type implements the LooseObject
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I didn't get it. This does not give me any error:
type LooseObject = {
[key: string]: string;
};
type AxiosHeaders = LooseObject & {
'Accept'?: string;
};
const y: AxiosHeaders = {
Accept: 'x-any',
'x-forwarded-for': '8.8.8.8',
anything: 'other'
};
What's the case where the result with object &
would be different?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea you're correct, my mistake. Fixed it now.
Probably, restricting header values to strings is a good thing, however, it's a compatibility break, as currently numbers and booleans can be passed and are properly handled. |
Closed in favor of #3237 (comment). Thanks for your contribution all the same. |
While argument in the comment does apply, headers should be |
Issue
There are no suggestions in TypeScript for HTTP Headers.
Solved
Added separate
headers.ts
file in root and imported theAxiosHeaders
type into the declaration file.Removed all Headers prefixed with X- from autosuggestions, as it is deprecated according to RFC 6648.