Skip to content
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

Closed
wants to merge 7 commits into from
Closed

Conversation

Keimeno
Copy link

@Keimeno Keimeno commented Jan 9, 2020

Issue

There are no suggestions in TypeScript for HTTP Headers.

Solved

Added separate headers.ts file in root and imported the AxiosHeaders type into the declaration file.
Removed all Headers prefixed with X- from autosuggestions, as it is deprecated according to RFC 6648.

headers.ts Outdated
@@ -0,0 +1,105 @@
type LooseObject = object & {

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 & ?

Copy link
Author

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

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?

Copy link
Author

@Keimeno Keimeno Jan 23, 2020

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.

@pelepelin
Copy link

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.

@Keimeno Keimeno requested review from yasuf and removed request for yasuf January 16, 2020 16:59
@chinesedfan
Copy link
Collaborator

Closed in favor of #3237 (comment). Thanks for your contribution all the same.

@chinesedfan chinesedfan closed this Oct 7, 2020
@pelepelin
Copy link

While argument in the comment does apply, headers should be key:any, not plain any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants