Giter Site home page Giter Site logo

api-proj-hzbn's People

Contributors

arcobale avatar b9891796435 avatar nooralu avatar

Stargazers

 avatar

Watchers

 avatar

api-proj-hzbn's Issues

Mock 响应类型完善

Mock 功能实现了一部分,但是需求文档的要求太复杂了,APIFox 中原先设计的响应似乎不能满足 Mock 部分的需求。现在将我改写的响应体类型放在下面,如果一会组会没有问题我再继续。

// 支持的响应类型
const enum ContentType {
  JSON = 'application/json',
  XML = 'text/xml',
  YAML = 'application/x-yaml',
  FILE = 'multipart/form-data',
}

// 响应体数据结构
type Scheme = {
  // 数据类型
  type: 'string' | 'number' | 'boolean' | 'object' | 'array' | 'null',
  // 如果类型为object
  properties?: {
    // 属性名
    [key: string]: Scheme
  },
  // 如果类型为 array,其值为数组元素的类型
  items?: Scheme,
  // 参考 mockjs
  mock?: string,
}

type Response = {
  // 响应码
  code: number,
  // 响应描述
  description: string,
  // 响应类型
  contentType: ContentType,
  // 响应体数据结构,如果是文件上传,不需要填写
  schema?: Scheme,
  // 响应体示例,类型与 content-type 一致
  example?: any,
}

// 接口数据结构
type API = {
  // ...
  reponses: Response[],
}

// example
const responseVo: Response = {
  code: 200,
  description: 'success',
  contentType: ContentType.JSON,
  schema: {
    type: 'object',
    properties: {
      name: {
        type: 'string',
        mock: '@name',
      },
      age: {
        type: 'number',
        mock: '@integer(10, 100)',
      },
      address: {
        type: 'object',
        properties: {
          province: {
            type: 'string',
            mock: '@province',
          },
          city: {
            type: 'string',
            mock: '@city',
          },
        },
      },
    },
  },
  example: {
    name: '张三',
    age: 18,
    address: {
      province: '福建省',
      city: '厦门市',
    },
  },
}

Mock 的思路大概就是提供一个接口生成某个项目的 Mock 服务,例如

PUT http://host/projects/{pid}/mock_server

再提供一个接口获取某个接口对应的 mock 地址,例如

GET http://host/projects/{pid}/apis/{api}/mock

然后,定义接口的时可以定义多个响应,通过 query 确定此次调用的响应。例如如下 mock 的就是 id 为 123 的项目下 path 为 /regiter 的接口,它的响应对应的 id 是 321。

POST http://host/mock/123/register?rid=321

更进一步,randomCode 返回随机响应码,randomMock 提供动态的 mock 数据。

POST http://host/mock/123/register?randomCode=true&randomMock=true

参考:

APIFox

但是这样的设计似乎满足不了下面的条件:

  1. 支持参数匹配和响应规则:接口Mock服务需要支持参数匹配和响应规则,以模拟API接口的行为。例如,可以根据请求参数的值来返回不同的响应结果,或者根据请求头中的信息来返回不同的响应结果;

关于 3,APIFox 的 mock 是不需要传入参数的(如下 body 是空对象),更别说根据参数响应。

图片

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.