-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.js
46 lines (40 loc) · 1.23 KB
/
tools.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
var Tools = module.exports = {};
//Formats an oData-V2-style JSON response
var format = Tools.format = function(host,uri,type,records) {
var __index = 0;
var baseuri = "//" + host;
if (!(records instanceof Array)) records = [records];
var oData = {d:{
__count : records.length,
results : records.map(function(rec){
rec.__index = __index++;
rec.__metadata = rec.__metadata || {};
rec.__metadata.uri = rec.__metadata.uri || (baseuri+uri);
rec.__metadata.type = rec.__metadata.type || type;
return rec;
})
}};
if (records.__prev) oData.d.__prev = records.__prev;
if (records.__next) oData.d.__next = records.__next;
return oData;
};
//Formats an oData-V2-style JSON error response
var error = Tools.error = function(err,code,message,innererror) {
code = (code||500).toString();
message = message||"Internal Server Error";
var oData = {error:{
code : code,
message : message,
innererror : innererror||err
}};
return oData;
};
var buildroutestring = Tools.buildroutestring = function(name,root,method) {
var routestring = root + name + "/";
for(var p in method.params) {
if (method.params.hasOwnProperty(p)) {
routestring += ":" + p + "/";
}
}
return routestring + "?";
};