Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit fba6d54

Browse files
author
Trevor Livingston
committed
merge to master
2 parents 593fc07 + b2a4b5d commit fba6d54

20 files changed

+121
-885
lines changed

.gitmodules

-3
This file was deleted.

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 1.0.0-rc.1
2+
3+
* `options.docs` is `options.docspath` and defaults to `/`.
4+
15
### 0.1.0-alpha.6
26

37
WARNING: Breaking changes!

README.md

+10-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# swaggerize-express
44

5-
- **Version:** `0.1.0-alpha.6`
5+
- **Version:** `1.0.0-rc.1`
66
- **Stability:** `unstable`
77
- **Changelog:** [https://github.com/krakenjs/swaggerize-express/blob/master/CHANGELOG.md](https://github.com/krakenjs/swaggerize-express/blob/master/CHANGELOG.md)
88

@@ -33,15 +33,15 @@ var swaggerize = require('swaggerize-express');
3333

3434
app.use(swaggerize({
3535
api: require('./api.json'),
36-
docs: '/api-docs',
36+
docspath: '/api-docs',
3737
handlers: './handlers'
3838
}));
3939
```
4040

4141
Options:
4242

4343
- `api` - a valid Swagger 1.2 document.
44-
- `docs` - the path to expose api docs for swagger-ui, etc. Defaults to `/api-docs`.
44+
- `docspath` - the path to expose api docs for swagger-ui, etc. Defaults to `/`.
4545
- `handlers` - either a directory structure for route handlers or a premade object (see *Handlers Object* below).
4646

4747
The base url for the api can also be updated via the `setUrl` function on the middleware.
@@ -59,7 +59,7 @@ var server = http.createServer(app);
5959

6060
var swagger = swaggerize({
6161
api: require('./api.json'),
62-
docs: '/api-docs',
62+
docspath: '/api-docs',
6363
handlers: './handlers'
6464
});
6565

@@ -86,11 +86,13 @@ handlers
8686
|--baz.js
8787
```
8888

89-
Matches:
89+
Routes as:
9090

91-
- `foo.js : /foo`
92-
- `foo/bar.js : /foo/bar`
93-
- `baz.js : /baz`
91+
```
92+
foo.js => /foo
93+
foo/bar.js => /foo/bar
94+
baz.js => /baz
95+
```
9496

9597
### Path Parameters
9698

@@ -178,13 +180,3 @@ swaggerize --api config/api.json --models resources/models --handlers resources/
178180
```
179181

180182
`--api` is required, but only one of `--models` or `--handlers` or `--tests` is required.
181-
182-
### Contribution
183-
184-
In order to run the swaggerize-express unit tests, execute the following commands:
185-
186-
```bash
187-
$ git submodule update --init --recursive
188-
$ npm install
189-
$ npm test
190-
```

bin/lib/swaggerize.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var minimist = require('minimist'),
44
fs = require('fs'),
55
path = require('path'),
66
mkdirp = require('mkdirp'),
7-
schema = require('../../lib/schema'),
7+
schema = require('swaggerize-builder/lib/schema'),
88
create = require('./create');
99

1010
module.exports = function (argp) {

bin/swaggerize.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env node
22
'use strict';
33

4-
var minimist = require('minimist'),
5-
swaggerize = require('swaggerize-express/bin/lib/swaggerize');
4+
var swaggerize = require('swaggerize-express/bin/lib/swaggerize');
65

76
var result = swaggerize(process.argv);
87

lib/buildroutes.js

-84
This file was deleted.

lib/expressroutes.js

+42-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
22

33
var path = require('path'),
4-
utils = require('./utils'),
5-
buildroutes = require('./buildroutes'),
4+
utils = require('swaggerize-builder/lib/utils'),
65
thing = require('core-util-is');
76

87
/**
@@ -12,34 +11,57 @@ var path = require('path'),
1211
* @param options
1312
*/
1413
function expressroutes(router, options) {
15-
var routes;
14+
var routes = options.routes || [];
1615

17-
routes = buildroutes(options);
18-
19-
router.get(options.api.resourcePath + utils.prefix(options.docs || '/api-docs', '/'), function (req, res) {
16+
router.get(options.api.resourcePath + utils.prefix(options.docspath || '', '/'), function (req, res) {
2017
res.json(options.api);
2118
});
2219

2320
routes.forEach(function (route) {
24-
var args;
21+
var args, path, before;
22+
23+
path = route.path.replace(/{([^}]+)}/g, ':$1');
24+
args = [options.api.resourcePath + utils.prefix(path, '/')];
25+
before = [];
26+
27+
route.validators.forEach(function (validator) {
28+
var parameter, validate;
29+
30+
parameter = validator.parameter;
31+
validate = validator.validate;
32+
33+
before.push(function validateInput(req, res, next) {
34+
var value, isPath;
35+
36+
isPath = parameter.paramType === 'path' || parameter.paramType === 'query';
37+
value = isPath ? req.param(parameter.name) : req.body;
38+
39+
validate(value, function (error, newvalue) {
40+
if (error) {
41+
res.statusCode = 400;
42+
next(error);
43+
return;
44+
}
45+
46+
if (isPath) {
47+
req.params[parameter.name] = newvalue;
48+
}
2549

26-
//If a handler exists, add it.
27-
if (route.handler) {
28-
args = [options.api.resourcePath + utils.prefix(route.path, '/')];
50+
next();
51+
});
52+
});
53+
});
2954

30-
if (thing.isArray(route.handler)) {
31-
if (route.handler.length > 1) {
32-
Array.prototype.push.apply(route.validators, route.handler.slice(0, route.handler.length - 1));
33-
}
34-
route.handler = route.handler[route.handler.length - 1];
55+
if (thing.isArray(route.handler)) {
56+
if (route.handler.length > 1) {
57+
Array.prototype.push.apply(before, route.handler.slice(0, route.handler.length - 1));
3558
}
36-
Array.prototype.push.apply(args, route.validators);
37-
args.push(route.handler);
38-
router[route.method].apply(router, args);
39-
return;
59+
route.handler = route.handler[route.handler.length - 1];
4060
}
4161

42-
utils.debuglog('no matching handler for %s.', route.path);
62+
Array.prototype.push.apply(args, before);
63+
args.push(route.handler);
64+
router[route.method].apply(router, args);
4365
});
4466
}
4567

lib/index.js

+8-19
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
11
'use strict';
22

33
var assert = require('assert'),
4-
schema = require('./schema'),
54
express = require('express'),
65
thing = require('core-util-is'),
76
path = require('path'),
8-
fs = require('fs'),
97
caller = require('caller'),
108
expressroutes = require('./expressroutes'),
11-
utils = require('./utils'),
12-
url = require('url');
9+
url = require('url'),
10+
builder = require('swaggerize-builder');
1311

1412
function swaggerize(options) {
15-
var app, validation, basePath;
13+
var app;
1614

1715
assert.ok(thing.isObject(options), 'Expected options to be an object.');
1816
assert.ok(thing.isObject(options.api), 'Expected an api definition.');
1917

20-
basePath = url.parse(options.api.basePath);
21-
options.api.resourcePath = utils.prefix(options.api.resourcePath || '/', '/');
22-
basePath.path = basePath.pathname = options.api.resourcePath;
23-
options.api.basePath = url.format(basePath);
18+
options.basedir = path.dirname(caller());
2419

25-
validation = schema.validate(options.api);
20+
options.routes = builder(options);
2621

27-
assert.ifError(validation.error);
28-
29-
if (thing.isString(options.handlers) || !options.handlers) {
30-
options.handlers = options.handlers && path.resolve(options.handlers) || path.join(path.dirname(caller()), 'handlers');
31-
}
32-
33-
options.docs = options.docs || '/api-docs';
22+
options.docspath = options.docspath || '/';
3423

3524
app = express();
3625

@@ -44,8 +33,9 @@ function swaggerize(options) {
4433
Object.defineProperty(app, 'setUrl', {
4534
enumerable: true,
4635
value: function (value) {
47-
value = url.parse(value);
36+
var basePath = url.parse(options.api.basePath);
4837

38+
value = url.parse(value);
4939
value.protocol && (basePath.protocol = value.protocol);
5040
value.host && (basePath.host = value.host);
5141

@@ -64,7 +54,6 @@ function swaggerize(options) {
6454
function mount(options) {
6555
return function onmount(parent) {
6656
parent._router.stack.pop();
67-
6857
expressroutes(parent._router, options);
6958
};
7059
}

0 commit comments

Comments
 (0)