Skip to content

Commit

Permalink
Merge pull request #1470 from hey-api/fix/axios-merge-headers
Browse files Browse the repository at this point in the history
fix: handle special Axios keywords when merging headers
  • Loading branch information
mrlubos authored Dec 19, 2024
2 parents 680e55f + 458ef50 commit 670e537
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
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

0 comments on commit 670e537

Please sign in to comment.