1.1.0 • Published 1 year ago

@tcatche/swagger-api v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

swagger-api

根据 Swagger 定义生成 axios api 的请求

Installation

npm install @tcatche/swagger-api --dev

生成代码

const path = require('path')
const swaggerGen = require('@tcatche/swagger-api')
const testJson = require('./test.json')
// 循环初始化API
const opt = {
  swaggers: [
    {
      swagger: swaggerUrl, // swagger 可以为swagger的地址
      ignore: false, // 使用 ignore: true 则不执行此配置,false或不填则会重新生成
      fileName: 'api1', // 指定保存的文件名为api1.js
    },
    {
      swagger: swaggerJSONObj, // swagger 也可以为json 对象
      ignore: false, // 使用 ignore: true 则不执行此配置,false或不填则会重新生成
      fileName: 'api2', // 指定保存的文件名为api2.js
    }
  ],
  swaggersDir: path.join(__dirname, './api/') // 指定生成的文件的目录
}
try {
  swaggerGen.generateApis(opt) // generate 代码
} catch (err) {
  console.log(err)
}

使用生成的代码

使用前必须初始化

import { requester } from './apis/api.js'
requester.setDomain('http://localhost:3000/api')

使用代码

import { userLogin } from '../apis/api.js'

userLogin({
  userInfo: {
    username: 'admin',
    password: 'admin'
  }
}).then(function (resp) {
  console.log(resp.data)
})

All requests use axios module with promise, for more information about that follow axios documentation

Links

License

MIT

1.1.0

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago