一个简单的 Swagger 文档解析器,支持生成演示数据与数据实体类。
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.editorconfig
.eslintrc.js
.gitignore
LICENSE
README.md
package.json
yarn.lock

README.md

Swagger Parser Mock

npm version

一个简单的 Swagger 文档解析器,支持生成演示数据与数据实体类。

Features

  • 支持 OpenAPI Specification (1.2 & 2.0 & 3.0)
  • 支持为 Parameters 与 Responses 生成演示数据
  • 支持根据 Schema 生成数据实体类(JavaScript/Objective-C)

API

swaggerParserMock(url, opts)

const swaggerParserMock = require('swagger-parser-mock')
const specs = swaggerParserMock('http://petstore.swagger.io/v2/swagger.json')

specs.then(docs => {
  const api = docs.paths['/store/order']['post']
  const example = api.responses['200'].example
  console.log(JSON.parse(example))
  // =>
  /**
   * { id: '@integer(60, 100)',
   *   petId: '@integer(60, 100)',
   *   quantity: '@integer(60, 100)',
   *   shipDate: '@datetime',
   *   status: 'placed',
   *   complete: '@boolean' }
   */
})

getJavaScriptEntities(schema)

const swaggerParserMock = require('swagger-parser-mock')
const {
  getJavaScriptEntities,
  getObjectiveCEntities
} = require('swagger-parser-mock/lib/entity')
const specs = swaggerParserMock('http://petstore.swagger.io/v2/swagger.json')

specs.then(docs => {
  const api = docs.paths['/store/order']['post']
  const orderEntity = getJavaScriptEntities(api.responses['200'])[0]
  console.log(orderEntity)
  // =>
  /**
   * class Order {
   *   constructor() {
   *     this.id = 0;
   *     this.petId = 0;
   *     this.quantity = 0;
   *     this.shipDate = '';
   *     this.status = '';
   *     this.complete = false;
   *   }
   * }
   */
})

getObjectiveCEntities(schema)

// ...
specs.then(docs => {
  const api = docs.paths['/store/order']['post']
  const orderEntity = getObjectiveCEntities(api.responses['200'])[0]
  console.log(orderEntity)
  // =>
  /**
   * @interface Order : NSObject
   * @property (nonatomic, strong) NSNumber *id;
   * @property (nonatomic, strong) NSNumber *petId;
   * @property (nonatomic, strong) NSNumber *quantity;
   * @property (nonatomic, copy) NSString *shipDate;
   * @property (nonatomic, copy) NSString *status;
   * @property (nonatomic, assign) BOOL complete;
   * @end
   */
})

License

MIT