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: Add optional chaining to headers returned by requestAdapter #639

Merged
merged 2 commits into from
Feb 21, 2025
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/famous-gorillas-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@alova/adapter-axios': patch
---

to be compatible with falsy data by axios adapter
6 changes: 3 additions & 3 deletions packages/adapter-axios/src/requestAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { noop, undefinedValue } from '@alova/shared';
import { newInstance, noop, undefinedValue } from '@alova/shared';
import type { ProgressUpdater } from 'alova';
import axios, { AxiosResponseHeaders } from 'axios';
import axios, { AxiosHeaders, AxiosResponseHeaders } from 'axios';
import { AdapterCreateOptions, AxiosRequestAdapter } from '~/typings';

/**
Expand Down Expand Up @@ -37,7 +37,7 @@ export default function requestAdapter(options: AdapterCreateOptions = {}) {

return {
response: () => responsePromise,
headers: () => responsePromise.then(res => res.headers as AxiosResponseHeaders),
headers: () => responsePromise.then(res => (res?.headers || newInstance(AxiosHeaders)) as AxiosResponseHeaders),
abort: () => {
controller.abort();
},
Expand Down
38 changes: 37 additions & 1 deletion packages/adapter-axios/test/browser/requestAdapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { axiosRequestAdapter } from '@/index';
import { createAlova } from 'alova';
import axios, { AxiosError } from 'axios';
import axios, { AxiosError, AxiosHeaders } from 'axios';
import { readFileSync } from 'node:fs';
import path from 'path';
import { Result, delay } from 'root/testUtils';
Expand Down Expand Up @@ -249,4 +249,40 @@ describe('request adapter', () => {
});
expect(data.data.path).toBe('/unit-test');
});

test('should return an empty axios header when receiving headers is falsy value', async () => {
const axiosInst = axios.create({
baseURL,
timeout: 5000
});
let mockResponse: any = null;
axiosInst.interceptors.response.use(() => mockResponse);

const alovaInst = createAlova({
requestAdapter: axiosRequestAdapter({
axios: axiosInst
})
});

const transformFn = vi.fn();
const Get = alovaInst.Get('/unit-test', {
params: {
a: '1',
b: '2'
},
transform(data: any, headers) {
transformFn(headers);
return data;
}
});

const data = await Get;
expect(data).toBeNull();
expect(transformFn).toHaveBeenLastCalledWith(new AxiosHeaders());

mockResponse = {};
const data2 = await Get;
expect(data2).toStrictEqual({});
expect(transformFn).toHaveBeenLastCalledWith(new AxiosHeaders());
});
});
Loading