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

fix response headers types #4136

Merged
merged 3 commits into from Oct 7, 2021
Merged

fix response headers types #4136

merged 3 commits into from Oct 7, 2021

Conversation

egmen
Copy link
Contributor

@egmen egmen commented Oct 4, 2021

Copy link
Contributor

@caugner caugner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably make sense to introduce a specifc AxiosHeaders type:

type AxiosHeaders = Record<Exclude<"set-cookie", string>, string> & {
  "set-cookie"?: string[]
}

Note that it is also used in the AxiosTransformer and AxiosRequestConfiginterfaces:

axios/index.d.ts

Lines 1 to 3 in 6c00232

export interface AxiosTransformer {
(data: any, headers?: Record<string, string>): any;
}

axios/index.d.ts

Lines 50 to 56 in 6c00232

export interface AxiosRequestConfig<D = any> {
url?: string;
method?: Method;
baseURL?: string;
transformRequest?: AxiosTransformer | AxiosTransformer[];
transformResponse?: AxiosTransformer | AxiosTransformer[];
headers?: Record<string, string>;

@caugner
Copy link
Contributor

caugner commented Oct 6, 2021

You might want to wait until #4140 is merged, because it adds a new HeadersDefaults interface with essentially 11 occurences of Record<string, string> that could be replaced by the new AxiosHeaders type.

@bfaulk96
Copy link
Contributor

bfaulk96 commented Oct 6, 2021

@caugner #4144 does exactly that

@caugner
Copy link
Contributor

caugner commented Oct 6, 2021

@caugner #4144 does exactly that

👍 for differentiating RequestHeaders and ResponseHeaders, but this PR extends the type to string | string[], whereas #4144 extends the type to string | number | boolean | undefined.

The common ground would be to keep RequestHeaders as Record<string, string> and have ResponseHeaders distinguish between set-cookie (string[]) and other headers (string).

@bfaulk96
Copy link
Contributor

bfaulk96 commented Oct 6, 2021

@caugner #4144 does exactly that

👍 for differentiating RequestHeaders and ResponseHeaders, but this PR extends the type to string | string[], whereas #4144 extends the type to string | number | boolean | undefined.

The common ground would be to keep RequestHeaders as Record<string, string> and have ResponseHeaders distinguish between set-cookie (string[]) and other headers (string).

Haha sorry – I was referring to that PR assigning things to type aliases, I should have clarified that! Was sharing that the two PRs could build from one another 😄

@caugner
Copy link
Contributor

caugner commented Oct 6, 2021

Actually, #4117 was the first PR to suggest adding a Headers type.

@remcohaszing
Copy link
Contributor

It would probably make sense to introduce a specifc AxiosHeaders type:

type AxiosHeaders = Record<Exclude<"set-cookie", string>, string> & {
  "set-cookie"?: string[]
}

I like this idea! But it doesn’t work. After some fiddling I came up with:

type AxiosHeaders = Record<string, string> & {
  "set-cookie"?: string[]
}

playground link

Also we may want to differentiate between request and response headers, as set-cookie is only defined as string[] in the response headers.

type AxiosRequestHeaders = Record<string, string>;

type AxiosResponseHeaders = Record<string, string> & {
  "set-cookie"?: string[]
};

@eyebrowkang
Copy link

It would probably make sense to introduce a specifc AxiosHeaders type:

type AxiosHeaders = Record<Exclude<"set-cookie", string>, string> & {
  "set-cookie"?: string[]
}

I like this idea! But it doesn’t work. After some fiddling I came up with:

type AxiosHeaders = Record<string, string> & {
  "set-cookie"?: string[]
}

playground link

Also we may want to differentiate between request and response headers, as set-cookie is only defined as string[] in the response headers.

type AxiosRequestHeaders = Record<string, string>;

type AxiosResponseHeaders = Record<string, string> & {
  "set-cookie"?: string[]
};

I have a question about this code, type AxiosResponseHeaders = Record<string, string> & { "set-cookie"?: string[] };, i wrote some codes here playground, but there are some error and i can't understand it, so could anyone help explain the reason?

@caugner
Copy link
Contributor

caugner commented Nov 17, 2021

i wrote some codes here playground, but there are some error and i can't understand it, so could anyone help explain the reason?

@EyebrowsWhite Not sure why it doesn't work when declaring the header object directly, but the following works (see playground):

type AxiosHeaders = Record<string, string> & {
  "set-cookie"?: string[]
}

declare let headers: AxiosHeaders;

headers = {}
headers['Content-Type'] = 'text/plain'
headers['set-cookie'] = ['abc', 'def']

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

6 participants