-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (63 loc) · 1.91 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
/**
* Created by karl on 14/07/15.
*/
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.init = void 0;
function init(express) {
function success(data, status) {
if (data === void 0) { data = "Done"; }
if (status === void 0) { status = 200; }
var message = {
status: "success",
data: data,
};
if (this.req.log) {
this.req.log.trace({ message: message }, "Sending success response");
}
this.status(status).json(message);
}
function fail(data, status) {
if (status === void 0) { status = 400; }
var message = {
status: "fail",
data: data,
};
if (this.req.log) {
this.req.log.trace({ message: message }, "Sending fail response");
}
this.status(status).json(message);
}
function error(err, status) {
if (status === void 0) { status = 500; }
if (typeof err !== "object") {
err = {
message: err,
};
}
err.status = "error";
if (this.req.log) {
this.req.log.trace({ message: err }, "Sending error response");
}
this.status(err.code || status).json(err);
}
function partial(data, status) {
if (status === void 0) { status = 206; }
if (!data.limit && data.data && data.data.length) {
data.limit = data.offset + (data.data.length - 1);
}
var header = data.offset + "-" + data.limit + "/" + data.count;
this.header("Content-Range", header);
if (this.req.log) {
this.req.log.trace({ header: header }, "Sending partial response");
}
this.success(data.data, status);
}
Object.assign(express.response, {
success: success,
fail: fail,
error: error,
partial: partial,
});
}
exports.init = init;