All Projects → jadepeng → generator-swagger-2-ts

jadepeng / generator-swagger-2-ts

Licence: other
A Swagger Codegen for typescript and javascript

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
typescript
32286 projects

Projects that are alternatives of or similar to generator-swagger-2-ts

utility-network-swagger
Utility Network REST API expressed in Swagger / Open API Specification, code samples
Stars: ✭ 27 (-64.47%)
Mutual labels:  swagger-codegen
rules openapi
🍃 bazel rules for generating code from openapi specifications
Stars: ✭ 49 (-35.53%)
Mutual labels:  swagger-codegen
Swagger Codegen
swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
Stars: ✭ 13,859 (+18135.53%)
Mutual labels:  swagger-codegen
Drf Yasg
Automated generation of real Swagger/OpenAPI 2.0 schemas from Django REST Framework code.
Stars: ✭ 2,523 (+3219.74%)
Mutual labels:  swagger-codegen
Go Swagger
Swagger 2.0 implementation for go
Stars: ✭ 7,058 (+9186.84%)
Mutual labels:  swagger-codegen
light-rest-4j
A RESTful framework built on top of light-4j with both Swagger 2.0 and OpenAPI 3.0 supports
Stars: ✭ 113 (+48.68%)
Mutual labels:  swagger-codegen
platform
Apinf - Open source API management platform with multi proxy and protocol support
Stars: ✭ 69 (-9.21%)
Mutual labels:  swagger-codegen
swagger2puml
Generate Class Diagrams (UML) for Given Swagger Definition
Stars: ✭ 43 (-43.42%)
Mutual labels:  swagger-codegen
swagger-rs
A set of common utilities for crates generated by swagger-codegen
Stars: ✭ 84 (+10.53%)
Mutual labels:  swagger-codegen

generator-swagger-2-ts NPM version Build Status Dependency Status

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 yo
npm 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:

export type AccountUserInfo = {
  disableTime?: string
  isDisable?: number
  lastLoginIp?: string
  lastLoginPlace?: string
  lastLoginTime?: string
  openId?: string
}


export type BasePayloadResponse = {
  data?: object
  desc?: string
  retcode?: string

}

/**
 * User Account Controller
 * @class UserAccountAPI
 */
export class UserAccountAPI {
/**
  * changeUserState
  * @method
  * @name UserAccountAPI#changeUserState
  
  * @param  accountUserInfo - accountUserInfo 
  
  * @param $domain API域名,没有指定则使用构造函数指定的
  */
  changeUserState(parameters: {
    'accountUserInfo': AccountUserInfo,
    $queryParameters?: any,
    $domain?: string
  }): Promise<AxiosResponse<BasePayloadResponse>> {

    let config: AxiosRequestConfig = {
      baseURL: parameters.$domain || this.$defaultDomain,
      url: '/userAccount/changeUserState',
      method: 'PUT'
    }

    config.headers = {}
    config.params = {}

    config.headers[ 'Accept' ] = '*/*'
    config.headers[ 'Content-Type' ] = 'application/json'

    config.data = parameters.accountUserInfo
    return axios.request(config)
  }

/**
  * changeUserState
  * @method
  * @name UserAccountAPI#changeUserState
  
  * @param  accountUserInfo - accountUserInfo 
  
  * @param $domain API域名,没有指定则使用构造函数指定的
  */
  async changeUserStateAsync(parameters: {
    'accountUserInfo': AccountUserInfo,
    $queryParameters?: any,
    $domain?: string
  }): Promise<BasePayloadResponse> {

    let resp = await this.changeUserStateAsync(parameters);
    return Promise.resolve(resp.data);
  }

}


/**
 * 管理系统接口描述
 * @class API
 */
export class API {
  /**
   *  API构造函数
   * @param domain API域名
   */
  constructor(domain?: string) {
    this.$defaultDomain = domain || 'http://localhost:8080'
  }

  _UserAccountAPI: UserAccountAPI = null;

  /**
  * 获取 User Account Controller API
  * return @class UserAccountAPI
  */
  getUserAccountAPI(): UserAccountAPI {
    if (!this._UserAccountAPI) {
      this._UserAccountAPI = new UserAccountAPI(this.$defaultDomain)
    }
    return this._UserAccountAPI
  }
}

Use the API Class

import { API } from './api'
// in main.ts
let api = new API('YOUR_API_HOST')
api.withAuthorization("YOUR_TOKEN")
// add Interceptors
api.withInterceptors(
    response => {
        if (response.status === 200) {
            return Promise.resolve(response)
        } else {
            return Promise.reject(response)
        }
    },
    error => {
        if (error.response.status) {
            switch (error.response.status) {
                case 401:
                    console.error("401 Unauthorized")
                    break
                case 403:
                    console.error("403 Forbidden")
                    break
                case 404:
                    console.error("404 Not Found")
                    break
                default:
                    console.error(error.response.data.message)
            }
            return Promise.reject(error.response)
        }
    })

api.getUserAccountAPI().changeUserState({
  isDisable: 1
  openId: 'open id'
}).then(resp=>{
  console.log(resp)
})

Reference

License

Apache-2.0 © jadepeng

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].