# generator-swagger-2-ts

> swagger to typescript/javascript api class use axios

Latest version **0.3.6** (published 2020-11-11) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install generator-swagger-2-ts
pnpm add generator-swagger-2-ts
yarn add generator-swagger-2-ts
bun add generator-swagger-2-ts
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.6 |
| Published | 2020-11-11 |
| First published | 2018-04-25 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 36.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 78 |
| Author | jadepeng |
| Maintainers | jadepeng |
| Keywords | swagger, typescript, yeoman-generator |

## Links

- npm: https://www.npmjs.com/package/generator-swagger-2-ts
- Repository: https://github.com/jadepeng/generator-swagger-2-ts
- Issues: https://github.com/jadepeng/generator-swagger-2-ts/issues
- npm.io page: https://npm.io/package/generator-swagger-2-ts

## Dependencies (3)

- [chalk](https://npm.io/package/chalk.md) ^4.0.0
- [yosay](https://npm.io/package/yosay.md) ^2.0.1
- [yeoman-generator](https://npm.io/package/yeoman-generator.md) ^4.10.1

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.3.6 (latest) — 2020-11-11
- 0.3.5 — 2020-11-11
- 0.3.4 — 2020-05-25
- 0.3.3 — 2020-05-22
- 0.3.2 — 2020-05-22
- 0.3.1 — 2020-05-22
- 0.3.0 — 2020-05-22
- 0.2.1 — 2018-04-25
- 0.2.0 — 2018-04-25
- 0.1.0 — 2018-04-25

## README

# generator-swagger-2-ts [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url]
> 

A Swagger Codegen for typescript and javascript, A [Yeoman](http://yeoman.io) 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

[中文文档](./README.cn.md)

## Installation

First, install [Yeoman](http://yeoman.io) and generator-swagger-2-ts using [npm](https://www.npmjs.com/) (we assume you have pre-installed [node.js](https://nodejs.org/)).

```bash
npm install -g yo
npm install -g generator-swagger-2-ts
```

Then generate your new project:

```bash
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:

```bash
 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：

```javascript

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)
  }

  _UserAccountAPI: UserAccountAPI = null;

  /**
  * 获取 User Account Controller API
  * return @class UserAccountAPI
  */
  getUserAccountAPI(): UserAccountAPI {
    if (!this._UserAccountAPI) {
      this._UserAccountAPI = new UserAccountAPI(this.$defaultDomain)
    }
    return this._UserAccountAPI
  }
}


/**
 * 管理系统接口描述
 * @class API
 */
export class API {
  /**
   *  API构造函数
   * @param domain API域名
   */
  constructor(domain?: string) {
    this.$defaultDomain = domain || 'http://localhost:8080'
  }
}

```

## Use the API Class

```javascript
import { API } from './http/api/manageApi'
// in main.ts
let api = new API("/api/")
api.getUserAccountAPI().changeUserState({
  isDisable: 1
  openId: 'open id'
})


```

 ## Reference

 - [swagger-js-codegen](https://github.com/wcandillon/swagger-js-codegen)

## License

Apache-2.0 © [jadepeng]()


[npm-image]: https://badge.fury.io/js/generator-swagger-2-ts.svg
[npm-url]: https://npmjs.org/package/generator-swagger-2-ts
[travis-image]: https://travis-ci.org/jadepeng/generator-swagger-2-ts.svg?branch=master
[travis-url]: https://travis-ci.org/jadepeng/generator-swagger-2-ts
[daviddm-image]: https://david-dm.org/jadepeng/generator-swagger-2-ts.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/jadepeng/generator-swagger-2-ts

---
_Source: https://npm.io/package/generator-swagger-2-ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
