Skip to content

Commit 5e1bd0a

Browse files
committed
Add example implementation with schema validation.
1 parent 9287358 commit 5e1bd0a

20 files changed

+1989
-3
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.idea
22
*.iml
3-
node_modules
3+
node_modules
4+
jspm_packages
5+
api.js

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,32 @@ gulp.task('schema', function() {
3434
gulp.task('default', ['schema']);
3535
```
3636

37-
Generate client-side API based on schema:
37+
38+
Generate client-side API based on schema for AngularJS:
39+
40+
```js
41+
var gulp = require('gulp');
42+
var swagger = require('gulp-swagger');
43+
44+
gulp.task('api', function() {
45+
gulp.src('./src/api/index.yaml')
46+
.pipe(swagger({
47+
filename: 'api.js',
48+
type: 'angular' // type can be 'angular', 'node' or 'custom' (default).
49+
}))
50+
.pipe(gulp.dest('./api'));
51+
});
52+
53+
gulp.task('default', ['api']);
54+
55+
// Rerun the task when a file changes
56+
gulp.task('watch', function () {
57+
gulp.watch('./src/api/*.yaml', ['api']);
58+
});
59+
```
60+
61+
62+
Generate client-side API based on schema using custom templates:
3863

3964
```js
4065
var gulp = require('gulp');
@@ -63,6 +88,7 @@ gulp.task('watch', function () {
6388
});
6489
```
6590

91+
6692
Differently from [Swagger to JS Codegen][swagger-js-codegen], Gulp-Swagger does not require the template field to be on the format `template: { class: "...", method: "...", request: "..." }`. You can pass either of them as you want. Eg. say that your custom `method` and `request` are super simple and you only really need one `class` template, you could only pass `template: { class: "..." }`. For this reason, as a shortcut, `template` can also be a string: `template: "..."`
6793

6894
Gulp-Swagger allows to you pass mustache options along to Codegen. Gulp-Swagger also passes the swagger schema as well as a compilation of all JSON-schemas to mustache options. This is useful if you want to carry schema validation validation on the client-side:
@@ -94,6 +120,7 @@ gulp.task('watch', function () {
94120
});
95121
```
96122

123+
97124
So inside your mustache template, you can:
98125

99126
```
@@ -226,10 +253,16 @@ var schemas = {
226253
```
227254

228255

256+
Example
257+
--------------------------
258+
The provided example implements client-side JSON schema validation using [tv4][tv4] of both Ajax requests and responses.
259+
260+
To play with the example, download this repo. Point your terminal to the example folder and run: `$ npm run setup`. Then open `http://localhost:8888` in your browser, open the dev tools console and play around with the `API` global object.
261+
262+
229263
Roadmap
230264
--------------------------
231265
- [ ] Test coverage
232-
- [ ] Implementation Examples
233266
- [ ] Built-in schema validation
234267

235268

@@ -256,3 +289,4 @@ Gulp-Swagger is 100% free and open-source, under the [MIT license](LICENSE). Use
256289
[swagger2spec]: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md
257290
[swagger-parser]: https://github.com/gersongoulart/gulp-swagger
258291
[swagger-js-codegen]: https://github.com/wcandillon/swagger-js-codegen
292+
[tv4]: https://github.com/geraintluff/tv4

example/config.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
System.config({
2+
"baseURL": "/",
3+
"transpiler": "traceur",
4+
"paths": {
5+
"*": "*.js",
6+
"github:*": "jspm_packages/github/*.js",
7+
"npm:*": "jspm_packages/npm/*.js"
8+
}
9+
});
10+
11+
System.config({
12+
"map": {
13+
"bluebird": "npm:bluebird@2.9.25",
14+
"nanoajax": "npm:nanoajax@0.2.4",
15+
"traceur": "github:jmcriffey/bower-traceur@0.0.88",
16+
"traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.88",
17+
"tv4": "npm:tv4@1.1.9",
18+
"github:jspm/nodelibs-events@0.1.0": {
19+
"events-browserify": "npm:events-browserify@0.0.1"
20+
},
21+
"github:jspm/nodelibs-process@0.1.1": {
22+
"process": "npm:process@0.10.1"
23+
},
24+
"npm:bluebird@2.9.25": {
25+
"events": "github:jspm/nodelibs-events@0.1.0",
26+
"process": "github:jspm/nodelibs-process@0.1.1"
27+
},
28+
"npm:events-browserify@0.0.1": {
29+
"process": "github:jspm/nodelibs-process@0.1.1"
30+
},
31+
"npm:tv4@1.1.9": {
32+
"fs": "github:jspm/nodelibs-fs@0.1.2"
33+
}
34+
}
35+
});
36+

example/gulpfile.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var gulp = require('gulp');
2+
var swagger = require('gulp-swagger');
3+
var schema = '' +
4+
// // Pick one for the demo:
5+
// './schemas/json/petstore-expanded.json';
6+
// './schemas/json/petstore-minimal.json';
7+
// './schemas/json/petstore-simple.json';
8+
// './schemas/json/petstore-with-external-docs.json';
9+
// './schemas/json/petstore.json';
10+
// './schemas/yaml/api-with-examples.yaml';
11+
'./schemas/yaml/petstore-expanded.yaml';
12+
// './schemas/yaml/petstore.yaml';
13+
// './schemas/yaml/uber.yaml';
14+
15+
gulp.task('default', function() {
16+
// place code for your default task here
17+
return gulp
18+
.src(schema)
19+
.pipe(
20+
swagger({
21+
filename: 'api.js',
22+
codegen: {
23+
template: {
24+
class: './templates/api-class.mustache',
25+
method: './templates/api-method.mustache'
26+
}
27+
}
28+
})
29+
)
30+
.pipe(gulp.dest('./'));
31+
});

example/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>Gulp-Swagger example implementation</title>
6+
<style>html,h1,p {margin:1rem;font-weight:normal;font-family:sans-serif;}</style>
7+
</head>
8+
<body>
9+
<h1>Nothing to see here.</h1>
10+
<p>Open the dev tools console and play around with the API global object.</p>
11+
<script src="jspm_packages/system.js"></script>
12+
<script src="config.js"></script>
13+
<script>System['import']('index');</script>
14+
</body>
15+
</html>

example/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
import API from 'api';
4+
5+
// Only exposing it for playtime at the console.
6+
window.API = API;
7+
8+
// Playtime
9+
console = console || {};
10+
console.clear();
11+
Object.keys(API).reduce(function (previous, current) {
12+
previous.push('API.' + current + '();');
13+
return previous;
14+
}, [
15+
'╔═╗┬ ┬┬ ┌─┐ ╔═╗┬ ┬┌─┐┌─┐┌─┐┌─┐┬─┐',
16+
'║ ╦│ ││ ├─┘───╚═╗│││├─┤│ ┬│ ┬├┤ ├┬┘',
17+
'╚═╝└─┘┴─┘┴ ╚═╝└┴┘┴ ┴└─┘└─┘└─┘┴└─',
18+
'Try invoking these available methods:'
19+
]).forEach(function logAPIMethods (method) {
20+
console.log(method);
21+
});

example/package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "gulp-swagger-example",
3+
"version": "1.0.0",
4+
"description": "Gulp-Swagger example implementation",
5+
"author": "Gerson Goulart <gerson.goulart@gmail.com> (https://github.com/gersongoulart)",
6+
"license": "MIT",
7+
"main": "index.js",
8+
"scripts": {
9+
"setup": "npm install -g gulp-cli jspm && npm install && jspm install && gulp && npm start",
10+
"start": "node server.js"
11+
},
12+
"jspm": {
13+
"directories": {},
14+
"dependencies": {
15+
"bluebird": "npm:bluebird@^2.9.25",
16+
"nanoajax": "npm:nanoajax@^0.2.4",
17+
"tv4": "npm:tv4@^1.1.9"
18+
},
19+
"devDependencies": {
20+
"traceur": "github:jmcriffey/bower-traceur@0.0.88",
21+
"traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.88"
22+
}
23+
},
24+
"devDependencies": {
25+
"gulp": "^3.8.11",
26+
"gulp-swagger": "^0.0.3"
27+
}
28+
}

0 commit comments

Comments
 (0)