@questwork/q-utilities 0.1.2 → 0.1.4
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/dist/index.min.cjs +170 -355
- package/dist/index.min.js +172 -357
- package/package.json +7 -7
package/dist/index.min.js
CHANGED
|
@@ -27,13 +27,13 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
27
27
|
sh: () => (/* reexport */ ApiResponse),
|
|
28
28
|
Yc: () => (/* reexport */ KeyValueObject),
|
|
29
29
|
OS: () => (/* reexport */ Metadata),
|
|
30
|
+
Z8: () => (/* reexport */ QMeta),
|
|
30
31
|
lc: () => (/* reexport */ Repo),
|
|
31
32
|
kl: () => (/* reexport */ Service),
|
|
32
33
|
l0: () => (/* reexport */ convertString),
|
|
33
34
|
Yq: () => (/* reexport */ formatDate),
|
|
34
35
|
G8: () => (/* reexport */ getValidation),
|
|
35
36
|
pY: () => (/* reexport */ getValueByKeys),
|
|
36
|
-
Q_: () => (/* reexport */ jwtHelper),
|
|
37
37
|
su: () => (/* reexport */ makeApiResponse),
|
|
38
38
|
Q6: () => (/* reexport */ makeService),
|
|
39
39
|
Lv: () => (/* reexport */ padZeros),
|
|
@@ -264,284 +264,6 @@ function getValueByKeys(keys, data) {
|
|
|
264
264
|
;// ./lib/helpers/getValueByKeys/index.js
|
|
265
265
|
|
|
266
266
|
|
|
267
|
-
;// ./lib/helpers/jwtHelper/jwtHelper.js
|
|
268
|
-
// 'use strict'
|
|
269
|
-
|
|
270
|
-
/* eslint-disable new-cap */
|
|
271
|
-
/* eslint-disable camelcase */
|
|
272
|
-
/* eslint-disable no-mixed-operators */
|
|
273
|
-
/* eslint-disable no-useless-escape */
|
|
274
|
-
/* eslint-disable no-param-reassign */
|
|
275
|
-
|
|
276
|
-
/* eslint func-names: 0 */
|
|
277
|
-
|
|
278
|
-
// const Buffer = require('buffer/').Buffer
|
|
279
|
-
|
|
280
|
-
const EXPIRY = 3600 // in second
|
|
281
|
-
const ALGORITHM = 'HS256'
|
|
282
|
-
const SECRET = 'ab1234cd'
|
|
283
|
-
|
|
284
|
-
const _hasBuffer = typeof Buffer === 'function'
|
|
285
|
-
|
|
286
|
-
const jwtHelper = {
|
|
287
|
-
create(obj, { secret, algorithm, expiry } = {}) {
|
|
288
|
-
const sAlgorithm = algorithm || ALGORITHM
|
|
289
|
-
const sSecret = secret || SECRET
|
|
290
|
-
const exp = expiry || getTimeInSecond() + EXPIRY
|
|
291
|
-
const payload = {
|
|
292
|
-
...obj,
|
|
293
|
-
exp
|
|
294
|
-
}
|
|
295
|
-
return encode(payload, sSecret, sAlgorithm)
|
|
296
|
-
},
|
|
297
|
-
createByUser(loginAccount) {
|
|
298
|
-
const exp = getTimeInSecond() + EXPIRY
|
|
299
|
-
const payload = {
|
|
300
|
-
loginAccount,
|
|
301
|
-
exp
|
|
302
|
-
}
|
|
303
|
-
return this.encode(payload)
|
|
304
|
-
},
|
|
305
|
-
encode(payload, algorithm) {
|
|
306
|
-
return encode(payload, SECRET, algorithm)
|
|
307
|
-
},
|
|
308
|
-
decode(token, algorithm) {
|
|
309
|
-
const noVerify = !this.verify(token)
|
|
310
|
-
return decode(token, SECRET, noVerify, algorithm) // if noVerify = true, may skip verification
|
|
311
|
-
},
|
|
312
|
-
getPayload(token) {
|
|
313
|
-
const payload = getPayload(token)
|
|
314
|
-
return {
|
|
315
|
-
payload
|
|
316
|
-
}
|
|
317
|
-
},
|
|
318
|
-
getPayloadIdByKey(token, key) {
|
|
319
|
-
const payload = getPayload(token)
|
|
320
|
-
const id = payload[key] ? payload[key].id : null
|
|
321
|
-
return {
|
|
322
|
-
id,
|
|
323
|
-
jwtToken: token
|
|
324
|
-
}
|
|
325
|
-
},
|
|
326
|
-
resolve(token, secret, algorithm) {
|
|
327
|
-
const sSecret = secret || SECRET
|
|
328
|
-
return decode(token, sSecret, false, algorithm) // need verification
|
|
329
|
-
},
|
|
330
|
-
verify(token) {
|
|
331
|
-
const payload = getPayload(token)
|
|
332
|
-
const today = getTimeInSecond()
|
|
333
|
-
return (payload.exp && (today <= payload.exp)) || false
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* Private functions
|
|
339
|
-
*/
|
|
340
|
-
|
|
341
|
-
const getPayload = (token) => decode(token, SECRET, true)
|
|
342
|
-
|
|
343
|
-
const getTimeInSecond = () => Math.floor(Date.now() / 1000)
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Private functions, based on jwt-simple 0.2.0
|
|
347
|
-
*/
|
|
348
|
-
|
|
349
|
-
const { cryptoHelper } = require('../cryptoHelper')
|
|
350
|
-
|
|
351
|
-
const algorithmMap = {
|
|
352
|
-
HS256: 'sha256',
|
|
353
|
-
HS384: 'sha384',
|
|
354
|
-
HS512: 'sha512',
|
|
355
|
-
RS256: 'RSA-SHA256'
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const typeMap = {
|
|
359
|
-
HS256: 'hmac',
|
|
360
|
-
HS384: 'hmac',
|
|
361
|
-
HS512: 'hmac',
|
|
362
|
-
RS256: 'sign'
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* Decode jwt
|
|
368
|
-
*
|
|
369
|
-
* @param {Object} token
|
|
370
|
-
* @param {String} key
|
|
371
|
-
* @param {Boolean} noVerify
|
|
372
|
-
* @param {String} algorithm
|
|
373
|
-
* @return {Object} payload
|
|
374
|
-
* @api public
|
|
375
|
-
*/
|
|
376
|
-
const decode = function jwt_decode(token, key, noVerify, algorithm) {
|
|
377
|
-
// check token
|
|
378
|
-
if (!token) {
|
|
379
|
-
throw new Error('No token supplied')
|
|
380
|
-
}
|
|
381
|
-
// check segments
|
|
382
|
-
const segments = token.split('.')
|
|
383
|
-
if (segments.length !== 3) {
|
|
384
|
-
throw new Error('Not enough or too many segments')
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
// All segment should be base64
|
|
388
|
-
const headerSeg = segments[0]
|
|
389
|
-
const payloadSeg = segments[1]
|
|
390
|
-
const signatureSeg = segments[2]
|
|
391
|
-
|
|
392
|
-
// base64 decode and parse JSON
|
|
393
|
-
const header = JSON.parse(base64urlDecode(headerSeg))
|
|
394
|
-
|
|
395
|
-
const payload = JSON.parse(base64urlDecode(payloadSeg))
|
|
396
|
-
|
|
397
|
-
if (!noVerify) {
|
|
398
|
-
const signingMethod = algorithmMap[algorithm || header.alg]
|
|
399
|
-
const signingType = typeMap[algorithm || header.alg]
|
|
400
|
-
if (!signingMethod || !signingType) {
|
|
401
|
-
throw new Error('Algorithm not supported')
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
// verify signature. `sign` will return base64 string.
|
|
405
|
-
const signingInput = [headerSeg, payloadSeg].join('.')
|
|
406
|
-
if (!verify(signingInput, key, signingMethod, signingType, signatureSeg)) {
|
|
407
|
-
throw new Error('Signature verification failed')
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
return payload
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
/**
|
|
416
|
-
* Encode jwt
|
|
417
|
-
*
|
|
418
|
-
* @param {Object} payload
|
|
419
|
-
* @param {String} key
|
|
420
|
-
* @param {String} algorithm
|
|
421
|
-
* @return {String} token
|
|
422
|
-
* @api public
|
|
423
|
-
*/
|
|
424
|
-
const encode = function jwt_encode(payload, key, algorithm) {
|
|
425
|
-
// Check key
|
|
426
|
-
if (!key) {
|
|
427
|
-
throw new Error('Require key')
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
// Check algorithm, default is HS256
|
|
431
|
-
if (!algorithm) {
|
|
432
|
-
algorithm = ALGORITHM
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
const signingMethod = algorithmMap[algorithm]
|
|
436
|
-
const signingType = typeMap[algorithm]
|
|
437
|
-
if (!signingMethod || !signingType) {
|
|
438
|
-
throw new Error('Algorithm not supported')
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
// header, typ is fixed value.
|
|
442
|
-
const header = { typ: 'JWT', alg: algorithm }
|
|
443
|
-
|
|
444
|
-
// create segments, all segments should be base64 string
|
|
445
|
-
const segments = []
|
|
446
|
-
segments.push(base64urlEncode(JSON.stringify(header)))
|
|
447
|
-
segments.push(base64urlEncode(JSON.stringify(payload)))
|
|
448
|
-
segments.push(sign(segments.join('.'), key, signingMethod, signingType))
|
|
449
|
-
|
|
450
|
-
return segments.join('.')
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
/**
|
|
455
|
-
* private util functions
|
|
456
|
-
*/
|
|
457
|
-
|
|
458
|
-
function verify(input, key, method, type, signature) {
|
|
459
|
-
if (type === 'hmac') {
|
|
460
|
-
return (signature === sign(input, key, method, type))
|
|
461
|
-
} else if (type === 'sign') {
|
|
462
|
-
try {
|
|
463
|
-
return cryptoHelper.createStringVerify({
|
|
464
|
-
algorithm: method,
|
|
465
|
-
data: input,
|
|
466
|
-
object: key,
|
|
467
|
-
signature: base64urlUnescape(signature),
|
|
468
|
-
signatureEncoding: 'base64'
|
|
469
|
-
})
|
|
470
|
-
} catch (error) {
|
|
471
|
-
throw new Error('createStringVerify failed')
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
throw new Error('Algorithm type not recognized')
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
function sign(input, key, method, type) {
|
|
478
|
-
let base64str
|
|
479
|
-
if (type === 'hmac') {
|
|
480
|
-
base64str = cryptoHelper.createStringHmac({
|
|
481
|
-
algorithm: method,
|
|
482
|
-
key,
|
|
483
|
-
data: input,
|
|
484
|
-
outputEncoding: 'base64'
|
|
485
|
-
})
|
|
486
|
-
} else if (type === 'sign') {
|
|
487
|
-
try {
|
|
488
|
-
base64str = cryptoHelper.createSignature({
|
|
489
|
-
algorithm: method,
|
|
490
|
-
data: input,
|
|
491
|
-
privateKey: key,
|
|
492
|
-
outputEncoding: 'base64'
|
|
493
|
-
})
|
|
494
|
-
} catch (error) {
|
|
495
|
-
throw new Error('createSignature failed')
|
|
496
|
-
}
|
|
497
|
-
} else {
|
|
498
|
-
throw new Error('Algorithm type not recognized')
|
|
499
|
-
}
|
|
500
|
-
return base64urlEscape(base64str)
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
function _decode(str) {
|
|
504
|
-
if (_hasBuffer) {
|
|
505
|
-
return Buffer.from(base64urlUnescape(str), 'base64').toString('utf8')
|
|
506
|
-
}
|
|
507
|
-
return atob(base64urlUnescape(str))
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
function _encode(str) {
|
|
511
|
-
if (_hasBuffer) {
|
|
512
|
-
return base64urlEscape(Buffer.from(str, 'utf8').toString('base64'))
|
|
513
|
-
}
|
|
514
|
-
return base64urlEscape(btoa(str))
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
function base64urlDecode(str) {
|
|
518
|
-
// fixed bug if decode string is incorrect
|
|
519
|
-
// return (new Buffer.from(base64urlUnescape(str), 'base64')).toString() || '{}'
|
|
520
|
-
return _decode(str)
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
function base64urlUnescape(str) {
|
|
524
|
-
str += new Array(5 - str.length % 4).join('=')
|
|
525
|
-
return str.replace(/\-/g, '+').replace(/_/g, '/')
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
function base64urlEncode(str) {
|
|
529
|
-
// return base64urlEscape(new Buffer.from((str)).toString('base64'))
|
|
530
|
-
return _encode(str)
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
function base64urlEscape(str) {
|
|
534
|
-
return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
;// ./lib/helpers/jwtHelper/index.js
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
267
|
;// ./lib/helpers/padZeros/padZeros.js
|
|
546
268
|
function padZeros(num, minLength = 6) {
|
|
547
269
|
num = num.toString()
|
|
@@ -578,7 +300,6 @@ function stringFormatter(str) {
|
|
|
578
300
|
|
|
579
301
|
|
|
580
302
|
|
|
581
|
-
|
|
582
303
|
;// ./lib/models/apiResponse/apiResponse.js
|
|
583
304
|
class ApiResponse {
|
|
584
305
|
constructor(options = {}) {
|
|
@@ -669,107 +390,85 @@ class KeyValueObject {
|
|
|
669
390
|
}
|
|
670
391
|
|
|
671
392
|
static addItem(arr, key, value) {
|
|
672
|
-
arr.push(
|
|
673
|
-
{ key, value }
|
|
674
|
-
)
|
|
393
|
+
arr.push(this.init({ key, value }))
|
|
675
394
|
}
|
|
676
395
|
static addRecord(arr = [], key, value) {
|
|
677
|
-
const self = this
|
|
678
396
|
if (!this.hasKeyValue(arr, key, value)) {
|
|
679
|
-
arr.push(
|
|
397
|
+
arr.push(this.init({ key, value }))
|
|
680
398
|
}
|
|
681
399
|
return arr
|
|
682
400
|
}
|
|
683
|
-
|
|
684
401
|
static appendRecord(arr = [], key, value) {
|
|
685
402
|
return arr.map((item) => {
|
|
686
|
-
if (item
|
|
403
|
+
if (this.sameKey(item, key)) {
|
|
687
404
|
item.value = [...item.value, ...value]
|
|
688
405
|
}
|
|
689
406
|
return item
|
|
690
407
|
})
|
|
691
408
|
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
acc.push(self.init({ key, value: options[key] }))
|
|
697
|
-
return acc
|
|
698
|
-
}, [])
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
static removeByKey(arr, key) {
|
|
702
|
-
return arr.reduce((acc, item) => {
|
|
703
|
-
if (item.key !== key) {
|
|
704
|
-
acc.push(item)
|
|
409
|
+
static appendValueArray(arr = [], key, value) {
|
|
410
|
+
return arr.map((item) => {
|
|
411
|
+
if (this.sameKey(item, key)) {
|
|
412
|
+
item.value = [...item.value, ...value]
|
|
705
413
|
}
|
|
706
|
-
return
|
|
707
|
-
}
|
|
414
|
+
return item
|
|
415
|
+
})
|
|
708
416
|
}
|
|
709
|
-
|
|
710
417
|
static foundByKey(arr = [], key) {
|
|
711
418
|
const found = arr.find((m) => {
|
|
712
|
-
return m
|
|
419
|
+
return this.sameKey(m, key)
|
|
713
420
|
})
|
|
714
421
|
return found || null
|
|
715
422
|
}
|
|
716
|
-
|
|
717
423
|
static foundValueByKey(arr = [], key) {
|
|
718
424
|
const found = this.foundByKey(arr, key)
|
|
719
425
|
return found ? found.value : null
|
|
720
426
|
}
|
|
721
|
-
|
|
427
|
+
static fromObject(options = {}) {
|
|
428
|
+
return Object.keys(options).reduce((acc, key) => {
|
|
429
|
+
acc.push(this.init({ key, value: options[key] }))
|
|
430
|
+
return acc
|
|
431
|
+
}, [])
|
|
432
|
+
}
|
|
722
433
|
static getValueByKey(arr = [], key) {
|
|
723
|
-
|
|
724
|
-
return i.key === key
|
|
725
|
-
})
|
|
726
|
-
if (found) {
|
|
727
|
-
return found.value
|
|
728
|
-
}
|
|
729
|
-
return null
|
|
434
|
+
return this.foundValueByKey(arr, key)
|
|
730
435
|
}
|
|
731
|
-
|
|
732
436
|
static getValueByKeyFromArray(arr = [], key) {
|
|
733
437
|
if (arr.length === 0) {
|
|
734
438
|
return null
|
|
735
439
|
}
|
|
736
440
|
const firstArr = arr.shift()
|
|
737
441
|
const found = firstArr.find((i) => {
|
|
738
|
-
return i
|
|
442
|
+
return this.sameKey(i, key)
|
|
739
443
|
})
|
|
740
444
|
if (found && found.value) {
|
|
741
445
|
return found.value
|
|
742
446
|
}
|
|
743
447
|
return this.getValueByKeyFromArray(arr, key)
|
|
744
448
|
}
|
|
745
|
-
|
|
746
449
|
static getValuesByKey(arr = [], key) {
|
|
747
450
|
return arr.reduce((acc, item) => {
|
|
748
|
-
if (item
|
|
451
|
+
if (this.sameKey(item, key)) {
|
|
749
452
|
acc.push(item.value)
|
|
750
453
|
}
|
|
751
454
|
return acc
|
|
752
455
|
}, [])
|
|
753
456
|
}
|
|
754
|
-
|
|
755
457
|
static hasKeyValue(arr = [], key, value) {
|
|
756
458
|
if (typeof value === 'undefined') {
|
|
757
|
-
return arr.filter((item) => item
|
|
459
|
+
return arr.filter((item) => this.sameKey(item, key)).length > 0
|
|
758
460
|
}
|
|
759
|
-
return arr.filter((item) => (item
|
|
461
|
+
return arr.filter((item) => (this.sameKey(item, key) && item.value === value)).length > 0
|
|
760
462
|
}
|
|
761
|
-
|
|
762
463
|
static insertOrUpdateRecord(arr = [], key, value) {
|
|
763
|
-
const self = this
|
|
764
464
|
let copy = [...arr]
|
|
765
|
-
if (!
|
|
766
|
-
copy.push(
|
|
465
|
+
if (!this.hasKeyValue(arr, key)) {
|
|
466
|
+
copy.push(this.init({ key, value }))
|
|
767
467
|
} else {
|
|
768
|
-
copy =
|
|
468
|
+
copy = this.updateRecord(arr, key, value)
|
|
769
469
|
}
|
|
770
470
|
return copy
|
|
771
471
|
}
|
|
772
|
-
|
|
773
472
|
static keys(arr = []) {
|
|
774
473
|
if (Array.isArray(arr)) {
|
|
775
474
|
return arr.reduce((acc, item) => {
|
|
@@ -779,7 +478,6 @@ class KeyValueObject {
|
|
|
779
478
|
}
|
|
780
479
|
return []
|
|
781
480
|
}
|
|
782
|
-
|
|
783
481
|
static merge(toArr, fromArr) {
|
|
784
482
|
(fromArr || []).map((from) => {
|
|
785
483
|
const found = toArr.find((to) => {
|
|
@@ -793,7 +491,17 @@ class KeyValueObject {
|
|
|
793
491
|
})
|
|
794
492
|
return toArr
|
|
795
493
|
}
|
|
796
|
-
|
|
494
|
+
static removeByKey(arr, key) {
|
|
495
|
+
return arr.reduce((acc, item) => {
|
|
496
|
+
if (!this.sameKey(item, key)) {
|
|
497
|
+
acc.push(item)
|
|
498
|
+
}
|
|
499
|
+
return acc
|
|
500
|
+
}, [])
|
|
501
|
+
}
|
|
502
|
+
static sameKey(item, key) {
|
|
503
|
+
return item.key === key
|
|
504
|
+
}
|
|
797
505
|
static toObject(arr = []) {
|
|
798
506
|
if (Array.isArray(arr)) {
|
|
799
507
|
return arr.reduce((acc, item) => {
|
|
@@ -803,7 +511,6 @@ class KeyValueObject {
|
|
|
803
511
|
}
|
|
804
512
|
return {}
|
|
805
513
|
}
|
|
806
|
-
|
|
807
514
|
static toString(arr = [], delimiter = '; ') {
|
|
808
515
|
if (Array.isArray(arr)) {
|
|
809
516
|
return arr.reduce((acc, item) => {
|
|
@@ -813,10 +520,9 @@ class KeyValueObject {
|
|
|
813
520
|
}
|
|
814
521
|
return ''
|
|
815
522
|
}
|
|
816
|
-
|
|
817
523
|
static updateRecord(arr = [], key, value) {
|
|
818
524
|
return arr.map((item) => {
|
|
819
|
-
if (item
|
|
525
|
+
if (this.sameKey(item, key)) {
|
|
820
526
|
return {
|
|
821
527
|
...item,
|
|
822
528
|
value
|
|
@@ -825,11 +531,9 @@ class KeyValueObject {
|
|
|
825
531
|
return item
|
|
826
532
|
})
|
|
827
533
|
}
|
|
828
|
-
|
|
829
534
|
static updateOrInsertRecord(arr = [], key, value) {
|
|
830
535
|
return this.insertOrUpdateRecord(arr, key, value)
|
|
831
536
|
}
|
|
832
|
-
|
|
833
537
|
static updateRecordsFromArray(arr = [], updateArr = []) {
|
|
834
538
|
if (Array.isArray(arr) && Array.isArray(updateArr)) {
|
|
835
539
|
const obj1 = this.toObject(arr)
|
|
@@ -841,7 +545,6 @@ class KeyValueObject {
|
|
|
841
545
|
}
|
|
842
546
|
return []
|
|
843
547
|
}
|
|
844
|
-
|
|
845
548
|
static values(arr = []) {
|
|
846
549
|
if (Array.isArray(arr)) {
|
|
847
550
|
return arr.reduce((acc, item) => {
|
|
@@ -888,22 +591,92 @@ class Metadata extends KeyValueObject {
|
|
|
888
591
|
})
|
|
889
592
|
return instance.isValid ? instance : null
|
|
890
593
|
}
|
|
594
|
+
static get _classname() {
|
|
595
|
+
return 'Metadata'
|
|
596
|
+
}
|
|
891
597
|
|
|
892
|
-
static
|
|
893
|
-
|
|
894
|
-
|
|
598
|
+
static merge(toArr, fromArr) {
|
|
599
|
+
(fromArr || []).map((from) => {
|
|
600
|
+
const found = toArr.find((to) => {
|
|
601
|
+
return stringFormatter(to.key) === stringFormatter(from.key)
|
|
602
|
+
})
|
|
603
|
+
if (found) {
|
|
604
|
+
found.value = (found.value || []).concat(from.value)
|
|
605
|
+
} else {
|
|
606
|
+
toArr.push(from)
|
|
607
|
+
}
|
|
895
608
|
})
|
|
896
|
-
return
|
|
609
|
+
return toArr
|
|
610
|
+
}
|
|
611
|
+
static sameKey(item, key) {
|
|
612
|
+
return stringFormatter(item.key) === stringFormatter(key)
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
;// ./lib/models/metadata/index.js
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
;// ./lib/models/qMeta/qMeta.js
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
const updateAllowedProps = [
|
|
627
|
+
'attributes',
|
|
628
|
+
'ref'
|
|
629
|
+
]
|
|
630
|
+
|
|
631
|
+
class QMeta {
|
|
632
|
+
constructor(options = {}) {
|
|
633
|
+
options = options || {}
|
|
634
|
+
this.attributes = KeyValueObject.initOnlyValidFromArray(options.attributes)
|
|
635
|
+
this.ref = options.ref || {}
|
|
897
636
|
}
|
|
898
637
|
|
|
899
638
|
static get _classname() {
|
|
900
|
-
return '
|
|
639
|
+
return 'QMeta'
|
|
640
|
+
}
|
|
641
|
+
static get _superclass() {
|
|
642
|
+
return 'QMeta'
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// Class methods
|
|
646
|
+
static init(options = {}) {
|
|
647
|
+
if (options instanceof QMeta) {
|
|
648
|
+
return options
|
|
649
|
+
}
|
|
650
|
+
return new QMeta(options)
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// instance methods
|
|
654
|
+
addAttribute(obj) {
|
|
655
|
+
const kvObject = KeyValueObject.init(obj)
|
|
656
|
+
if (!kvObject) {
|
|
657
|
+
throw new Error('invalid meta attribute')
|
|
658
|
+
}
|
|
659
|
+
this.attributes.push(kvObject)
|
|
660
|
+
return this
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
update(obj) {
|
|
664
|
+
Object.keys(obj).forEach((key) => {
|
|
665
|
+
if (updateAllowedProps.includes(key)) {
|
|
666
|
+
if (key === 'attributes') {
|
|
667
|
+
this[key] = KeyValueObject.initOnlyValidFromArray(obj[key])
|
|
668
|
+
} else {
|
|
669
|
+
this[key] = obj[key]
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
})
|
|
673
|
+
return this
|
|
901
674
|
}
|
|
902
675
|
}
|
|
903
676
|
|
|
904
677
|
|
|
905
678
|
|
|
906
|
-
;// ./lib/models/
|
|
679
|
+
;// ./lib/models/qMeta/index.js
|
|
907
680
|
|
|
908
681
|
|
|
909
682
|
|
|
@@ -911,10 +684,22 @@ class Metadata extends KeyValueObject {
|
|
|
911
684
|
;// ./lib/models/repo/repo.js
|
|
912
685
|
class Repo {
|
|
913
686
|
constructor(options) {
|
|
687
|
+
options = options || {}
|
|
914
688
|
this.model = options.model
|
|
915
|
-
this.
|
|
689
|
+
this._sharedOptions = options._sharedOptions // { session: this.dbTransaction }
|
|
690
|
+
this._queryOptions = options._queryOptions
|
|
691
|
+
this._saveOptions = options._saveOptions
|
|
692
|
+
this._Class = options._constructor && options._constructor._Class
|
|
693
|
+
? options._constructor._Class
|
|
694
|
+
: null
|
|
695
|
+
}
|
|
696
|
+
static init(options = {}) {
|
|
697
|
+
if (options instanceof this) {
|
|
698
|
+
return options
|
|
699
|
+
}
|
|
700
|
+
const instance = new this(options)
|
|
701
|
+
return instance.isValid ? instance : null
|
|
916
702
|
}
|
|
917
|
-
|
|
918
703
|
static get _classname() {
|
|
919
704
|
return 'Repo'
|
|
920
705
|
}
|
|
@@ -922,8 +707,40 @@ class Repo {
|
|
|
922
707
|
return 'Repo'
|
|
923
708
|
}
|
|
924
709
|
|
|
710
|
+
get _classname() {
|
|
711
|
+
return 'Repo'
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
get _superclass() {
|
|
715
|
+
return 'Repo'
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
get isValid() {
|
|
719
|
+
return this.model
|
|
720
|
+
&& (typeof this.model.deleteOne === 'function')
|
|
721
|
+
&& (typeof this.model.findAll === 'function')
|
|
722
|
+
&& (typeof this.model.saveOne === 'function')
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
get queryOptions() {
|
|
726
|
+
return {
|
|
727
|
+
...this._sharedOptions,
|
|
728
|
+
...this._queryOptions,
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
get saveOptions() {
|
|
733
|
+
return {
|
|
734
|
+
...this._sharedOptions,
|
|
735
|
+
...this._saveOptions,
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
925
739
|
init(options) {
|
|
926
|
-
|
|
740
|
+
if (this._Class && typeof this._Class.init === 'function') {
|
|
741
|
+
return this._Class.init(options)
|
|
742
|
+
}
|
|
743
|
+
return options
|
|
927
744
|
}
|
|
928
745
|
|
|
929
746
|
async deleteOne({ id }) {
|
|
@@ -940,9 +757,8 @@ class Repo {
|
|
|
940
757
|
}
|
|
941
758
|
|
|
942
759
|
findAll({ query }) {
|
|
943
|
-
const options = {}
|
|
944
760
|
return new Promise((resolve, reject) => {
|
|
945
|
-
this.model.findAll(query,
|
|
761
|
+
this.model.findAll(query, this.queryOptions, (err, data, total) => {
|
|
946
762
|
if (err) {
|
|
947
763
|
reject(err)
|
|
948
764
|
} else {
|
|
@@ -957,9 +773,8 @@ class Repo {
|
|
|
957
773
|
}
|
|
958
774
|
|
|
959
775
|
findOne({ query }) {
|
|
960
|
-
const options = { session: this.dbTransaction }
|
|
961
776
|
return new Promise((resolve, reject) => {
|
|
962
|
-
this.model.findAll(query,
|
|
777
|
+
this.model.findAll(query, this.queryOptions, (err, data) => {
|
|
963
778
|
if (err) {
|
|
964
779
|
reject(err)
|
|
965
780
|
} else if (data.length === 1) {
|
|
@@ -978,13 +793,13 @@ class Repo {
|
|
|
978
793
|
}
|
|
979
794
|
|
|
980
795
|
saveAll({ docs }) {
|
|
981
|
-
const self = this
|
|
982
796
|
let isNew
|
|
983
797
|
return Promise.all(docs.map(async (doc) => {
|
|
984
798
|
if (doc) {
|
|
985
|
-
const result = await
|
|
799
|
+
const result = await this.saveOne({ doc })
|
|
986
800
|
isNew = result.isNew
|
|
987
|
-
|
|
801
|
+
const _data = result._data || result.data
|
|
802
|
+
return _data[0]
|
|
988
803
|
}
|
|
989
804
|
return null
|
|
990
805
|
})).then((savedData) => {
|
|
@@ -998,9 +813,8 @@ class Repo {
|
|
|
998
813
|
}
|
|
999
814
|
|
|
1000
815
|
saveOne({ doc }) {
|
|
1001
|
-
const options = { session: this.dbTransaction }
|
|
1002
816
|
return new Promise((resolve, reject) => {
|
|
1003
|
-
this.model.saveOne(doc,
|
|
817
|
+
this.model.saveOne(doc, this.saveOptions, (err, result) => {
|
|
1004
818
|
if (err) {
|
|
1005
819
|
reject(err)
|
|
1006
820
|
} else {
|
|
@@ -1096,9 +910,9 @@ function makeService({ repo }) {
|
|
|
1096
910
|
if (repo === undefined) {
|
|
1097
911
|
throw new Error('repo is required.')
|
|
1098
912
|
}
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
913
|
+
if (repo._superclass !== Repo._superclass) {
|
|
914
|
+
throw new Error('repo is not an instance of Repo.')
|
|
915
|
+
}
|
|
1102
916
|
return new Service({ repo })
|
|
1103
917
|
}
|
|
1104
918
|
|
|
@@ -1116,6 +930,7 @@ function makeService({ repo }) {
|
|
|
1116
930
|
|
|
1117
931
|
|
|
1118
932
|
|
|
933
|
+
|
|
1119
934
|
;// ./lib/index.js
|
|
1120
935
|
|
|
1121
936
|
|
|
@@ -1126,15 +941,15 @@ function makeService({ repo }) {
|
|
|
1126
941
|
var __webpack_exports__ApiResponse = __webpack_exports__.sh;
|
|
1127
942
|
var __webpack_exports__KeyValueObject = __webpack_exports__.Yc;
|
|
1128
943
|
var __webpack_exports__Metadata = __webpack_exports__.OS;
|
|
944
|
+
var __webpack_exports__QMeta = __webpack_exports__.Z8;
|
|
1129
945
|
var __webpack_exports__Repo = __webpack_exports__.lc;
|
|
1130
946
|
var __webpack_exports__Service = __webpack_exports__.kl;
|
|
1131
947
|
var __webpack_exports__convertString = __webpack_exports__.l0;
|
|
1132
948
|
var __webpack_exports__formatDate = __webpack_exports__.Yq;
|
|
1133
949
|
var __webpack_exports__getValidation = __webpack_exports__.G8;
|
|
1134
950
|
var __webpack_exports__getValueByKeys = __webpack_exports__.pY;
|
|
1135
|
-
var __webpack_exports__jwtHelper = __webpack_exports__.Q_;
|
|
1136
951
|
var __webpack_exports__makeApiResponse = __webpack_exports__.su;
|
|
1137
952
|
var __webpack_exports__makeService = __webpack_exports__.Q6;
|
|
1138
953
|
var __webpack_exports__padZeros = __webpack_exports__.Lv;
|
|
1139
954
|
var __webpack_exports__stringFormatter = __webpack_exports__.Qy;
|
|
1140
|
-
export { __webpack_exports__ApiResponse as ApiResponse, __webpack_exports__KeyValueObject as KeyValueObject, __webpack_exports__Metadata as Metadata, __webpack_exports__Repo as Repo, __webpack_exports__Service as Service, __webpack_exports__convertString as convertString, __webpack_exports__formatDate as formatDate, __webpack_exports__getValidation as getValidation, __webpack_exports__getValueByKeys as getValueByKeys,
|
|
955
|
+
export { __webpack_exports__ApiResponse as ApiResponse, __webpack_exports__KeyValueObject as KeyValueObject, __webpack_exports__Metadata as Metadata, __webpack_exports__QMeta as QMeta, __webpack_exports__Repo as Repo, __webpack_exports__Service as Service, __webpack_exports__convertString as convertString, __webpack_exports__formatDate as formatDate, __webpack_exports__getValidation as getValidation, __webpack_exports__getValueByKeys as getValueByKeys, __webpack_exports__makeApiResponse as makeApiResponse, __webpack_exports__makeService as makeService, __webpack_exports__padZeros as padZeros, __webpack_exports__stringFormatter as stringFormatter };
|