Skip to content

Commit

Permalink
merge to master
Browse files Browse the repository at this point in the history
  • Loading branch information
Trevor Livingston committed Aug 27, 2014
2 parents 593fc07 + b2a4b5d commit fba6d54
Show file tree
Hide file tree
Showing 20 changed files with 121 additions and 885 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 1.0.0-rc.1

* `options.docs` is `options.docspath` and defaults to `/`.

### 0.1.0-alpha.6

WARNING: Breaking changes!
Expand Down
28 changes: 10 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# swaggerize-express

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

Expand Down Expand Up @@ -33,15 +33,15 @@ var swaggerize = require('swaggerize-express');

app.use(swaggerize({
api: require('./api.json'),
docs: '/api-docs',
docspath: '/api-docs',
handlers: './handlers'
}));
```

Options:

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

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

var swagger = swaggerize({
api: require('./api.json'),
docs: '/api-docs',
docspath: '/api-docs',
handlers: './handlers'
});

Expand All @@ -86,11 +86,13 @@ handlers
|--baz.js
```

Matches:
Routes as:

- `foo.js : /foo`
- `foo/bar.js : /foo/bar`
- `baz.js : /baz`
```
foo.js => /foo
foo/bar.js => /foo/bar
baz.js => /baz
```

### Path Parameters

Expand Down Expand Up @@ -178,13 +180,3 @@ swaggerize --api config/api.json --models resources/models --handlers resources/
```

`--api` is required, but only one of `--models` or `--handlers` or `--tests` is required.

### Contribution

In order to run the swaggerize-express unit tests, execute the following commands:

```bash
$ git submodule update --init --recursive
$ npm install
$ npm test
```
2 changes: 1 addition & 1 deletion bin/lib/swaggerize.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var minimist = require('minimist'),
fs = require('fs'),
path = require('path'),
mkdirp = require('mkdirp'),
schema = require('../../lib/schema'),
schema = require('swaggerize-builder/lib/schema'),
create = require('./create');

module.exports = function (argp) {
Expand Down
3 changes: 1 addition & 2 deletions bin/swaggerize.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node
'use strict';

var minimist = require('minimist'),
swaggerize = require('swaggerize-express/bin/lib/swaggerize');
var swaggerize = require('swaggerize-express/bin/lib/swaggerize');

var result = swaggerize(process.argv);

Expand Down
84 changes: 0 additions & 84 deletions lib/buildroutes.js

This file was deleted.

62 changes: 42 additions & 20 deletions lib/expressroutes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

var path = require('path'),
utils = require('./utils'),
buildroutes = require('./buildroutes'),
utils = require('swaggerize-builder/lib/utils'),
thing = require('core-util-is');

/**
Expand All @@ -12,34 +11,57 @@ var path = require('path'),
* @param options
*/
function expressroutes(router, options) {
var routes;
var routes = options.routes || [];

routes = buildroutes(options);

router.get(options.api.resourcePath + utils.prefix(options.docs || '/api-docs', '/'), function (req, res) {
router.get(options.api.resourcePath + utils.prefix(options.docspath || '', '/'), function (req, res) {
res.json(options.api);
});

routes.forEach(function (route) {
var args;
var args, path, before;

path = route.path.replace(/{([^}]+)}/g, ':$1');
args = [options.api.resourcePath + utils.prefix(path, '/')];
before = [];

route.validators.forEach(function (validator) {
var parameter, validate;

parameter = validator.parameter;
validate = validator.validate;

before.push(function validateInput(req, res, next) {
var value, isPath;

isPath = parameter.paramType === 'path' || parameter.paramType === 'query';
value = isPath ? req.param(parameter.name) : req.body;

validate(value, function (error, newvalue) {
if (error) {
res.statusCode = 400;
next(error);
return;
}

if (isPath) {
req.params[parameter.name] = newvalue;
}

//If a handler exists, add it.
if (route.handler) {
args = [options.api.resourcePath + utils.prefix(route.path, '/')];
next();
});
});
});

if (thing.isArray(route.handler)) {
if (route.handler.length > 1) {
Array.prototype.push.apply(route.validators, route.handler.slice(0, route.handler.length - 1));
}
route.handler = route.handler[route.handler.length - 1];
if (thing.isArray(route.handler)) {
if (route.handler.length > 1) {
Array.prototype.push.apply(before, route.handler.slice(0, route.handler.length - 1));
}
Array.prototype.push.apply(args, route.validators);
args.push(route.handler);
router[route.method].apply(router, args);
return;
route.handler = route.handler[route.handler.length - 1];
}

utils.debuglog('no matching handler for %s.', route.path);
Array.prototype.push.apply(args, before);
args.push(route.handler);
router[route.method].apply(router, args);
});
}

Expand Down
27 changes: 8 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
'use strict';

var assert = require('assert'),
schema = require('./schema'),
express = require('express'),
thing = require('core-util-is'),
path = require('path'),
fs = require('fs'),
caller = require('caller'),
expressroutes = require('./expressroutes'),
utils = require('./utils'),
url = require('url');
url = require('url'),
builder = require('swaggerize-builder');

function swaggerize(options) {
var app, validation, basePath;
var app;

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

basePath = url.parse(options.api.basePath);
options.api.resourcePath = utils.prefix(options.api.resourcePath || '/', '/');
basePath.path = basePath.pathname = options.api.resourcePath;
options.api.basePath = url.format(basePath);
options.basedir = path.dirname(caller());

validation = schema.validate(options.api);
options.routes = builder(options);

assert.ifError(validation.error);

if (thing.isString(options.handlers) || !options.handlers) {
options.handlers = options.handlers && path.resolve(options.handlers) || path.join(path.dirname(caller()), 'handlers');
}

options.docs = options.docs || '/api-docs';
options.docspath = options.docspath || '/';

app = express();

Expand All @@ -44,8 +33,9 @@ function swaggerize(options) {
Object.defineProperty(app, 'setUrl', {
enumerable: true,
value: function (value) {
value = url.parse(value);
var basePath = url.parse(options.api.basePath);

value = url.parse(value);
value.protocol && (basePath.protocol = value.protocol);
value.host && (basePath.host = value.host);

Expand All @@ -64,7 +54,6 @@ function swaggerize(options) {
function mount(options) {
return function onmount(parent) {
parent._router.stack.pop();

expressroutes(parent._router, options);
};
}
Expand Down
Loading

0 comments on commit fba6d54

Please sign in to comment.