anymock-openapi
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -10,2 +10,1 @@ import { IAnymockConfigUpdate } from 'anymock-include'; | ||
} | ||
//# sourceMappingURL=base.d.ts.map |
@@ -21,3 +21,2 @@ "use strict"; | ||
throw new Error("missing or invalid project token: " + projectToken); | ||
return [2 /*return*/]; | ||
} | ||
@@ -24,0 +23,0 @@ // 序列化数据,去掉 undefined |
@@ -17,2 +17,1 @@ import { IAnymockConfigUpdate, IRequestPipe } from 'anymock-include'; | ||
export * from './types'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -7,6 +7,6 @@ /** | ||
import { IAnymockResponse, ID, IInterface } from './types'; | ||
interface IQuery { | ||
export interface IQuery { | ||
[key: string]: any; | ||
} | ||
interface IInterfaceModifiableParams { | ||
export interface IInterfaceModifiableParams { | ||
type: string; | ||
@@ -45,3 +45,1 @@ matching: string; | ||
} | ||
export {}; | ||
//# sourceMappingURL=interface.d.ts.map |
import { IAnymockConfigUpdate, IMockQueryPayload } from 'anymock-include'; | ||
import Base from './base'; | ||
import { IAnymockResponse, ID, IMock } from './types'; | ||
interface IMockResp { | ||
export interface IMockResp { | ||
mock: { | ||
@@ -18,3 +18,1 @@ data: string; | ||
} | ||
export {}; | ||
//# sourceMappingURL=mock.d.ts.map |
@@ -7,3 +7,3 @@ /** | ||
import { IAnymockResponse, IProject } from './types'; | ||
interface IGetProjectPayload { | ||
export interface IGetProjectPayload { | ||
[key: string]: any; | ||
@@ -19,3 +19,1 @@ } | ||
} | ||
export {}; | ||
//# sourceMappingURL=project.d.ts.map |
@@ -7,3 +7,3 @@ /** | ||
import { IAnymockResponse, ID, IRequest } from './types'; | ||
interface IAddRequestParam { | ||
export interface IAddRequestParam { | ||
/** | ||
@@ -60,3 +60,1 @@ * 若有则通过 interfaceId 唯一定位一个 interface | ||
} | ||
export {}; | ||
//# sourceMappingURL=request.d.ts.map |
@@ -72,2 +72,1 @@ /** | ||
} | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "anymock-openapi", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"main": "lib/index.js", | ||
@@ -19,2 +19,3 @@ "types": "./lib/index.d.ts", | ||
"dependencies": { | ||
"@types/node": "^13.7.4", | ||
"anymock-client-token": "^1.1.7", | ||
@@ -21,0 +22,0 @@ "anymock-include": "^0.1.10", |
432
README.md
@@ -62,12 +62,14 @@ # anymock-openapi | ||
const api = new AnymockOpenapi(config, pipe); | ||
const openapi = new AnymockOpenapi(config, pipe); | ||
// 1. fetch mock data | ||
const result = await api.mock.query({ | ||
type: ETypes.RPC, | ||
matching: 'com.aaa.w34', | ||
const result = await openapi.mock.query({ | ||
type: ETypes.HTTP, | ||
subType: 'GET', | ||
matching: '/api/projects', | ||
// parameters: {}, | ||
}); | ||
// 2. fetch project details | ||
const { data: projectDetails } = await api.project.get(); | ||
const { data: projectDetails } = await openapi.project.get(); | ||
@@ -118,5 +120,5 @@ // projectDetails interface | ||
const api = new AnymockOpenapi(config, pipe); | ||
const openapi = new AnymockOpenapi(config, pipe); | ||
const result = await api.mock.query({ | ||
const result = await openapi.mock.query({ | ||
type: ETypes.RPC, | ||
@@ -127,2 +129,418 @@ matching: 'com.aaa.w34', | ||
### openapi | ||
所有 openapi 方法返回值类型均遵循: | ||
```typescript | ||
interface IAnymockResponse<T = null> { | ||
success: boolean; | ||
data: T; | ||
errorMessage: string; | ||
errorCode: string; | ||
errorExtra: any; | ||
} | ||
``` | ||
公共类型: | ||
```typescript | ||
// import { IAnymockConfigUpdate } from 'anymock-include'; | ||
export interface IAnymockConfigUpdate { | ||
host?: string; // 用户是否自定义host | ||
userToken?: string; // 用户 Token | ||
projectToken?: string; // 用户 Token | ||
fromClient?: EClients | string; // `${bizType}.${subBizType}` 打点用 | ||
} | ||
``` | ||
#### Mock 数据 | ||
##### 获取 Mock 数据 | ||
方法:`openapi.mock.query({ payload: IMockQueryPayload, options?: IAnymockConfigUpdate })` | ||
入参: | ||
```typescript | ||
// import { IMockQueryPayload } from 'anymock-include'; | ||
export interface IMockQueryPayload { | ||
type: ETypes; // | ||
subType?: string; // 比如 GET POST | ||
matching: string; | ||
sceneId?: string; | ||
parameters?: any; | ||
} | ||
export enum ETypes { | ||
RPC = 'RPC', | ||
HTTP = 'HTTP', | ||
MTOP = 'MTOP', | ||
JSAPI = 'JSAPI', | ||
} | ||
``` | ||
返回值:`Promise<IAnymockResponse<IMockResp>>` | ||
```typescript | ||
export interface IMockResp { | ||
mock: { | ||
data: string; | ||
}; | ||
} | ||
``` | ||
示例: | ||
```typescript | ||
const resp = await openapi.mock.query({ | ||
type: ETypes.HTTP, | ||
subType: 'GET', | ||
matching: '/api/projects', | ||
// parameters: {}, | ||
}); | ||
``` | ||
##### 更新 Mock 数据 | ||
方法:`openapi.mock.update(requestId: ID, mockId: ID, payload: Partial<IMock>, config?: IAnymockConfigUpdate)` | ||
入参: | ||
```typescript | ||
export type ID = string | number; | ||
export interface IMock { | ||
data: string; | ||
dataType: number; | ||
gmtCreate: string; | ||
gmtModified: string; | ||
id: number; | ||
mockType: MockTypeEnum; | ||
requestId: number; | ||
} | ||
``` | ||
返回值:`Promise<IAnymockResponse<any>>` | ||
示例: | ||
```typescript | ||
const resp = await openapi.mock.update({ | ||
requestId: 1001, | ||
mockId: 2001, | ||
payload: { | ||
data: '{ success: false, name: "anymock" }' | ||
}, | ||
}); | ||
``` | ||
#### 项目 | ||
##### 获取项目详情 | ||
方法:`openapi.project.get(options?: IAnymockConfigUpdate, payload: IGetProjectPayload = {})` | ||
入参:可选 | ||
返回值:`Promise<IAnymockResponse<IProject>>` | ||
```typescript | ||
export interface IProject { | ||
id: number; | ||
gmtCreate: string; | ||
gmtModified: string; | ||
name: string; | ||
cname: string; | ||
avatar?: any; | ||
description: string; | ||
spaceId: number; | ||
status: number; | ||
token: string; | ||
members: IMember[]; | ||
} | ||
export interface IMember { | ||
id: number; | ||
anymockId: string; | ||
userId: string; | ||
userName: string; | ||
nickName: string; | ||
avatar: string; | ||
} | ||
``` | ||
示例: | ||
```typescript | ||
openapi.project.get() | ||
``` | ||
#### 接口 | ||
##### 获取接口列表 | ||
方法:`openapi.interface.list(options?: IAnymockConfigUpdate, payload?: IQuery = {})` | ||
入参:可选 | ||
```typescript | ||
export interface IQuery { | ||
[key: string]: any; | ||
} | ||
``` | ||
返回值:`Promise<IAnymockResponse<IInterface[]>>` | ||
```typescript | ||
export interface IInterface { | ||
cname?: any; | ||
description?: any; | ||
gmtCreate: string; | ||
gmtModified: string; | ||
id: number; | ||
matching: string; | ||
matchingType: string; | ||
name: string; | ||
projectId: number; | ||
request: IRequest[]; | ||
subType: string; | ||
type: string; | ||
} | ||
export interface IRequest { | ||
cname: string; | ||
gmtCreate: string; | ||
gmtModified: string; | ||
id: number; | ||
interfaceId: number; | ||
isActive: boolean; | ||
mock: IMock[]; | ||
status: number; | ||
} | ||
export interface IMock { | ||
data: string; | ||
dataType: number; | ||
gmtCreate: string; | ||
gmtModified: string; | ||
id: number; | ||
mockType: MockTypeEnum; | ||
requestId: number; | ||
} | ||
export enum MockTypeEnum { | ||
ResponseBody = 1, | ||
ResponseHeader = 2, | ||
RequestBody = 3, | ||
RequestHeader = 4, | ||
} | ||
``` | ||
##### 新增接口 | ||
方法:`openapi.interface.create(payload: IInterfaceModifiableParams, options?: IAnymockConfigUpdate)` | ||
入参:必须 | ||
```typescript | ||
export interface IInterfaceModifiableParams { | ||
type: string; | ||
/** 若 HTTP 接口则该字段必须 */ | ||
subType?: string; | ||
matching: string; | ||
name?: string; | ||
description?: string; | ||
} | ||
``` | ||
返回值:`Promise<IAnymockResponse<number>>` 返回新增接口的 id | ||
示例: | ||
```typescript | ||
openapi.interface.create({ | ||
type: ETypes.HTTP, | ||
subType: 'GET', | ||
matching: '/api/projects/:pid', | ||
}) | ||
``` | ||
##### 获取接口详情 | ||
方法:`openapi.interface.get(interfaceId?: string | number, config?: IAnymockConfigUpdate)` | ||
入参:接口 id | ||
返回值:`Promise<IAnymockResponse<IInterface>>` | ||
```typescript | ||
// 已有,省略 | ||
``` | ||
示例: | ||
```typescript | ||
openapi.interface.get(10001) | ||
``` | ||
##### 更新接口 | ||
方法:`openapi.interface.update(interfaceId: ID, payload: IInterfaceModifiableParams, config?: IAnymockConfigUpdate)` | ||
入参:必须 | ||
```typescript | ||
export interface IQuery { | ||
[key: string]: any; | ||
} | ||
``` | ||
返回值:`Promise<IAnymockResponse<null>>` | ||
示例: | ||
```typescript | ||
openapi.interface.update(1001, { matching: '/api/projects/:pid/interfaces' }) | ||
``` | ||
##### 删除接口 | ||
方法:`openapi.interface.delete(interfaceId: ID, config?: IAnymockConfigUpdate)` | ||
入参:必须 | ||
返回值:`Promise<IAnymockResponse<null>>` | ||
示例: | ||
```typescript | ||
openapi.interface.update(1001) | ||
``` | ||
#### 请求 | ||
##### 创建请求(有明确的 interfaceId) | ||
方法:`openapi.request.create(interfaceId: ID, payload: Partial<IRequest>, config?: IAnymockConfigUpdate)` | ||
入参:必须 | ||
返回值:`Promise<IAnymockResponse<object>>` | ||
示例: | ||
```typescript | ||
openapi.request.create(1001, { | ||
cname: '成功' | ||
}) | ||
``` | ||
##### 获取请求详情 | ||
方法:`openapi.request.get(requestId: ID, config?: IAnymockConfigUpdate)` | ||
入参:必须 | ||
返回值:`Promise<IAnymockResponse<IRequest>>` | ||
示例: | ||
```typescript | ||
openapi.request.get(2001) | ||
``` | ||
##### 更新请求 | ||
方法:`openapi.request.update(interfaceId: ID, requestId: ID, payload: Partial<IRequest>, config?: IAnymockConfigUpdate)` | ||
入参:必须 | ||
返回值:`Promise<IAnymockResponse<object>>` | ||
示例: | ||
```typescript | ||
openapi.request.update(1001, 2001, { | ||
cname: '失败' | ||
}) | ||
``` | ||
##### 删除请求 | ||
方法:`openapi.request.delete(requestId: ID, config?: IAnymockConfigUpdate)` | ||
入参:必须 | ||
返回值:`Promise<IAnymockResponse<null>>` | ||
示例: | ||
```typescript | ||
openapi.request.delete(2001) | ||
``` | ||
##### 新增请求(可能存在 interfaceId) | ||
> 复合接口,有可能会先创建一个接口。 | ||
> | ||
> 当接口存在则在接口下创建一个 request,否则先创建接口然后在其下新增一个 request | ||
> | ||
> 定位接口是否存在的规则:优先通过 interfaceId 或通过三要素(type / subType / matching) | ||
方法:`openapi.request.addRequest(payload: IAddRequestParam, config?: IAnymockConfigUpdate)` | ||
入参:必须 | ||
```typescript | ||
export interface IAddRequestParam { | ||
/** | ||
* 若有则通过 interfaceId 唯一定位一个 interface | ||
* 否则通过三要素唯一定位(性能相对较差,但是为了客户端方便) | ||
*/ | ||
interfaceId?: ID; | ||
// 这些属性来自 interface | ||
// 三要素 | ||
type: string; | ||
subType: string; | ||
matching: string; | ||
description?: string; | ||
name?: string; | ||
// 这才是 request 的真正属性 | ||
request: { | ||
cname: string; | ||
mock: { data: string }; | ||
}; | ||
} | ||
``` | ||
返回值:`Promise<IAnymockResponse<{ interface: { id: ID }; request: any }>>` | ||
示例: | ||
```typescript | ||
// 如果 type subType matching 相同的接口存在,则在该接口下新增一个 request | ||
// 否则先创建接口,然后在其下新增一个 request | ||
openapi.request.addRequest({ | ||
type: 'HTTP', | ||
subType: 'POST'; | ||
matching: '/api/projects/:pid'; | ||
request: { | ||
cname: '成功', | ||
mock: { | ||
data: "{ success: true, name: 'anymock' }" | ||
} | ||
} | ||
}); | ||
``` | ||
## Develop | ||
@@ -129,0 +547,0 @@ |
Sorry, the diff of this file is not supported yet
43320
552
4
529
+ Added@types/node@^13.7.4
+ Added@types/node@13.13.52(transitive)