@flowerforce/flowerbase 1.2.0 → 1.2.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.
Files changed (92) hide show
  1. package/dist/auth/controller.d.ts.map +1 -1
  2. package/dist/auth/controller.js +3 -0
  3. package/dist/auth/providers/custom-function/controller.d.ts.map +1 -1
  4. package/dist/auth/providers/custom-function/controller.js +5 -2
  5. package/dist/auth/providers/local-userpass/controller.d.ts.map +1 -1
  6. package/dist/auth/providers/local-userpass/controller.js +7 -10
  7. package/dist/auth/utils.d.ts +1 -1
  8. package/dist/auth/utils.d.ts.map +1 -1
  9. package/dist/auth/utils.js +4 -3
  10. package/dist/constants.d.ts +5 -0
  11. package/dist/constants.d.ts.map +1 -1
  12. package/dist/constants.js +5 -1
  13. package/dist/features/functions/controller.d.ts.map +1 -1
  14. package/dist/features/functions/controller.js +28 -2
  15. package/dist/features/rules/utils.d.ts.map +1 -1
  16. package/dist/features/rules/utils.js +11 -2
  17. package/dist/features/triggers/utils.d.ts.map +1 -1
  18. package/dist/features/triggers/utils.js +52 -2
  19. package/dist/index.d.ts +8 -1
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +10 -9
  22. package/dist/services/mongodb-atlas/index.d.ts.map +1 -1
  23. package/dist/services/mongodb-atlas/index.js +540 -483
  24. package/dist/services/mongodb-atlas/utils.d.ts +9 -2
  25. package/dist/services/mongodb-atlas/utils.d.ts.map +1 -1
  26. package/dist/services/mongodb-atlas/utils.js +113 -23
  27. package/dist/shared/handleUserRegistration.d.ts.map +1 -1
  28. package/dist/shared/handleUserRegistration.js +1 -0
  29. package/dist/shared/models/handleUserRegistration.model.d.ts +6 -2
  30. package/dist/shared/models/handleUserRegistration.model.d.ts.map +1 -1
  31. package/dist/utils/context/helpers.d.ts +6 -5
  32. package/dist/utils/context/helpers.d.ts.map +1 -1
  33. package/dist/utils/context/helpers.js +3 -0
  34. package/dist/utils/context/index.d.ts.map +1 -1
  35. package/dist/utils/context/index.js +2 -0
  36. package/dist/utils/initializer/exposeRoutes.d.ts.map +1 -1
  37. package/dist/utils/initializer/exposeRoutes.js +11 -4
  38. package/dist/utils/initializer/registerPlugins.d.ts +3 -1
  39. package/dist/utils/initializer/registerPlugins.d.ts.map +1 -1
  40. package/dist/utils/initializer/registerPlugins.js +9 -6
  41. package/dist/utils/roles/helpers.js +9 -2
  42. package/dist/utils/roles/machines/commonValidators.d.ts.map +1 -1
  43. package/dist/utils/roles/machines/commonValidators.js +10 -6
  44. package/dist/utils/roles/machines/read/B/validators.d.ts +4 -0
  45. package/dist/utils/roles/machines/read/B/validators.d.ts.map +1 -0
  46. package/dist/utils/roles/machines/read/B/validators.js +8 -0
  47. package/dist/utils/roles/machines/read/C/index.d.ts.map +1 -1
  48. package/dist/utils/roles/machines/read/C/index.js +10 -7
  49. package/dist/utils/roles/machines/read/C/validators.d.ts +5 -0
  50. package/dist/utils/roles/machines/read/C/validators.d.ts.map +1 -0
  51. package/dist/utils/roles/machines/read/C/validators.js +29 -0
  52. package/dist/utils/roles/machines/read/D/index.d.ts.map +1 -1
  53. package/dist/utils/roles/machines/read/D/index.js +13 -11
  54. package/dist/utils/rules.d.ts +1 -1
  55. package/dist/utils/rules.d.ts.map +1 -1
  56. package/dist/utils/rules.js +26 -17
  57. package/jest.config.ts +2 -12
  58. package/jest.setup.ts +28 -0
  59. package/package.json +1 -1
  60. package/src/auth/controller.ts +3 -0
  61. package/src/auth/providers/custom-function/controller.ts +5 -2
  62. package/src/auth/providers/local-userpass/controller.ts +13 -10
  63. package/src/auth/utils.ts +7 -4
  64. package/src/constants.ts +7 -2
  65. package/src/fastify.d.ts +32 -15
  66. package/src/features/functions/controller.ts +36 -2
  67. package/src/features/rules/utils.ts +11 -2
  68. package/src/features/triggers/utils.ts +59 -2
  69. package/src/index.ts +21 -8
  70. package/src/services/mongodb-atlas/__tests__/utils.test.ts +141 -0
  71. package/src/services/mongodb-atlas/index.ts +143 -90
  72. package/src/services/mongodb-atlas/utils.ts +158 -22
  73. package/src/shared/handleUserRegistration.ts +3 -3
  74. package/src/shared/models/handleUserRegistration.model.ts +8 -3
  75. package/src/types/fastify-raw-body.d.ts +22 -0
  76. package/src/utils/__tests__/STEP_B_STATES.test.ts +1 -1
  77. package/src/utils/__tests__/STEP_C_STATES.test.ts +1 -1
  78. package/src/utils/__tests__/STEP_D_STATES.test.ts +2 -2
  79. package/src/utils/__tests__/checkIsValidFieldNameFn.test.ts +9 -4
  80. package/src/utils/__tests__/registerPlugins.test.ts +16 -1
  81. package/src/utils/context/helpers.ts +3 -0
  82. package/src/utils/context/index.ts +1 -0
  83. package/src/utils/initializer/exposeRoutes.ts +15 -8
  84. package/src/utils/initializer/registerPlugins.ts +15 -7
  85. package/src/utils/roles/helpers.ts +20 -3
  86. package/src/utils/roles/machines/commonValidators.ts +10 -5
  87. package/src/utils/roles/machines/read/B/validators.ts +8 -0
  88. package/src/utils/roles/machines/read/C/index.ts +11 -7
  89. package/src/utils/roles/machines/read/C/validators.ts +21 -0
  90. package/src/utils/roles/machines/read/D/index.ts +22 -12
  91. package/src/utils/rules.ts +31 -22
  92. package/tsconfig.spec.json +7 -0
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  checkFieldsPropertyExists,
3
- evaluateTopLevelPermissionsFn
4
- } from '../../commonValidators'
3
+ evaluateTopLevelReadFn,
4
+ evaluateTopLevelWriteFn
5
+ } from './validators'
5
6
  import { States } from '../../interface'
