groupcore-utils 1.3.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc +26 -0
- package/README.md +238 -0
- package/communication/mail.js +54 -0
- package/database/crud.js +124 -0
- package/database/init.js +47 -0
- package/package.json +18 -6
- package/rest/index.js +109 -0
- package/schemas/delete.js +29 -0
- package/schemas/headers.js +11 -0
- package/security/encryption.js +31 -0
- package/security/jwt.js +41 -0
- package/test-utils/mocks/request.js +17 -0
- package/test-utils/mocks/response.js +14 -0
- package/timestamp/index.js +65 -0
package/.eslintrc
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
{
|
2
|
+
"env": {
|
3
|
+
"browser": true,
|
4
|
+
"commonjs": true,
|
5
|
+
"es2021": true
|
6
|
+
},
|
7
|
+
"extends": ["eslint:recommended", "airbnb", "prettier", "plugin:jest/all"],
|
8
|
+
"parserOptions": {
|
9
|
+
"ecmaVersion": 12
|
10
|
+
},
|
11
|
+
"rules": {
|
12
|
+
"no-underscore-dangle": "off",
|
13
|
+
"class-methods-use-this": "off",
|
14
|
+
"no-console": "off",
|
15
|
+
"jest/no-hooks": "off",
|
16
|
+
"jest/no-test-return-statement": "off",
|
17
|
+
"jest/prefer-strict-equal": "off",
|
18
|
+
"consistent-return": "off",
|
19
|
+
"array-callback-return": "off",
|
20
|
+
"no-param-reassign": "off",
|
21
|
+
"no-restricted-syntax": "off",
|
22
|
+
"prefer-destructuring": "off",
|
23
|
+
"no-case-declarations": "off",
|
24
|
+
"jest/require-hook": "off"
|
25
|
+
}
|
26
|
+
}
|
package/README.md
CHANGED
@@ -86,6 +86,244 @@ These are the methods you can call with this package. This list will continually
|
|
86
86
|
*/
|
87
87
|
```
|
88
88
|
|
89
|
+
### Communication
|
90
|
+
#### Email
|
91
|
+
Send mail to a specified recipient.
|
92
|
+
```
|
93
|
+
require('groupcore-utils/communication/email')
|
94
|
+
|
95
|
+
/**
|
96
|
+
* @description constructor
|
97
|
+
* @param {string} hostname - hostname of the mail service provider
|
98
|
+
* @param port {number} - smtp port of the mail service provider
|
99
|
+
* @param username {string} - smtp username
|
100
|
+
* @param password {password} - smtp password
|
101
|
+
*/
|
102
|
+
|
103
|
+
/**
|
104
|
+
* @method send()
|
105
|
+
* @description send email to a specified recipient
|
106
|
+
* @param email {string} - the recipient's email address
|
107
|
+
* @param subject {string} - subject of the email
|
108
|
+
* @param message {string} - mail message
|
109
|
+
* @param attachment {string} - url of attached file
|
110
|
+
* @param from {string} - sender email address
|
111
|
+
* @returns {Promise<*>}
|
112
|
+
*/
|
113
|
+
```
|
114
|
+
|
115
|
+
### SQL Database
|
116
|
+
#### Initialization
|
117
|
+
Initialize db with this and pass the object to the CRUD class constructor
|
118
|
+
```
|
119
|
+
require('groupcore-utils/database/init')
|
120
|
+
|
121
|
+
/**
|
122
|
+
* @description constructor
|
123
|
+
* @param {string} host - sql db hostname
|
124
|
+
* @param {string} user - db user username
|
125
|
+
* @param {string} password - db user password
|
126
|
+
* @param {string} db - db name
|
127
|
+
* @param {string} socket - path to a unix domain socket to connect to. when used host and port are ignored
|
128
|
+
*/
|
129
|
+
```
|
130
|
+
|
131
|
+
#### CRUD Methods
|
132
|
+
```
|
133
|
+
require('groupcore-utils/database/crud')
|
134
|
+
|
135
|
+
/**
|
136
|
+
* @description constructor
|
137
|
+
* @param {string} dbTable - name of the table
|
138
|
+
* @param {object} db - the initialised db instance
|
139
|
+
*/
|
140
|
+
|
141
|
+
/**
|
142
|
+
* @method create()
|
143
|
+
* @description Insert values into db
|
144
|
+
* @param {Object} data - the key value pair of the data to be inserted into the db
|
145
|
+
* @param {Boolean} unique - if set to true, ensure no duplication of data during creation
|
146
|
+
* @param {Array} ignore - fields to ignore when checking for unique values
|
147
|
+
* @returns {Promise | string | Object} - resolves if successfully and rejects if there's an error
|
148
|
+
*/
|
149
|
+
|
150
|
+
/**
|
151
|
+
* @method read()
|
152
|
+
* @description get data from table
|
153
|
+
* @param {null, Object} where - if no where, all the data will be returned from db table without any 'where' clause. shape {field, value}
|
154
|
+
* @param {null | Array} orderBy - explicitly specify the order, either asc or desc
|
155
|
+
* @returns {Promise}
|
156
|
+
*/
|
157
|
+
|
158
|
+
/**
|
159
|
+
* @method update
|
160
|
+
* @description update db table by id
|
161
|
+
* @param {Object} data - fields to update
|
162
|
+
* @param {number} id - id in where clause
|
163
|
+
* @returns {Promise}
|
164
|
+
*/
|
165
|
+
|
166
|
+
/**
|
167
|
+
* @method delete()
|
168
|
+
* @description delete data from db by id
|
169
|
+
* @param {number} id - id of data to deleted
|
170
|
+
* @returns {Promise}
|
171
|
+
*/
|
172
|
+
|
173
|
+
/**
|
174
|
+
* @method query()
|
175
|
+
* @description make a custom db query
|
176
|
+
* @param {string} queryStatement - SQL query
|
177
|
+
* @returns {Promise}
|
178
|
+
*/
|
179
|
+
```
|
180
|
+
|
181
|
+
### REST
|
182
|
+
```
|
183
|
+
require('groupcore-utils/rbac')
|
184
|
+
|
185
|
+
/**
|
186
|
+
* @method get()
|
187
|
+
* @description send a GET request
|
188
|
+
* @param {string} url - api url
|
189
|
+
* @param {object} headers - headers to be added to request
|
190
|
+
* @returns {AxiosPromise<any>}
|
191
|
+
*/
|
192
|
+
|
193
|
+
/**
|
194
|
+
* @method post()
|
195
|
+
* @description send a POST request
|
196
|
+
* @param {string} url - api url
|
197
|
+
* @param {object} data - payload
|
198
|
+
* @param {object} headers - headers to be added to request
|
199
|
+
* @returns {AxiosPromise<any>}
|
200
|
+
*/
|
201
|
+
|
202
|
+
/**
|
203
|
+
* @method put()
|
204
|
+
* @description send PUT request
|
205
|
+
* @param {string} url - api url
|
206
|
+
* @param {object} data - payload
|
207
|
+
* @param {object} headers - headers to be added to request
|
208
|
+
* @returns {AxiosPromise<any>}
|
209
|
+
*/
|
210
|
+
|
211
|
+
/**
|
212
|
+
* @method patch()
|
213
|
+
* @description send PATCH request
|
214
|
+
* @param {string} url - api url
|
215
|
+
* @param {object} data - payload
|
216
|
+
* @param {object} headers - headers to be added to request
|
217
|
+
* @returns {AxiosPromise<any>}
|
218
|
+
*/
|
219
|
+
|
220
|
+
/**
|
221
|
+
* @method delete()
|
222
|
+
* @description send DELETE request
|
223
|
+
* @param {string} urls - api url
|
224
|
+
* @param {object} headers - headers to be added to request
|
225
|
+
* @returns {AxiosPromise<any>}
|
226
|
+
*/
|
227
|
+
```
|
228
|
+
|
229
|
+
### Encryption
|
230
|
+
```
|
231
|
+
require('groupcore-utils/security/encryption')
|
232
|
+
/**
|
233
|
+
* @method hash()
|
234
|
+
* @description create a hash of a text
|
235
|
+
* @param {string} text - text to be hashed
|
236
|
+
* @returns {*}
|
237
|
+
*/
|
238
|
+
|
239
|
+
/**
|
240
|
+
* @method verify()
|
241
|
+
* @description verify a text by its hash
|
242
|
+
* @param {string} hash - the hashed text
|
243
|
+
* @param {string} text - the text to be compared with the hash
|
244
|
+
* @returns {*}
|
245
|
+
*/
|
246
|
+
```
|
247
|
+
|
248
|
+
### JWT
|
249
|
+
```
|
250
|
+
require('groupcore-utils/security/encryption')
|
251
|
+
|
252
|
+
/**
|
253
|
+
* @description constructor
|
254
|
+
* @param {string} jwtKey - jwt key
|
255
|
+
*/
|
256
|
+
|
257
|
+
/**
|
258
|
+
* @method jwtSign()
|
259
|
+
* @description create a JWT token
|
260
|
+
* @param {object} payload - payload to be tokenized
|
261
|
+
* @returns {*}
|
262
|
+
*/
|
263
|
+
|
264
|
+
/**
|
265
|
+
* @method jwtDecode()
|
266
|
+
* @description decode a JWT token
|
267
|
+
* @param {string} token - JWT token to be decoded
|
268
|
+
* @returns {{payload: any, signature: *, header: *}|*|SourceMapPayload|TokenPayload}
|
269
|
+
*/
|
270
|
+
|
271
|
+
/**
|
272
|
+
* @method jwtVerify()
|
273
|
+
* @description verify a JWT token
|
274
|
+
* @param {string} token - token to be verified
|
275
|
+
* @returns {*}
|
276
|
+
*/
|
277
|
+
```
|
278
|
+
|
279
|
+
### Date and Time
|
280
|
+
```
|
281
|
+
require('groupcore-utils/timestamp')
|
282
|
+
|
283
|
+
/**
|
284
|
+
* @method now()
|
285
|
+
* @description returns the present unix timestamp
|
286
|
+
* @returns {number}
|
287
|
+
*/
|
288
|
+
|
289
|
+
/**
|
290
|
+
* @method unixToDate()
|
291
|
+
* @description convert a unix timestamp to specific date format
|
292
|
+
* @param {number} unix - the unix timestamp to be converted
|
293
|
+
* @param {string} format - the format to use, default is 'MMMM DD, YYYY'
|
294
|
+
* @returns {string}
|
295
|
+
*/
|
296
|
+
|
297
|
+
/**
|
298
|
+
* @method dateToUnix()
|
299
|
+
* @description convert a unix date to unix
|
300
|
+
* @param {number} date - the date to be converted
|
301
|
+
* @param {string} format - the format of the date
|
302
|
+
* @returns {number}
|
303
|
+
*/
|
304
|
+
|
305
|
+
/**
|
306
|
+
* @method unixToStartOfDay()
|
307
|
+
* @description take whatever date is given to 12:00:00 AM of that day
|
308
|
+
* @param {string} unix - unix timestamp
|
309
|
+
* @returns {number}
|
310
|
+
*/
|
311
|
+
|
312
|
+
/**
|
313
|
+
* @method unixToEndOfDay()
|
314
|
+
* @description take whatever date is given to 11:59:59 PM of the day
|
315
|
+
* @param {string} unix - unix timestamp
|
316
|
+
* @returns {number}
|
317
|
+
*/
|
318
|
+
|
319
|
+
/**
|
320
|
+
* @method toSeconds()
|
321
|
+
* @description convert time (HH:MM) to seconds
|
322
|
+
* @param {string} time - time to be converted
|
323
|
+
* @returns {number}
|
324
|
+
*/
|
325
|
+
```
|
326
|
+
|
89
327
|
### Testing
|
90
328
|
```
|
91
329
|
npm run test
|
@@ -0,0 +1,54 @@
|
|
1
|
+
const nodemailer = require("nodemailer");
|
2
|
+
|
3
|
+
module.exports = class {
|
4
|
+
/**
|
5
|
+
* @description constructor
|
6
|
+
* @param {string} hostname - hostname of the mail service provider
|
7
|
+
* @param port {number} - smtp port of the mail service provider
|
8
|
+
* @param username {string} - smtp username
|
9
|
+
* @param password {password} - smtp password
|
10
|
+
*/
|
11
|
+
constructor({ hostname, port, username, password }) {
|
12
|
+
this.hostname = hostname;
|
13
|
+
this.port = port;
|
14
|
+
this.username = username;
|
15
|
+
this.password = password;
|
16
|
+
}
|
17
|
+
|
18
|
+
async _init() {
|
19
|
+
this.transporter = nodemailer.createTransport({
|
20
|
+
host: this.hostname,
|
21
|
+
port: this.port,
|
22
|
+
secure: false,
|
23
|
+
auth: {
|
24
|
+
user: this.username,
|
25
|
+
pass: this.password,
|
26
|
+
},
|
27
|
+
tls: {
|
28
|
+
rejectUnauthorized: false,
|
29
|
+
},
|
30
|
+
});
|
31
|
+
}
|
32
|
+
|
33
|
+
/**
|
34
|
+
* @method send()
|
35
|
+
* @description send email to a specified recipient
|
36
|
+
* @param email {string} - the recipient's email address
|
37
|
+
* @param subject {string} - subject of the email
|
38
|
+
* @param message {string} - mail message
|
39
|
+
* @param attachment {string} - url of attached file
|
40
|
+
* @param from {string} - sender email address
|
41
|
+
* @returns {Promise<*>}
|
42
|
+
*/
|
43
|
+
async send({ email, subject, message, attachment = null, from }) {
|
44
|
+
await this._init();
|
45
|
+
|
46
|
+
return this.transporter.sendMail({
|
47
|
+
subject,
|
48
|
+
from,
|
49
|
+
to: email,
|
50
|
+
html: message,
|
51
|
+
attachments: attachment,
|
52
|
+
});
|
53
|
+
}
|
54
|
+
};
|
package/database/crud.js
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
const escapeQuotes = require('escape-quotes');
|
2
|
+
const _ = require('lodash');
|
3
|
+
|
4
|
+
module.exports = class {
|
5
|
+
/**
|
6
|
+
* @description constructor
|
7
|
+
* @param {string} dbTable - name of the table
|
8
|
+
* @param {object} db - the initialised db instance
|
9
|
+
*/
|
10
|
+
constructor({ dbTable, db }) {
|
11
|
+
this.dbTable = dbTable;
|
12
|
+
this.db = db;
|
13
|
+
}
|
14
|
+
|
15
|
+
/**
|
16
|
+
* @method create()
|
17
|
+
* @description Insert values into db
|
18
|
+
* @param {Object} data - the key value pair of the data to be inserted into the db
|
19
|
+
* @param {Boolean} unique - if set to true, ensure no duplication of data during creation
|
20
|
+
* @param {Array} ignore - fields to ignore when checking for unique values
|
21
|
+
* @returns {Promise | string | Object} - resolves if successfully and rejects if there's an error
|
22
|
+
*/
|
23
|
+
|
24
|
+
async create({ data }) {
|
25
|
+
const fields = Object.keys(data).join(',');
|
26
|
+
|
27
|
+
const rawValues = [];
|
28
|
+
Object.values(data).map((value) => {
|
29
|
+
rawValues.push(`'${escapeQuotes(value)}'`);
|
30
|
+
return true;
|
31
|
+
});
|
32
|
+
const values = rawValues.join(',');
|
33
|
+
|
34
|
+
const query = `insert into ${this.dbTable} (${fields}) values (${values})`;
|
35
|
+
|
36
|
+
return this.db.query(query);
|
37
|
+
}
|
38
|
+
|
39
|
+
/**
|
40
|
+
* @method read()
|
41
|
+
* @description get data from table
|
42
|
+
* @param {Object} where - if no where, all the data will be returned from db table without any 'where' clause. shape {field, value}
|
43
|
+
* @param {Array} orderBy - explicitly specify the order, either asc or desc
|
44
|
+
* @returns {Promise}
|
45
|
+
*/
|
46
|
+
read({ where, orderBy }) {
|
47
|
+
let query;
|
48
|
+
|
49
|
+
if (orderBy) {
|
50
|
+
if (where) {
|
51
|
+
query = `select * from ${this.dbTable} where ${where.field} = '${where.value}' order by ${orderBy}`;
|
52
|
+
} else {
|
53
|
+
query = `select * from ${this.dbTable} order by ${orderBy}`;
|
54
|
+
}
|
55
|
+
} else if (where) {
|
56
|
+
query = `select * from ${this.dbTable} where ${where.field} = '${where.value}'`;
|
57
|
+
} else {
|
58
|
+
query = `select * from ${this.dbTable}`;
|
59
|
+
}
|
60
|
+
|
61
|
+
return this.db.query(query);
|
62
|
+
}
|
63
|
+
|
64
|
+
// with orderby
|
65
|
+
// readOrderBy(where = null) {
|
66
|
+
// let query;
|
67
|
+
//
|
68
|
+
// if (where) {
|
69
|
+
// query = `select * from ${this.dbTable} where ${where.field} = '${where.value}'`;
|
70
|
+
// } else {
|
71
|
+
// query = `select * from ${this.dbTable}`;
|
72
|
+
// }
|
73
|
+
//
|
74
|
+
// return new Db().query(query);
|
75
|
+
// }
|
76
|
+
|
77
|
+
/**
|
78
|
+
* @method update
|
79
|
+
* @description update db table by id
|
80
|
+
* @param {Object} data - fields to update
|
81
|
+
* @param {number} id - id in where clause
|
82
|
+
* @returns {Promise}
|
83
|
+
*/
|
84
|
+
update({ data, id }) {
|
85
|
+
// variable to hold the dynamic part of the update sql query
|
86
|
+
let updateQuery = '';
|
87
|
+
|
88
|
+
// set this to be used inside the map function to check when we get to the last index key so that we dont add the comma to the last key in the generated sql query
|
89
|
+
const count = Object.keys(data).length;
|
90
|
+
Object.keys(data).map((key, i) => {
|
91
|
+
if (count - i === 1) {
|
92
|
+
updateQuery += `${key} = '${escapeQuotes(data[key])}'`;
|
93
|
+
} else {
|
94
|
+
updateQuery += `${key} = '${escapeQuotes(data[key])}', `;
|
95
|
+
}
|
96
|
+
return true;
|
97
|
+
});
|
98
|
+
|
99
|
+
const query = `update ${this.dbTable} set ${updateQuery} where id = '${id}'`;
|
100
|
+
|
101
|
+
return this.db.query(query);
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* @method delete()
|
106
|
+
* @description delete data from db by id
|
107
|
+
* @param {number} id - id of data to deleted
|
108
|
+
* @returns {Promise}
|
109
|
+
*/
|
110
|
+
delete({ id }) {
|
111
|
+
return this.db.query(`delete from ${this.dbTable} where id = '${id}'`);
|
112
|
+
}
|
113
|
+
|
114
|
+
/**
|
115
|
+
* @method query()
|
116
|
+
* @description make a custom db query
|
117
|
+
* @param {string} statement - SQL query
|
118
|
+
* @returns {Promise}
|
119
|
+
*/
|
120
|
+
// eslint-disable-next-line class-methods-use-this
|
121
|
+
query({ statement }) {
|
122
|
+
return this.db.query(statement);
|
123
|
+
}
|
124
|
+
};
|
package/database/init.js
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
const mysql = require("mysql");
|
2
|
+
|
3
|
+
module.exports = class {
|
4
|
+
/**
|
5
|
+
* @description constructor
|
6
|
+
* @param {string} host - sql db hostname
|
7
|
+
* @param {string} user - db user username
|
8
|
+
* @param {string} password - db user password
|
9
|
+
* @param {string} db - db name
|
10
|
+
* @param {string} socket - path to a unix domain socket to connect to. when used host and port are ignored
|
11
|
+
*/
|
12
|
+
constructor({ host, user, password, db, socket }) {
|
13
|
+
this.host = host;
|
14
|
+
this.user = user;
|
15
|
+
this.password = password;
|
16
|
+
this.db = db;
|
17
|
+
this.socket = socket;
|
18
|
+
}
|
19
|
+
|
20
|
+
/**
|
21
|
+
* @method query()
|
22
|
+
* @description send a db query
|
23
|
+
* @param {string} statement sql statement
|
24
|
+
* @returns {Promise<unknown>}
|
25
|
+
*/
|
26
|
+
query(statement) {
|
27
|
+
const db = mysql.createConnection({
|
28
|
+
host: this.host,
|
29
|
+
user: this.user,
|
30
|
+
password: this.password,
|
31
|
+
database: this.db,
|
32
|
+
multipleStatements: true,
|
33
|
+
socketPath: this.socket,
|
34
|
+
});
|
35
|
+
|
36
|
+
return new Promise((resolve, reject) => {
|
37
|
+
db.query(statement, (err, result) => {
|
38
|
+
if (err) {
|
39
|
+
reject(Error(`DB Error: ${err}, Statement: ${statement}`));
|
40
|
+
}
|
41
|
+
|
42
|
+
resolve(result);
|
43
|
+
db.end();
|
44
|
+
});
|
45
|
+
});
|
46
|
+
}
|
47
|
+
};
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "groupcore-utils",
|
3
|
-
"version": "
|
4
|
-
"description": "Utilities for working with some
|
3
|
+
"version": "2.0.0",
|
4
|
+
"description": "Utilities for working with some core features",
|
5
5
|
"main": "Utils.js",
|
6
6
|
"scripts": {
|
7
7
|
"test": "jest --coverage"
|
@@ -17,11 +17,23 @@
|
|
17
17
|
},
|
18
18
|
"homepage": "https://bitbucket.org/thegroupc/groupcore-utils/src/master",
|
19
19
|
"dependencies": {
|
20
|
-
"
|
21
|
-
"
|
20
|
+
"lodash": "^4.17.21",
|
21
|
+
"axios": "^0.26.1",
|
22
|
+
"bcrypt": "^5.0.1",
|
23
|
+
"easy-rbac": "^3.2.0",
|
24
|
+
"escape-quotes": "^1.0.2",
|
25
|
+
"jsonwebtoken": "^8.5.1",
|
26
|
+
"moment": "^2.29.2",
|
27
|
+
"nodemailer": "^6.7.3",
|
28
|
+
"random-number": "^0.0.9"
|
22
29
|
},
|
23
30
|
"devDependencies": {
|
24
|
-
"
|
25
|
-
"
|
31
|
+
"eslint": "^8.24.0",
|
32
|
+
"eslint-config-airbnb": "^19.0.4",
|
33
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
34
|
+
"eslint-config-prettier": "^8.5.0",
|
35
|
+
"eslint-plugin-jest": "^27.1.0",
|
36
|
+
"jest": "^27.5.1",
|
37
|
+
"prettier": "^2.7.1"
|
26
38
|
}
|
27
39
|
}
|
package/rest/index.js
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
const axios = require('axios');
|
2
|
+
|
3
|
+
module.exports = {
|
4
|
+
/**
|
5
|
+
* @method get()
|
6
|
+
* @description send a GET request
|
7
|
+
* @param {string} url - api url
|
8
|
+
* @param {object} headers - headers to be added to request
|
9
|
+
* @returns {AxiosPromise<any>}
|
10
|
+
*/
|
11
|
+
async get({ url, headers }) {
|
12
|
+
try {
|
13
|
+
return axios({
|
14
|
+
url,
|
15
|
+
headers,
|
16
|
+
method: 'get',
|
17
|
+
});
|
18
|
+
} catch (err) {
|
19
|
+
console.log('Axios GET Error:', err.message);
|
20
|
+
throw err;
|
21
|
+
}
|
22
|
+
},
|
23
|
+
|
24
|
+
/**
|
25
|
+
* @method post()
|
26
|
+
* @description send a POST request
|
27
|
+
* @param {string} url - api url
|
28
|
+
* @param {object} data - payload
|
29
|
+
* @param {object} headers - headers to be added to request
|
30
|
+
* @returns {AxiosPromise<any>}
|
31
|
+
*/
|
32
|
+
async post({ url, data, headers }) {
|
33
|
+
try {
|
34
|
+
return axios({
|
35
|
+
url,
|
36
|
+
data,
|
37
|
+
headers,
|
38
|
+
method: 'post',
|
39
|
+
});
|
40
|
+
} catch (err) {
|
41
|
+
console.log('Axios POST Error:', err.message);
|
42
|
+
throw err;
|
43
|
+
}
|
44
|
+
},
|
45
|
+
|
46
|
+
/**
|
47
|
+
* @method put()
|
48
|
+
* @description send PUT request
|
49
|
+
* @param {string} url - api url
|
50
|
+
* @param {object} data - payload
|
51
|
+
* @param {object} headers - headers to be added to request
|
52
|
+
* @returns {AxiosPromise<any>}
|
53
|
+
*/
|
54
|
+
async put({ url, data, headers }) {
|
55
|
+
try {
|
56
|
+
return axios({
|
57
|
+
url,
|
58
|
+
data,
|
59
|
+
headers,
|
60
|
+
method: 'put',
|
61
|
+
});
|
62
|
+
} catch (err) {
|
63
|
+
console.log('Axios PUT Error:', err.message);
|
64
|
+
throw err;
|
65
|
+
}
|
66
|
+
},
|
67
|
+
|
68
|
+
/**
|
69
|
+
* @method patch()
|
70
|
+
* @description send PATCH request
|
71
|
+
* @param {string} url - api url
|
72
|
+
* @param {object} data - payload
|
73
|
+
* @param {object} headers - headers to be added to request
|
74
|
+
* @returns {AxiosPromise<any>}
|
75
|
+
*/
|
76
|
+
async patch({ url, data, headers }) {
|
77
|
+
try {
|
78
|
+
return axios({
|
79
|
+
url,
|
80
|
+
data,
|
81
|
+
headers,
|
82
|
+
method: 'patch',
|
83
|
+
});
|
84
|
+
} catch (err) {
|
85
|
+
console.log('Axios PUT Error:', err.message);
|
86
|
+
throw err;
|
87
|
+
}
|
88
|
+
},
|
89
|
+
|
90
|
+
/**
|
91
|
+
* @method delete()
|
92
|
+
* @description send DELETE request
|
93
|
+
* @param {string} urls - api url
|
94
|
+
* @param {object} headers - headers to be added to request
|
95
|
+
* @returns {AxiosPromise<any>}
|
96
|
+
*/
|
97
|
+
async delete({ url, headers }) {
|
98
|
+
try {
|
99
|
+
return axios({
|
100
|
+
url,
|
101
|
+
headers,
|
102
|
+
method: 'delete',
|
103
|
+
});
|
104
|
+
} catch (err) {
|
105
|
+
console.log('Axios DELETE Error:', err.message);
|
106
|
+
throw err;
|
107
|
+
}
|
108
|
+
},
|
109
|
+
};
|
@@ -0,0 +1,29 @@
|
|
1
|
+
const headers = require("./headers");
|
2
|
+
|
3
|
+
const schema = {
|
4
|
+
headers,
|
5
|
+
querystring: {
|
6
|
+
type: "object",
|
7
|
+
maxProperties: 1,
|
8
|
+
properties: {
|
9
|
+
id: { type: "number" },
|
10
|
+
email: { type: "string" },
|
11
|
+
},
|
12
|
+
oneOf: [
|
13
|
+
{
|
14
|
+
required: ["id"],
|
15
|
+
},
|
16
|
+
{
|
17
|
+
required: ["email"],
|
18
|
+
},
|
19
|
+
],
|
20
|
+
},
|
21
|
+
|
22
|
+
response: {
|
23
|
+
204: {
|
24
|
+
type: "null",
|
25
|
+
},
|
26
|
+
},
|
27
|
+
};
|
28
|
+
|
29
|
+
module.exports = schema;
|
@@ -0,0 +1,31 @@
|
|
1
|
+
const bcrypt = require('bcrypt');
|
2
|
+
|
3
|
+
module.exports = class {
|
4
|
+
/**
|
5
|
+
* @description constructor
|
6
|
+
*/
|
7
|
+
constructor() {
|
8
|
+
this.saltRounds = 10;
|
9
|
+
}
|
10
|
+
|
11
|
+
/**
|
12
|
+
* @method hash()
|
13
|
+
* @description create a hash of a text
|
14
|
+
* @param {string} text - text to be hashed
|
15
|
+
* @returns {*}
|
16
|
+
*/
|
17
|
+
hash({ text }) {
|
18
|
+
return bcrypt.hashSync(text, bcrypt.genSaltSync(this.saltRounds));
|
19
|
+
}
|
20
|
+
|
21
|
+
/**
|
22
|
+
* @method verify()
|
23
|
+
* @description verify a text by its hash
|
24
|
+
* @param {string} hash - the hashed text
|
25
|
+
* @param {string} text - the text to be compared with the hash
|
26
|
+
* @returns {*}
|
27
|
+
*/
|
28
|
+
verify({ hash, text }) {
|
29
|
+
return bcrypt.compareSync(text, hash);
|
30
|
+
}
|
31
|
+
};
|
package/security/jwt.js
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
const jwt = require('jsonwebtoken');
|
2
|
+
|
3
|
+
module.exports = class {
|
4
|
+
/**
|
5
|
+
* @description constructor
|
6
|
+
* @param {string} jwtKey - jwt key
|
7
|
+
*/
|
8
|
+
constructor({ jwtKey }) {
|
9
|
+
this.jwtKey = jwtKey;
|
10
|
+
}
|
11
|
+
|
12
|
+
/**
|
13
|
+
* @method jwtSign()
|
14
|
+
* @description create a JWT token
|
15
|
+
* @param {object} payload - payload to be tokenized
|
16
|
+
* @returns {*}
|
17
|
+
*/
|
18
|
+
jwtSign({ payload }) {
|
19
|
+
return jwt.sign(payload, this.jwtKey);
|
20
|
+
}
|
21
|
+
|
22
|
+
/**
|
23
|
+
* @method jwtDecode()
|
24
|
+
* @description decode a JWT token
|
25
|
+
* @param {string} token - JWT token to be decoded
|
26
|
+
* @returns {{payload: any, signature: *, header: *}|*|SourceMapPayload|TokenPayload}
|
27
|
+
*/
|
28
|
+
jwtDecode({ token }) {
|
29
|
+
return jwt.decode(token);
|
30
|
+
}
|
31
|
+
|
32
|
+
/**
|
33
|
+
* @method jwtVerify()
|
34
|
+
* @description verify a JWT token
|
35
|
+
* @param {string} token - token to be verified
|
36
|
+
* @returns {*}
|
37
|
+
*/
|
38
|
+
jwtVerify({ token }) {
|
39
|
+
return jwt.verify(token, this.jwtKey);
|
40
|
+
}
|
41
|
+
};
|
@@ -0,0 +1,65 @@
|
|
1
|
+
const moment = require('moment');
|
2
|
+
|
3
|
+
module.exports = class {
|
4
|
+
/**
|
5
|
+
* @method now()
|
6
|
+
* @description returns the present unix timestamp
|
7
|
+
* @returns {number}
|
8
|
+
*/
|
9
|
+
now() {
|
10
|
+
return moment().unix();
|
11
|
+
}
|
12
|
+
|
13
|
+
/**
|
14
|
+
* @method unixToDate()
|
15
|
+
* @description convert a unix timestamp to specific date format
|
16
|
+
* @param {number} unix - the unix timestamp to be converted
|
17
|
+
* @param {string} format - the format to use, default is 'MMMM DD, YYYY'
|
18
|
+
* @returns {string}
|
19
|
+
*/
|
20
|
+
unixToDate({ unix, format = 'MMMM DD, YYYY' }) {
|
21
|
+
return moment.unix(unix).format(format);
|
22
|
+
}
|
23
|
+
|
24
|
+
/**
|
25
|
+
* @method dateToUnix()
|
26
|
+
* @description convert a unix date to unix
|
27
|
+
* @param {number} date - the date to be converted
|
28
|
+
* @param {string} format - the format of the date
|
29
|
+
* @returns {number}
|
30
|
+
*/
|
31
|
+
dateToUnix({ date, format }) {
|
32
|
+
return moment(date, format).valueOf() / 1000;
|
33
|
+
}
|
34
|
+
|
35
|
+
/**
|
36
|
+
* @method unixToStartOfDay()
|
37
|
+
* @description take whatever date is given to 12:00:00 AM of that day
|
38
|
+
* @param {string} unix - unix timestamp
|
39
|
+
* @returns {number}
|
40
|
+
*/
|
41
|
+
unixToStartOfDay(unix) {
|
42
|
+
return new Date(unix * 1000).setHours(0, 0, 0, 0) / 1000;
|
43
|
+
}
|
44
|
+
|
45
|
+
/**
|
46
|
+
* @method unixToEndOfDay()
|
47
|
+
* @description take whatever date is given to 11:59:59 PM of the day
|
48
|
+
* @param {string} unix - unix timestamp
|
49
|
+
* @returns {number}
|
50
|
+
*/
|
51
|
+
unixToEndOfDay(unix) {
|
52
|
+
return new Date(unix * 1000).setHours(23, 59, 59, 999) / 1000;
|
53
|
+
}
|
54
|
+
|
55
|
+
/**
|
56
|
+
* @method toSeconds()
|
57
|
+
* @description convert time (HH:MM) to seconds
|
58
|
+
* @param {string} time - time to be converted
|
59
|
+
* @returns {number}
|
60
|
+
*/
|
61
|
+
toSeconds(time) {
|
62
|
+
time = time.split(':');
|
63
|
+
return time[0] * 3600 + time[1] * 60;
|
64
|
+
}
|
65
|
+
};
|