New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

hapi-route-manager

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-route-manager - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

.eslintrc

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"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc