Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature, #22] add path-specific middleware #33

Merged
merged 2 commits into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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