diff --git a/.changeset/old-seals-visit.md b/.changeset/old-seals-visit.md new file mode 100644 index 000000000..8eaccd6b9 --- /dev/null +++ b/.changeset/old-seals-visit.md @@ -0,0 +1,5 @@ +--- +'@hey-api/client-axios': patch +--- + +fix: handle special Axios keywords when merging headers diff --git a/packages/client-axios/src/__tests__/utils.test.ts b/packages/client-axios/src/__tests__/utils.test.ts index 337dc7119..088338555 100644 --- a/packages/client-axios/src/__tests__/utils.test.ts +++ b/packages/client-axios/src/__tests__/utils.test.ts @@ -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 () => { @@ -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'); diff --git a/packages/client-axios/src/utils.ts b/packages/client-axios/src/utils.ts index 196928cf7..d5dc4e6f7 100644 --- a/packages/client-axios/src/utils.ts +++ b/packages/client-axios/src/utils.ts @@ -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['headers'] | undefined> ): Record => { @@ -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), + ...value, + }; + } else if (value === null) { delete mergedHeaders[key]; } else if (Array.isArray(value)) { for (const v of value) { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3-hey-api-client-axios-bundle/client/utils.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3-hey-api-client-axios-bundle/client/utils.ts.snap index 196928cf7..d5dc4e6f7 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3-hey-api-client-axios-bundle/client/utils.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3-hey-api-client-axios-bundle/client/utils.ts.snap @@ -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['headers'] | undefined> ): Record => { @@ -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), + ...value, + }; + } else if (value === null) { delete mergedHeaders[key]; } else if (Array.isArray(value)) { for (const v of value) { diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3-hey-api-client-axios-bundle_transform/client/utils.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3-hey-api-client-axios-bundle_transform/client/utils.ts.snap index 196928cf7..d5dc4e6f7 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3-hey-api-client-axios-bundle_transform/client/utils.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3-hey-api-client-axios-bundle_transform/client/utils.ts.snap @@ -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['headers'] | undefined> ): Record => { @@ -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), + ...value, + }; + } else if (value === null) { delete mergedHeaders[key]; } else if (Array.isArray(value)) { for (const v of value) {