在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):gersongoulart/gulp-swagger开源软件地址(OpenSource Url):https://github.com/gersongoulart/gulp-swagger开源编程语言(OpenSource Language):JavaScript 62.9%开源软件介绍(OpenSource Introduction):gulp-swagger v1.0.0
Installnpm install gulp-swagger UsageOutput fully parsed schema: var gulp = require('gulp');
var swagger = require('gulp-swagger');
gulp.task('schema', function() {
gulp.src('./src/api/index.yaml')
.pipe(swagger('schema.json'))
.pipe(gulp.dest('./build'));
});
gulp.task('default', ['schema']); Generate client-side API based on schema for AngularJS: var gulp = require('gulp');
var swagger = require('gulp-swagger');
gulp.task('api', function() {
gulp.src('./src/api/index.yaml')
.pipe(swagger({
filename: 'api.js',
codegen: {
type: 'angular' // type can be 'angular', 'node' or 'custom' (default).
}
}))
.pipe(gulp.dest('./api'));
});
gulp.task('default', ['api']);
// Rerun the task when a file changes
gulp.task('watch', function () {
gulp.watch('./src/api/*.yaml', ['api']);
}); Generate client-side API based on schema using custom templates: var gulp = require('gulp');
var swagger = require('gulp-swagger');
gulp.task('api', function() {
gulp.src('./src/api/index.yaml')
.pipe(swagger({
filename: 'api.js',
codegen: {
template: {
class: './src/templates/api-class.mustache',
method: './src/templates/api-method.mustache',
request: './src/templates/api-request.mustache'
}
}
}))
.pipe(gulp.dest('./api'));
});
gulp.task('default', ['api']);
// Rerun the task when a file changes
gulp.task('watch', function () {
gulp.watch('./src/api/*.yaml', ['api']);
}); Differently from Swagger to JS Codegen, Gulp-Swagger does not require the template field to be on the format var gulp = require('gulp');
var swagger = require('gulp-swagger');
gulp.task('api', function() {
gulp.src('./src/api/index.yaml')
.pipe(swagger({
filename: 'api.js',
codegen: {
template: './src/templates/api.mustache',
mustache: {
// E.g. Passing variables to mustache to envify the templates...
NODE_ENV: process.env.NODE_ENV
}
}
}))
.pipe(gulp.dest('./api'));
});
gulp.task('default', ['api']);
// Rerun the task when a file changes
gulp.task('watch', function () {
gulp.watch('./src/api/*.yaml', ['api']);
}); Hadling Circular reference object : While parsing the JSON back from the string use "circular-json" to parse the string to JSON by filling all the circular reference. var CircularJSON = require('circular-json');
CircularJSON.parse(jsonString); Gulp-Swagger also passes the swagger schema to mustache options, both as an object (
The
Note: By default, the parser will dereference all internal $refs, which could result in a bloated json when many paths share the same schema/parameters/responses, etc. You can have fine grained control over the parser by using the
Example ImplementationThe provided example implements client-side JSON schema validation using tv4 of both Ajax requests and responses. To play with the example, download this repo. Point your terminal to the example folder and run: Roadmap
See AlsoContributingI welcome any contributions, enhancements, and bug-fixes. File an issue on GitHub and submit a pull request. LicenseGulp-Swagger is 100% free and open-source, under the MIT license. Use it however you want. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论