# swagger-jsblade
[![npm version](https://img.shields.io/npm/v/swagger-jsblade.svg?style=flat)](https://www.npmjs.com/package/swagger-jsblade)[![NPM downloads](http://img.shields.io/npm/dm/swagger-jsblade.svg)](https://npmjs.org/package/swagger-jsblade)[![Join the chat at https://gitter.im/jsblade/Lobby](https://badges.gitter.im/swagger-jsblade/jsblade.svg)](https://gitter.im/jsblade/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
## Introduction
Jsblade is a tool kit for Front-End developers who use Swagger as their API definition.
These are the main it solved.
* Boilerplate
* Auto generate API based built-in templates and custom template
* Auto mock API and custom mock response
For complete information about Swaggerâ¢, you can check the Swagger Specification project. It contains general information and the actual Swagger specification.
## Install
```shell
npm i swagger-jsblade -g
```
### Boilerplate Example
```shell
blade create myProject -f vue
```
| Command | Options | Description |
| --- | --- | ---
| -f, â-framework| 'angular1' , 'vue' |Built-in Angular 1.5.x and Vue 1.x project bolierplate|
**Detail Usage**
```shell
blade create -h
Usage: create [options] [name]
å建ä¸ä¸ªæ件夹å
å«ä¸ä¸ªå端项ç®
Options:
-h, --help output usage information
-f, --framework angular1|vue ä¸çä¸ä¸ª
```
### API Generation Example
```
blade api DataApi ./input/swagger.json ./output/service dataApi
```
| Command | Options | Description |
| --- | --- | ---
| -a, â-ajax | n, s|generated API type : n for angular's $http; s for NodeJS or browser's superagent
| -s, â-surround | 1,2,3,4 |API Module: 1 for UMD; 2 for AMD; 3 for CommonJS; 4 for JavaScript closure
| -w, â-withCredentials | |Enable CORS
| -t, â-tags | Swagger definition's tagName |Only generate API based on tagName: @tagOne -> generate tagOne. @tagOne@tagTwo -> generate both tagOne and tageTwo
| -p, â-promise | |Enable Promise
**Detail Usage**
```shell
blade api -h
Usage: api [options] [outFileName]
å建ä¸ä¸ªapiæ¥å£éå
å¿
å¡«: <è¾åºæ件ä½ç½®> [è¾åºæ件å称]
Options:
-a, --ajax åé请æ±ç±»å(-cæ¶æ æ), n: $httpç±»å, s: superagentç±»å, f: fetchç±»å, a: axiosç±»å, b: superbridgeç±»å, c: configç±»å,åªçæé
ç½®
-s, --surround å
å´æ¨¡å¼(-cæ¶æ æ), å°çæç代ç å
å«å¨UMD-1 AMD-2 CommonJS-3 éå
-4 ES6-5 ä¸
-c, --custom èªå®ä¹æ¨¡æ¿(ä¼å
级é«äº -aå-s)
-w, --withCredentials æ¯æè·¨åä¼ cookie
-t, --tags ætagåç»çææ件,(@)çæå
¨é¨tagç, (@aaa@bbb)çæaaaåbbbç
-p, --promise 注å
¥Promiseä¾èµ,é»è®¤ä¸æ³¨å
¥
-V, --version output the version number
-h, --help output usage information
```
#### Using API
**Simple Usage**
```javascript
import API from '../api/dataApi';
let api = new API('http://xxx.com');
// This will send an AJAX to http://xxx.com/xxx
api.xxxx(param).then(res => {
console.log('get respone:' + res)
})
```
**Superagent/Superbridge/Fetch Interceptor Enhanced**
like Angular's $http , we add an interceptor to superagent/superbridge/fetch type API,
angular/axios has interceptor yet.
you can use it like this:
```
import API from '../api/dataApi';
API.interceptor({
request: function (config) {
console.log(config);
return (new Promise(function (resolve, reject) {
resolve(config);
}));
},
requestError: function (err) {
console.log(err);
return (new Promise(function (resolve, reject) {
resolve(err);
}));
},
response: function (response) {
console.log(response);
return (new Promise(function (resolve, reject) {
resolve(response);
}));
},
responseError: function (err) {
console.log(err);
return (new Promise(function (resolve, reject) {
resolve(err);
}));
}
});
let api = new API('http://xxx.com');
```
**å¦æçæçæ¯cofigå½¢å¼,å¯åç
§ä¸é¢demo使ç¨**
```
import ApiUtil from '@/common/services/ApiUtil'; // å¼å
¥å·¥å
·å
import ApiConfig from '@/common/services/config'; // å¼å
¥çæçé
ç½®æ件
import axios from 'axios';
axios.interceptors.request.use(function (config) {
return config;
}, function (err) {
return Promise.reject(err);
});
axios.interceptors.response.use(function (response) {
return response;
}, function (err) {
return Promise.reject(err);
});
export const Apis = new ApiUtil(ApiConfig, {
domain: ''
});
export default Apis;
```
ApiUtilè°ç¨å·¥å
·å¯ç¨ blade utilå½ä»¤çæ
###APIè°ç¨å·¥å
·çæ
```
blade util -h
Usage: util [options] [outFileName]
å建ä¸ä¸ªapiè°ç¨å·¥å
·
å¿
å¡«:<è¾åºæ件ä½ç½®> [è¾åºæ件å称,é»è®¤ApiUtil]
Options:
-a, --ajax åé请æ±ç±»å, a: axiosç±»å, ç®ååªæ¯æaxiosç±»å
-s, --surround å
å´æ¨¡å¼, å°çæç代ç å
å«å¨UMD-1 AMD-2 CommonJS-3 éå
-4 ES6-5 ä¸
-V, --version output the version number
-h, --help output usage information
```
### Mock Example
#### Mock data generation
```
blade mock ./input/swagger.json -f ./output/swagger.mock.json
```
The mock data will generate at ```responses.[code].schema.example```
```
```
if your original swagger file has defined some example,will remain the same otherwhise random generate.
#### Mock server
```
blade mock ./input/swagger.json -s 8001
```
| Command | Options | Description |
| --- | --- | ---
| -f, â-file | file path|generate swagger mock file which definition's example is filled with random mock data
| -s, â-server | port number|mock server's port ,default is 8000
| -c, â-config |config file path| mock 2.0(v0.2.0) added: custom config file,default config will prompt whether generate at your project root dir, named with 'mock.config.js'
| -l, â-lite||disable mock 2.0 default prompt
**Detail Usage**
```shell
blade mock -h
Usage: mock [options]
çæå«æmockæ°æ®çswaggeræ件 å¿
å¡«: <è¾åºJSONæ件ä½ç½®åå称>
Options:
-h, --help output usage information
-f, --file çæmock file
-s, --server [portNum] å¯å¨mock server,端å£å·,é»è®¤8000
-c, --config mockæ°æ®é
ç½®æ件
-l, --lite ä¸èªå¨è¯å«configæ件
```
## Changelog
Detailed changes for each release are documented in the [release notes](https://github.com/lincolnlau/swagger-jsblade/releases).
## FAQ
We have collected some [frequently asked questions](https://github.com/lincolnlau/swagger-jsblade/blob/master/FAQ.md). Before reporting an issue, please search if the FAQ has the answer to your problem.
## LICENSE
MIT