@sera4/essentia 1.1.65 → 3.0.6
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/README.md +352 -8
- package/dist/ts/cache/index.d.ts +33 -0
- package/dist/ts/cache/index.js +171 -0
- package/dist/ts/cache/index.js.map +1 -0
- package/dist/ts/configurations/server-config.d.ts +4 -0
- package/dist/ts/configurations/server-config.js +2 -0
- package/dist/ts/configurations/server-config.js.map +1 -0
- package/dist/ts/constants/index.d.ts +7 -0
- package/{constants → dist/ts/constants}/index.js +5 -6
- package/dist/ts/constants/index.js.map +1 -0
- package/dist/ts/cycle/index.d.ts +53 -0
- package/dist/ts/cycle/index.js +169 -0
- package/dist/ts/cycle/index.js.map +1 -0
- package/dist/ts/dns/DnsCache.d.ts +50 -0
- package/dist/ts/dns/DnsCache.js +137 -0
- package/dist/ts/dns/DnsCache.js.map +1 -0
- package/dist/ts/dns/index.d.ts +4 -0
- package/dist/ts/dns/index.js +4 -0
- package/dist/ts/dns/index.js.map +1 -0
- package/dist/ts/formatter/index.d.ts +44 -0
- package/dist/ts/formatter/index.js +76 -0
- package/dist/ts/formatter/index.js.map +1 -0
- package/dist/ts/hal/index.d.ts +26 -0
- package/dist/ts/hal/index.js +70 -0
- package/dist/ts/hal/index.js.map +1 -0
- package/dist/ts/health/index.d.ts +53 -0
- package/dist/ts/health/index.js +165 -0
- package/dist/ts/health/index.js.map +1 -0
- package/dist/ts/helpers/index.d.ts +1 -0
- package/dist/ts/helpers/index.js +2 -0
- package/dist/ts/helpers/index.js.map +1 -0
- package/dist/ts/helpers/test-server-wrapper.d.ts +56 -0
- package/dist/ts/helpers/test-server-wrapper.js +144 -0
- package/dist/ts/helpers/test-server-wrapper.js.map +1 -0
- package/dist/ts/index.d.ts +21 -0
- package/dist/ts/index.js +25 -0
- package/dist/ts/index.js.map +1 -0
- package/dist/ts/last-commit/index.d.ts +12 -0
- package/dist/ts/last-commit/index.js +82 -0
- package/dist/ts/last-commit/index.js.map +1 -0
- package/dist/ts/logger/index.d.ts +3 -0
- package/dist/ts/logger/index.js +4 -0
- package/dist/ts/logger/index.js.map +1 -0
- package/dist/ts/logger/s4-logger.d.ts +23 -0
- package/dist/ts/logger/s4-logger.js +103 -0
- package/dist/ts/logger/s4-logger.js.map +1 -0
- package/dist/ts/paginator/index.d.ts +2 -0
- package/dist/ts/paginator/index.js +3 -0
- package/dist/ts/paginator/index.js.map +1 -0
- package/dist/ts/paginator/s4-pagination.d.ts +26 -0
- package/dist/ts/paginator/s4-pagination.js +49 -0
- package/dist/ts/paginator/s4-pagination.js.map +1 -0
- package/dist/ts/paginator/sql-pagination.d.ts +52 -0
- package/dist/ts/paginator/sql-pagination.js +157 -0
- package/dist/ts/paginator/sql-pagination.js.map +1 -0
- package/dist/ts/paper-trail/helpers.d.ts +14 -0
- package/dist/ts/paper-trail/helpers.js +74 -0
- package/dist/ts/paper-trail/helpers.js.map +1 -0
- package/dist/ts/paper-trail/index.d.ts +54 -0
- package/dist/ts/paper-trail/index.js +236 -0
- package/dist/ts/paper-trail/index.js.map +1 -0
- package/dist/ts/prompts/index.d.ts +21 -0
- package/dist/ts/prompts/index.js +68 -0
- package/dist/ts/prompts/index.js.map +1 -0
- package/dist/ts/queue/index.d.ts +196 -0
- package/dist/ts/queue/index.js +371 -0
- package/dist/ts/queue/index.js.map +1 -0
- package/dist/ts/queue/publisher.d.ts +41 -0
- package/dist/ts/queue/publisher.js +84 -0
- package/dist/ts/queue/publisher.js.map +1 -0
- package/dist/ts/queue/queue-logger.d.ts +10 -0
- package/dist/ts/queue/queue-logger.js +35 -0
- package/dist/ts/queue/queue-logger.js.map +1 -0
- package/dist/ts/queue/subscriber.d.ts +55 -0
- package/dist/ts/queue/subscriber.js +98 -0
- package/dist/ts/queue/subscriber.js.map +1 -0
- package/dist/ts/safe-proxy/index.d.ts +35 -0
- package/dist/ts/safe-proxy/index.js +54 -0
- package/dist/ts/safe-proxy/index.js.map +1 -0
- package/dist/ts/serializer/index.d.ts +52 -0
- package/dist/ts/serializer/index.js +83 -0
- package/dist/ts/serializer/index.js.map +1 -0
- package/dist/ts/utils/index.d.ts +49 -0
- package/dist/ts/utils/index.js +157 -0
- package/dist/ts/utils/index.js.map +1 -0
- package/package.json +52 -11
- package/package.tar.gz +0 -0
- package/cache/index.js +0 -188
- package/formatter/index.js +0 -81
- package/hal/README.md +0 -54
- package/hal/index.js +0 -75
- package/health/health-logger.js +0 -40
- package/health/index.js +0 -188
- package/helpers/index.js +0 -1
- package/helpers/test-server-wrapper.js +0 -196
- package/last-commit.js +0 -12
- package/last_commit/index.js +0 -82
- package/logger/s4-logger.js +0 -128
- package/paginator/README.md +0 -183
- package/paginator/s4-pagination.js +0 -51
- package/paginator/sql-pagination.js +0 -178
- package/paper-trail/README.md +0 -68
- package/paper-trail/helpers.js +0 -99
- package/paper-trail/index.js +0 -842
- package/prompts/index.js +0 -78
- package/queue/index.js +0 -414
- package/queue/publisher.js +0 -96
- package/queue/queue-logger.js +0 -40
- package/queue/subscriber.js +0 -107
- package/safe_proxy/index.js +0 -61
- package/serializer/index.js +0 -90
- package/utils/index.js +0 -74
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
|
|
4
|
-
const s4Domain = "sera4.com";
|
|
5
|
-
const mobileDomain = "tws-mob." + s4Domain;
|
|
6
|
-
const webDomain = "tws-web." + s4Domain;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* A small wrapper around an express js server
|
|
10
|
-
* meant to be used in unit tests
|
|
11
|
-
*/
|
|
12
|
-
export class TestServerWrapper {
|
|
13
|
-
|
|
14
|
-
constructor(apiPrefix, expressServer, defaultHeaders, oldAuthToken, routePrinter, chai, factory) {
|
|
15
|
-
this.server = expressServer;
|
|
16
|
-
this.defaultHeaders = defaultHeaders;
|
|
17
|
-
this.apiPrefix = apiPrefix;
|
|
18
|
-
this.routePrinter = routePrinter;
|
|
19
|
-
this.chai = chai;
|
|
20
|
-
this.mobileDomain = mobileDomain;
|
|
21
|
-
this.webDomain = webDomain;
|
|
22
|
-
this.factory = factory;
|
|
23
|
-
|
|
24
|
-
// Support for older/deprecated apis
|
|
25
|
-
const oldAuthHeaders = {headers: {"Authorization" : `Token organization_token=${oldAuthToken}`}};
|
|
26
|
-
this.v1 = {
|
|
27
|
-
get: (url) => this.getWithPrefix("/v1", url, oldAuthHeaders),
|
|
28
|
-
post: (url) => this.postWithPrefix("/v1", url, oldAuthHeaders),
|
|
29
|
-
put: (url) => this.putWithPrefix("/v1", url, oldAuthHeaders),
|
|
30
|
-
delete: (url) => this.deleteWithPrefix("/v1", url, oldAuthHeaders)
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Adds a leading slash to the url if missing
|
|
36
|
-
* @param {String} url the url to sanitize
|
|
37
|
-
* @returns sanitized url
|
|
38
|
-
*/
|
|
39
|
-
sanitizeUrl (url) {
|
|
40
|
-
// add leading slash if missing
|
|
41
|
-
if (!url.startsWith("/")) {
|
|
42
|
-
return "/" + url;
|
|
43
|
-
} else {
|
|
44
|
-
return url;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Apply given headers to the request object
|
|
50
|
-
* @param {Object} request
|
|
51
|
-
* @param {Object} headers
|
|
52
|
-
*/
|
|
53
|
-
applyHeaders (request, headers) {
|
|
54
|
-
Object.keys(headers).map(k => {
|
|
55
|
-
request.set(k, headers[k]);
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
get (url, options) {
|
|
60
|
-
return this.getWithPrefix(this.apiPrefix, url, options);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
getWithPrefix (prefix, url, options) {
|
|
64
|
-
const sanitizedUrl = this.sanitizeUrl(url);
|
|
65
|
-
const request = this.chai.request(this.server).get(`${prefix}${sanitizedUrl}`);
|
|
66
|
-
this.applyHeaders(request, this.defaultHeaders);
|
|
67
|
-
|
|
68
|
-
if (options?.headers) {
|
|
69
|
-
this.applyHeaders(request, options.headers);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (options?.body) {
|
|
73
|
-
request.send(options.body);
|
|
74
|
-
}
|
|
75
|
-
return request;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
post (url, options) {
|
|
79
|
-
return this.postWithPrefix(this.apiPrefix, url, options);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
postWithPrefix (prefix, url, options) {
|
|
83
|
-
const sanitizedUrl = this.sanitizeUrl(url);
|
|
84
|
-
const request = this.chai.request(this.server).post(`${prefix}${sanitizedUrl}`);
|
|
85
|
-
this.applyHeaders(request, this.defaultHeaders);
|
|
86
|
-
|
|
87
|
-
if (options?.headers) {
|
|
88
|
-
this.applyHeaders(request, options.headers);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return request;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
put (url, options) {
|
|
95
|
-
return this.putWithPrefix(this.apiPrefix, url, options);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
putWithPrefix (prefix, url, options) {
|
|
99
|
-
const sanitizedUrl = this.sanitizeUrl(url);
|
|
100
|
-
const request = this.chai.request(this.server).put(`${prefix}${sanitizedUrl}`);
|
|
101
|
-
this.applyHeaders(request, this.defaultHeaders);
|
|
102
|
-
|
|
103
|
-
if (options?.headers) {
|
|
104
|
-
this.applyHeaders(request, options.headers);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return request;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
delete (url, options) {
|
|
111
|
-
return this.deleteWithPrefix(this.apiPrefix, url, options);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
deleteWithPrefix (prefix, url, options) {
|
|
115
|
-
const sanitizedUrl = this.sanitizeUrl(url);
|
|
116
|
-
const request = this.chai.request(this.server).delete(`${prefix}${sanitizedUrl}`);
|
|
117
|
-
this.applyHeaders(request, this.defaultHeaders);
|
|
118
|
-
|
|
119
|
-
if (options?.headers) {
|
|
120
|
-
this.applyHeaders(request, options.headers);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return request;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
upload ( url, fields, file, options) {
|
|
127
|
-
return this.uploadWithPrefix(this.apiPrefix, url, fields, file, options);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
uploadWithPrefix (prefix, url, fields, file, options) {
|
|
131
|
-
const sanitizedUrl = this.sanitizeUrl(url);
|
|
132
|
-
const request = this.chai.request(this.server).post(`${prefix}${sanitizedUrl}`);
|
|
133
|
-
this.applyHeaders(request, this.defaultHeaders);
|
|
134
|
-
|
|
135
|
-
if (options?.headers) {
|
|
136
|
-
this.applyHeaders(request, options.headers);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
request.attach("data", fs.readFileSync(file), {filename: "data"});
|
|
140
|
-
const keys = Object.keys(fields);
|
|
141
|
-
keys.forEach(k => {
|
|
142
|
-
request.field(k, fields[k]);
|
|
143
|
-
});
|
|
144
|
-
return request;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
stop () {
|
|
148
|
-
this.server.close();
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
allRoutes () {
|
|
152
|
-
return this.routePrinter.getAll();
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
withHeaders (headers) {
|
|
156
|
-
return {
|
|
157
|
-
get: (url, options) => this.get(url, {headers, ...options}),
|
|
158
|
-
post: (url, options) => this.post(url, {headers, ...options}),
|
|
159
|
-
put: (url, options) => this.put(url, {headers, ...options}),
|
|
160
|
-
delete: (url, options) => this.delete(url, {headers, ...options}),
|
|
161
|
-
upload: (url, fields, file, options) => this.upload(url, fields, file, {headers, ...options}),
|
|
162
|
-
stop: this.stop,
|
|
163
|
-
allRoutes: this.allRoutes,
|
|
164
|
-
v1: this.v1
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// setup
|
|
169
|
-
async setMembership (membership, addlHeaders) {
|
|
170
|
-
let headers;
|
|
171
|
-
|
|
172
|
-
if (membership) {
|
|
173
|
-
headers = { "tws-account-id" : membership.account_id, "tws-membership-id": membership.id, "tws-tenant-id" : membership.tenant_id };
|
|
174
|
-
} else {
|
|
175
|
-
// auto create an s4 user
|
|
176
|
-
const m = await this.factory.create("membership", { s4Admin: true })
|
|
177
|
-
headers = { "tws-account-id" : m.account_id, "tws-membership-id": m.id, "tws-tenant-id" : m.tenant_id };
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
headers = { ...headers, "tws-request-host": this.webDomain, ...addlHeaders }
|
|
181
|
-
return this.withHeaders(headers);
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
withMembership (m) {
|
|
185
|
-
const h = { "tws-membership" : JSON.stringify(m) }
|
|
186
|
-
return this.withHeaders(h);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
withS4 () {
|
|
190
|
-
return this.withMembership({s4_role: 1, id: uuidv4(), account_id: uuidv4(), tenant_id: uuidv4()});
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
withTap() {
|
|
194
|
-
return this.withMembership({s4_role: 2, id: uuidv4(), account_id: uuidv4(), tenant_id: uuidv4()});
|
|
195
|
-
}
|
|
196
|
-
}
|
package/last-commit.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
import lastCommit from "./last_commit/index.js"
|
|
3
|
-
|
|
4
|
-
lastCommit.writeLastCommit()
|
|
5
|
-
.then(c => {
|
|
6
|
-
console.debug("Last commit written");
|
|
7
|
-
console.debug(c);
|
|
8
|
-
})
|
|
9
|
-
.catch(e => {
|
|
10
|
-
console.error("Failed to write last commit");
|
|
11
|
-
console.error(e);
|
|
12
|
-
})
|
package/last_commit/index.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import git from "git-rev";
|
|
4
|
-
import fs from "fs";
|
|
5
|
-
import async from "async";
|
|
6
|
-
import { dirname } from 'path';
|
|
7
|
-
import { fileURLToPath } from 'url';
|
|
8
|
-
|
|
9
|
-
const appDir = dirname(fileURLToPath(import.meta.url));
|
|
10
|
-
const commitFile = `${appDir}/last-commit.json`;
|
|
11
|
-
|
|
12
|
-
const lastCommit = {
|
|
13
|
-
parseLastCommit() {
|
|
14
|
-
return new Promise((resolve, reject) => {
|
|
15
|
-
async.waterfall([
|
|
16
|
-
(next) => {
|
|
17
|
-
var commit = {};
|
|
18
|
-
git.branch((str) => {
|
|
19
|
-
commit["branch"] = str;
|
|
20
|
-
next(null, commit);
|
|
21
|
-
});
|
|
22
|
-
},
|
|
23
|
-
(commit, next) => {
|
|
24
|
-
git.long((str) => {
|
|
25
|
-
commit["commmit"] = str;
|
|
26
|
-
next(null, commit);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
], (err, commit) => {
|
|
30
|
-
if (err) {
|
|
31
|
-
reject(err);
|
|
32
|
-
} else {
|
|
33
|
-
resolve(commit);
|
|
34
|
-
}
|
|
35
|
-
})
|
|
36
|
-
})
|
|
37
|
-
},
|
|
38
|
-
getLastCommit() {
|
|
39
|
-
return new Promise((resolve, reject) => {
|
|
40
|
-
fs.readFile(commitFile, 'utf8', (err, data) => {
|
|
41
|
-
if (err) {
|
|
42
|
-
reject(err);
|
|
43
|
-
} else {
|
|
44
|
-
try {
|
|
45
|
-
var commit = JSON.parse(data);
|
|
46
|
-
resolve(commit);
|
|
47
|
-
} catch (e) {
|
|
48
|
-
reject(e);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
},
|
|
54
|
-
getLastCommitSync() {
|
|
55
|
-
var data = fs.readFileSync(commitFile, 'utf8');
|
|
56
|
-
return JSON.parse(data);
|
|
57
|
-
},
|
|
58
|
-
writeLastCommit() {
|
|
59
|
-
return new Promise((resolve, reject) => {
|
|
60
|
-
async.waterfall([
|
|
61
|
-
(next) => {
|
|
62
|
-
this.parseLastCommit()
|
|
63
|
-
.then((commit) => next(null, commit))
|
|
64
|
-
.catch(e => next(e));
|
|
65
|
-
},
|
|
66
|
-
(commit, next) => {
|
|
67
|
-
fs.writeFile(commitFile, JSON.stringify(commit, null, 2), 'utf8', (err) => {
|
|
68
|
-
next(err, commit);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
],(err, commit) => {
|
|
72
|
-
if (err) {
|
|
73
|
-
reject(err);
|
|
74
|
-
} else {
|
|
75
|
-
resolve(commit);
|
|
76
|
-
}
|
|
77
|
-
})
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export default lastCommit;
|
package/logger/s4-logger.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import winston from "winston";
|
|
2
|
-
import util from "util";
|
|
3
|
-
import jsonStringify from "fast-safe-stringify";
|
|
4
|
-
const colorizer = winston.format.colorize();
|
|
5
|
-
import "winston-logstash";
|
|
6
|
-
|
|
7
|
-
// IETF RFC5424:
|
|
8
|
-
// Severity of all levels is assumed to be numerically ascending from most important to least important.
|
|
9
|
-
const levels = {
|
|
10
|
-
error: 0,
|
|
11
|
-
warn: 1,
|
|
12
|
-
info: 2,
|
|
13
|
-
debug: 3,
|
|
14
|
-
trace: 4,
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
// These colors are not used, however if colors are not defined for custom levels
|
|
18
|
-
// then winston will throw an exception.
|
|
19
|
-
winston.addColors({
|
|
20
|
-
trace: 'white'
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
const defaultLevel = process.env.NODE_ENV === 'production' ? 'info' : 'debug';
|
|
24
|
-
|
|
25
|
-
// These colors are not used, however if colors are not defined for custom levels
|
|
26
|
-
// then winston will throw an exception.
|
|
27
|
-
winston.addColors({
|
|
28
|
-
trace: 'white'
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
const devFormat = {
|
|
32
|
-
transform(info) {
|
|
33
|
-
const { message } = info;
|
|
34
|
-
const level = info[Symbol.for('level')];
|
|
35
|
-
const args = info[Symbol.for('splat')];
|
|
36
|
-
const strArgs = args ? args.map(jsonStringify).join(' ') : "";
|
|
37
|
-
info[Symbol.for('message')] = colorizer.colorize(level, `${level}:\t`) + `${message} ${strArgs}`;
|
|
38
|
-
return info;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const defaultTransport = new winston.transports.Console({
|
|
43
|
-
format: devFormat,
|
|
44
|
-
level: 'trace'
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const logger = winston.createLogger({
|
|
48
|
-
levels: levels,
|
|
49
|
-
level: defaultLevel,
|
|
50
|
-
defaultMeta: { },
|
|
51
|
-
transports: [defaultTransport]
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
class S4Logger {
|
|
55
|
-
configure(options) {
|
|
56
|
-
if (!options) {
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
this.setLevel(options.level);
|
|
61
|
-
|
|
62
|
-
logger.defaultMeta.service = options.service;
|
|
63
|
-
switch(options.format.toLowerCase()) {
|
|
64
|
-
case "logstash":
|
|
65
|
-
defaultTransport.format = winston.format.logstash();
|
|
66
|
-
break;
|
|
67
|
-
case "json":
|
|
68
|
-
defaultTransport.format = winston.format.json();
|
|
69
|
-
break;
|
|
70
|
-
default:
|
|
71
|
-
defaultTransport.format = devFormat;
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
setLevel(level) {
|
|
77
|
-
if (level && ["trace", "debug", "info", "error", "warn"].indexOf(level.toLowerCase())) {
|
|
78
|
-
logger.level = level;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
mute() {
|
|
83
|
-
logger.transports.forEach(t => {
|
|
84
|
-
t.silent = true;
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
unmute() {
|
|
89
|
-
logger.transports.forEach(t => {
|
|
90
|
-
t.silent = false;
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
trace(...args) {
|
|
95
|
-
logger.trace(util.format(...args));
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
debug(...args) {
|
|
99
|
-
logger.debug(util.format(...args));
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
log(...args) {
|
|
103
|
-
logger.debug(util.format(...args));
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
warn(...args) {
|
|
107
|
-
logger.warn(util.format(...args));
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
info(...args) {
|
|
111
|
-
logger.info(util.format(...args));
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
error(...args) {
|
|
115
|
-
logger.error(util.format(...args));
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
getStream() {
|
|
119
|
-
return {
|
|
120
|
-
write: function(message, encoding){
|
|
121
|
-
logger.info(message);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const s4logger = new S4Logger();
|
|
128
|
-
export default s4logger;
|
package/paginator/README.md
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
# PAGINATOR
|
|
2
|
-
|
|
3
|
-
This is a module to add pagination to Sequelize ORM based projects.
|
|
4
|
-
|
|
5
|
-
## Usage
|
|
6
|
-
|
|
7
|
-
In your model definition, set pagination on your model
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
import { paginator } from "@sera4/essentia";
|
|
11
|
-
...
|
|
12
|
-
|
|
13
|
-
module.exports = (sequelize, DataTypes) => {
|
|
14
|
-
const Post = sequelize.define('Post', {...}, {...});
|
|
15
|
-
// Add pagination
|
|
16
|
-
paginator.paginate(Post);
|
|
17
|
-
return Post;
|
|
18
|
-
};
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Later in your controller you can call the paginate method on your model
|
|
22
|
-
to return paginated results:
|
|
23
|
-
|
|
24
|
-
```
|
|
25
|
-
app.get('/posts', (req, res) => {
|
|
26
|
-
Post.using(req.query).paginate()
|
|
27
|
-
.then(p => res.status(200).json(p));
|
|
28
|
-
})
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
For example, calling `/posts?page_size=2&page_index=1 will return
|
|
32
|
-
something similar to the following:
|
|
33
|
-
|
|
34
|
-
```
|
|
35
|
-
{
|
|
36
|
-
"_pagination": {
|
|
37
|
-
"current_page": 1,
|
|
38
|
-
"first_page": false,
|
|
39
|
-
"last_page": false,
|
|
40
|
-
"page_size": 2,
|
|
41
|
-
"total_pages": 2500,
|
|
42
|
-
"total_size": 5000,
|
|
43
|
-
"out_of_bounds": false,
|
|
44
|
-
"previous_page": 0,
|
|
45
|
-
"next_page": 2
|
|
46
|
-
},
|
|
47
|
-
"_collection": [
|
|
48
|
-
{
|
|
49
|
-
"id": 42803,
|
|
50
|
-
"title": "FYrlujXyrcviLIkQNQsyIXEaRdARCJqNPIDsDiFuLxENtFXPRS",
|
|
51
|
-
"name": "uLHnWSekmNNKueYiglAkAwWckFetoSnudXalelnAkpnayIgiUi",
|
|
52
|
-
"content": "THISkjbnGVprKVFsMnlNEhgDYSGRZcomhNkcrbnuvnqogCdUen",
|
|
53
|
-
"created_at": "2018-11-03T16:04:10.526Z",
|
|
54
|
-
"updated_at": "2018-11-03T16:04:10.526Z"
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
"id": 42804,
|
|
58
|
-
"title": "VHgYIBGIuyObzHjrpAwFwbtZSVMnCZdTIAIzzlQHChEPbwGwmU",
|
|
59
|
-
"name": "upvMbeIKAcuwHBruLyPcwEUYlAbQqqljUcMajOyvGaqCTahwfs",
|
|
60
|
-
"content": "EeopiRHsUdNNqzshkafYqkVzACOyWQpdPXCBSzuPBIDHWGqWuk",
|
|
61
|
-
"created_at": "2018-11-03T16:04:10.534Z",
|
|
62
|
-
"updated_at": "2018-11-03T16:04:10.534Z"
|
|
63
|
-
}
|
|
64
|
-
]
|
|
65
|
-
}
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
The `using` method takes 2 parameters:
|
|
69
|
-
|
|
70
|
-
* options - this is where the pagination parameters are read from. By default the method will be looking for `page_size` and `page_index`
|
|
71
|
-
* res - this is the express js response object and the method will also add pagination in the headers (if configured as such)
|
|
72
|
-
|
|
73
|
-
You can configure the pagination globally via `paginator.configure(options)`. The following options are available:
|
|
74
|
-
|
|
75
|
-
* Configuring the url keys for pagination:
|
|
76
|
-
* `paginateIn.size=page_size` - This is the key that will be used to parse number of items to read when the `using` method is called. Renaming this to `per_page` for examle, the url must contain the following query parameters `/posts?per_page=20` to load 20 posts
|
|
77
|
-
* `paginateIn.index=page_index` - This is the key that will be used to parse the offset to read when the `using` method is called. Renaming this to `page` requires an url with `/posts?page_size=20&page=2` to read rows 21 through 40 from the database.
|
|
78
|
-
* Configure the way the pagination data is displayed
|
|
79
|
-
* `paginateOut.current_page=current_page` Label used to show the current page value in the pagination data
|
|
80
|
-
* `paginateOut.first_page=first_page` Label used to show the first page value in the pagination data
|
|
81
|
-
* `paginateOut.last_page=last_page` Label used to show the last page value in the pagination data
|
|
82
|
-
* `paginateOut.next_page=next_page` Label used to show the next page value in the pagination data
|
|
83
|
-
* `paginateOut.out_of_bounds=out_of_bounds` Label used to show the out of bounds value in the pagination data
|
|
84
|
-
* `paginateOut.page_size=page_size` Label used to show the page size value in the pagination data
|
|
85
|
-
* `paginateOut.previous_page=previous_page` Label used to show the previous size value in the pagination data
|
|
86
|
-
* `paginateOut.total_pages=total_pages` Label used to show the total pages value in the pagination data
|
|
87
|
-
* `paginateOut.total_size=total_size` Label used to show the total size value in the pagination data
|
|
88
|
-
* `paginateOut.links=""` Label used to show the links in the pagination data. If this value is empty or null, no links are added in the pagination data. If set to `true`, then a `_links` key is added to the pagination data with links to previous and next pages if available. You can provide a non empty string instead of `true` to use your own lable for this key.
|
|
89
|
-
* Other configurations:
|
|
90
|
-
* `zeroBasedOffset=true` When false, the indexing is done from 1, otherwise it's done from 0 like in arrays
|
|
91
|
-
* `paginationHeader=tws-pagination` The name of the pagination header used when adding pagination in the headers
|
|
92
|
-
* `headersOnly=false` If this is set to true, then the pagination will return an array of objects and all the pagination data can be found in the headers. Otherwise the result will have a key for pagination (`_pagination`) and a key for the results (`_collection`)
|
|
93
|
-
* `paginationTag=_pagination` Used to overwrite the key name used for pagination
|
|
94
|
-
* `collectionTag=_collection` Used to overwrite the key name used for results
|
|
95
|
-
|
|
96
|
-
The configuration is done globally, so you can add something like this as early as possible to your code:
|
|
97
|
-
|
|
98
|
-
```
|
|
99
|
-
paginator.configure({
|
|
100
|
-
paginateIn : {
|
|
101
|
-
size: "items_per_page",
|
|
102
|
-
index: "page_number",
|
|
103
|
-
},
|
|
104
|
-
paginateOut : {
|
|
105
|
-
links: "myLinks",
|
|
106
|
-
current_page: "the_current_page"
|
|
107
|
-
},
|
|
108
|
-
zeroBasedOffset : false,
|
|
109
|
-
paginationTag : "paginationData",
|
|
110
|
-
collectionTag : "paginatedCollection"
|
|
111
|
-
});
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
This will require an url like `/posts?items_per_page=2&page_number=1` and will produce the following results:
|
|
115
|
-
|
|
116
|
-
```
|
|
117
|
-
{
|
|
118
|
-
"paginationData": {
|
|
119
|
-
"the_current_page": 1,
|
|
120
|
-
"first_page": true,
|
|
121
|
-
"last_page": false,
|
|
122
|
-
"page_size": 2,
|
|
123
|
-
"total_pages": 2500,
|
|
124
|
-
"total_size": 5000,
|
|
125
|
-
"out_of_bounds": false,
|
|
126
|
-
"next_page": 2,
|
|
127
|
-
"myLinks": {
|
|
128
|
-
"first_page": "?page_number=1&items_per_page=2",
|
|
129
|
-
"last_page": "?page_number=2500&items_per_page=2",
|
|
130
|
-
"next_page": "?page_number=2&items_per_page=2"
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
"paginatedCollection": [
|
|
134
|
-
{
|
|
135
|
-
"id": 42801,
|
|
136
|
-
"title": "LJitZgbNPLpOlnkYdynKlSYnCjuLBYiVHYMfAPlSALeoVTuNKw",
|
|
137
|
-
"name": "cvnititqxdPnHdomgxJJVGynNInkScmDlkzAALunYFHNRjffqE",
|
|
138
|
-
"content": "LcURropLnJiudElgAVhsBSMRBRNUzHLDiTebZxxpplXdpeGEFa",
|
|
139
|
-
"created_at": "2018-11-03T16:04:10.511Z",
|
|
140
|
-
"updated_at": "2018-11-03T16:04:10.511Z"
|
|
141
|
-
},
|
|
142
|
-
{
|
|
143
|
-
"id": 42802,
|
|
144
|
-
"title": "mTyXHFoJjgXmwASYvfFgZJcstUTbHjvrcuVoaUMdXZmXuMrfBz",
|
|
145
|
-
"name": "yxrOfMUdiJqNMNdWAfKXbsMsskKbpeRzxfEoicEcSdNDhGaPBE",
|
|
146
|
-
"content": "TEAEugRIeOfceEyREmAGcxldDbtzqlaWGDRMIjAhmfAuwJraot",
|
|
147
|
-
"created_at": "2018-11-03T16:04:10.518Z",
|
|
148
|
-
"updated_at": "2018-11-03T16:04:10.518Z"
|
|
149
|
-
}
|
|
150
|
-
]
|
|
151
|
-
}
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
You may notice that the links in the pagination only have query parameters. If you want to include the resource, provide the resource name when adding pagination to your model.
|
|
155
|
-
|
|
156
|
-
For example:
|
|
157
|
-
```
|
|
158
|
-
import { paginator } from "@sera4/essentia"
|
|
159
|
-
...
|
|
160
|
-
|
|
161
|
-
module.exports = (sequelize, DataTypes) => {
|
|
162
|
-
const Post = sequelize.define('Post', {...}, {...});
|
|
163
|
-
// Add pagination
|
|
164
|
-
paginator.paginate(Post, "/posts");
|
|
165
|
-
return Post;
|
|
166
|
-
};
|
|
167
|
-
```
|
|
168
|
-
This will return pagination links in the pagination data prependend with the provided resource:
|
|
169
|
-
```
|
|
170
|
-
"myLinks": {
|
|
171
|
-
"first_page": "?page_number=1&items_per_page=2",
|
|
172
|
-
"last_page": "?page_number=2500&items_per_page=2",
|
|
173
|
-
"next_page": "?page_number=2&items_per_page=2"
|
|
174
|
-
}
|
|
175
|
-
```
|
|
176
|
-
becomes
|
|
177
|
-
```
|
|
178
|
-
"myLinks": {
|
|
179
|
-
"first_page": "/posts?page_number=1&items_per_page=2",
|
|
180
|
-
"last_page": "/posts?page_number=2500&items_per_page=2",
|
|
181
|
-
"next_page": "/posts?page_number=2&items_per_page=2"
|
|
182
|
-
}
|
|
183
|
-
```
|
|
@@ -1,51 +0,0 @@
|
|
|
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
|
-
const paginator = new S4Pagination();
|
|
51
|
-
export default paginator;
|