@sera4/essentia 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +4 -0
- package/logger/s4-logger.js +35 -0
- package/package.json +24 -0
- package/paginator/s4-pagination.js +50 -0
- package/test/s4-pagination.spec.js +195 -0
- package/test-results/result.xml +61 -0
package/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const winston = require('winston');
|
|
2
|
+
const logger = new winston.Logger();
|
|
3
|
+
const loggerName = "console.debug";
|
|
4
|
+
|
|
5
|
+
logger.add(winston.transports.Console, {
|
|
6
|
+
name: loggerName,
|
|
7
|
+
colorize: true,
|
|
8
|
+
showLevel: true,
|
|
9
|
+
level: 'debug'
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
class S4Logger {
|
|
13
|
+
setLevel(level) {
|
|
14
|
+
if (level && ["debug", "info", "error"].indexOf(level.toLowerCase())) {
|
|
15
|
+
logger.transports[loggerName].level = level;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
mute() {
|
|
19
|
+
logger.transports[loggerName].silent = true;
|
|
20
|
+
}
|
|
21
|
+
unmute() {
|
|
22
|
+
logger.transports[loggerName].silent = false;
|
|
23
|
+
}
|
|
24
|
+
debug(str, args) {
|
|
25
|
+
logger.log("debug", str, args);
|
|
26
|
+
}
|
|
27
|
+
info(str, args) {
|
|
28
|
+
logger.log("info", str, args);
|
|
29
|
+
}
|
|
30
|
+
error(str, args) {
|
|
31
|
+
logger.log("error", str, args);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = new S4Logger();
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sera4/essentia",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A library of utilities for Teleporte Web Services",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "./node_modules/.bin/mocha --reporter mocha-junit-reporter --reporter-options mochaFile=./test-results/result.xml --exit",
|
|
8
|
+
"testdev": "./node_modules/.bin/mocha --watch"
|
|
9
|
+
},
|
|
10
|
+
"author": "Dev DM <dev@sera4.com>",
|
|
11
|
+
"license": "UNLICENSED",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"chai": "^4.1.2",
|
|
14
|
+
"mocha": "^5.1.1",
|
|
15
|
+
"mocha-junit-reporter": "^1.17.0",
|
|
16
|
+
"node-mocks-http": "^1.5.8",
|
|
17
|
+
"nodemon": "^1.17.3",
|
|
18
|
+
"should": "^13.2.1",
|
|
19
|
+
"sinon": "^5.0.1"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"winston": "^2.4.2"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const default_page = 1;
|
|
2
|
+
const default_size = 5;
|
|
3
|
+
|
|
4
|
+
function parseRequest(req, item, default_value) {
|
|
5
|
+
if (!req || !req.query) {
|
|
6
|
+
return default_value;
|
|
7
|
+
}
|
|
8
|
+
var r = parseInt(req.query[item]);
|
|
9
|
+
if (isNaN(r) || r < default_value) {
|
|
10
|
+
r = default_value;
|
|
11
|
+
}
|
|
12
|
+
return r;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
class S4Pagination {
|
|
16
|
+
createPaginator(req, res, next) {
|
|
17
|
+
var paginator = {
|
|
18
|
+
page : parseRequest(req, "page", default_page),
|
|
19
|
+
page_size : parseRequest(req, "page_size", default_size),
|
|
20
|
+
reply : function(data) {
|
|
21
|
+
if (!data) {
|
|
22
|
+
res.status(500).end();
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
var pagination = {
|
|
26
|
+
total : data.total,
|
|
27
|
+
total_pages : data.pages,
|
|
28
|
+
current_page : data.page,
|
|
29
|
+
first_page : data.page === 1,
|
|
30
|
+
last_page : data.offset === data.pages,
|
|
31
|
+
page_size : data.limit,
|
|
32
|
+
out_of_bounds : data.docs.length === 0
|
|
33
|
+
}
|
|
34
|
+
if (!pagination.out_of_bounds) {
|
|
35
|
+
if (pagination.current_page > 1) {
|
|
36
|
+
pagination.previous_page = pagination.current_page - 1;
|
|
37
|
+
}
|
|
38
|
+
if (pagination.current_page < pagination.total_pages) {
|
|
39
|
+
pagination.next_page = pagination.current_page + 1;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
res.set("s4-pagination", JSON.stringify(pagination));
|
|
43
|
+
res.json(data.docs)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return paginator;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports = new S4Pagination();
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
var should = require('should');
|
|
2
|
+
var S4Pagination = require('../paginator/s4-pagination');
|
|
3
|
+
var http_mocks = require('node-mocks-http');
|
|
4
|
+
|
|
5
|
+
describe("Pagination", function () {
|
|
6
|
+
var paginator = null;
|
|
7
|
+
var request = null;
|
|
8
|
+
var response = null;
|
|
9
|
+
var next = null;
|
|
10
|
+
|
|
11
|
+
before("Setup", function () {
|
|
12
|
+
next = function () { };
|
|
13
|
+
response = http_mocks.createResponse({ eventEmitter: require('events').EventEmitter });
|
|
14
|
+
request = http_mocks.createRequest({
|
|
15
|
+
method: "GET",
|
|
16
|
+
url: "/"
|
|
17
|
+
})
|
|
18
|
+
paginator = S4Pagination.createPaginator(request, response, next);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe("Parsing query parameters", function () {
|
|
22
|
+
describe("Default values", function () {
|
|
23
|
+
it("should have default page set to 1", function () {
|
|
24
|
+
paginator.should.have.property("page", 1);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should have default page size set to 5", function () {
|
|
28
|
+
paginator.should.have.property("page_size", 5);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("Page value set to 0", function () {
|
|
33
|
+
before("Setup", function () {
|
|
34
|
+
request = http_mocks.createRequest({
|
|
35
|
+
method: "GET",
|
|
36
|
+
url: "/?page=0"
|
|
37
|
+
})
|
|
38
|
+
paginator = S4Pagination.createPaginator(request, response, next);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should set page to 1", function () {
|
|
42
|
+
paginator.should.have.property("page", 1);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("Page value set to negative", function () {
|
|
47
|
+
before("Setup", function () {
|
|
48
|
+
request = http_mocks.createRequest({
|
|
49
|
+
method: "GET",
|
|
50
|
+
url: "/?page=-1"
|
|
51
|
+
})
|
|
52
|
+
paginator = S4Pagination.createPaginator(request, response, next);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("should set page to 1", function () {
|
|
56
|
+
paginator.should.have.property("page", 1);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("Page value set to 2", function () {
|
|
61
|
+
before("Setup", function () {
|
|
62
|
+
request = http_mocks.createRequest({
|
|
63
|
+
method: "GET",
|
|
64
|
+
url: "/?page=2"
|
|
65
|
+
})
|
|
66
|
+
paginator = S4Pagination.createPaginator(request, response, next);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should set page to 2", function () {
|
|
70
|
+
paginator.should.have.property("page", 2);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("Page value set to 10", function () {
|
|
75
|
+
before("Setup", function () {
|
|
76
|
+
request = http_mocks.createRequest({
|
|
77
|
+
method: "GET",
|
|
78
|
+
url: "/?page=10"
|
|
79
|
+
})
|
|
80
|
+
paginator = S4Pagination.createPaginator(request, response, next);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should set page to 10", function () {
|
|
84
|
+
paginator.should.have.property("page", 10);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("Page value set to 100", function () {
|
|
89
|
+
before("Setup", function () {
|
|
90
|
+
request = http_mocks.createRequest({
|
|
91
|
+
method: "GET",
|
|
92
|
+
url: "/?page=100"
|
|
93
|
+
})
|
|
94
|
+
paginator = S4Pagination.createPaginator(request, response, next);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("should set page to 100", function () {
|
|
98
|
+
paginator.should.have.property("page", 100);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe("Page Size value set to 0", function () {
|
|
103
|
+
before("Setup", function () {
|
|
104
|
+
request = http_mocks.createRequest({
|
|
105
|
+
method: "GET",
|
|
106
|
+
url: "/?page_size=0"
|
|
107
|
+
})
|
|
108
|
+
paginator = S4Pagination.createPaginator(request, response, next);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("should set page size to 5", function () {
|
|
112
|
+
paginator.should.have.property("page_size", 5);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("Page Size value set to negative", function () {
|
|
117
|
+
before("Setup", function () {
|
|
118
|
+
request = http_mocks.createRequest({
|
|
119
|
+
method: "GET",
|
|
120
|
+
url: "/?page_size=-1"
|
|
121
|
+
})
|
|
122
|
+
paginator = S4Pagination.createPaginator(request, response, next);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("should set page size to 5", function () {
|
|
126
|
+
paginator.should.have.property("page_size", 5);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe("Page Size value set to 2", function () {
|
|
131
|
+
before("Setup", function () {
|
|
132
|
+
request = http_mocks.createRequest({
|
|
133
|
+
method: "GET",
|
|
134
|
+
url: "/?page_size=2"
|
|
135
|
+
})
|
|
136
|
+
paginator = S4Pagination.createPaginator(request, response, next);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("should set page size to 5", function () {
|
|
140
|
+
paginator.should.have.property("page_size", 5);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe("Page Size value set to 10", function () {
|
|
145
|
+
before("Setup", function () {
|
|
146
|
+
request = http_mocks.createRequest({
|
|
147
|
+
method: "GET",
|
|
148
|
+
url: "/?page_size=10"
|
|
149
|
+
})
|
|
150
|
+
paginator = S4Pagination.createPaginator(request, response, next);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("should set page size to 10", function () {
|
|
154
|
+
paginator.should.have.property("page_size", 10);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe("Page Size value set to 100", function () {
|
|
159
|
+
before("Setup", function () {
|
|
160
|
+
request = http_mocks.createRequest({
|
|
161
|
+
method: "GET",
|
|
162
|
+
url: "/?page_size=100"
|
|
163
|
+
})
|
|
164
|
+
paginator = S4Pagination.createPaginator(request, response, next);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("should set page size to 100", function () {
|
|
168
|
+
paginator.should.have.property("page_size", 100);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
describe("Replying", function() {
|
|
174
|
+
beforeEach("Clear request", function() {
|
|
175
|
+
response = http_mocks.createResponse({ eventEmitter: require('events').EventEmitter });
|
|
176
|
+
paginator = S4Pagination.createPaginator(request, response, next);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it ("should return 500 if data is null", function(){
|
|
180
|
+
paginator.reply(null);
|
|
181
|
+
response.statusCode.should.equal(500);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it ("should return pagination data", function(){
|
|
185
|
+
var items = [1,2,3,4,5,6,7,8,9,10];
|
|
186
|
+
data = {
|
|
187
|
+
docs : items,
|
|
188
|
+
total : items.length,
|
|
189
|
+
};
|
|
190
|
+
paginator.reply(data);
|
|
191
|
+
response.statusCode.should.equal(200);
|
|
192
|
+
response._getHeaders().should.have.property("s4-pagination");
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<testsuites name="Mocha Tests" time="0.007" tests="14" failures="0">
|
|
3
|
+
<testsuite name="Root Suite" timestamp="2018-06-06T13:57:10" tests="0" failures="0" time="0">
|
|
4
|
+
</testsuite>
|
|
5
|
+
<testsuite name="Pagination" timestamp="2018-06-06T13:57:10" tests="0" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0">
|
|
6
|
+
</testsuite>
|
|
7
|
+
<testsuite name="Parsing query parameters" timestamp="2018-06-06T13:57:10" tests="0" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0">
|
|
8
|
+
</testsuite>
|
|
9
|
+
<testsuite name="Default values" timestamp="2018-06-06T13:57:10" tests="2" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0.003">
|
|
10
|
+
<testcase name="Pagination Parsing query parameters Default values should have default page set to 1" time="0.002" classname="should have default page set to 1">
|
|
11
|
+
</testcase>
|
|
12
|
+
<testcase name="Pagination Parsing query parameters Default values should have default page size set to 5" time="0.001" classname="should have default page size set to 5">
|
|
13
|
+
</testcase>
|
|
14
|
+
</testsuite>
|
|
15
|
+
<testsuite name="Page value set to 0" timestamp="2018-06-06T13:57:10" tests="1" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0.001">
|
|
16
|
+
<testcase name="Pagination Parsing query parameters Page value set to 0 should set page to 1" time="0.001" classname="should set page to 1">
|
|
17
|
+
</testcase>
|
|
18
|
+
</testsuite>
|
|
19
|
+
<testsuite name="Page value set to negative" timestamp="2018-06-06T13:57:10" tests="1" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0">
|
|
20
|
+
<testcase name="Pagination Parsing query parameters Page value set to negative should set page to 1" time="0" classname="should set page to 1">
|
|
21
|
+
</testcase>
|
|
22
|
+
</testsuite>
|
|
23
|
+
<testsuite name="Page value set to 2" timestamp="2018-06-06T13:57:10" tests="1" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0">
|
|
24
|
+
<testcase name="Pagination Parsing query parameters Page value set to 2 should set page to 2" time="0" classname="should set page to 2">
|
|
25
|
+
</testcase>
|
|
26
|
+
</testsuite>
|
|
27
|
+
<testsuite name="Page value set to 10" timestamp="2018-06-06T13:57:10" tests="1" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0">
|
|
28
|
+
<testcase name="Pagination Parsing query parameters Page value set to 10 should set page to 10" time="0" classname="should set page to 10">
|
|
29
|
+
</testcase>
|
|
30
|
+
</testsuite>
|
|
31
|
+
<testsuite name="Page value set to 100" timestamp="2018-06-06T13:57:10" tests="1" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0">
|
|
32
|
+
<testcase name="Pagination Parsing query parameters Page value set to 100 should set page to 100" time="0" classname="should set page to 100">
|
|
33
|
+
</testcase>
|
|
34
|
+
</testsuite>
|
|
35
|
+
<testsuite name="Page Size value set to 0" timestamp="2018-06-06T13:57:10" tests="1" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0">
|
|
36
|
+
<testcase name="Pagination Parsing query parameters Page Size value set to 0 should set page size to 5" time="0" classname="should set page size to 5">
|
|
37
|
+
</testcase>
|
|
38
|
+
</testsuite>
|
|
39
|
+
<testsuite name="Page Size value set to negative" timestamp="2018-06-06T13:57:10" tests="1" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0">
|
|
40
|
+
<testcase name="Pagination Parsing query parameters Page Size value set to negative should set page size to 5" time="0" classname="should set page size to 5">
|
|
41
|
+
</testcase>
|
|
42
|
+
</testsuite>
|
|
43
|
+
<testsuite name="Page Size value set to 2" timestamp="2018-06-06T13:57:10" tests="1" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0.001">
|
|
44
|
+
<testcase name="Pagination Parsing query parameters Page Size value set to 2 should set page size to 5" time="0.001" classname="should set page size to 5">
|
|
45
|
+
</testcase>
|
|
46
|
+
</testsuite>
|
|
47
|
+
<testsuite name="Page Size value set to 10" timestamp="2018-06-06T13:57:10" tests="1" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0">
|
|
48
|
+
<testcase name="Pagination Parsing query parameters Page Size value set to 10 should set page size to 10" time="0" classname="should set page size to 10">
|
|
49
|
+
</testcase>
|
|
50
|
+
</testsuite>
|
|
51
|
+
<testsuite name="Page Size value set to 100" timestamp="2018-06-06T13:57:10" tests="1" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0">
|
|
52
|
+
<testcase name="Pagination Parsing query parameters Page Size value set to 100 should set page size to 100" time="0" classname="should set page size to 100">
|
|
53
|
+
</testcase>
|
|
54
|
+
</testsuite>
|
|
55
|
+
<testsuite name="Replying" timestamp="2018-06-06T13:57:10" tests="2" file="/Users/denis/Development/lockedup/repos/s4-node-modules/test/s4-pagination.spec.js" failures="0" time="0.002">
|
|
56
|
+
<testcase name="Pagination Replying should return 500 if data is null" time="0.001" classname="should return 500 if data is null">
|
|
57
|
+
</testcase>
|
|
58
|
+
<testcase name="Pagination Replying should return pagination data" time="0.001" classname="should return pagination data">
|
|
59
|
+
</testcase>
|
|
60
|
+
</testsuite>
|
|
61
|
+
</testsuites>
|