apis-endpoint
Advanced tools
Comparing version 0.2.0 to 0.2.1
67
index.js
@@ -6,2 +6,3 @@ var express = require('express'); | ||
var _ = require('lodash'); | ||
var domain = require('domain'); | ||
@@ -56,31 +57,47 @@ module.exports = Endpoint; | ||
if(cb.length === 3){ | ||
//A function has been promisified | ||
callback = cb(); | ||
}else if(typeof cb === 'object'){ | ||
//It is just a new Promise, should not be like that though | ||
//it always returns the same response | ||
callback = cb; | ||
}else if(cb.length === 2){ | ||
//It is a standard callback | ||
callback = Promise.promisify(cb)(obj); | ||
}else{ | ||
//It is already a promise | ||
callback = cb(obj); | ||
} | ||
//If the endpoint does something terribly wrong it crashes the whole app. | ||
//Therefore we need a good strategy to solve it. | ||
//For now lets use error domains | ||
var d = domain.create(); | ||
callback | ||
.then(function(data) { | ||
res.json(data); | ||
}) | ||
.catch(function(err) { | ||
console.error(err.stack); | ||
d.on('error', function(err) { | ||
console.error(err.stack); | ||
var code = parseInt(err.message); | ||
res.status(500).json({ | ||
error: statuses[500] | ||
}); | ||
}); | ||
err = !isNaN(parseFloat(code)) && isFinite(code) ? code : 500; | ||
res.status(err).json({ | ||
error: statuses[err] | ||
d.run(function() { | ||
if(cb.length === 3){ | ||
//A function has been promisified | ||
callback = cb(); | ||
}else if(typeof cb === 'object'){ | ||
//It is just a new Promise, should not be like that though | ||
//it always returns the same response | ||
callback = cb; | ||
}else if(cb.length === 2){ | ||
//It is a standard callback | ||
callback = Promise.promisify(cb)(obj); | ||
}else{ | ||
//It is already a promise | ||
callback = cb(obj); | ||
} | ||
callback | ||
.then(function(data) { | ||
res.json(data); | ||
}) | ||
.catch(function(err) { | ||
console.error(err.stack); | ||
var code = parseInt(err.message); | ||
err = !isNaN(parseFloat(code)) && isFinite(code) ? code : 500; | ||
res.status(err).json({ | ||
error: statuses[err] | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -87,0 +104,0 @@ }; |
{ | ||
"name": "apis-endpoint", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
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
2730
82
1