mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-02 03:58:07 +08:00
feat: api 发送适配器支持异步 (#7525)
This commit is contained in:
parent
9026a19a93
commit
e6c231c887
@ -223,7 +223,10 @@ export interface ApiObject extends BaseApiObject {
|
||||
api: ApiObject,
|
||||
context: any
|
||||
) => any;
|
||||
requestAdaptor?: (api: ApiObject, context: any) => ApiObject;
|
||||
requestAdaptor?: (
|
||||
api: ApiObject,
|
||||
context: any
|
||||
) => ApiObject | Promise<ApiObject>;
|
||||
/** 是否过滤为空字符串的 query 参数 */
|
||||
filterEmptyQuery?: boolean;
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ export function buildApi(
|
||||
}
|
||||
|
||||
if (api.requestAdaptor && typeof api.requestAdaptor === 'string') {
|
||||
api.requestAdaptor = str2function(
|
||||
api.requestAdaptor = str2AsyncFunction(
|
||||
api.requestAdaptor,
|
||||
'api',
|
||||
'context'
|
||||
@ -84,7 +84,7 @@ export function buildApi(
|
||||
}
|
||||
|
||||
if (api.adaptor && typeof api.adaptor === 'string') {
|
||||
api.adaptor = str2function(
|
||||
api.adaptor = str2AsyncFunction(
|
||||
api.adaptor,
|
||||
'payload',
|
||||
'response',
|
||||
@ -464,12 +464,16 @@ export function wrapFetcher(
|
||||
return fn as any;
|
||||
}
|
||||
|
||||
const wrappedFetcher = function (api: Api, data: object, options?: object) {
|
||||
const wrappedFetcher = async function (
|
||||
api: Api,
|
||||
data: object,
|
||||
options?: object
|
||||
) {
|
||||
api = buildApi(api, data, options) as ApiObject;
|
||||
|
||||
if (api.requestAdaptor) {
|
||||
debug('api', 'before requestAdaptor', api);
|
||||
api = api.requestAdaptor(api, data) || api;
|
||||
api = (await api.requestAdaptor(api, data)) || api;
|
||||
debug('api', 'after requestAdaptor', api);
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import {render as amisRender} from '../../src';
|
||||
import {wait, makeEnv} from '../helper';
|
||||
import {render, fireEvent, cleanup} from '@testing-library/react';
|
||||
import {render, fireEvent, cleanup, waitFor} from '@testing-library/react';
|
||||
import {buildApi, isApiOutdated, isValidApi} from 'amis-core';
|
||||
|
||||
test('api:buildApi', () => {
|
||||
@ -358,3 +358,74 @@ test('api:isvalidapi', () => {
|
||||
)
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
test('api:requestAdaptor', async () => {
|
||||
const notify = jest.fn();
|
||||
const fetcher = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
data: {
|
||||
status: 0,
|
||||
msg: 'ok',
|
||||
data: {
|
||||
id: 1
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
const requestAdaptor = jest.fn().mockImplementation(api => {
|
||||
return Promise.resolve({
|
||||
...api,
|
||||
data: {
|
||||
...api.data,
|
||||
email: 'appended@test.com'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const {container, getByText} = render(
|
||||
amisRender(
|
||||
{
|
||||
type: 'page',
|
||||
body: [
|
||||
{
|
||||
type: 'form',
|
||||
id: 'form_submit',
|
||||
submitText: '提交表单',
|
||||
api: {
|
||||
method: 'post',
|
||||
url: '/api/mock2/form/saveForm',
|
||||
requestAdaptor: requestAdaptor
|
||||
},
|
||||
body: [
|
||||
{
|
||||
type: 'input-text',
|
||||
name: 'name',
|
||||
label: '姓名:',
|
||||
value: 'fex'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
notify,
|
||||
fetcher
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getByText('提交表单')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(getByText(/提交表单/));
|
||||
await wait(300);
|
||||
|
||||
expect(requestAdaptor).toHaveBeenCalled();
|
||||
expect(fetcher).toHaveBeenCalled();
|
||||
expect(fetcher.mock.calls[0][0].data).toMatchObject({
|
||||
name: 'fex',
|
||||
email: 'appended@test.com'
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user