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: handle special Axios keywords when merging headers #1470

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-seals-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/client-axios': patch
---

fix: handle special Axios keywords when merging headers
31 changes: 30 additions & 1 deletion packages/client-axios/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { describe, expect, it, vi } from 'vitest';

import { getAuthToken, setAuthParams } from '../utils';
import {
axiosHeadersKeywords,
getAuthToken,
mergeHeaders,
setAuthParams,
} from '../utils';

describe('getAuthToken', () => {
it('returns access token', async () => {
Expand Down Expand Up @@ -70,6 +75,30 @@ describe('getAuthToken', () => {
});
});

describe('mergeHeaders', () => {
it.each(axiosHeadersKeywords)(
'handles "%s" Axios special keyword',
(keyword) => {
const headers = mergeHeaders(
{
foo: 'foo',
},
{
[keyword]: {
foo: 'foo',
},
},
);
expect(headers).toEqual({
foo: 'foo',
[keyword]: {
foo: 'foo',
},
});
},
);
});

describe('setAuthParams', () => {
it('sets access token in headers', async () => {
const accessToken = vi.fn().mockReturnValue('foo');
Expand Down
25 changes: 24 additions & 1 deletion packages/client-axios/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,19 @@ export const mergeConfigs = (a: Config, b: Config): Config => {
return config;
};

/**
* Special Axios headers keywords allowing to set headers by request method.
*/
export const axiosHeadersKeywords = [
'common',
'delete',
'get',
'head',
'patch',
'post',
'put',
] as const;

export const mergeHeaders = (
...headers: Array<Required<Config>['headers'] | undefined>
): Record<any, unknown> => {
Expand All @@ -435,7 +448,17 @@ export const mergeHeaders = (
const iterator = Object.entries(header);

for (const [key, value] of iterator) {
if (value === null) {
if (
axiosHeadersKeywords.includes(
key as (typeof axiosHeadersKeywords)[number],
) &&
typeof value === 'object'
) {
mergedHeaders[key] = {
...(mergedHeaders[key] as Record<any, unknown>),
...value,
};
} else if (value === null) {
delete mergedHeaders[key];
} else if (Array.isArray(value)) {
for (const v of value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,19 @@ export const mergeConfigs = (a: Config, b: Config): Config => {
return config;
};

/**
* Special Axios headers keywords allowing to set headers by request method.
*/
export const axiosHeadersKeywords = [
'common',
'delete',
'get',
'head',
'patch',
'post',
'put',
] as const;

export const mergeHeaders = (
...headers: Array<Required<Config>['headers'] | undefined>
): Record<any, unknown> => {
Expand All @@ -435,7 +448,17 @@ export const mergeHeaders = (
const iterator = Object.entries(header);

for (const [key, value] of iterator) {
if (value === null) {
if (
axiosHeadersKeywords.includes(
key as (typeof axiosHeadersKeywords)[number],
) &&
typeof value === 'object'
) {
mergedHeaders[key] = {
...(mergedHeaders[key] as Record<any, unknown>),
...value,
};
} else if (value === null) {
delete mergedHeaders[key];
} else if (Array.isArray(value)) {
for (const v of value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,19 @@ export const mergeConfigs = (a: Config, b: Config): Config => {
return config;
};

/**
* Special Axios headers keywords allowing to set headers by request method.
*/
export const axiosHeadersKeywords = [
'common',
'delete',
'get',
'head',
'patch',
'post',
'put',
] as const;

export const mergeHeaders = (
...headers: Array<Required<Config>['headers'] | undefined>
): Record<any, unknown> => {
Expand All @@ -435,7 +448,17 @@ export const mergeHeaders = (
const iterator = Object.entries(header);

for (const [key, value] of iterator) {
if (value === null) {
if (
axiosHeadersKeywords.includes(
key as (typeof axiosHeadersKeywords)[number],
) &&
typeof value === 'object'
) {
mergedHeaders[key] = {
...(mergedHeaders[key] as Record<any, unknown>),
...value,
};
} else if (value === null) {
delete mergedHeaders[key];
} else if (Array.isArray(value)) {
for (const v of value) {
Expand Down
Loading