Skip to content

Commit

Permalink
feat(adapter-xhr): export xhr instance in options.create of adapter (
Browse files Browse the repository at this point in the history
…#611)

* feat(adapter-xhr): export xhr instance in `options.create` of adapter

* chore: modify `create` to `onCreate`
  • Loading branch information
JOU-amjs authored Dec 18, 2024
1 parent 73b0cf8 commit 61407b0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-tips-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@alova/adapter-xhr': minor
---

export xhr instance in `options.create` of adapter
7 changes: 5 additions & 2 deletions packages/adapter-xhr/src/requestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import {
trueValue
} from '@alova/shared';
import type { ProgressUpdater } from 'alova';
import { AlovaXHRAdapter, AlovaXHRResponse } from '~/typings';
import { AlovaXHRAdapter, AlovaXHRAdapterOptions, AlovaXHRResponse } from '~/typings';

const err = (msg: string) => newInstance(Error, msg);
const isBodyData = (data: any): data is XMLHttpRequestBodyInit => isString(data) || isSpecialRequestBody(data);
/**
* XMLHttpRequest request adapter
*/
export default function requestAdapter() {
export default function requestAdapter({ onCreate = noop }: AlovaXHRAdapterOptions = {}) {
const adapter: AlovaXHRAdapter = ({ type, url, data = null, headers }, method) => {
const { config } = method;
const { auth, withCredentials, mimeType, responseType } = config;
Expand Down Expand Up @@ -115,6 +115,9 @@ export default function requestAdapter() {
if (dataSend !== nullValue) {
dataSend = isBodyData(dataSend) ? dataSend : JSON.stringify(dataSend);
}

// export xhr in `onCreate`
onCreate(xhr);
xhr.send(dataSend);
} catch (error) {
reject(error);
Expand Down
17 changes: 17 additions & 0 deletions packages/adapter-xhr/test/requestAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,21 @@ describe('request adapter', () => {

await expect(Promise.all([Get1, Get2, Get3])).resolves.toStrictEqual(['object', 'object', 'string']);
});

test('should respect the type', async () => {
const mockFn = vi.fn();
const alovaInst = createAlova({
baseURL,
requestAdapter: xhrRequestAdapter({
onCreate(xhr) {
mockFn(xhr);
}
})
});

expect(mockFn).not.toHaveBeenCalled();
await alovaInst.Get('/unit-test');
expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith(expect.any(XMLHttpRequest));
});
});
9 changes: 8 additions & 1 deletion packages/adapter-xhr/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,17 @@ export interface AlovaXHRResponse<T = any> {
*/
export type AlovaXHRAdapter = AlovaRequestAdapter<AlovaXHRRequestConfig, AlovaXHRResponse, AlovaXHRResponseHeaders>;

/**
* XMLHttpRequest request adapter options
*/
export interface AlovaXHRAdapterOptions {
onCreate?: (xhr: XMLHttpRequest) => void;
}

/**
* XMLHttpRequest request adapter
*/
export declare function xhrRequestAdapter(): AlovaXHRAdapter;
export declare function xhrRequestAdapter(options: AlovaXHRAdapterOptions): AlovaXHRAdapter;

/**
* Mock response adapter, which is used in @alova/mock to allow xhr response data to be returned in a format compatible with mock requests.
Expand Down

0 comments on commit 61407b0

Please sign in to comment.