6
7
  import { logMachineInfo } from '../../utils'
7
8
 
@@ -13,10 +14,13 @@ export const STEP_C_STATES: States = {
13
14
  step: 1,
14
15
  stepName: 'evaluateTopLevelRead'
15
16
  })
16
- const check = await evaluateTopLevelPermissionsFn(context, 'read')
17
- return check
18
- ? endValidation({ success: true })
19
- : next('evaluateTopLevelWrite', { check })
17
+ const check = await evaluateTopLevelReadFn(context)
18
+ if (check) {
19
+ return checkFieldsPropertyExists(context)
20
+ ? next('checkFieldsProperty')
21
+ : endValidation({ success: true })
22
+ }
23
+ return next('evaluateTopLevelWrite', { check })
20
24
  },
21
25
  evaluateTopLevelWrite: async ({ context, next, endValidation }) => {
22
26
  logMachineInfo({
@@ -25,7 +29,7 @@ export const STEP_C_STATES: States = {
25
29
  step: 2,
26
30
  stepName: 'evaluateTopLevelWrite'
27
31
  })
28
- const check = await evaluateTopLevelPermissionsFn(context, 'write')
32
+ const check = await evaluateTopLevelWriteFn(context)
29
33
  if (check) return endValidation({ success: true })
30
34
  return context?.prevParams?.check === false
31
35
  ? endValidation({ success: false })
@@ -0,0 +1,21 @@
1
+ import { MachineContext } from '../../interface'
2
+ import {
3
+ checkFieldsPropertyExists,
4
+ evaluateTopLevelPermissionsFn
5
+ } from '../../commonValidators'
6
+
7
+ export const evaluateTopLevelReadFn = async (context: MachineContext) => {
8
+ if (context.params.type !== 'read') {
9
+ return false
10
+ }
11
+ return evaluateTopLevelPermissionsFn(context, 'read')
12
+ }
13
+
14
+ export const evaluateTopLevelWriteFn = async (context: MachineContext) => {
15
+ if (!['read', 'write'].includes(context.params.type)) {
16
+ return undefined
17
+ }
18
+ return evaluateTopLevelPermissionsFn(context, 'write')
19
+ }
20
+
21
+ export { checkFieldsPropertyExists } from '../../commonValidators'
@@ -1,7 +1,25 @@
1
- import { States } from '../../interface'
1
+ import { Document } from 'mongodb'
2
+ import { MachineContext, States } from '../../interface'
2
3
  import { logMachineInfo } from '../../utils'
3
4
  import { checkAdditionalFieldsFn, checkIsValidFieldNameFn } from './validators'
4
5
 
6
+ const runCheckIsValidFieldName = async ({
7
+ context,
8
+ endValidation
9
+ }: {
10
+ context: MachineContext
11
+ endValidation: ({ success, document }: { success: boolean; document?: Document }) => void
12
+ }) => {
13
+ logMachineInfo({
14
+ enabled: context.enableLog,
15
+ machine: 'D',
16
+ step: 2,
17
+ stepName: 'checkIsValidFieldName'
18
+ })
19
+ const document = checkIsValidFieldNameFn(context)
20
+ return endValidation({ success: !!Object.keys(document).length, document })
21
+ }
22
+
5
23
  export const STEP_D_STATES: States = {
6
24
  checkAdditionalFields: async ({ context, next, endValidation }) => {
7
25
  logMachineInfo({
@@ -11,16 +29,8 @@ export const STEP_D_STATES: States = {
11
29
  stepName: 'checkAdditionalFields'
12
30
  })
13
31
  const check = checkAdditionalFieldsFn(context)
14
- return check ? next('checkIsValidFieldName') : endValidation({ success: false })
32
+ return check ? next('evaluateRead') : endValidation({ success: false })
15
33
  },
16
- checkIsValidFieldName: async ({ context, endValidation }) => {
17
- logMachineInfo({
18
- enabled: context.enableLog,
19
- machine: 'D',
20
- step: 2,
21
- stepName: 'checkIsValidFieldName'
22
- })
23
- const document = checkIsValidFieldNameFn(context)
24
- return endValidation({ success: !!Object.keys(document).length, document })
25
- }
34
+ evaluateRead: runCheckIsValidFieldName,
35
+ checkIsValidFieldName: runCheckIsValidFieldName
26
36
  }
@@ -1,31 +1,40 @@
1
1
  import get from 'lodash/get'
2
2
 
3
- const removeExtraColons = (val: unknown) => {
4
- return val?.toString().replace(/:+/g, ":")
3
+ const resolvePlaceholder = (value: string, objs: Record<string, unknown>) => {
4
+ if (!value.startsWith('%%')) return value
5
+
6
+ const path = value.slice(2)
7
+ const [rootKey, ...rest] = path.split('.')
8
+ const rootToken = `%%${rootKey}`
9
+ const rootValue = objs[rootToken]
10
+
11
+ if (!rest.length) {
12
+ return rootValue === undefined ? value : rootValue
13
+ }
14
+
15
+ const resolved = get(rootValue as object, rest.join('.'))
16
+ return resolved === undefined ? value : resolved
5
17
  }
6
18
 
7
- // Funzione che espande dinamicamente i placeholder con supporto per percorsi annidati
19
+ const expandValue = (input: unknown, objs: Record<string, unknown>): unknown => {
20
+ if (Array.isArray(input)) {
21
+ return input.map((item) => expandValue(item, objs))
22
+ }
23
+ if (input && typeof input === 'object') {
24
+ return Object.fromEntries(
25
+ Object.entries(input).map(([key, val]) => [key, expandValue(val, objs)])
26
+ )
27
+ }
28
+ if (typeof input === 'string') {
29
+ return resolvePlaceholder(input, objs)
30
+ }
31
+ return input
32
+ }
33
+
34
+ // Espande dinamicamente i placeholder con supporto per array e percorsi annidati
8
35
  export function expandQuery(
9
36
  template: Record<string, unknown>,
10
37
  objs: Record<string, unknown>
11
38
  ) {
12
- let expandedQuery = JSON.stringify(template) // Converti l'oggetto in una stringa per sostituire i placeholder
13
- const regex = /:\s*"%%([a-zA-Z0-9_.]+)"/g
14
- Object.keys(objs).forEach(() => {
15
- // Espandi tutti i placeholder %%values.<nested.property>
16
-
17
- const callback = (match: string, path: string) => {
18
- const value = get(objs, `%%${path}`) // Recupera il valore annidato da values
19
- const finalValue = typeof value === 'string' ? `"${value}"` : value && JSON.stringify(value)
20
- // TODO tolto i primi : creava questo tipo di oggetto {"userId"::"%%user.id"}
21
- const val = `:${value !== undefined ? finalValue : match}`; // Sostituisci se esiste, altrimenti lascia il placeholder
22
- return removeExtraColons(val)
23
- }
24
-
25
- expandedQuery = expandedQuery.replace(
26
- regex,
27
- callback as Parameters<typeof expandedQuery.replaceAll>[1]
28
- )
29
- })
30
- return JSON.parse(expandedQuery) // Converti la stringa JSON di nuovo in un oggetto
39
+ return expandValue(template, objs) as Record<string, unknown>
31
40
  }
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "types": ["node", "jest"]
5
+ },
6
+ "include": ["src/**/*.test.ts", "src/**/*.spec.ts"]
7
+ }