generator-swagger-2-ts
A Swagger Codegen for typescript and javascript, A Yeoman generator
This package generates a js/typescript class from a swagger url. The code is generated using Yeoman and you can modify template to diy the generated code
Installation
First, install Yeoman and generator-swagger-2-ts using npm (we assume you have pre-installed node.js).
npm install -g yonpm install -g generator-swagger-2-ts
Then generate your new project:
yo swagger-2-ts
According to the prompt:
- input swagger - UI address, for example:
http://192.168.86.8:8051/swagger-ui.html
, the package will automatically replace the url address 'swagger-ui.html' to 'v2/api-docs' to get really swagger API docs url, then download and parse it to generate the API class - choose generate js or typescript
- you can customize the generated API class name, API file name
or you can pass arguments by command:
yo swagger-2-ts --swaggerUrl=http://localhost:8080/swagger-ui.html --className=API --type=typescript --outputFile=api.ts
- swaggerUrl: swagger ui url
- className: API class name
- type: typescript or javascipt
- outputFile: api file save to
generated CODE:
/** * User Account Controller * @class UserAccountAPI */ /** * changeUserState * @method * @name UserAccountAPI#changeUserState * @param accountUserInfo - accountUserInfo * @param $domain API域名,没有指定则使用构造函数指定的 */ : Promise<AxiosResponse<BasePayloadResponse>> let config: AxiosRequestConfig = baseURL: parameters$domain || this$defaultDomain url: '/userAccount/changeUserState' method: 'PUT' configheaders = {} configparams = {} configheaders 'Accept' = '*/*' configheaders 'Content-Type' = 'application/json' configdata = parametersaccountUserInfo return axios _UserAccountAPI: UserAccountAPI = null; /** * 获取 User Account Controller API * return @class UserAccountAPI */ : UserAccountAPI if !this_UserAccountAPI this_UserAccountAPI = this$defaultDomain return this_UserAccountAPI /** * 管理系统接口描述 * @class API */ /** * API构造函数 * @param domain API域名 */ { this$defaultDomain = domain || 'http://localhost:8080' }
Use the API Class
// in main.tslet api = "/api/"api
## Reference
License
Apache-2.0 © jadepeng