-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to specify routes by hand (#60)
Format is { path: '/foo/{id}', module: require('./foo') }
- Loading branch information
1 parent
0e35223
commit c40a51f
Showing
10 changed files
with
399 additions
and
25 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
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
44 changes: 44 additions & 0 deletions
44
test/sample-projects/with-route-specs-using-modules/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,44 @@ | ||
// 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 = { | ||
swagger: '2.0', | ||
|
||
// all routes will now have /v3 prefixed. | ||
basePath: '/v3', | ||
|
||
info: { | ||
title: 'express-openapi sample project', | ||
version: '3.0.0' | ||
}, | ||
|
||
definitions: { | ||
Error: { | ||
additionalProperties: true | ||
}, | ||
User: { | ||
properties: { | ||
name: { | ||
type: 'string' | ||
}, | ||
friends: { | ||
type: 'array', | ||
items: { | ||
$ref: '#/definitions/User' | ||
} | ||
} | ||
}, | ||
required: ['name'] | ||
} | ||
}, | ||
|
||
// paths are derived from args.routes. These are filled in by fs-routes. | ||
paths: {}, | ||
|
||
// tags is optional, and is generated / sorted by the tags defined in your path | ||
// docs. This API also defines 2 tags in operations: "creating" and "fooey". | ||
tags: [ | ||
// {name: 'creating'} will be inserted by ./api-routes/users.js | ||
// {name: 'fooey'} will be inserted by ./api-routes/users/{id}.js | ||
{description: 'Everything users', name: 'users'} | ||
] | ||
}; |
37 changes: 37 additions & 0 deletions
37
test/sample-projects/with-route-specs-using-modules/api-routes/apiDocs.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,37 @@ | ||
module.exports = { | ||
get: get | ||
}; | ||
|
||
function get(req, res, next) { | ||
if (req.query.type === 'apiDoc') { | ||
return res.json(req.apiDoc); | ||
} | ||
return res.json(req.operationDoc); | ||
} | ||
get.apiDoc = { | ||
operationId: 'getApiDoc', | ||
description: 'Returns the requested apiDoc', | ||
parameters: [ | ||
{ | ||
description: 'The type of apiDoc to return.', | ||
in: 'query', | ||
name: 'type', | ||
type: 'string', | ||
enum: [ | ||
'apiDoc', | ||
'operationDoc' | ||
] | ||
} | ||
], | ||
responses: { | ||
200: { | ||
description: 'The requested apiDoc.', | ||
schema: { | ||
type: 'object' | ||
} | ||
}, | ||
default: { | ||
description: 'The requested apiDoc.', | ||
} | ||
} | ||
}; |
48 changes: 48 additions & 0 deletions
48
test/sample-projects/with-route-specs-using-modules/api-routes/users.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,48 @@ | ||
// Showing that you don't need to have apiDoc defined on methodHandlers. | ||
module.exports = { | ||
del: function(req, res, next) { | ||
// Showing how to validate responses | ||
var validationError = res.validateResponse(204, null); | ||
|
||
if (validationError) { | ||
return next(validationError); | ||
} | ||
|
||
res.status(204).send('').end(); | ||
}, | ||
get: [function(req, res, next) { | ||
res.status(200).json([{name: 'fred'}]); | ||
}], | ||
|
||
post: function(req, res, next) { | ||
res.status(500).json({}); | ||
} | ||
}; | ||
|
||
module.exports.del.apiDoc = { | ||
description: 'Delete users.', | ||
operationId: 'deleteUsers', | ||
tags: ['users'], | ||
parameters: [], | ||
responses: { | ||
204: { | ||
description: 'Users were successfully deleted.' | ||
// 204 should not return a body so not defining a schema. This adds an implicit | ||
// schema of {"type": "null"}. | ||
} | ||
} | ||
}; | ||
|
||
// showing that if parameters are empty, express-openapi adds no input middleware. | ||
// response middleware is always added. | ||
module.exports.post.apiDoc = { | ||
description: 'Create a new user.', | ||
operationId: 'createUser', | ||
tags: ['users', 'creating'], | ||
parameters: [], | ||
responses: { | ||
default: { | ||
$ref: '#/definitions/Error' | ||
} | ||
} | ||
}; |
93 changes: 93 additions & 0 deletions
93
test/sample-projects/with-route-specs-using-modules/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,93 @@ | ||
module.exports = { | ||
// parameters for all operations in this path | ||
parameters: [ | ||
{ | ||
name: 'id', | ||
in: 'path', | ||
type: 'string', | ||
required: true, | ||
description: 'Fred\'s age.' | ||
} | ||
], | ||
// method handlers may just be the method handler... | ||
get: get, | ||
// or they may also be an array of middleware + the method handler. This allows | ||
// for flexible middleware management. express-openapi middleware generated from | ||
// the <path>.parameters + <methodHandler>.apiDoc.parameters is prepended to this | ||
// array. | ||
post: [function(req, res, next) {next();}, post] | ||
}; | ||
|
||
function post(req, res) { | ||
res.status(200).json({id: req.params.id}); | ||
} | ||
|
||
// verify that apiDoc is available with middleware | ||
post.apiDoc = { | ||
description: 'Create a user.', | ||
operationId: 'createUser', | ||
tags: ['users'], | ||
parameters: [ | ||
{ | ||
name: 'user', | ||
in: 'body', | ||
schema: { | ||
$ref: '#/definitions/User' | ||
} | ||
} | ||
], | ||
|
||
responses: { | ||
default: { | ||
$ref: '#/definitions/Error' | ||
} | ||
} | ||
}; | ||
|
||
function get(req, res) { | ||
res.status(200).json({ | ||
id: req.params.id, | ||
name: req.query.name, | ||
age: req.query.age | ||
}); | ||
} | ||
|
||
get.apiDoc = { | ||
description: 'Retrieve a user.', | ||
operationId: 'getUser', | ||
tags: ['users', 'fooey'], | ||
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: { | ||
200: { | ||
$ref: '#/definitions/User' | ||
}, | ||
|
||
default: { | ||
$ref: '#/definitions/Error' | ||
} | ||
} | ||
}; |
Oops, something went wrong.