# @tcatche/swagger-vue

> Swagger to JS & Vue & Axios Codegen,fork from https://github.com/Lvison/swagger-vue

Latest version **2.0.0** (published 2022-07-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tcatche/swagger-vue
pnpm add @tcatche/swagger-vue
yarn add @tcatche/swagger-vue
bun add @tcatche/swagger-vue
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2022-07-11 |
| First published | 2021-03-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 47 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | tcatche |
| Maintainers | tcatche |
| Keywords | swagger, js, vue, codegen |

## Links

- npm: https://www.npmjs.com/package/@tcatche/swagger-vue
- Repository: https://github.com/tcatche/swagger-vue
- Issues: https://github.com/tcatche/swagger-vue/issues
- npm.io page: https://npm.io/package/@tcatche/swagger-vue

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [handlebars](https://npm.io/package/handlebars.md) ^4.0.6
- [js-beautify](https://npm.io/package/js-beautify.md) ^1.6.12

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.0 (latest) — 2022-07-11
- 1.0.4 — 2022-07-11
- 1.0.3 — 2021-07-21
- 1.0.0 — 2021-03-23

## README

# swagger-vue
Swagger to JS &amp; Vue &amp; Axios Codegen

# Installation
```shell
npm install @tcatche/swagger-vue --dev
```
# Generate
## Using NodeJS file
```javascript
const swaggerGen = require('@tcatche/swagger-vue')
const jsonData = require('../api-docs.json')
const fs = require('fs')
const path = require('path')

let opt = {
  swagger: jsonData,
  moduleName: 'api',
  className: 'api'
}
const codeResult = swaggerGen.getApi(opt)
fs.writeFileSync(path.join(__dirname, '../config/api.js'), codeResult)//生成API文档

const filterContent = swaggerGen.getFilter(opt)
fs.writeFileSync(path.join(__dirname, '../config/filter.js'), filterContent)//生成filter文档及枚举
```
### if api-docs.json from API
```javascript
const swaggerGen = require('@tcatche/swagger-vue')
const fs = require('fs')
const path = require('path')
const url = 'http://demo.api.com';

swaggerGen.apiRequest(url).then(jsondata=>{
  let opt = {
    swagger: jsonData,
    moduleName: 'api',
    className: 'api'
  }

  const codeResult = swaggerGen.getApi(opt)
  fs.writeFileSync(path.join(__dirname, '../config/api.js'), codeResult)//生成API文档

  const filterContent = swaggerGen.getFilter(opt)
  fs.writeFileSync(path.join(__dirname, '../config/filter.js'), filterContent)//生成filter文档及枚举
})
```

## Using Grunt task

Create **Gruntfile.js**
```javascript
const fs = require('fs')
const path = require('path')
const swaggerGen = require('@tcatche/swagger-vue')

module.exports = function(grunt) {

  grunt.initConfig({
    'pkg': grunt.file.readJSON('package.json'),
    'swagger-vue-codegen': {
      options: {
        swagger: "<%= pkg.swagger.definition %>",
        className: "<%= pkg.swagger.className %>",
        moduleName: "vue-<%= pkg.swagger.moduleName %>",
        dest: 'client/javascript'
      },
      dist: {

      }
    }
  });

  grunt.registerMultiTask('swagger-vue-codegen', function() {
    var callback = this.async();
    var opt = this.options();
    var distFileName = path.join(opt.dest, opt.moduleName + '.js');

    fs.readFile(opt.swagger, function(err, data) {
      if (err) {
        grunt.log.error(err.toString());
        callback(false);
      } else {
        var parsedData = JSON.parse(data);

        var codeResult = swaggerGen({
          swagger: parsedData,
          moduleName: opt.moduleName,
          className: opt.className
        });

        fs.writeFile(distFileName, codeResult, function(err) {
          if (err) {
            grunt.log.error(err.toString());
            callback(false);
          } else {
            grunt.log.writeln('Generated ' + distFileName + ' from ' + opt.swagger);
          }
        })
      }
    });
  });

  grunt.registerTask('vue', ['swagger-vue-codegen']);

};

```
And set options in package.json
```json
...
  "swagger": {
    "definition": "client/swagger.json",
    "className": "API",
    "moduleName": "api-client"
  }
...
```
Now you can use `grunt vue` to run build task

# Generated client usage

In Vue.js main file set API domain
```javascript
import { setDomain } from './lib/api-client.js'
setDomain('http://localhost:3000/api')
```

Import API function into Vue.js component, for example to log in
```javascript
import { user_login as userLogin } from '../lib/api-client.js'

userLogin({
  credentials: {
    username: 'admin',
    password: 'admin'
  }
}).then(function (response) {
  console.log(response.data) // {id: "<token>", ttl: 1209600, created: "2017-01-01T00:00:00.000Z", userId: 1}
})
```
All requests use **axios** module with promise, for more information about that follow axios documentation

# Links
 - [swagger-js-codegen](https://github.com/wcandillon/swagger-js-codegen)
 - [axinos](https://www.npmjs.com/package/axios)

# License

[MIT](https://opensource.org/licenses/MIT)


# changelog
# [1.0.0](http://) (2019-08-13)
* swgger2.0生成代码注释错乱优化
* 优化example
* 优化模版
* 修改filter bug
* 暴露Apirequet函数,如果API文件是以接口形式提供的，我们可以调用该函数请求接口，将文档转换为JSON数据
* 修改日志路径
* 新增：抓取swgger枚举变量，生成filter(支持swgger3.0)
* 删除生成api时生成的无用函数
* 暴露生成api和filter的接口,引用方式更改
---


# [1.0.1](http://) (2019-03-19)
### Features
* 取消parameters参数camelCase转换 by lvison
* 兼容openApi by lvison
---

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