This repository was archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathJayService.js
More file actions
98 lines (84 loc) · 3.32 KB
/
JayService.js
File metadata and controls
98 lines (84 loc) · 3.32 KB
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
$data.Class.define("$data.JayService", null, null, {
constructor:function () {
}
}, {
serviceFunction:function (fn) {
var f = fn || function (fn) {
var keys = Object.keys(f);
for (var i = 0; i < keys.length; i++) {
fn[keys[i]] = f[keys[i]];
}
return fn;
};
f.param = function (name, type) {
f.params = f.params || [];
f.params.push({name:name, type:type});
return f;
};
f.returns = function (type) {
f.returnType = type;
return f;
};
f.returnsArrayOf = function (type) {
f.returnType = "Array";
f.elementType = type;
return f;
};
f.returnsCollectionOf = function (type) {
f.returnType = "Collection";
f.elementType = type;
return f;
};
f.httpMethod = function(type){
f.method = type || 'GET';
return f;
};
return f;
},
createAdapter: function (type, instanceFactory) {
/// <signature>
/// <summary>JSONObjectAdapter middleware factory for Express and Connect</summary>
/// <description>JSONObjectAdapter middleware factory for Express and Connect</description>
/// <param name="type" type="function">Service class type</param>
/// <param name="instanceFactory" type="function">Service class instance factory</param>
/// <return type="function" />
/// </signature>
var self = this;
return function (req, res, next) {
self.routeParser(this, req);
var factory = instanceFactory || function() { return new type; };
var adapter = new $data.JSObjectAdapter(type, factory);
adapter.handleRequest(req, res, function (err) {
if (typeof err === "string") err = new Error(err);
self.errorHandler.call(this, err, req, res, next);
});
}
},
resultAsXml:function (data) {
return new $data.XmlResult(data);
},
routeParser: function (app, req) {
if (!(typeof req.fullRoute === 'string' && req.fullRoute.length)){
var schema = 'http';
if (req && req.headers) {
if (req.connection.encrypted || req.headers['X-Forwarded-Protocol'] === 'https' || req.headers['x-forwarded-protocol'] === 'https')
schema += 's';
req.fullRoute = (req.baseRoute || (schema + '://' + req.headers.host)) + (app.route || req.baseUrl || req.originalUrl.replace(req.url, ''));
}
}
},
errorHandler: function (err, req, res, next) {
var accept = req.headers.accept || '';
if (~accept.indexOf('json')) {
if (err.status) res.statusCode = err.status;
if (res.statusCode < 400) res.statusCode = 500;
var error = { message: { value: err.message, lang: err.lang || 'en-US' }, stack: err.stack };
for (var prop in err) { if (prop !== 'message') error[prop] = err[prop]; }
var json = JSON.stringify({ error: error });
res.setHeader('Content-Type', 'application/json');
res.end(json);
} else {
next(err);
}
}
});