From 61407b0e647cdb0e0be887d86cf469840ac4b5e1 Mon Sep 17 00:00:00 2001 From: Scott Hu Date: Wed, 18 Dec 2024 21:14:24 +0800 Subject: [PATCH] feat(adapter-xhr): export xhr instance in `options.create` of adapter (#611) * feat(adapter-xhr): export xhr instance in `options.create` of adapter * chore: modify `create` to `onCreate` --- .changeset/silent-tips-chew.md | 5 +++++ packages/adapter-xhr/src/requestAdapter.ts | 7 +++++-- .../adapter-xhr/test/requestAdapter.spec.ts | 17 +++++++++++++++++ packages/adapter-xhr/typings/index.d.ts | 9 ++++++++- 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 .changeset/silent-tips-chew.md diff --git a/.changeset/silent-tips-chew.md b/.changeset/silent-tips-chew.md new file mode 100644 index 00000000..0cedddef --- /dev/null +++ b/.changeset/silent-tips-chew.md @@ -0,0 +1,5 @@ +--- +'@alova/adapter-xhr': minor +--- + +export xhr instance in `options.create` of adapter diff --git a/packages/adapter-xhr/src/requestAdapter.ts b/packages/adapter-xhr/src/requestAdapter.ts index 7499f59e..016e3465 100644 --- a/packages/adapter-xhr/src/requestAdapter.ts +++ b/packages/adapter-xhr/src/requestAdapter.ts @@ -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; @@ -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); diff --git a/packages/adapter-xhr/test/requestAdapter.spec.ts b/packages/adapter-xhr/test/requestAdapter.spec.ts index e7bb4f85..ce003883 100644 --- a/packages/adapter-xhr/test/requestAdapter.spec.ts +++ b/packages/adapter-xhr/test/requestAdapter.spec.ts @@ -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)); + }); }); diff --git a/packages/adapter-xhr/typings/index.d.ts b/packages/adapter-xhr/typings/index.d.ts index 39b77357..d806671e 100644 --- a/packages/adapter-xhr/typings/index.d.ts +++ b/packages/adapter-xhr/typings/index.d.ts @@ -63,10 +63,17 @@ export interface AlovaXHRResponse { */ export type AlovaXHRAdapter = AlovaRequestAdapter; +/** + * 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.