mock async
This commit is contained in:
parent
8bef4c845a
commit
a5779bd4d0
@ -9,12 +9,12 @@ export const client = new HttpClient(serviceProto, {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Client Mock
|
// Client Mock
|
||||||
client.flows.preCallApiFlow.push(v => {
|
client.flows.preCallApiFlow.push(async v => {
|
||||||
// 有对应的 MockAPI 则 Mock,否则请求真实后端
|
// 有对应的 MockAPI 则 Mock,否则请求真实后端
|
||||||
let mockApi = mockApis[v.apiName];
|
let mockApi = mockApis[v.apiName];
|
||||||
if (mockApi) {
|
if (mockApi) {
|
||||||
client.logger?.log('[MockReq]', v.apiName, v.req);
|
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);
|
client.logger?.log('[MockRes]', v.apiName, v.return);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,9 @@ const data: {
|
|||||||
}[] = [];
|
}[] = [];
|
||||||
|
|
||||||
// { 接口名: (req: 请求) => 响应 }
|
// { 接口名: (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 => {
|
AddData: req => {
|
||||||
let time = new Date();
|
let time = new Date();
|
||||||
data.unshift({ content: req.content, time: time })
|
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 {
|
return {
|
||||||
isSucc: true,
|
isSucc: true,
|
||||||
res: {
|
res: {
|
||||||
|
Loading…
Reference in New Issue
Block a user