mock async
This commit is contained in:
parent
8bef4c845a
commit
a5779bd4d0
@ -9,12 +9,12 @@ export const client = new HttpClient(serviceProto, {
|
||||
});
|
||||
|
||||
// Client Mock
|
||||
client.flows.preCallApiFlow.push(v => {
|
||||
client.flows.preCallApiFlow.push(async v => {
|
||||
// 有对应的 MockAPI 则 Mock,否则请求真实后端
|
||||
let mockApi = mockApis[v.apiName];
|
||||
if (mockApi) {
|
||||
client.logger?.log('[MockReq]', v.apiName, v.req);
|
||||
v.return = mockApi!(v.req as any);
|
||||
v.return = await mockApi!(v.req as any);
|
||||
client.logger?.log('[MockRes]', v.apiName, v.return);
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,9 @@ const data: {
|
||||
}[] = [];
|
||||
|
||||
// { 接口名: (req: 请求) => 响应 }
|
||||
export const mockApis: { [K in keyof ServiceType['api']]?: (req: ServiceType['api'][K]['req']) => ApiReturn<ServiceType['api'][K]['res']> } = {
|
||||
export const mockApis: {
|
||||
[K in keyof ServiceType['api']]?: (req: ServiceType['api'][K]['req']) => ApiReturn<ServiceType['api'][K]['res']> | Promise<ApiReturn<ServiceType['api'][K]['res']>>
|
||||
} = {
|
||||
AddData: req => {
|
||||
let time = new Date();
|
||||
data.unshift({ content: req.content, time: time })
|
||||
@ -20,7 +22,12 @@ export const mockApis: { [K in keyof ServiceType['api']]?: (req: ServiceType['ap
|
||||
}
|
||||
},
|
||||
|
||||
GetData: req => {
|
||||
GetData: async req => {
|
||||
// 模拟 1 秒延时
|
||||
await new Promise(rs => {
|
||||
setTimeout(rs, 1000);
|
||||
})
|
||||
|
||||
return {
|
||||
isSucc: true,
|
||||
res: {
|
||||
|
Loading…
Reference in New Issue
Block a user