# swaggerize-express

> Design-driven apis with swagger 2.0 and express.

Latest version **4.0.5** (published 2015-10-14) · 0 weekly downloads

## Install

```sh
npm install swaggerize-express
pnpm add swaggerize-express
yarn add swaggerize-express
bun add swaggerize-express
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.5 |
| Published | 2015-10-14 |
| First published | 2014-07-25 |
| Weekly downloads | 0 |
| TypeScript types | separate (@types/swaggerize-express) |
| Module format | CommonJS |
| Node | 0.10.x |
| Dependencies | 6 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 350 |
| Author | Trevor Livingston |
| Maintainers | tlivings |
| Keywords | swagger, swagger 2.0, swagger-node, swagger-express, swagger-ui, express, node, node.js, rest, restful, service, api |

## Links

- npm: https://www.npmjs.com/package/swaggerize-express
- Repository: https://github.com/krakenjs/swaggerize-express
- Homepage: https://github.com/krakenjs/swaggerize-express#readme
- Issues: http://github.com/krakenjs/swaggerize-express/issues
- npm.io page: https://npm.io/package/swaggerize-express

## Dependencies (6)

- [async](https://npm.io/package/async.md) ^0.9.0
- [caller](https://npm.io/package/caller.md) ^0.0.1
- [js-yaml](https://npm.io/package/js-yaml.md) ^3.2.6
- [debuglog](https://npm.io/package/debuglog.md) ^1.0.1
- [core-util-is](https://npm.io/package/core-util-is.md) ^1.0.1
- [swaggerize-routes](https://npm.io/package/swaggerize-routes.md) ^1.0.0

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 4.0.5 (latest) — 2015-10-14
- 5.0.0-alpha.2 (alpha) — 2021-04-27
- 5.0.0-alpha.1 — 2017-10-28
- 4.0.4 — 2015-09-24
- 4.0.3 — 2015-08-09
- 4.0.2 — 2015-06-03
- 4.0.1 — 2015-05-12
- 4.0.0 — 2015-05-01
- 3.0.3 — 2015-02-10
- 3.0.2 — 2015-01-26
- 3.0.1 — 2014-12-02
- 3.0.0 — 2014-10-22
- 3.0.0-alpha.3 — 2014-10-15
- 3.0.0-alpha.2 — 2014-09-30
- 3.0.0-alpha.1 — 2014-09-24
- … 13 more at https://npm.io/package/swaggerize-express/versions

## README

swaggerize-express
==================

Lead Maintainer: [Trevor Livingston](https://github.com/tlivings/)  

[![Build Status](https://travis-ci.org/krakenjs/swaggerize-express.svg?branch=master)](https://travis-ci.org/krakenjs/swaggerize-express)  
[![NPM version](https://badge.fury.io/js/swaggerize-express.png)](http://badge.fury.io/js/swaggerize-express)  

`swaggerize-express` is a design-driven approach to building RESTful apis with [Swagger](http://swagger.io) and [Express](http://expressjs.com).

`swaggerize-express` provides the following features:

- API schema validation.
- Routes based on the Swagger document.
- API documentation route.
- Input validation.

See also:
- [swaggerize-routes](https://github.com/krakenjs/swaggerize-routes)
- [swaggerize-hapi](https://github.com/krakenjs/swaggerize-hapi)
- [generator-swaggerize](https://www.npmjs.org/package/generator-swaggerize)

### Why "Design Driven"

There are already a number of modules that help build RESTful APIs for node with swagger. However,
these modules tend to focus on building the documentation or specification as a side effect of writing
the application business logic.

`swaggerize-express` begins with the swagger document first. This facilitates writing APIs that are easier to design, review, and test.

### Quick Start with a Generator

This guide will let you go from an `api.json` to a service project in no time flat.

First install `generator-swaggerize` (and `yo` if you haven't already):

```bash
$ npm install -g yo
$ npm install -g generator-swaggerize
```

Now run the generator.

```bash
$ mkdir petstore && cd $_
$ yo swaggerize
```

Follow the prompts (note: make sure to choose `express` as your framework choice).

When asked for a swagger document, you can try this one:

```
https://raw.githubusercontent.com/wordnik/swagger-spec/master/examples/v2.0/json/petstore.json
```

You now have a working api and can use something like [Swagger UI](https://github.com/wordnik/swagger-ui) to explore it.

### Manual Usage

```javascript
var swaggerize = require('swaggerize-express');

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

Options:

- `api` - a valid Swagger 2.0 document.
- `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).
- `express` - express settings overrides.

After using this middleware, a new property will be available on the `app` called `swagger`, containing the following properties:

- `api` - the api document.
- `routes` - the route definitions based on the api document.

Example:

```javascript
var http = require('http');
var express = require('express');
var swaggerize = require('swaggerize-express');

app = express();

var server = http.createServer(app);

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

server.listen(port, 'localhost', function () {
    app.swagger.api.host = server.address().address + ':' + server.address().port;
});
```

### Mount Path

Api `path` values will be prefixed with the swagger document's `basePath` value.

### Handlers Directory

The `options.handlers` option specifies a directory to scan for handlers. These handlers are bound to the api `paths` defined in the swagger document.

```
handlers
  |--foo
  |    |--bar.js
  |--foo.js
  |--baz.js
```

Will route as:

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

### Path Parameters

The file and directory names in the handlers directory can also represent path parameters.

For example, to represent the path `/users/{id}`:

```shell
handlers
  |--users
  |    |--{id}.js
```

This works with directory names as well:

```shell
handlers
  |--users
  |    |--{id}.js
  |    |--{id}
  |        |--foo.js
```

To represent `/users/{id}/foo`.

### Handlers File

Each provided javascript file should export an object containing functions with HTTP verbs as keys.

Example:

```javascript
module.exports = {
    get: function (req, res) { ... },
    put: function (req, res) { ... },
    ...
}
```

### Handler Middleware

Handlers can also specify middleware chains by providing an array of handler functions under the verb:

```javascript
module.exports = {
    get: [
        function m1(req, res, next) { ... },
        function m2(req, res, next) { ... },
        function handler(req, res)  { ... }
    ],
    ...
}
```

### Handlers Object

The directory generation will yield this object, but it can be provided directly as `options.handlers`.

Note that if you are programatically constructing a handlers obj this way, you must namespace HTTP verbs with `$` to
avoid conflicts with path names. These keys should also be *lowercase*.

Example:

```javascript
{
    'foo': {
        '$get': function (req, res) { ... },
        'bar': {
            '$get': function (req, res) { ... },
            '$post': function (req, res) { ... }
        }
    }
    ...
}
```

Handler keys in files do *not* have to be namespaced in this way.

### Security Middleware

If a security definition exists for a path in the swagger document, and an appropriate authorize function exists (defined using
`x-authorize` in the `securityDefinitions` as per [swaggerize-routes](https://github.com/krakenjs/swaggerize-routes#security-object)),
then it will be used as middleware for that path.

In addition, a `requiredScopes` property will be injected onto the `request` object to check against.

For example:

```javascript
//x-authorize: auth_oauth.js
function authorize(req, res, next) {
    validate(req, function (error, availablescopes) {
        if (!error) {
            for (var i = 0; i < req.requiredScopes.length; i++) {
                if (availablescopes.indexOf(req.requiredScopes[i]) > -1) {
                    next();
                    return;
                }
            }

            error = new Error('Do not have the required scopes.');
            error.status = 403;

            next(error);
            return;
        }

        next(error);
    });
}
```

---
_Source: https://npm.io/package/swaggerize-express · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
