@flowerforce/flowerbase 1.0.1-beta.3
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/CHANGELOG.md +0 -0
- package/LICENSE +3 -0
- package/README.md +18 -0
- package/dist/auth/controller.d.ts +8 -0
- package/dist/auth/controller.d.ts.map +1 -0
- package/dist/auth/controller.js +76 -0
- package/dist/auth/dtos.d.ts +6 -0
- package/dist/auth/dtos.d.ts.map +1 -0
- package/dist/auth/dtos.js +2 -0
- package/dist/auth/plugins/jwt.d.ts +14 -0
- package/dist/auth/plugins/jwt.d.ts.map +1 -0
- package/dist/auth/plugins/jwt.js +68 -0
- package/dist/auth/providers/local-userpass/controller.d.ts +8 -0
- package/dist/auth/providers/local-userpass/controller.d.ts.map +1 -0
- package/dist/auth/providers/local-userpass/controller.js +184 -0
- package/dist/auth/providers/local-userpass/dtos.d.ts +35 -0
- package/dist/auth/providers/local-userpass/dtos.d.ts.map +1 -0
- package/dist/auth/providers/local-userpass/dtos.js +2 -0
- package/dist/auth/utils.d.ts +126 -0
- package/dist/auth/utils.d.ts.map +1 -0
- package/dist/auth/utils.js +122 -0
- package/dist/constants.d.ts +18 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +34 -0
- package/dist/features/endpoints/index.d.ts +10 -0
- package/dist/features/endpoints/index.d.ts.map +1 -0
- package/dist/features/endpoints/index.js +31 -0
- package/dist/features/endpoints/interface.d.ts +27 -0
- package/dist/features/endpoints/interface.d.ts.map +1 -0
- package/dist/features/endpoints/interface.js +2 -0
- package/dist/features/endpoints/utils.d.ts +31 -0
- package/dist/features/endpoints/utils.d.ts.map +1 -0
- package/dist/features/endpoints/utils.js +85 -0
- package/dist/features/functions/controller.d.ts +9 -0
- package/dist/features/functions/controller.d.ts.map +1 -0
- package/dist/features/functions/controller.js +88 -0
- package/dist/features/functions/dtos.d.ts +34 -0
- package/dist/features/functions/dtos.d.ts.map +1 -0
- package/dist/features/functions/dtos.js +2 -0
- package/dist/features/functions/index.d.ts +9 -0
- package/dist/features/functions/index.d.ts.map +1 -0
- package/dist/features/functions/index.js +28 -0
- package/dist/features/functions/interface.d.ts +32 -0
- package/dist/features/functions/interface.d.ts.map +1 -0
- package/dist/features/functions/interface.js +2 -0
- package/dist/features/functions/utils.d.ts +23 -0
- package/dist/features/functions/utils.d.ts.map +1 -0
- package/dist/features/functions/utils.js +75 -0
- package/dist/features/rules/index.d.ts +1 -0
- package/dist/features/rules/index.d.ts.map +1 -0
- package/dist/features/rules/index.js +1 -0
- package/dist/features/rules/interface.d.ts +22 -0
- package/dist/features/rules/interface.d.ts.map +1 -0
- package/dist/features/rules/interface.js +2 -0
- package/dist/features/rules/utils.d.ts +3 -0
- package/dist/features/rules/utils.d.ts.map +1 -0
- package/dist/features/rules/utils.js +31 -0
- package/dist/features/triggers/dtos.d.ts +9 -0
- package/dist/features/triggers/dtos.d.ts.map +1 -0
- package/dist/features/triggers/dtos.js +2 -0
- package/dist/features/triggers/index.d.ts +10 -0
- package/dist/features/triggers/index.d.ts.map +1 -0
- package/dist/features/triggers/index.js +57 -0
- package/dist/features/triggers/interface.d.ts +44 -0
- package/dist/features/triggers/interface.d.ts.map +1 -0
- package/dist/features/triggers/interface.js +2 -0
- package/dist/features/triggers/utils.d.ts +16 -0
- package/dist/features/triggers/utils.d.ts.map +1 -0
- package/dist/features/triggers/utils.js +153 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +84 -0
- package/dist/model.d.ts +2 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +2 -0
- package/dist/services/api/index.d.ts +36 -0
- package/dist/services/api/index.d.ts.map +1 -0
- package/dist/services/api/index.js +36 -0
- package/dist/services/api/model.d.ts +33 -0
- package/dist/services/api/model.d.ts.map +1 -0
- package/dist/services/api/model.js +2 -0
- package/dist/services/api/utils.d.ts +16 -0
- package/dist/services/api/utils.d.ts.map +1 -0
- package/dist/services/api/utils.js +45 -0
- package/dist/services/aws/index.d.ts +13 -0
- package/dist/services/aws/index.d.ts.map +1 -0
- package/dist/services/aws/index.js +50 -0
- package/dist/services/index.d.ts +41 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +14 -0
- package/dist/services/interface.d.ts +3 -0
- package/dist/services/interface.d.ts.map +1 -0
- package/dist/services/interface.js +2 -0
- package/dist/services/mongodb-atlas/index.d.ts +4 -0
- package/dist/services/mongodb-atlas/index.d.ts.map +1 -0
- package/dist/services/mongodb-atlas/index.js +483 -0
- package/dist/services/mongodb-atlas/model.d.ts +39 -0
- package/dist/services/mongodb-atlas/model.d.ts.map +1 -0
- package/dist/services/mongodb-atlas/model.js +2 -0
- package/dist/services/mongodb-atlas/utils.d.ts +8 -0
- package/dist/services/mongodb-atlas/utils.d.ts.map +1 -0
- package/dist/services/mongodb-atlas/utils.js +33 -0
- package/dist/state.d.ts +6 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +18 -0
- package/dist/utils/context/helpers.d.ts +74 -0
- package/dist/utils/context/helpers.d.ts.map +1 -0
- package/dist/utils/context/helpers.js +60 -0
- package/dist/utils/context/index.d.ts +14 -0
- package/dist/utils/context/index.d.ts.map +1 -0
- package/dist/utils/context/index.js +50 -0
- package/dist/utils/context/interface.d.ts +18 -0
- package/dist/utils/context/interface.d.ts.map +1 -0
- package/dist/utils/context/interface.js +2 -0
- package/dist/utils/crypto/index.d.ts +19 -0
- package/dist/utils/crypto/index.d.ts.map +1 -0
- package/dist/utils/crypto/index.js +50 -0
- package/dist/utils/helpers/someAsync.d.ts +12 -0
- package/dist/utils/helpers/someAsync.d.ts.map +1 -0
- package/dist/utils/helpers/someAsync.js +56 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +11 -0
- package/dist/utils/initializer/exposeRoutes.d.ts +8 -0
- package/dist/utils/initializer/exposeRoutes.d.ts.map +1 -0
- package/dist/utils/initializer/exposeRoutes.js +41 -0
- package/dist/utils/initializer/registerPlugins.d.ts +19 -0
- package/dist/utils/initializer/registerPlugins.d.ts.map +1 -0
- package/dist/utils/initializer/registerPlugins.js +84 -0
- package/dist/utils/roles/helpers.d.ts +4 -0
- package/dist/utils/roles/helpers.d.ts.map +1 -0
- package/dist/utils/roles/helpers.js +47 -0
- package/dist/utils/roles/interface.d.ts +33 -0
- package/dist/utils/roles/interface.d.ts.map +1 -0
- package/dist/utils/roles/interface.js +2 -0
- package/dist/utils/roles/machines/commonValidators.d.ts +6 -0
- package/dist/utils/roles/machines/commonValidators.d.ts.map +1 -0
- package/dist/utils/roles/machines/commonValidators.js +34 -0
- package/dist/utils/roles/machines/index.d.ts +14 -0
- package/dist/utils/roles/machines/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/index.js +27 -0
- package/dist/utils/roles/machines/interface.d.ts +46 -0
- package/dist/utils/roles/machines/interface.d.ts.map +1 -0
- package/dist/utils/roles/machines/interface.js +2 -0
- package/dist/utils/roles/machines/machine.d.ts +15 -0
- package/dist/utils/roles/machines/machine.d.ts.map +1 -0
- package/dist/utils/roles/machines/machine.js +97 -0
- package/dist/utils/roles/machines/read/A/index.d.ts +3 -0
- package/dist/utils/roles/machines/read/A/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/A/index.js +27 -0
- package/dist/utils/roles/machines/read/B/index.d.ts +3 -0
- package/dist/utils/roles/machines/read/B/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/B/index.js +36 -0
- package/dist/utils/roles/machines/read/C/index.d.ts +3 -0
- package/dist/utils/roles/machines/read/C/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/C/index.js +38 -0
- package/dist/utils/roles/machines/read/D/index.d.ts +3 -0
- package/dist/utils/roles/machines/read/D/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/D/index.js +26 -0
- package/dist/utils/roles/machines/read/D/validators.d.ts +4 -0
- package/dist/utils/roles/machines/read/D/validators.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/D/validators.js +24 -0
- package/dist/utils/roles/machines/read/index.d.ts +2 -0
- package/dist/utils/roles/machines/read/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/index.js +8 -0
- package/dist/utils/roles/machines/utils.d.ts +37 -0
- package/dist/utils/roles/machines/utils.d.ts.map +1 -0
- package/dist/utils/roles/machines/utils.js +54 -0
- package/dist/utils/roles/machines/write/A/index.d.ts +3 -0
- package/dist/utils/roles/machines/write/A/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/write/A/index.js +29 -0
- package/dist/utils/roles/machines/write/B/index.d.ts +3 -0
- package/dist/utils/roles/machines/write/B/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/write/B/index.js +47 -0
- package/dist/utils/roles/machines/write/C/index.d.ts +3 -0
- package/dist/utils/roles/machines/write/C/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/write/C/index.js +26 -0
- package/dist/utils/roles/machines/write/C/validators.d.ts +4 -0
- package/dist/utils/roles/machines/write/C/validators.d.ts.map +1 -0
- package/dist/utils/roles/machines/write/C/validators.js +24 -0
- package/dist/utils/roles/machines/write/index.d.ts +2 -0
- package/dist/utils/roles/machines/write/index.d.ts.map +1 -0
- package/dist/utils/roles/machines/write/index.js +7 -0
- package/dist/utils/rules-matcher/interface.d.ts +338 -0
- package/dist/utils/rules-matcher/interface.d.ts.map +1 -0
- package/dist/utils/rules-matcher/interface.js +26 -0
- package/dist/utils/rules-matcher/utils.d.ts +11 -0
- package/dist/utils/rules-matcher/utils.d.ts.map +1 -0
- package/dist/utils/rules-matcher/utils.js +214 -0
- package/dist/utils/rules.d.ts +2 -0
- package/dist/utils/rules.d.ts.map +1 -0
- package/dist/utils/rules.js +22 -0
- package/jest.config.ts +24 -0
- package/package.json +63 -0
- package/project.json +10 -0
- package/rollup.config.js +17 -0
- package/src/auth/controller.ts +78 -0
- package/src/auth/dtos.ts +6 -0
- package/src/auth/plugins/jwt.ts +68 -0
- package/src/auth/providers/local-userpass/controller.ts +226 -0
- package/src/auth/providers/local-userpass/dtos.ts +40 -0
- package/src/auth/utils.ts +165 -0
- package/src/babel.config.json +3 -0
- package/src/constants.ts +22 -0
- package/src/fastify.d.ts +28 -0
- package/src/features/endpoints/index.ts +27 -0
- package/src/features/endpoints/interface.ts +29 -0
- package/src/features/endpoints/utils.ts +72 -0
- package/src/features/functions/controller.ts +102 -0
- package/src/features/functions/dtos.ts +41 -0
- package/src/features/functions/index.ts +21 -0
- package/src/features/functions/interface.ts +38 -0
- package/src/features/functions/utils.ts +82 -0
- package/src/features/rules/index.tsx +0 -0
- package/src/features/rules/interface.ts +24 -0
- package/src/features/rules/utils.ts +20 -0
- package/src/features/triggers/dtos.ts +9 -0
- package/src/features/triggers/index.ts +34 -0
- package/src/features/triggers/interface.ts +44 -0
- package/src/features/triggers/utils.ts +157 -0
- package/src/global.d.ts +0 -0
- package/src/index.ts +75 -0
- package/src/model.ts +1 -0
- package/src/services/api/index.ts +50 -0
- package/src/services/api/model.ts +38 -0
- package/src/services/api/utils.ts +39 -0
- package/src/services/aws/index.ts +48 -0
- package/src/services/index.ts +9 -0
- package/src/services/interface.ts +3 -0
- package/src/services/mongodb-atlas/index.ts +569 -0
- package/src/services/mongodb-atlas/model.ts +67 -0
- package/src/services/mongodb-atlas/utils.ts +44 -0
- package/src/state.ts +24 -0
- package/src/utils/__tests__/STEP_A_STATES.test.ts +54 -0
- package/src/utils/__tests__/STEP_B_STATES.test.ts +113 -0
- package/src/utils/__tests__/STEP_C_STATES.test.ts +87 -0
- package/src/utils/__tests__/STEP_D_STATES.test.ts +93 -0
- package/src/utils/__tests__/checkAdditionalFieldsFn.test.ts +45 -0
- package/src/utils/__tests__/checkApplyWhen.test.ts +49 -0
- package/src/utils/__tests__/checkFieldsPropertyExists.test.ts +47 -0
- package/src/utils/__tests__/checkIsValidFieldNameFn.test.ts +190 -0
- package/src/utils/__tests__/comparePassword.test.ts +38 -0
- package/src/utils/__tests__/evaluateDocumentsFiltersReadFn.test.ts +57 -0
- package/src/utils/__tests__/evaluateDocumentsFiltersWriteFn.test.ts +57 -0
- package/src/utils/__tests__/evaluateTopLevelReadFn.test.ts +58 -0
- package/src/utils/__tests__/evaluateTopLevelWriteFn.test.ts +66 -0
- package/src/utils/__tests__/exposeRoutes.test.ts +65 -0
- package/src/utils/__tests__/generateContextData.test.ts +75 -0
- package/src/utils/__tests__/getDefaultRule.test.ts +29 -0
- package/src/utils/__tests__/getKey.test.ts +12 -0
- package/src/utils/__tests__/getKeys.test.ts +11 -0
- package/src/utils/__tests__/getWinningRole.test.ts +66 -0
- package/src/utils/__tests__/hashPassword.test.ts +28 -0
- package/src/utils/__tests__/isEmpty.test.ts +17 -0
- package/src/utils/__tests__/logMachineInfo.test.ts +15 -0
- package/src/utils/__tests__/operators.test.ts +99 -0
- package/src/utils/__tests__/readFileContent.test.ts +35 -0
- package/src/utils/__tests__/registerPlugins.test.ts +59 -0
- package/src/utils/__tests__/rule.test.ts +51 -0
- package/src/utils/__tests__/rulesMatcherInterfaces.test.ts +57 -0
- package/src/utils/__tests__/rulesMatcherUtils.test.ts +56 -0
- package/src/utils/__tests__/someAsync.test.ts +55 -0
- package/src/utils/context/helpers.ts +71 -0
- package/src/utils/context/index.ts +52 -0
- package/src/utils/context/interface.ts +19 -0
- package/src/utils/crypto/index.ts +36 -0
- package/src/utils/helpers/someAsync.ts +24 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/initializer/exposeRoutes.ts +26 -0
- package/src/utils/initializer/registerPlugins.ts +97 -0
- package/src/utils/roles/helpers.ts +47 -0
- package/src/utils/roles/interface.ts +42 -0
- package/src/utils/roles/machines/commonValidators.ts +24 -0
- package/src/utils/roles/machines/index.ts +20 -0
- package/src/utils/roles/machines/interface.ts +46 -0
- package/src/utils/roles/machines/machine.ts +85 -0
- package/src/utils/roles/machines/read/A/index.ts +19 -0
- package/src/utils/roles/machines/read/B/index.ts +31 -0
- package/src/utils/roles/machines/read/C/index.ts +30 -0
- package/src/utils/roles/machines/read/D/index.ts +20 -0
- package/src/utils/roles/machines/read/D/validators.ts +24 -0
- package/src/utils/roles/machines/read/index.ts +6 -0
- package/src/utils/roles/machines/utils.ts +54 -0
- package/src/utils/roles/machines/write/A/index.ts +25 -0
- package/src/utils/roles/machines/write/B/index.ts +43 -0
- package/src/utils/roles/machines/write/C/index.ts +20 -0
- package/src/utils/roles/machines/write/C/validators.ts +24 -0
- package/src/utils/roles/machines/write/index.ts +5 -0
- package/src/utils/rules-matcher/interface.ts +365 -0
- package/src/utils/rules-matcher/utils.ts +281 -0
- package/src/utils/rules.ts +19 -0
- package/tsconfig.json +28 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import _get from 'lodash/get'
|
|
2
|
+
import _intersection from 'lodash/intersection'
|
|
3
|
+
import _trimStart from 'lodash/trimStart'
|
|
4
|
+
import { Operators, RulesMatcherUtils, RulesObject } from './interface'
|
|
5
|
+
|
|
6
|
+
const EMPTY_STRING_REGEXP = /^\s*$/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Defines a utility object named rulesMatcherUtils, which contains various helper functions used for processing rules and data in a rule-matching context.
|
|
10
|
+
*/
|
|
11
|
+
const rulesMatcherUtils: RulesMatcherUtils = {
|
|
12
|
+
isNumber: (el) => {
|
|
13
|
+
const num = String(el)
|
|
14
|
+
return !!num.match(/(^-?|^\d+\.)\d+$/)
|
|
15
|
+
},
|
|
16
|
+
rule: (val, data, options) => {
|
|
17
|
+
const { prefix } = options || {}
|
|
18
|
+
const path = Object.keys(val)[0]
|
|
19
|
+
const valueBlock = val
|
|
20
|
+
const pathWithPrefix = rulesMatcherUtils.getPath(path, prefix)
|
|
21
|
+
const valueForKey = _get(data, pathWithPrefix, undefined)
|
|
22
|
+
const { name } = _get(valueBlock, [path], {}) || {}
|
|
23
|
+
const { op, value, opt } = rulesMatcherUtils.getDefaultRule(valueBlock[path])
|
|
24
|
+
const valueRef =
|
|
25
|
+
value && String(value).indexOf('$ref:') === 0
|
|
26
|
+
? _get(
|
|
27
|
+
data,
|
|
28
|
+
rulesMatcherUtils.getPath(value.replace('$ref:', ''), prefix),
|
|
29
|
+
undefined
|
|
30
|
+
)
|
|
31
|
+
: value
|
|
32
|
+
|
|
33
|
+
if (!operators[op]) {
|
|
34
|
+
throw new Error(`Error missing operator:${op}`)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const valid = operators[op]?.(valueForKey, valueRef, opt, data)
|
|
38
|
+
return { valid, name: `${pathWithPrefix}___${name || op}` }
|
|
39
|
+
},
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
getKey: (block: RulesObject<any>, keys, options) => {
|
|
42
|
+
if (Object.prototype.hasOwnProperty.call(block, '$and')) {
|
|
43
|
+
return block.$and.map((item: unknown) => rulesMatcherUtils.getKey(item, keys, options))
|
|
44
|
+
}
|
|
45
|
+
if (Object.prototype.hasOwnProperty.call(block, '$or')) {
|
|
46
|
+
return block.$or.map((item: unknown) => rulesMatcherUtils.getKey(item, keys, options))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const { prefix } = options || {}
|
|
50
|
+
const path = Object.keys(block)[0]
|
|
51
|
+
|
|
52
|
+
const valueBlock = block
|
|
53
|
+
const res = rulesMatcherUtils.getPath(path, prefix)
|
|
54
|
+
const { value } = rulesMatcherUtils.getDefaultRule(valueBlock[path])
|
|
55
|
+
if (value && String(value).indexOf('$ref:') === 0) {
|
|
56
|
+
keys[rulesMatcherUtils.getPath(value.replace('$ref:', ''), prefix)] = true
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return (keys[res] = true)
|
|
60
|
+
},
|
|
61
|
+
isDate: (v) => v instanceof Date,
|
|
62
|
+
isDefined: (v) => v !== null && v !== undefined,
|
|
63
|
+
isObject: (v) => v === Object(v),
|
|
64
|
+
isFunction: (v) => typeof v === 'function',
|
|
65
|
+
isString: (v) => typeof v === 'string',
|
|
66
|
+
getDefaultStringValue: (value) => {
|
|
67
|
+
switch (value) {
|
|
68
|
+
case '$required':
|
|
69
|
+
return { op: '$exists', value: true }
|
|
70
|
+
case '$exists':
|
|
71
|
+
return { op: '$exists', value: true }
|
|
72
|
+
default:
|
|
73
|
+
return { op: '$eq', value }
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
getTypeOf: (value) =>
|
|
77
|
+
Array.isArray(value)
|
|
78
|
+
? 'array'
|
|
79
|
+
: rulesMatcherUtils.isNumber(value)
|
|
80
|
+
? 'number'
|
|
81
|
+
: value === null
|
|
82
|
+
? null
|
|
83
|
+
: typeof value,
|
|
84
|
+
getDefaultRule: (value) => {
|
|
85
|
+
const valueType = rulesMatcherUtils.getTypeOf(value)
|
|
86
|
+
switch (valueType) {
|
|
87
|
+
case 'number':
|
|
88
|
+
return { op: '$eq', value }
|
|
89
|
+
|
|
90
|
+
case 'string':
|
|
91
|
+
return rulesMatcherUtils.getDefaultStringValue(value)
|
|
92
|
+
|
|
93
|
+
case 'boolean':
|
|
94
|
+
return { op: '$exists', value }
|
|
95
|
+
|
|
96
|
+
case 'array':
|
|
97
|
+
return { op: '$in', value }
|
|
98
|
+
|
|
99
|
+
case 'object':
|
|
100
|
+
return {
|
|
101
|
+
...value,
|
|
102
|
+
op: value.op || Object.keys(value)[0],
|
|
103
|
+
value: value.value || value[Object.keys(value)[0]]
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
default:
|
|
107
|
+
return { op: '$eq', value }
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
isEmpty: (value) => {
|
|
111
|
+
// Null and undefined are empty
|
|
112
|
+
if (!rulesMatcherUtils.isDefined(value)) {
|
|
113
|
+
return true
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// functions are non empty
|
|
117
|
+
if (rulesMatcherUtils.isFunction(value)) {
|
|
118
|
+
return false
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/* if (isBool(value)) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
*/
|
|
125
|
+
// Whitespace only strings are empty
|
|
126
|
+
if (rulesMatcherUtils.isString(value)) {
|
|
127
|
+
return EMPTY_STRING_REGEXP.test(value)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// For arrays we use the length property
|
|
131
|
+
if (Array.isArray(value)) {
|
|
132
|
+
return value.length === 0
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Dates have no attributes but aren't empty
|
|
136
|
+
if (rulesMatcherUtils.isDate(value)) {
|
|
137
|
+
return false
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// If we find at least one property we consider it non empty
|
|
141
|
+
let attr
|
|
142
|
+
if (rulesMatcherUtils.isObject(value)) {
|
|
143
|
+
for (attr in value) {
|
|
144
|
+
return false
|
|
145
|
+
}
|
|
146
|
+
return true
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return false
|
|
150
|
+
},
|
|
151
|
+
forceArray: (a) => (Array.isArray(a) ? a : [a]),
|
|
152
|
+
getPath: (path, prefix) => {
|
|
153
|
+
if (path.indexOf('^') === 0) {
|
|
154
|
+
return _trimStart(path, '^')
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// da verificare se è ancora utilizzato
|
|
158
|
+
if (path.indexOf('$') === 0) {
|
|
159
|
+
return path
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return prefix ? `${prefix}.${path}` : path
|
|
163
|
+
},
|
|
164
|
+
// TODO BUG NUMERI CON LETTERE 1asdas o solo
|
|
165
|
+
|
|
166
|
+
forceNumber: (el) => {
|
|
167
|
+
if (Array.isArray(el)) {
|
|
168
|
+
return el.length
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (rulesMatcherUtils.isNumber(String(el))) {
|
|
172
|
+
return parseFloat(String(el))
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// fix perchè un valore false < 1 è true, quindi sbagliato, mentre un valore undefined < 1 è false
|
|
176
|
+
return 0
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
checkRule: (block, data, options) => {
|
|
180
|
+
if (
|
|
181
|
+
!Array.isArray(block) &&
|
|
182
|
+
block &&
|
|
183
|
+
Object.prototype.hasOwnProperty.call(block, '$and')
|
|
184
|
+
) {
|
|
185
|
+
if (block && block['$and'] && !block['$and'].length) return true
|
|
186
|
+
return block['$and'].every((item: RulesObject<Record<string, unknown>>) =>
|
|
187
|
+
rulesMatcherUtils.checkRule(item, data, options)
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (
|
|
192
|
+
!Array.isArray(block) &&
|
|
193
|
+
block &&
|
|
194
|
+
Object.prototype.hasOwnProperty.call(block, '$or')
|
|
195
|
+
) {
|
|
196
|
+
if (block && block['$or'] && !block['$or'].length) return true
|
|
197
|
+
return block['$or'].some((item: RulesObject<Record<string, unknown>>) =>
|
|
198
|
+
rulesMatcherUtils.checkRule(item, data, options)
|
|
199
|
+
)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const res = rulesMatcherUtils.rule(block, data, options)
|
|
203
|
+
return res.valid
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
getKeys: (rules, options) => {
|
|
207
|
+
if (!rules) return null
|
|
208
|
+
if (typeof rules == 'function') return []
|
|
209
|
+
|
|
210
|
+
if (!rulesMatcherUtils.forceArray(rules).every((r) => rulesMatcherUtils.isObject(r)))
|
|
211
|
+
return null
|
|
212
|
+
|
|
213
|
+
const keys = {}
|
|
214
|
+
const conditions = Array.isArray(rules) ? { $and: rules } : rules
|
|
215
|
+
rulesMatcherUtils.getKey(conditions, keys, options ?? {})
|
|
216
|
+
return Object.keys(keys)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Defines a set of comparison operators used for matching rules against user input.
|
|
222
|
+
*/
|
|
223
|
+
export const operators: Operators = {
|
|
224
|
+
$exists: (a, b) => !rulesMatcherUtils.isEmpty(a) === b,
|
|
225
|
+
|
|
226
|
+
$eq: (a, b) => a === b,
|
|
227
|
+
|
|
228
|
+
$ne: (a, b) => a !== b,
|
|
229
|
+
|
|
230
|
+
$gt: (a, b) => rulesMatcherUtils.forceNumber(a) > parseFloat(b),
|
|
231
|
+
|
|
232
|
+
$gte: (a, b) => rulesMatcherUtils.forceNumber(a) >= parseFloat(b),
|
|
233
|
+
|
|
234
|
+
$lt: (a, b) => rulesMatcherUtils.forceNumber(a) < parseFloat(b),
|
|
235
|
+
|
|
236
|
+
$lte: (a, b) => rulesMatcherUtils.forceNumber(a) <= parseFloat(b),
|
|
237
|
+
|
|
238
|
+
$strGt: (a, b) => String(a || '').length > parseFloat(b),
|
|
239
|
+
|
|
240
|
+
$strGte: (a, b) => String(a || '').length >= parseFloat(b),
|
|
241
|
+
|
|
242
|
+
$strLt: (a, b) => String(a || '').length < parseFloat(b),
|
|
243
|
+
|
|
244
|
+
$strLte: (a, b) => String(a || '').length <= parseFloat(b),
|
|
245
|
+
|
|
246
|
+
$in: (a, b) =>
|
|
247
|
+
rulesMatcherUtils
|
|
248
|
+
.forceArray(b)
|
|
249
|
+
.some(
|
|
250
|
+
(c) =>
|
|
251
|
+
_intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c))
|
|
252
|
+
.length
|
|
253
|
+
),
|
|
254
|
+
|
|
255
|
+
$nin: (a, b) =>
|
|
256
|
+
!rulesMatcherUtils
|
|
257
|
+
.forceArray(b)
|
|
258
|
+
.some(
|
|
259
|
+
(c) =>
|
|
260
|
+
_intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c))
|
|
261
|
+
.length
|
|
262
|
+
),
|
|
263
|
+
|
|
264
|
+
$all: (a, b) =>
|
|
265
|
+
rulesMatcherUtils
|
|
266
|
+
.forceArray(b)
|
|
267
|
+
.every(
|
|
268
|
+
(c) =>
|
|
269
|
+
_intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c))
|
|
270
|
+
.length
|
|
271
|
+
),
|
|
272
|
+
|
|
273
|
+
$regex: (a, b, opt) =>
|
|
274
|
+
rulesMatcherUtils
|
|
275
|
+
.forceArray(b)
|
|
276
|
+
.some((c) => (c instanceof RegExp ? c.test(a) : new RegExp(c, opt).test(a)))
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// export default operators
|
|
280
|
+
|
|
281
|
+
export default rulesMatcherUtils
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import get from 'lodash/get'
|
|
2
|
+
|
|
3
|
+
// Funzione che espande dinamicamente i placeholder con supporto per percorsi annidati
|
|
4
|
+
export function expandQuery(template: Record<string, unknown>, objs: Record<string, unknown>) {
|
|
5
|
+
let expandedQuery = JSON.stringify(template) // Converti l'oggetto in una stringa per sostituire i placeholder
|
|
6
|
+
const regex = /:\s*"%%([a-zA-Z0-9_.]+)"/g;
|
|
7
|
+
Object.keys(objs).forEach(() => {
|
|
8
|
+
// Espandi tutti i placeholder %%values.<nested.property>
|
|
9
|
+
|
|
10
|
+
const callback = (match: string, path: string) => {
|
|
11
|
+
const value = get(objs, `%%${path}`) // Recupera il valore annidato da values
|
|
12
|
+
const finalValue = typeof value === 'string' ? `"${value}"` : value && JSON.stringify(value)
|
|
13
|
+
return `:${value !== undefined ? finalValue : match}` // Sostituisci se esiste, altrimenti lascia il placeholder
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
expandedQuery = expandedQuery.replace(regex, callback as Parameters<typeof expandedQuery.replaceAll>[1])
|
|
17
|
+
})
|
|
18
|
+
return JSON.parse(expandedQuery) // Converti la stringa JSON di nuovo in un oggetto
|
|
19
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"outDir": "./dist",
|
|
4
|
+
"rootDir": "./src",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"target": "ES6",
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"declarationMap": true,
|
|
9
|
+
"noImplicitAny": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"moduleResolution": "node",
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"baseUrl": ".",
|
|
15
|
+
"paths": {
|
|
16
|
+
"*": ["../../node_modules/*"]
|
|
17
|
+
},
|
|
18
|
+
"lib": ["ES2021", "DOM"]
|
|
19
|
+
},
|
|
20
|
+
"include": ["src/**/*"],
|
|
21
|
+
"exclude": [
|
|
22
|
+
"node_modules",
|
|
23
|
+
"**/*.test.ts",
|
|
24
|
+
"**/*.spec.ts",
|
|
25
|
+
"jest.config.ts",
|
|
26
|
+
"dist"
|
|
27
|
+
]
|
|
28
|
+
}
|