Skip to content

Commit

Permalink
Merge pull request #33 from davesag/feature/22/add-middleware
Browse files Browse the repository at this point in the history
[Feature, #22] add path-specific middleware
  • Loading branch information
davesag committed Apr 18, 2019
2 parents 197bfdb + 78ba5b6 commit d748a03
Show file tree
Hide file tree
Showing 17 changed files with 507 additions and 46 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Expand Up @@ -39,6 +39,10 @@ jobs:
name: All Unit Tests with Code Coverage
command: npm run test:coverage

- run:
name: Mutation Tests
command: npm run test:mutants

- run:
name: Push any lockfile changes
command: greenkeeper-lockfile-upload
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc.js
@@ -1,6 +1,6 @@
module.exports = {
extends: ['standard', 'prettier', 'prettier/standard'],
plugins: ['prettier', 'standard', 'import'],
plugins: ['prettier', 'standard', 'import', 'promise'],
parserOptions: {
sourceType: 'module'
},
Expand Down
33 changes: 31 additions & 2 deletions README.md
Expand Up @@ -304,6 +304,33 @@ async function correspondingMiddlewareFunction(req, res, next) {

OpenAPI V3 allows you to define a global `security` definition as well as path specific ones. The global `security` block will be applied if there is no path specific one defined.

### Adding other path-level middleware

You can add your own path specific middleware by passing in a `middleware` option

```js
{
middleware: {
myMiddleware: someMiddlewareFunction
}
}
```

and then in the path specification adding an `x-middleware` option

```yml
paths:
/special
get:
summary: some special route
x-middleware:
- myMiddleware
```

The `someMiddlewareFunction` will be inserted **after** any auth middleware.

This works for both Swagger v2 and OpenAPI v3 documents.

### Adding hooks

You can supply an `onCreateRoute` handler function with the options with signature
Expand Down Expand Up @@ -367,9 +394,10 @@ If you don't pass in any options the defaults are:
notFound: : require('./routes/notFound'),
notImplemented: require('./routes/notImplemented'),
onCreateRoute: undefined,
rootTag: 'root', // unused in OpenAPI v3 docs
rootTag: 'root', // only used in Swagger V2 docs
security: {},
variables: {}, // unused in Swagger V2 docs
variables: {}, // only used in OpenAPI v3 docs
middleware: {},
INVALID_VERSION: require('./errors').INVALID_VERSION
}
```
Expand All @@ -384,6 +412,7 @@ If you don't pass in any options the defaults are:

- `npm test` — runs the unit tests.
- `npm run test:coverage` - run the unit tests with coverage.
- `npm run test:mutants` - run mutation testing of the unit tests.

### Lint it

Expand Down

0 comments on commit d748a03

Please sign in to comment.