mock async

This commit is contained in:
King Wang 2021-06-30 00:09:54 +08:00
parent 8bef4c845a
commit a5779bd4d0
2 changed files with 11 additions and 4 deletions

View File

@ -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);
}

View File

@ -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: {