This repository has been archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
V2.x #72
Merged
Merged
V2.x #72
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d0c2864
use eslint
subeeshcbabu a2f1e94
use merge-object-files and validator modules
subeeshcbabu 2a83f79
Unit tests
subeeshcbabu a46b49e
Fix lint errors
subeeshcbabu 9f4f8d1
bad api def test case
subeeshcbabu 9f9f0d6
test case for callback response
subeeshcbabu cc45191
Use swagvali module
subeeshcbabu f976a12
Use spec instead of parameter key
subeeshcbabu 9409d1e
API change to add spec instead of parameter. joi schema is not suppor…
subeeshcbabu 54e57c7
use latest swagvali
subeeshcbabu 6d8cebf
return resolved api object also as part of route response object
subeeshcbabu 6ce4ddc
omitting, handler options should not fail. Use default handler dir, o…
subeeshcbabu cd5885e
The constructed handler obj option should have $ prefixed operation keys
subeeshcbabu aadc569
2.0.0 CHANGELOG
subeeshcbabu 65aa4f6
breaking changes in changelog
subeeshcbabu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage | ||
templates | ||
test/fixtures |
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,25 @@ | ||
{ | ||
"rules": { | ||
"indent": [ | ||
2, | ||
4 | ||
], | ||
"quotes": [ | ||
2, | ||
"single" | ||
], | ||
"linebreak-style": [ | ||
2, | ||
"unix" | ||
], | ||
"semi": [ | ||
2, | ||
"always" | ||
] | ||
}, | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": "eslint:recommended" | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,32 +1,15 @@ | ||
### Unreleased | ||
## v2.0.0 | ||
|
||
- `options.security` to define the security authorize handlers directory. | ||
### Bugfixes | ||
|
||
### 1.0.8 | ||
- #67 - swagger schema extensions (^x-) in info section are not allowed | ||
- #63 - Incorrect property path on validation errors | ||
- #73 - Validation for multiple $ref parameters doesn't work | ||
|
||
- Added support for allowEmptyValue. | ||
### Features | ||
|
||
### 1.0.7 | ||
|
||
### 1.0.6 | ||
|
||
### 1.0.5 | ||
|
||
### 1.0.4 | ||
|
||
### 1.0.3 | ||
|
||
- Add schema from NPM. | ||
|
||
### 1.0.2 | ||
|
||
- Added custom 'file' type to validators. | ||
|
||
### 1.0.1 | ||
|
||
- Fixed a bug where basePath was being changed to resourcePath. | ||
|
||
### 1.0.0 (renamed to swaggerize-routes) | ||
|
||
- Alternately specify `x-handler` properties to reference handlers at either the operation or path level. | ||
- Added security definitions to route. | ||
- Use [swagger-parser](https://github.com/BigstickCarpet/swagger-parser) to validate the Swagger spec. | ||
- The api is dereferenced (both remote and local $ref are dereferenced) using [swagger-parser](https://github.com/BigstickCarpet/swagger-parser) #40 | ||
- Use [JSON schema validator](https://github.com/mafintosh/is-my-json-valid) as the default validator. #30. | ||
- Option to set `joischema` to `true` to use [Joi](https://github.com/hapijs/joi) schema validator. Uses [enjoi](https://github.com/tlivings/enjoi) - The json to joi schema converter - to build the validator functions. | ||
- By default, validators are provided for `response` schema also (in addition to input `parameters`). |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
//const Fs = require('fs'); | ||
const { merge } = require('merge-object-files'); | ||
const Thing = require('core-util-is'); | ||
const Utils = require('../utils'); | ||
const buildSecurity = require('./security'); | ||
const buildValidator = require('swagvali'); | ||
|
||
const Buildroutes = (apiResolver, options) => { | ||
let { handlers, basedir } = options; | ||
let fileResolver = Thing.isObject(handlers) | ||
// Use the handlers object | ||
? Promise.resolve(handlers) | ||
// Construct the handler object using `merge-object-files` for a valid dir path. | ||
: (Thing.isString(handlers) ? merge(handlers, [ 'js' ]) : Promise.resolve(null)); | ||
let validatorResolver = buildValidator(apiResolver, { validated: true}); | ||
|
||
return Promise.all([ apiResolver, fileResolver, validatorResolver ]).then(resolved => { | ||
let routes = []; | ||
let [ api, files, validators ] = resolved; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guessing order is maintained from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
Object.keys(api.paths).forEach(path => { | ||
let pathObj = api.paths[path]; | ||
Object.keys(pathObj).forEach(operation => { | ||
let operationObj; | ||
let handler; | ||
//If the key is not a valid operation, skip the iteration. | ||
if (!Utils.verbs.includes(operation)) { | ||
return; | ||
} | ||
operationObj = pathObj[operation]; | ||
//Find the handler function from handler directory. | ||
handler = pathfinder(path, operation, files); | ||
if(!handler) { | ||
//Resolve the x-handler if exists | ||
handler = operationObj['x-handler'] || pathObj['x-handler']; | ||
if (handler) { | ||
handler = Utils.resolve(basedir, handler, operation); | ||
} | ||
} | ||
//Push the handler to the route definition. | ||
if (handler){ | ||
routes.push({ | ||
path, | ||
name: operationObj.operationId, | ||
description: operationObj.description, | ||
method: operation, | ||
security: buildSecurity(operationObj.security || pathObj.security || api.security, api.securityDefinitions, options), | ||
validators: validators[path][operation]['parameters'], | ||
handler, | ||
consumes: operationObj.consumes || api.consumes, | ||
produces: operationObj.produces || api.produces | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
return { | ||
api, | ||
routes | ||
}; | ||
}); | ||
}; | ||
|
||
const opsfinder = (source, operation) => { | ||
let $find = source[`$${operation}`]; | ||
// Check if there is an operation key prefixed with `$` sign. (e.g:- $get, $post) | ||
// Handlers option passed as an object should have `$` prefixed operation key | ||
// to avoid namespace collisions. | ||
if ($find) { | ||
return $find; | ||
} | ||
return source[operation]; | ||
}; | ||
|
||
const pathfinder = (path, operation, files) => { | ||
let pathnames = path.split('/').filter(el => el); | ||
if (!files) { | ||
return; | ||
} | ||
return (pathnames[0] ? matchpath(operation, pathnames, files[pathnames[0]]) : opsfinder(files, operation)); | ||
}; | ||
|
||
/** | ||
* Match a route handler to a given path name set. | ||
* @param method | ||
* @param pathnames | ||
* @param handlers | ||
* @returns {*} | ||
*/ | ||
const matchpath = (method, pathnames, handlers) => { | ||
if (!handlers) { | ||
return null; | ||
} | ||
if (pathnames.length > 1) { | ||
pathnames.shift(); | ||
return matchpath(method, pathnames, handlers[pathnames[0]]); | ||
} | ||
|
||
return handlers[pathnames[0]] ? handlers[pathnames[0]] : opsfinder(handlers, method); | ||
}; | ||
|
||
module.exports = Buildroutes; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lingering comment