hapi-route-manager
Advanced tools
Comparing version 1.1.0 to 2.0.0
144
index.js
@@ -1,10 +0,43 @@ | ||
var walk = require("walk"), | ||
path = require("path"); | ||
var walk = require('walk'), | ||
path = require('path'), | ||
verbs = require('http-verbs'); | ||
function objectToRoutes(apiVersion, cleanRoute, routeVerbObject) { | ||
var routes = []; | ||
Object.keys(routeVerbObject).forEach((verb) => { | ||
if(!verbs[verb]) return; // only allow legit http methods in :) | ||
var routeObject = routeVerbObject[verb]; | ||
// Used for SDK creation by many plugins ;) | ||
var nickname; | ||
if (!routeObject.nickname) { | ||
nickname = cleanRoute.replace(/\/([a-z])/g, function(g) { return g[1].toUpperCase(); }); | ||
} else { | ||
nickname = routeObject.nickname; | ||
} | ||
routeObject.config = routeObject.config || {}; | ||
routeObject.config.plugins = routeObject.config.plugins || {}; | ||
routeObject.config.plugins['hapi-swaggered'] = { | ||
custom: {} | ||
}; | ||
routeObject.config.plugins['hapi-swaggered'].custom['x-swagger-js-method-name'] = nickname; | ||
routeObject.method = verb; | ||
routeObject.apiVersion = apiVersion; | ||
routes.push(routeObject); | ||
}); | ||
return routes | ||
} | ||
exports.register = function(server, options, next) { | ||
console.log("Registering routes from " + options.directory + "..."); | ||
var versionRegex = /^[v][0-9]+?$/g; | ||
var apiServer = options.select ? server.select(options.select) : server, | ||
lastErr = null, | ||
versions = {}; | ||
var versions = {}; | ||
@@ -14,44 +47,44 @@ walk.walkSync(options.directory, { | ||
file: function(root, fileStats, nextFile) { | ||
if (fileStats.name.indexOf(".route.js") !== -1) { | ||
if (fileStats.name.indexOf('.route.js') !== -1) { | ||
try { | ||
var filename = root + "/" + fileStats.name, | ||
route = root.replace(options.directory, ""), | ||
routeObject = require(path.resolve(filename)), | ||
apiVersion = route.split("/")[1], | ||
cleanRoute = route.replace("/" + apiVersion + "/", ""), | ||
nickname; | ||
var route = `${root.replace(options.directory, '')}/${fileStats.name.replace('.route.js', '')}`; | ||
var apiVersion = route.split('/')[1].match(versionRegex)[0]; | ||
var cleanRoute = route.replace('/' + apiVersion + '/', ''); // aka just no version info | ||
var filename = root + '/' + fileStats.name; | ||
var routeObject = require(path.resolve(filename)); | ||
} catch(e) { | ||
// $lab:coverage:off$ | ||
console.log(e); | ||
// $lab:coverage:on$ | ||
} | ||
// append the filenames route info | ||
route = route + "/" + fileStats.name.replace(".route.js", ""); | ||
if (!routeObject.config || !routeObject.config.nickname) { | ||
if (fileStats.name.replace(".route.js", "") !== "index") { | ||
cleanRoute = cleanRoute + "/" + fileStats.name.replace(".route.js", ""); | ||
} | ||
nickname = cleanRoute.replace(/\/([a-z])/g, function(g) { return g[1].toUpperCase(); }); | ||
} else { | ||
nickname = routeObject.config.clientName; | ||
delete routeObject.config.clientName; | ||
// move from the hapi std config to the http verb based export object | ||
if(routeObject.handler && routeObject.method) { | ||
routeObject = { | ||
[routeObject.method]: routeObject | ||
}; | ||
} | ||
if (!versions[route]) { | ||
versions[route] = []; | ||
if (!versions[apiVersion]) { | ||
versions[apiVersion] = []; | ||
} | ||
routeObject.config = routeObject.config || {}; | ||
routeObject.config.plugins = routeObject.config.plugins || {}; | ||
routeObject.config.plugins["hapi-swaggered"] = { | ||
custom: {} | ||
}; | ||
if(nickname) { | ||
routeObject.config.plugins["hapi-swaggered"].custom['x-swagger-js-method-name'] = nickname; | ||
var routes = objectToRoutes(apiVersion, cleanRoute, routeObject); | ||
try { | ||
routes.forEach((routeObj) => { | ||
routeObj.filename = path.resolve(filename); | ||
routeObj.path = path.join(route, routeObj.route || '') | ||
}); | ||
versions[apiVersion] = versions[apiVersion].concat(routes); | ||
} catch(e) { | ||
// $lab:coverage:off$ | ||
console.log(e); | ||
// $lab:coverage:on$ | ||
} | ||
routeObject.apiVersion = apiVersion; | ||
routeObject.filename = path.resolve(filename); | ||
versions[route].push(routeObject); | ||
} | ||
@@ -63,26 +96,17 @@ | ||
}); | ||
console.log(Object.keys(versions).length + " routes found. Loading now..."); | ||
Object.keys(versions).forEach(function(urlPath) { | ||
versions[urlPath].forEach(function(route, index, routes) { | ||
try { | ||
if (route.config.tags) { | ||
route.config.tags = route.config.tags.concat([ "api", route.apiVersion ]); | ||
} else { | ||
route.config.tags = [ "api", route.apiVersion ]; | ||
} | ||
server.route({ | ||
method: route.method, | ||
path: urlPath, | ||
handler: route.handler, | ||
config: route.config | ||
}); | ||
} catch (e) { | ||
console.log(e); | ||
var error = "[ERROR] A duplicate route was detected with a " + route.method; | ||
error += " request to " + path.join(urlPath, route.path || "") | ||
error += " in the file " + route.filename; | ||
Object.keys(versions).forEach(function(version) { | ||
versions[version].forEach(function(route) { | ||
if (route.config.tags) { | ||
route.config.tags = route.config.tags.concat([ 'api', route.apiVersion ]); | ||
} else { | ||
route.config.tags = [ 'api', route.apiVersion ]; | ||
} | ||
throw error; | ||
} | ||
server.route({ | ||
method: route.method, | ||
path: route.path, | ||
handler: route.handler, | ||
config: route.config | ||
}); | ||
}); | ||
@@ -95,3 +119,3 @@ }); | ||
exports.register.attributes = { | ||
pkg: require("./package.json") | ||
pkg: require('./package.json') | ||
}; |
{ | ||
"name": "hapi-route-manager", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "(Under Development) Yet another route manager for versioned HAPI REST APIs", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "./node_modules/lab/bin/lab -f -r console -t 100 -m 3000 $(find tests -name '*.test.js')" | ||
}, | ||
@@ -12,4 +12,15 @@ "author": "Matthew Apperson", | ||
"dependencies": { | ||
"boom": "^3.1.2", | ||
"http-verbs": "^1.0.0", | ||
"walk": "^2.3.9" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"code": "^2.1.1", | ||
"eslint": "^2.4.0", | ||
"hapi": "^13.2.1", | ||
"joi": "^8.0.5", | ||
"lab": "^10.3.0", | ||
"mocha": "^2.4.5" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
9829
22
237
1
1
64
3
7
3
+ Addedboom@^3.1.2
+ Addedhttp-verbs@^1.0.0
+ Addedboom@3.2.2(transitive)
+ Addedhoek@4.3.1(transitive)
+ Addedhttp-verbs@1.0.0(transitive)