-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocols.js
57 lines (51 loc) · 1.35 KB
/
protocols.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
var instance = {
init : init,
ok : ok,
notOk : notOk,
customMessage : customMessage,
appendMessage : appendMessage,
STATUS_OK : 200,
STATUS_ERR : 501,
STATUS_ERR_UNKNOWN : 500,
STATUS_NOT_FOUND : 404,
OK : { ok: true },
NOT_OK : { ok: false },
DATA_NOT_FOUND : { ok: false, message: 'Data not found' },
INTERNAL_SERVER_ERROR : { ok: false, message: 'Internal Server Error' }
};
function ok(data)
{
return Object.assign(Object.assign({}, instance.OK), data);
}
/**
* err : Error object
* - protocol : will use this object along with NOT_OK
* - errCode
* - message
*/
function notOk(err)
{
if (err) {
if (err.protocol) {
var protocol = Object.assign({}, instance.NOT_OK);
return Object.assign(protocol, err.protocol);
}
return { ok: false, errCode: err.errCode, error: err.message };
}
return instance.NOT_OK;
}
function appendMessage(protocol, message)
{
return customMessage(protocol, protocol.message + ' ' + message);
}
function customMessage(protocol, message)
{
var newProtocol = Object.assign({}, protocol);
newProtocol.message = message;
return newProtocol;
}
function init(otherProtocols)
{
Object.assign(instance, otherProtocols);
}
module.exports = instance;