@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,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.operators = void 0;
|
|
7
|
+
const get_1 = __importDefault(require("lodash/get"));
|
|
8
|
+
const intersection_1 = __importDefault(require("lodash/intersection"));
|
|
9
|
+
const trimStart_1 = __importDefault(require("lodash/trimStart"));
|
|
10
|
+
const EMPTY_STRING_REGEXP = /^\s*$/;
|
|
11
|
+
/**
|
|
12
|
+
* Defines a utility object named rulesMatcherUtils, which contains various helper functions used for processing rules and data in a rule-matching context.
|
|
13
|
+
*/
|
|
14
|
+
const rulesMatcherUtils = {
|
|
15
|
+
isNumber: (el) => {
|
|
16
|
+
const num = String(el);
|
|
17
|
+
return !!num.match(/(^-?|^\d+\.)\d+$/);
|
|
18
|
+
},
|
|
19
|
+
rule: (val, data, options) => {
|
|
20
|
+
var _a;
|
|
21
|
+
const { prefix } = options || {};
|
|
22
|
+
const path = Object.keys(val)[0];
|
|
23
|
+
const valueBlock = val;
|
|
24
|
+
const pathWithPrefix = rulesMatcherUtils.getPath(path, prefix);
|
|
25
|
+
const valueForKey = (0, get_1.default)(data, pathWithPrefix, undefined);
|
|
26
|
+
const { name } = (0, get_1.default)(valueBlock, [path], {}) || {};
|
|
27
|
+
const { op, value, opt } = rulesMatcherUtils.getDefaultRule(valueBlock[path]);
|
|
28
|
+
const valueRef = value && String(value).indexOf('$ref:') === 0
|
|
29
|
+
? (0, get_1.default)(data, rulesMatcherUtils.getPath(value.replace('$ref:', ''), prefix), undefined)
|
|
30
|
+
: value;
|
|
31
|
+
if (!exports.operators[op]) {
|
|
32
|
+
throw new Error(`Error missing operator:${op}`);
|
|
33
|
+
}
|
|
34
|
+
const valid = (_a = exports.operators[op]) === null || _a === void 0 ? void 0 : _a.call(exports.operators, valueForKey, valueRef, opt, data);
|
|
35
|
+
return { valid, name: `${pathWithPrefix}___${name || op}` };
|
|
36
|
+
},
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
|
+
getKey: (block, keys, options) => {
|
|
39
|
+
if (Object.prototype.hasOwnProperty.call(block, '$and')) {
|
|
40
|
+
return block.$and.map((item) => rulesMatcherUtils.getKey(item, keys, options));
|
|
41
|
+
}
|
|
42
|
+
if (Object.prototype.hasOwnProperty.call(block, '$or')) {
|
|
43
|
+
return block.$or.map((item) => rulesMatcherUtils.getKey(item, keys, options));
|
|
44
|
+
}
|
|
45
|
+
const { prefix } = options || {};
|
|
46
|
+
const path = Object.keys(block)[0];
|
|
47
|
+
const valueBlock = block;
|
|
48
|
+
const res = rulesMatcherUtils.getPath(path, prefix);
|
|
49
|
+
const { value } = rulesMatcherUtils.getDefaultRule(valueBlock[path]);
|
|
50
|
+
if (value && String(value).indexOf('$ref:') === 0) {
|
|
51
|
+
keys[rulesMatcherUtils.getPath(value.replace('$ref:', ''), prefix)] = true;
|
|
52
|
+
}
|
|
53
|
+
return (keys[res] = true);
|
|
54
|
+
},
|
|
55
|
+
isDate: (v) => v instanceof Date,
|
|
56
|
+
isDefined: (v) => v !== null && v !== undefined,
|
|
57
|
+
isObject: (v) => v === Object(v),
|
|
58
|
+
isFunction: (v) => typeof v === 'function',
|
|
59
|
+
isString: (v) => typeof v === 'string',
|
|
60
|
+
getDefaultStringValue: (value) => {
|
|
61
|
+
switch (value) {
|
|
62
|
+
case '$required':
|
|
63
|
+
return { op: '$exists', value: true };
|
|
64
|
+
case '$exists':
|
|
65
|
+
return { op: '$exists', value: true };
|
|
66
|
+
default:
|
|
67
|
+
return { op: '$eq', value };
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
getTypeOf: (value) => Array.isArray(value)
|
|
71
|
+
? 'array'
|
|
72
|
+
: rulesMatcherUtils.isNumber(value)
|
|
73
|
+
? 'number'
|
|
74
|
+
: value === null
|
|
75
|
+
? null
|
|
76
|
+
: typeof value,
|
|
77
|
+
getDefaultRule: (value) => {
|
|
78
|
+
const valueType = rulesMatcherUtils.getTypeOf(value);
|
|
79
|
+
switch (valueType) {
|
|
80
|
+
case 'number':
|
|
81
|
+
return { op: '$eq', value };
|
|
82
|
+
case 'string':
|
|
83
|
+
return rulesMatcherUtils.getDefaultStringValue(value);
|
|
84
|
+
case 'boolean':
|
|
85
|
+
return { op: '$exists', value };
|
|
86
|
+
case 'array':
|
|
87
|
+
return { op: '$in', value };
|
|
88
|
+
case 'object':
|
|
89
|
+
return Object.assign(Object.assign({}, value), { op: value.op || Object.keys(value)[0], value: value.value || value[Object.keys(value)[0]] });
|
|
90
|
+
default:
|
|
91
|
+
return { op: '$eq', value };
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
isEmpty: (value) => {
|
|
95
|
+
// Null and undefined are empty
|
|
96
|
+
if (!rulesMatcherUtils.isDefined(value)) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
// functions are non empty
|
|
100
|
+
if (rulesMatcherUtils.isFunction(value)) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
/* if (isBool(value)) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
*/
|
|
107
|
+
// Whitespace only strings are empty
|
|
108
|
+
if (rulesMatcherUtils.isString(value)) {
|
|
109
|
+
return EMPTY_STRING_REGEXP.test(value);
|
|
110
|
+
}
|
|
111
|
+
// For arrays we use the length property
|
|
112
|
+
if (Array.isArray(value)) {
|
|
113
|
+
return value.length === 0;
|
|
114
|
+
}
|
|
115
|
+
// Dates have no attributes but aren't empty
|
|
116
|
+
if (rulesMatcherUtils.isDate(value)) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
// If we find at least one property we consider it non empty
|
|
120
|
+
let attr;
|
|
121
|
+
if (rulesMatcherUtils.isObject(value)) {
|
|
122
|
+
for (attr in value) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
return false;
|
|
128
|
+
},
|
|
129
|
+
forceArray: (a) => (Array.isArray(a) ? a : [a]),
|
|
130
|
+
getPath: (path, prefix) => {
|
|
131
|
+
if (path.indexOf('^') === 0) {
|
|
132
|
+
return (0, trimStart_1.default)(path, '^');
|
|
133
|
+
}
|
|
134
|
+
// da verificare se è ancora utilizzato
|
|
135
|
+
if (path.indexOf('$') === 0) {
|
|
136
|
+
return path;
|
|
137
|
+
}
|
|
138
|
+
return prefix ? `${prefix}.${path}` : path;
|
|
139
|
+
},
|
|
140
|
+
// TODO BUG NUMERI CON LETTERE 1asdas o solo
|
|
141
|
+
forceNumber: (el) => {
|
|
142
|
+
if (Array.isArray(el)) {
|
|
143
|
+
return el.length;
|
|
144
|
+
}
|
|
145
|
+
if (rulesMatcherUtils.isNumber(String(el))) {
|
|
146
|
+
return parseFloat(String(el));
|
|
147
|
+
}
|
|
148
|
+
// fix perchè un valore false < 1 è true, quindi sbagliato, mentre un valore undefined < 1 è false
|
|
149
|
+
return 0;
|
|
150
|
+
},
|
|
151
|
+
checkRule: (block, data, options) => {
|
|
152
|
+
if (!Array.isArray(block) &&
|
|
153
|
+
block &&
|
|
154
|
+
Object.prototype.hasOwnProperty.call(block, '$and')) {
|
|
155
|
+
if (block && block['$and'] && !block['$and'].length)
|
|
156
|
+
return true;
|
|
157
|
+
return block['$and'].every((item) => rulesMatcherUtils.checkRule(item, data, options));
|
|
158
|
+
}
|
|
159
|
+
if (!Array.isArray(block) &&
|
|
160
|
+
block &&
|
|
161
|
+
Object.prototype.hasOwnProperty.call(block, '$or')) {
|
|
162
|
+
if (block && block['$or'] && !block['$or'].length)
|
|
163
|
+
return true;
|
|
164
|
+
return block['$or'].some((item) => rulesMatcherUtils.checkRule(item, data, options));
|
|
165
|
+
}
|
|
166
|
+
const res = rulesMatcherUtils.rule(block, data, options);
|
|
167
|
+
return res.valid;
|
|
168
|
+
},
|
|
169
|
+
getKeys: (rules, options) => {
|
|
170
|
+
if (!rules)
|
|
171
|
+
return null;
|
|
172
|
+
if (typeof rules == 'function')
|
|
173
|
+
return [];
|
|
174
|
+
if (!rulesMatcherUtils.forceArray(rules).every((r) => rulesMatcherUtils.isObject(r)))
|
|
175
|
+
return null;
|
|
176
|
+
const keys = {};
|
|
177
|
+
const conditions = Array.isArray(rules) ? { $and: rules } : rules;
|
|
178
|
+
rulesMatcherUtils.getKey(conditions, keys, options !== null && options !== void 0 ? options : {});
|
|
179
|
+
return Object.keys(keys);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* Defines a set of comparison operators used for matching rules against user input.
|
|
184
|
+
*/
|
|
185
|
+
exports.operators = {
|
|
186
|
+
$exists: (a, b) => !rulesMatcherUtils.isEmpty(a) === b,
|
|
187
|
+
$eq: (a, b) => a === b,
|
|
188
|
+
$ne: (a, b) => a !== b,
|
|
189
|
+
$gt: (a, b) => rulesMatcherUtils.forceNumber(a) > parseFloat(b),
|
|
190
|
+
$gte: (a, b) => rulesMatcherUtils.forceNumber(a) >= parseFloat(b),
|
|
191
|
+
$lt: (a, b) => rulesMatcherUtils.forceNumber(a) < parseFloat(b),
|
|
192
|
+
$lte: (a, b) => rulesMatcherUtils.forceNumber(a) <= parseFloat(b),
|
|
193
|
+
$strGt: (a, b) => String(a || '').length > parseFloat(b),
|
|
194
|
+
$strGte: (a, b) => String(a || '').length >= parseFloat(b),
|
|
195
|
+
$strLt: (a, b) => String(a || '').length < parseFloat(b),
|
|
196
|
+
$strLte: (a, b) => String(a || '').length <= parseFloat(b),
|
|
197
|
+
$in: (a, b) => rulesMatcherUtils
|
|
198
|
+
.forceArray(b)
|
|
199
|
+
.some((c) => (0, intersection_1.default)(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c))
|
|
200
|
+
.length),
|
|
201
|
+
$nin: (a, b) => !rulesMatcherUtils
|
|
202
|
+
.forceArray(b)
|
|
203
|
+
.some((c) => (0, intersection_1.default)(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c))
|
|
204
|
+
.length),
|
|
205
|
+
$all: (a, b) => rulesMatcherUtils
|
|
206
|
+
.forceArray(b)
|
|
207
|
+
.every((c) => (0, intersection_1.default)(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c))
|
|
208
|
+
.length),
|
|
209
|
+
$regex: (a, b, opt) => rulesMatcherUtils
|
|
210
|
+
.forceArray(b)
|
|
211
|
+
.some((c) => (c instanceof RegExp ? c.test(a) : new RegExp(c, opt).test(a)))
|
|
212
|
+
};
|
|
213
|
+
// export default operators
|
|
214
|
+
exports.default = rulesMatcherUtils;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../../src/utils/rules.ts"],"names":[],"mappings":"AAGA,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,OAe3F"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.expandQuery = expandQuery;
|
|
7
|
+
const get_1 = __importDefault(require("lodash/get"));
|
|
8
|
+
// Funzione che espande dinamicamente i placeholder con supporto per percorsi annidati
|
|
9
|
+
function expandQuery(template, objs) {
|
|
10
|
+
let expandedQuery = JSON.stringify(template); // Converti l'oggetto in una stringa per sostituire i placeholder
|
|
11
|
+
const regex = /:\s*"%%([a-zA-Z0-9_.]+)"/g;
|
|
12
|
+
Object.keys(objs).forEach(() => {
|
|
13
|
+
// Espandi tutti i placeholder %%values.<nested.property>
|
|
14
|
+
const callback = (match, path) => {
|
|
15
|
+
const value = (0, get_1.default)(objs, `%%${path}`); // Recupera il valore annidato da values
|
|
16
|
+
const finalValue = typeof value === 'string' ? `"${value}"` : value && JSON.stringify(value);
|
|
17
|
+
return `:${value !== undefined ? finalValue : match}`; // Sostituisci se esiste, altrimenti lascia il placeholder
|
|
18
|
+
};
|
|
19
|
+
expandedQuery = expandedQuery.replace(regex, callback);
|
|
20
|
+
});
|
|
21
|
+
return JSON.parse(expandedQuery); // Converti la stringa JSON di nuovo in un oggetto
|
|
22
|
+
}
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
preset: '../../jest.preset.js',
|
|
3
|
+
transform: {
|
|
4
|
+
'^.+\\.[tj]s$': [
|
|
5
|
+
'ts-jest',
|
|
6
|
+
{
|
|
7
|
+
tsconfig: './tsconfig.json'
|
|
8
|
+
}
|
|
9
|
+
]
|
|
10
|
+
},
|
|
11
|
+
collectCoverage: false,
|
|
12
|
+
collectCoverageFrom: ['./**/*.ts'],
|
|
13
|
+
coverageDirectory: 'coverage',
|
|
14
|
+
coverageThreshold: {
|
|
15
|
+
global: {
|
|
16
|
+
branches: 50,
|
|
17
|
+
functions: 90,
|
|
18
|
+
lines: 90,
|
|
19
|
+
statements: 90
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
testEnvironment: 'node',
|
|
23
|
+
testMatch: ['./**/*.test.ts']
|
|
24
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flowerforce/flowerbase",
|
|
3
|
+
"version": "1.0.1-beta.3",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "npx jest",
|
|
12
|
+
"build": "rm -rf dist/ && tsc",
|
|
13
|
+
"start": "node src/index.ts"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"author": "",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@fastify/cors": "^10.0.1",
|
|
20
|
+
"@fastify/jwt": "^9.0.1",
|
|
21
|
+
"@fastify/mongodb": "^9.0.1",
|
|
22
|
+
"aws-sdk": "^2.1692.0",
|
|
23
|
+
"bson": "^6.8.0",
|
|
24
|
+
"dotenv": "^16.4.7",
|
|
25
|
+
"fastify": "^5.0.0",
|
|
26
|
+
"fastify-mongoose-driver": "^3.2.0",
|
|
27
|
+
"fastify-plugin": "^5.0.1",
|
|
28
|
+
"lodash": "^4.17.21",
|
|
29
|
+
"node-cron": "^3.0.3",
|
|
30
|
+
"@sendgrid/mail": "^8.1.4",
|
|
31
|
+
"nodemon": "^3.1.7",
|
|
32
|
+
"undici": "^7.1.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@babel/cli": "^7.26.4",
|
|
36
|
+
"@babel/core": "^7.26.0",
|
|
37
|
+
"@babel/preset-env": "^7.26.0",
|
|
38
|
+
"@eslint/compat": "^1.2.3",
|
|
39
|
+
"@eslint/eslintrc": "^3.1.0",
|
|
40
|
+
"@eslint/js": "^9.12.0",
|
|
41
|
+
"@types/cors": "^2.8.17",
|
|
42
|
+
"@types/jest": "^29.5.14",
|
|
43
|
+
"@types/lodash": "^4.17.9",
|
|
44
|
+
"@types/node": "22.6.1",
|
|
45
|
+
"@types/node-cron": "^3.0.11",
|
|
46
|
+
"@typescript-eslint/eslint-plugin": "^8.14.0",
|
|
47
|
+
"@typescript-eslint/parser": "^8.14.0",
|
|
48
|
+
"eslint": "^9.12.0",
|
|
49
|
+
"eslint-config-prettier": "^9.1.0",
|
|
50
|
+
"eslint-plugin-eslint-comments": "^3.2.0",
|
|
51
|
+
"eslint-plugin-eslint-plugin": "^6.3.1",
|
|
52
|
+
"eslint-plugin-import": "^2.31.0",
|
|
53
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
54
|
+
"globals": "^15.11.0",
|
|
55
|
+
"jest": "^29.7.0",
|
|
56
|
+
"prettier": "^3.3.3",
|
|
57
|
+
"ts-jest": "^29.2.5",
|
|
58
|
+
"ts-node": "^10.9.2",
|
|
59
|
+
"typescript": "^5.6.2",
|
|
60
|
+
"typescript-eslint": "^8.8.1"
|
|
61
|
+
},
|
|
62
|
+
"peerDependencies": {}
|
|
63
|
+
}
|
package/project.json
ADDED
package/rollup.config.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const { withNx } = require('@nx/rollup/with-nx')
|
|
2
|
+
|
|
3
|
+
module.exports = withNx(
|
|
4
|
+
{
|
|
5
|
+
main: './src/index.ts',
|
|
6
|
+
outputPath: './dist',
|
|
7
|
+
tsConfig: './tsconfig.lib.json',
|
|
8
|
+
compiler: 'tsc',
|
|
9
|
+
format: ['cjs', 'esm'],
|
|
10
|
+
generateExportsField: true
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
// Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options
|
|
14
|
+
// e.g.
|
|
15
|
+
// output: { sourcemap: true },
|
|
16
|
+
}
|
|
17
|
+
)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { ObjectId } from 'bson'
|
|
2
|
+
import { FastifyInstance } from 'fastify'
|
|
3
|
+
import { AUTH_CONFIG, DB_NAME } from '../constants'
|
|
4
|
+
import { SessionCreatedDto } from './dtos'
|
|
5
|
+
import { AUTH_ENDPOINTS, AUTH_ERRORS } from './utils'
|
|
6
|
+
|
|
7
|
+
const HANDLER_TYPE = 'preHandler'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Controller for handling user authentication, profile retrieval, and session management.
|
|
11
|
+
* @testable
|
|
12
|
+
* @param {FastifyInstance} app - The Fastify instance.
|
|
13
|
+
*/
|
|
14
|
+
export async function authController(app: FastifyInstance) {
|
|
15
|
+
const { authCollection, userCollection } = AUTH_CONFIG
|
|
16
|
+
|
|
17
|
+
const db = app.mongo.client.db(DB_NAME)
|
|
18
|
+
|
|
19
|
+
app.addHook(HANDLER_TYPE, app.jwtAuthentication)
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Endpoint to retrieve the authenticated user's profile.
|
|
23
|
+
*
|
|
24
|
+
* @route {GET} /profile
|
|
25
|
+
* @param {import('fastify').FastifyRequest} req - The request object.
|
|
26
|
+
* @returns {Promise<Object>} A promise resolving with the user's profile data.
|
|
27
|
+
*/
|
|
28
|
+
app.get(AUTH_ENDPOINTS.PROFILE, async function (req) {
|
|
29
|
+
const user = await db
|
|
30
|
+
.collection<Record<string, unknown>>(authCollection)
|
|
31
|
+
.findOne({ _id: ObjectId.createFromHexString(req.user.id) })
|
|
32
|
+
return {
|
|
33
|
+
_id: user?._id.toString(),
|
|
34
|
+
identities: user?.identities,
|
|
35
|
+
type: 'normal',
|
|
36
|
+
custom_data: user?.curstom_data,
|
|
37
|
+
data: {
|
|
38
|
+
_id: user?._id.toString(),
|
|
39
|
+
email: user?.email
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Endpoint to create a new session and generate a new access token.
|
|
46
|
+
*
|
|
47
|
+
* @route {POST} /session
|
|
48
|
+
* @param {import('fastify').FastifyRequest} req - The request object containing the refresh token.
|
|
49
|
+
* @param {import('fastify').FastifyReply} res - The response object.
|
|
50
|
+
* @returns {Promise<SessionCreatedDto>} A promise resolving with the newly created session data.
|
|
51
|
+
*/
|
|
52
|
+
app.post<{ Reply: SessionCreatedDto }>(
|
|
53
|
+
AUTH_ENDPOINTS.SESSION,
|
|
54
|
+
async function (req, res) {
|
|
55
|
+
if (req.user.typ !== 'refresh') {
|
|
56
|
+
throw new Error(AUTH_ERRORS.INVALID_TOKEN)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const auth_user = await db
|
|
60
|
+
?.collection(authCollection)
|
|
61
|
+
.findOne({ _id: new this.mongo.ObjectId(req.user.sub) })
|
|
62
|
+
|
|
63
|
+
if (!auth_user) {
|
|
64
|
+
throw new Error(`User with ID ${req.user.sub} not found`)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const user = await db!.collection(userCollection).findOne({ [AUTH_CONFIG.user_id_field]: req.user.sub })
|
|
68
|
+
|
|
69
|
+
res.status(201)
|
|
70
|
+
return {
|
|
71
|
+
access_token: this.createAccessToken({
|
|
72
|
+
...auth_user,
|
|
73
|
+
user_data: user
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
)
|
|
78
|
+
}
|
package/src/auth/dtos.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import fastifyJwt from '@fastify/jwt'
|
|
2
|
+
import fp from 'fastify-plugin'
|
|
3
|
+
import { Document, ObjectId, WithId } from 'mongodb'
|
|
4
|
+
|
|
5
|
+
type Options = {
|
|
6
|
+
secret: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* This module is a Fastify plugin that sets up JWT-based authentication and token creation.
|
|
11
|
+
* It registers JWT authentication, and provides methods to create access and refresh tokens.
|
|
12
|
+
* @testable
|
|
13
|
+
* @param {import('fastify').FastifyInstance} fastify - The Fastify instance.
|
|
14
|
+
* @param {Object} opts - Options for the plugin.
|
|
15
|
+
* @param {string} opts.secret - The secret key used for signing JWTs.
|
|
16
|
+
*/
|
|
17
|
+
export default fp(async function (fastify, opts: Options) {
|
|
18
|
+
const BAAS_ID = new ObjectId().toString()
|
|
19
|
+
|
|
20
|
+
fastify.register(fastifyJwt, {
|
|
21
|
+
secret: opts.secret
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
fastify.decorate('jwtAuthentication', async function (request, reply) {
|
|
25
|
+
try {
|
|
26
|
+
await request.jwtVerify()
|
|
27
|
+
} catch (err) {
|
|
28
|
+
// TODO: handle error
|
|
29
|
+
reply.send(err)
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
fastify.decorate('createAccessToken', function (user: WithId<Document>) {
|
|
34
|
+
const id = user._id.toString()
|
|
35
|
+
const user_data = {
|
|
36
|
+
_id: id,
|
|
37
|
+
id,
|
|
38
|
+
...user.user_data
|
|
39
|
+
}
|
|
40
|
+
return this.jwt.sign(
|
|
41
|
+
{
|
|
42
|
+
typ: 'access',
|
|
43
|
+
id,
|
|
44
|
+
user_data: user_data,
|
|
45
|
+
custom_data: user_data
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
iss: BAAS_ID,
|
|
49
|
+
jti: BAAS_ID,
|
|
50
|
+
sub: user._id.toJSON(),
|
|
51
|
+
expiresIn: '300m'
|
|
52
|
+
}
|
|
53
|
+
)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
fastify.decorate('createRefreshToken', function (user: WithId<Document>) {
|
|
57
|
+
return this.jwt.sign(
|
|
58
|
+
{
|
|
59
|
+
typ: 'refresh',
|
|
60
|
+
baas_id: BAAS_ID
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
sub: user._id.toJSON(),
|
|
64
|
+
expiresIn: '60d'
|
|
65
|
+
}
|
|
66
|
+
)
|
|
67
|
+
})
|
|
68
|
+
})
|