https://github.com/bendrucker/swole
HTTP request router for Swagger/OpenAPI
https://github.com/bendrucker/swole
Last synced: 9 months ago
JSON representation
HTTP request router for Swagger/OpenAPI
- Host: GitHub
- URL: https://github.com/bendrucker/swole
- Owner: bendrucker
- License: mit
- Created: 2016-07-28T02:44:20.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2019-08-19T22:16:11.000Z (almost 7 years ago)
- Last Synced: 2025-10-11T07:28:29.857Z (10 months ago)
- Language: JavaScript
- Homepage:
- Size: 55.7 KB
- Stars: 7
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# swole [](https://travis-ci.org/bendrucker/swole) [](https://greenkeeper.io/)
> HTTP request router for [Swagger/OpenAPI](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md)
Swole is a configuration oriented router that glues your [Swagger API](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md) definition to request handlers without any framework dependencies. Swole uses your API definition to handle parsing/validation of user inputs and can even validate responses.
## Install
```
$ npm install --save swole
```
## Usage
```js
var Swole = require('swole')
var api = {
swagger: '2.0',
info: {
title: 'API'
},
paths: {
'/beep': {
get: {
'x-handler': 'beep',
responses: {
200: {
schema: {
type: 'string',
enum: ['boop']
}
}
}
}
}
}
}
var swole = Swole(api, {
handlers: {
beep: (req, res, callback) => res.end('boop', callback)
}
})
server.on('request', function (req, res) {
swole(req, res, function (err) {
if (err) {
res.statusCode = err.statusCode || 500
return res.end(JSON.stringify(err))
}
})
})
```
And make a request:
```
GET /beep
#=> 200 boop
```
## API
#### `Swole(swagger, options)` -> `function`
Creates a new Swole API handler using a [Swagger](https://github.com/OAI/OpenAPI-Specification) definition and options.
Returns a `req, res, callback` middleware function. Swole will append the matched path and operation values as `req.swole` as a `{path, operation}` object.
##### swagger
*Required*
Type: `object`
A Swagger API definition object.
##### options
###### handlers
*Required*
Type: `object{function}`
An object containing `req, res, callback` handler functions that match to `x-handler` keys in your [operations objects](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#operationObject).
###### hooks
Type: `array[function]`
An array of `req, res, callback` handler functions that will be called in series before executing handlers. These functions are run after the parameters for the request have been parsed and validated.
###### accepts
Type: `array[string]`
Default: `['json']`
A list of [body parsers](https://github.com/expressjs/body-parser) to use for parsing request streams.
###### strict
Type: `boolean`
Default: `false`
In `strict` mode, swole will validate *outgoing* payloads in addition to incoming data. This is slow and expensive and should only be used for development/debugging.
###### lowercase
Type: `boolean`
Default: `true`
By default, Swole coerces paths into lowercase for simplicity. In practice, this makes everything but your path parameters case insensitive. To make routing case sensitive, set this to `false`.
## License
MIT © [Ben Drucker](http://bendrucker.me)