-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
164 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.swaggerJSON = undefined; | ||
|
||
var _lodash = require('lodash'); | ||
|
||
var _lodash2 = _interopRequireDefault(_lodash); | ||
|
||
var _swaggerTemplate = require('./swaggerTemplate'); | ||
|
||
var _swaggerTemplate2 = _interopRequireDefault(_swaggerTemplate); | ||
|
||
var _utils = require('./utils'); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
/** | ||
* build swagger json from apiObjects | ||
*/ | ||
const swaggerJSON = (options = {}, apiObjects) => { | ||
const { | ||
title, | ||
description, | ||
version, | ||
prefix = '', | ||
swaggerOptions = {} | ||
} = options; | ||
const swaggerJSON = (0, _swaggerTemplate2.default)(title, description, version, swaggerOptions); | ||
|
||
_lodash2.default.chain(apiObjects).forEach(value => { | ||
if (!Object.keys(value).includes('request')) { | ||
throw new Error('missing [request] field'); | ||
} | ||
|
||
const { method } = value.request; | ||
let { path } = value.request; | ||
path = (0, _utils.getPath)(prefix, path); // 根据前缀补全path | ||
const summary = value.summary ? value.summary : ''; | ||
const description = value.description ? value.description : summary; | ||
const responses = value.responses ? value.responses : { | ||
200: { | ||
description: 'success' | ||
} | ||
}; | ||
const { | ||
query = [], | ||
path: pathParams = [], | ||
body = [], | ||
tags, | ||
formData = [], | ||
security | ||
} = value; | ||
|
||
const parameters = [...pathParams, ...query, ...formData, ...body]; | ||
|
||
// init path object first | ||
if (!swaggerJSON.paths[path]) { | ||
swaggerJSON.paths[path] = {}; | ||
} | ||
|
||
// add content type [multipart/form-data] to support file upload | ||
const consumes = formData.length > 0 ? ['multipart/form-data'] : undefined; | ||
|
||
swaggerJSON.paths[path][method] = { | ||
consumes, | ||
summary, | ||
description, | ||
parameters, | ||
responses, | ||
tags, | ||
security | ||
}; | ||
}).value(); | ||
return swaggerJSON; | ||
}; | ||
|
||
exports.default = swaggerJSON; | ||
exports.swaggerJSON = swaggerJSON; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.readSync = exports.isSwaggerRouter = exports.getPath = exports.convertPath = undefined; | ||
|
||
var _koaRouter = require('koa-router'); | ||
|
||
var _koaRouter2 = _interopRequireDefault(_koaRouter); | ||
|
||
var _fs2 = require('fs'); | ||
|
||
var _fs3 = _interopRequireDefault(_fs2); | ||
|
||
var _path2 = require('path'); | ||
|
||
var _path3 = _interopRequireDefault(_path2); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
/** | ||
* eg. /api/{id} -> /api/:id | ||
* @param {String} path | ||
*/ | ||
const convertPath = path => { | ||
const re = new RegExp('{(.*?)}', 'g'); | ||
return path.replace(re, ':$1'); | ||
}; | ||
|
||
const getPath = (prefix, path) => `${prefix}${path}`.replace('//', '/'); | ||
|
||
/** | ||
* check if an object is an instance of SwaggerRouter | ||
* @param {Object} o | ||
*/ | ||
const isSwaggerRouter = o => { | ||
if (!o || o instanceof _koaRouter2.default) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
/** | ||
* read all files in the dir | ||
* @param {String} dir | ||
* @param {Array} result | ||
* @param {Boolean} recursive | ||
* @returns {Array} an array containing file url | ||
*/ | ||
const readSync = (dir, result = [], recursive = false) => { | ||
const files = _fs3.default.readdirSync(dir); | ||
files.forEach(filename => { | ||
const filedir = _path3.default.join(dir, filename); | ||
const stat = _fs3.default.statSync(filedir); | ||
if (stat.isFile()) result.push(filedir); | ||
if (recursive && stat.isDirectory()) readSync(filedir, result, true); | ||
}); | ||
return result; | ||
}; | ||
|
||
exports.convertPath = convertPath; | ||
exports.getPath = getPath; | ||
exports.isSwaggerRouter = isSwaggerRouter; | ||
exports.readSync = readSync; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters