-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (82 loc) · 2.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var express = require('express');
var supertest = require('supertest');
var Promise = require('bluebird');
var statuses = require('statuses');
var _ = require('lodash');
var domain = require('domain');
module.exports = Endpoint;
// TODO: export express' properties as well.
function Endpoint() {
var endpoint = express();
endpoint.root = '/';
endpoint.parent = endpoint;
endpoint._workers = [];
// Create a worker
endpoint.worker = function(filename, options) {
endpoint._workers.push = {
filename: filename,
options: options
};
};
endpoint.tester = function(path) {
return supertest(endpoint.parent)
.get(endpoint.mountpath + path)
.expect('Content-Type', /json/);
};
endpoint.mounted = false;
endpoint.on('mount', function(parent) {
endpoint.mounted = true;
endpoint.parent = parent;
});
//Create a mock of the express get method
var _get = endpoint.get.bind(endpoint);
//This is our own get interface
endpoint.get = function(path, cb) {
//@TOOD add support for middleware
_get(path, function(req, res, next) {
var ourmagic = {};
//@TODO explain what is going on here for the endpoint developer
var obj = _.merge({},req.query, req.params, ourmagic);
var callback;
//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();
d.on('error', function(err) {
console.error(err.stack);
res.status(500).json({
error: statuses[500]
});
});
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]
});
});
});
});
};
return endpoint;
}