bixi
Version:
企业级中后台前端解决方案
89 lines (75 loc) • 1.2 kB
Markdown
type: Basic
title: Mock 配置
order: 2
#### 1. 模拟 Get 请求
```ts
import { IMock } from '@bixi/mock';
export const test: IMock = {
// 一个 get 请求
'/users': () => [1, 2, 3],
// 等价于
'/users': {
get: () => [1, 2, 3]
}
}
```
#### 2. 模拟其他请求方式
```ts
export const test: IMock = {
'/users': {
delete: () => 'ok',
patch: () = 'ok',
post: () = 'ok'
}
}
```
#### 3. 获取请求参数
```ts
import { IMockRequest, IMock } from '@bixi/mock';
export const test: IMock = {
'/users/:id': {
get: (req: IMockRequest) => {
return {
id: req.params.id
name: '123'
}
}
}
}
```
#### 4. 禁用/延迟返回
```ts
export const test: IMock = {
'/users': {
get: {
delay: 400, // 接口延时返回 单位毫秒
disable: false,
value: (req: IMockRequest) => 'ok'
}
}
}
```
#### 5. 返回非 200
```ts
export const test: IMock = {
'/users': {
post() {
return new HttpResponse({
status: 400,
body: {}
})
}
}
}
```
### Type
#### IMockRequest
```ts
interface IMockRequest {
params?: any;
query?: any;
_httpReq: HttpRequest;
}
```