-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vendor extension for scoped middleware (closes #7)
* The extension name is x-express-openapi-additional-middleware and it's value is an array of functions (middleware).
- Loading branch information
Showing
6 changed files
with
179 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
test/sample-projects/with-additional-middleware/api-doc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// args.apiDoc needs to be a js object. This file could be a json file, but we can't add | ||
// comments in json files. | ||
module.exports = { | ||
'x-express-openapi-additional-middleware': [/* generate a warning */ null, | ||
function(req, res, next) { | ||
req.apiDocAdded = true; | ||
next(); | ||
}], | ||
|
||
swagger: '2.0', | ||
|
||
// all routes will now have /v3 prefixed. | ||
basePath: '/v3', | ||
|
||
info: { | ||
title: 'express-openapi sample project', | ||
version: '3.0.0' | ||
}, | ||
|
||
definitions: {}, | ||
|
||
// paths are derived from args.routes. These are filled in by fs-routes. | ||
paths: { | ||
'/users/{id}': { | ||
'x-express-openapi-additional-middleware': [function(req, res, next) { | ||
req.pathDocAdded = true; | ||
next(); | ||
}] | ||
} | ||
}, | ||
|
||
tags: [ | ||
{name: 'users'} | ||
] | ||
}; |
75 changes: 75 additions & 0 deletions
75
test/sample-projects/with-additional-middleware/api-routes/users/{id}.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module.exports = { | ||
'x-express-openapi-additional-middleware': [function(req, res, next) { | ||
req.pathModuleAdded = true; | ||
next(); | ||
}], | ||
|
||
// parameters for all operations in this path | ||
parameters: [ | ||
{ | ||
name: 'id', | ||
in: 'path', | ||
type: 'string', | ||
required: true, | ||
description: 'Fred\'s age.' | ||
} | ||
], | ||
get: get | ||
}; | ||
|
||
function get(req, res) { | ||
res.status(200).json({ | ||
apiDocAdded: req.apiDocAdded, | ||
pathDocAdded: req.pathDocAdded, | ||
pathModuleAdded: req.pathModuleAdded | ||
}); | ||
} | ||
|
||
get.apiDoc = { | ||
description: 'Retrieve a user.', | ||
operationId: 'getUser', | ||
tags: ['users'], | ||
parameters: [ | ||
{ | ||
name: 'name', | ||
in: 'query', | ||
type: 'string', | ||
pattern: '^fred$', | ||
description: 'The name of this person. It may only be "fred".' | ||
}, | ||
// showing that operation parameters override path parameters | ||
{ | ||
name: 'id', | ||
in: 'path', | ||
type: 'integer', | ||
required: true, | ||
description: 'Fred\'s age.' | ||
}, | ||
{ | ||
name: 'age', | ||
in: 'query', | ||
type: 'integer', | ||
description: 'Fred\'s age.', | ||
default: 80 | ||
} | ||
], | ||
|
||
responses: { | ||
default: { | ||
description: 'showing that additional middleware should have been added at all levels.', | ||
schema: { | ||
properties: { | ||
apiDocAdded: { | ||
type: 'boolean' | ||
}, | ||
pathDocAdded: { | ||
type: 'boolean' | ||
}, | ||
pathModuleAdded: { | ||
type: 'boolean' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var app = require('express')(); | ||
var bodyParser = require('body-parser'); | ||
// normally you'd just do require('express-openapi'), but this is for test purposes. | ||
var openapi = require('../../../'); | ||
var path = require('path'); | ||
var cors = require('cors'); | ||
|
||
app.use(cors()); | ||
app.use(bodyParser.json()); | ||
|
||
openapi.initialize({ | ||
apiDoc: require('./api-doc.js'), | ||
app: app, | ||
routes: path.resolve(__dirname, 'api-routes') | ||
}); | ||
|
||
app.use(function(err, req, res, next) { | ||
res.status(err.status).json(err); | ||
}); | ||
|
||
module.exports = app; | ||
|
||
var port = parseInt(process.argv[2]); | ||
if (port) { | ||
app.listen(port); | ||
} |