-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (68 loc) · 1.63 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
var Response = function() {
var _context
, _contentType
, _defaultContentType = 'application/json'
, _defaultHttpStatusCode = 200
, _headers = {}
, _httpStatusCode;
this.setContentType = function(type) {
this.setHeader('Content-Type', type);
_contentType = type;
};
this.setContext = function(context) {
_context = context;
};
this.setHeader = function(name, value) {
_headers[name] = value;
};
this.setHttpStatusCode = function(code) {
var headerString
, reason = '';
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html
switch(code) {
case 200:
reason = 'OK';
break;
case 201:
reason = 'Created';
break;
case 202:
reason = 'Accepted';
break;
case 400:
reason = 'Bad Request';
break;
case 401:
reason = 'Unauthorized';
break;
case 403:
reason = 'Forbidden';
break;
case 404:
reason = 'Not Found';
break;
case 409:
reason = 'Conflict';
break;
default:
reason = 'Unknown';
break;
}
this.setHeader('Status', code + ' ' + reason);
_httpStatusCode = code;
},
// success codes
this.send = function(data) {
result = {};
if(typeof(_httpStatusCode) === 'undefined') {
this.setHttpStatusCode(_defaultHttpStatusCode);
}
if(typeof(_contentType) === 'undefined') {
this.setContentType(_defaultContentType);
}
result['headers'] = _headers;
result['data'] = data;
_context.succeed(result);
}
};
module.exports = Response;