wabe 0.6.9 → 0.6.11

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 (162) hide show
  1. package/README.md +156 -50
  2. package/bucket/b.txt +1 -0
  3. package/dev/index.ts +215 -0
  4. package/dist/authentication/Session.d.ts +4 -1
  5. package/dist/authentication/interface.d.ts +16 -0
  6. package/dist/cron/index.d.ts +0 -1
  7. package/dist/database/DatabaseController.d.ts +41 -13
  8. package/dist/database/interface.d.ts +1 -0
  9. package/dist/email/DevAdapter.d.ts +0 -1
  10. package/dist/email/interface.d.ts +1 -1
  11. package/dist/graphql/resolvers.d.ts +4 -2
  12. package/dist/hooks/index.d.ts +8 -2
  13. package/dist/index.d.ts +0 -1
  14. package/dist/index.js +32144 -32058
  15. package/dist/schema/Schema.d.ts +2 -1
  16. package/dist/server/index.d.ts +4 -2
  17. package/dist/utils/crypto.d.ts +7 -0
  18. package/dist/utils/helper.d.ts +5 -1
  19. package/generated/schema.graphql +22 -14
  20. package/generated/wabe.ts +4 -4
  21. package/package.json +23 -23
  22. package/src/authentication/OTP.test.ts +69 -0
  23. package/src/authentication/OTP.ts +64 -0
  24. package/src/authentication/Session.test.ts +629 -0
  25. package/src/authentication/Session.ts +493 -0
  26. package/src/authentication/defaultAuthentication.ts +209 -0
  27. package/src/authentication/index.ts +3 -0
  28. package/src/authentication/interface.ts +155 -0
  29. package/src/authentication/oauth/GitHub.test.ts +91 -0
  30. package/src/authentication/oauth/GitHub.ts +121 -0
  31. package/src/authentication/oauth/Google.test.ts +91 -0
  32. package/src/authentication/oauth/Google.ts +101 -0
  33. package/src/authentication/oauth/Oauth2Client.test.ts +219 -0
  34. package/src/authentication/oauth/Oauth2Client.ts +135 -0
  35. package/src/authentication/oauth/index.ts +2 -0
  36. package/src/authentication/oauth/utils.test.ts +33 -0
  37. package/src/authentication/oauth/utils.ts +27 -0
  38. package/src/authentication/providers/EmailOTP.test.ts +127 -0
  39. package/src/authentication/providers/EmailOTP.ts +84 -0
  40. package/src/authentication/providers/EmailPassword.test.ts +176 -0
  41. package/src/authentication/providers/EmailPassword.ts +116 -0
  42. package/src/authentication/providers/EmailPasswordSRP.test.ts +208 -0
  43. package/src/authentication/providers/EmailPasswordSRP.ts +179 -0
  44. package/src/authentication/providers/GitHub.ts +24 -0
  45. package/src/authentication/providers/Google.ts +24 -0
  46. package/src/authentication/providers/OAuth.test.ts +185 -0
  47. package/src/authentication/providers/OAuth.ts +106 -0
  48. package/src/authentication/providers/PhonePassword.test.ts +176 -0
  49. package/src/authentication/providers/PhonePassword.ts +115 -0
  50. package/src/authentication/providers/QRCodeOTP.test.ts +77 -0
  51. package/src/authentication/providers/QRCodeOTP.ts +58 -0
  52. package/src/authentication/providers/index.ts +6 -0
  53. package/src/authentication/resolvers/refreshResolver.test.ts +30 -0
  54. package/src/authentication/resolvers/refreshResolver.ts +19 -0
  55. package/src/authentication/resolvers/signInWithResolver.inte.test.ts +59 -0
  56. package/src/authentication/resolvers/signInWithResolver.test.ts +293 -0
  57. package/src/authentication/resolvers/signInWithResolver.ts +92 -0
  58. package/src/authentication/resolvers/signOutResolver.test.ts +38 -0
  59. package/src/authentication/resolvers/signOutResolver.ts +18 -0
  60. package/src/authentication/resolvers/signUpWithResolver.test.ts +180 -0
  61. package/src/authentication/resolvers/signUpWithResolver.ts +65 -0
  62. package/src/authentication/resolvers/verifyChallenge.test.ts +133 -0
  63. package/src/authentication/resolvers/verifyChallenge.ts +62 -0
  64. package/src/authentication/roles.test.ts +49 -0
  65. package/src/authentication/roles.ts +40 -0
  66. package/src/authentication/utils.test.ts +97 -0
  67. package/src/authentication/utils.ts +39 -0
  68. package/src/cache/InMemoryCache.test.ts +62 -0
  69. package/src/cache/InMemoryCache.ts +45 -0
  70. package/src/cron/index.test.ts +17 -0
  71. package/src/cron/index.ts +43 -0
  72. package/src/database/DatabaseController.test.ts +613 -0
  73. package/src/database/DatabaseController.ts +1007 -0
  74. package/src/database/index.test.ts +1372 -0
  75. package/src/database/index.ts +9 -0
  76. package/src/database/interface.ts +302 -0
  77. package/src/email/DevAdapter.ts +7 -0
  78. package/src/email/EmailController.test.ts +29 -0
  79. package/src/email/EmailController.ts +13 -0
  80. package/src/email/index.ts +2 -0
  81. package/src/email/interface.ts +36 -0
  82. package/src/email/templates/sendOtpCode.ts +120 -0
  83. package/src/file/FileController.ts +28 -0
  84. package/src/file/FileDevAdapter.ts +51 -0
  85. package/src/file/hookDeleteFile.ts +25 -0
  86. package/src/file/hookReadFile.ts +66 -0
  87. package/src/file/hookUploadFile.ts +50 -0
  88. package/src/file/index.test.ts +932 -0
  89. package/src/file/index.ts +2 -0
  90. package/src/file/interface.ts +39 -0
  91. package/src/graphql/GraphQLSchema.test.ts +4408 -0
  92. package/src/graphql/GraphQLSchema.ts +880 -0
  93. package/src/graphql/index.ts +2 -0
  94. package/src/graphql/parseGraphqlSchema.ts +85 -0
  95. package/src/graphql/parser.test.ts +203 -0
  96. package/src/graphql/parser.ts +542 -0
  97. package/src/graphql/pointerAndRelationFunction.ts +191 -0
  98. package/src/graphql/resolvers.ts +442 -0
  99. package/src/graphql/tests/aggregation.test.ts +1115 -0
  100. package/src/graphql/tests/e2e.test.ts +590 -0
  101. package/src/graphql/tests/scalars.test.ts +250 -0
  102. package/src/graphql/types.ts +227 -0
  103. package/src/hooks/HookObject.test.ts +122 -0
  104. package/src/hooks/HookObject.ts +165 -0
  105. package/src/hooks/authentication.ts +67 -0
  106. package/src/hooks/createUser.test.ts +77 -0
  107. package/src/hooks/createUser.ts +10 -0
  108. package/src/hooks/defaultFields.test.ts +176 -0
  109. package/src/hooks/defaultFields.ts +32 -0
  110. package/src/hooks/deleteSession.test.ts +181 -0
  111. package/src/hooks/deleteSession.ts +20 -0
  112. package/src/hooks/hashFieldHook.test.ts +152 -0
  113. package/src/hooks/hashFieldHook.ts +89 -0
  114. package/src/hooks/index.test.ts +258 -0
  115. package/src/hooks/index.ts +414 -0
  116. package/src/hooks/permissions.test.ts +412 -0
  117. package/src/hooks/permissions.ts +93 -0
  118. package/src/hooks/protected.test.ts +551 -0
  119. package/src/hooks/protected.ts +60 -0
  120. package/src/hooks/searchableFields.test.ts +147 -0
  121. package/src/hooks/searchableFields.ts +86 -0
  122. package/src/hooks/session.test.ts +134 -0
  123. package/src/hooks/session.ts +76 -0
  124. package/src/hooks/setEmail.test.ts +216 -0
  125. package/src/hooks/setEmail.ts +33 -0
  126. package/src/hooks/setupAcl.test.ts +618 -0
  127. package/src/hooks/setupAcl.ts +25 -0
  128. package/src/index.ts +9 -0
  129. package/src/schema/Schema.test.ts +482 -0
  130. package/src/schema/Schema.ts +757 -0
  131. package/src/schema/defaultResolvers.ts +93 -0
  132. package/src/schema/index.ts +1 -0
  133. package/src/schema/resolvers/meResolver.test.ts +62 -0
  134. package/src/schema/resolvers/meResolver.ts +10 -0
  135. package/src/schema/resolvers/resetPassword.test.ts +341 -0
  136. package/src/schema/resolvers/resetPassword.ts +63 -0
  137. package/src/schema/resolvers/sendEmail.test.ts +118 -0
  138. package/src/schema/resolvers/sendEmail.ts +21 -0
  139. package/src/schema/resolvers/sendOtpCode.test.ts +141 -0
  140. package/src/schema/resolvers/sendOtpCode.ts +52 -0
  141. package/src/security.test.ts +3434 -0
  142. package/src/server/defaultSessionHandler.test.ts +62 -0
  143. package/src/server/defaultSessionHandler.ts +105 -0
  144. package/src/server/generateCodegen.ts +433 -0
  145. package/src/server/index.test.ts +532 -0
  146. package/src/server/index.ts +334 -0
  147. package/src/server/interface.ts +11 -0
  148. package/src/server/routes/authHandler.ts +169 -0
  149. package/src/server/routes/index.ts +39 -0
  150. package/src/utils/crypto.test.ts +41 -0
  151. package/src/utils/crypto.ts +105 -0
  152. package/src/utils/export.ts +11 -0
  153. package/src/utils/helper.ts +204 -0
  154. package/src/utils/index.test.ts +11 -0
  155. package/src/utils/index.ts +189 -0
  156. package/src/utils/preload.ts +8 -0
  157. package/src/utils/testHelper.ts +116 -0
  158. package/tsconfig.json +32 -0
  159. package/bunfig.toml +0 -4
  160. package/dist/ai/index.d.ts +0 -1
  161. package/dist/ai/interface.d.ts +0 -9
  162. /package/dist/server/{defaultHandlers.d.ts → defaultSessionHandler.d.ts} +0 -0
@@ -0,0 +1,20 @@
1
+ import { contextWithRoot } from '../utils/export'
2
+ import type { DevWabeTypes } from '../utils/helper'
3
+ import type { HookObject } from './HookObject'
4
+
5
+ // TODO: It should better to do this in after delete to avoid case when deleteUser failed
6
+ // For the moment KISS
7
+ export const defaultDeleteSessionOnDeleteUser = async (
8
+ object: HookObject<DevWabeTypes, 'User'>,
9
+ ) => {
10
+ const userId = object.object?.id
11
+
12
+ await object.context.wabe.controllers.database.deleteObjects({
13
+ className: '_Session',
14
+ context: contextWithRoot(object.context),
15
+ where: {
16
+ user: { id: { equalTo: userId } },
17
+ },
18
+ select: {},
19
+ })
20
+ }
@@ -0,0 +1,152 @@
1
+ import { hashFieldHook } from './hashFieldHook'
2
+ import { describe, expect, it } from 'bun:test'
3
+ import { HookObject } from './HookObject'
4
+ import type { SchemaFields } from '../schema'
5
+ import { OperationType } from '.'
6
+ import { hashArgon2, verifyArgon2 } from 'src/utils/crypto'
7
+
8
+ const makeHookObject = ({ className, operationType, newData, fields }: any) => {
9
+ const config = {
10
+ port: 0,
11
+ isProduction: false,
12
+ database: {} as any,
13
+ rootKey: '',
14
+ schema: {
15
+ classes: [{ name: className, fields }],
16
+ },
17
+ }
18
+ return new HookObject({
19
+ className,
20
+ operationType,
21
+ newData,
22
+ context: {
23
+ wabe: { config } as any,
24
+ isRoot: false,
25
+ },
26
+ object: { id: 'dummy' },
27
+ select: {},
28
+ })
29
+ }
30
+
31
+ describe('hashFieldHook', () => {
32
+ const fields: SchemaFields<any> = {
33
+ password: { type: 'Hash' },
34
+ email: { type: 'Email' },
35
+ authentication: {
36
+ type: 'Object',
37
+ object: {
38
+ name: 'Authentication',
39
+ fields: {
40
+ emailPassword: {
41
+ type: 'Object',
42
+ object: {
43
+ name: 'EmailPassword',
44
+ fields: {
45
+ password: { type: 'Hash' },
46
+ },
47
+ },
48
+ },
49
+ },
50
+ },
51
+ },
52
+ twoHashes: {
53
+ type: 'Object',
54
+ object: {
55
+ name: 'TwoHashes',
56
+ fields: {
57
+ hash1: { type: 'Hash' },
58
+ hash2: { type: 'Hash' },
59
+ },
60
+ },
61
+ },
62
+ }
63
+
64
+ it('should hashed a plain value in a sub object in beforeCreate', async () => {
65
+ const newData = {
66
+ authentication: { emailPassword: { password: 'mysecret' } },
67
+ }
68
+
69
+ const hookObject = makeHookObject({
70
+ className: 'User',
71
+ operationType: OperationType.BeforeCreate,
72
+ newData,
73
+ fields,
74
+ })
75
+
76
+ await hashFieldHook(hookObject)
77
+
78
+ expect(hookObject.getNewData().authentication.emailPassword.password).not.toBe('mysecret')
79
+
80
+ const isValid = await verifyArgon2(
81
+ 'mysecret',
82
+ hookObject.getNewData().authentication.emailPassword.password,
83
+ )
84
+ expect(isValid).toBe(true)
85
+ })
86
+
87
+ it('should hashed a plain value in a sub object with 2 hash fields in beforeCreate', async () => {
88
+ const newData = {
89
+ twoHashes: { hash1: 'mysecret', hash2: 'mysecret' },
90
+ }
91
+
92
+ const hookObject = makeHookObject({
93
+ className: 'User',
94
+ operationType: OperationType.BeforeCreate,
95
+ newData,
96
+ fields,
97
+ })
98
+
99
+ await hashFieldHook(hookObject)
100
+
101
+ expect(hookObject.getNewData().twoHashes.hash1).not.toBe('mysecret')
102
+
103
+ const isValid = await verifyArgon2('mysecret', hookObject.getNewData().twoHashes.hash1)
104
+ expect(isValid).toBe(true)
105
+
106
+ expect(hookObject.getNewData().twoHashes.hash2).not.toBe('mysecret')
107
+
108
+ const isValid2 = await verifyArgon2('mysecret', hookObject.getNewData().twoHashes.hash2)
109
+ expect(isValid2).toBe(true)
110
+ })
111
+
112
+ it('hashes a plain value on beforeCreate', async () => {
113
+ const newData = { password: 'mysecret', email: 'test@example.com' }
114
+ const hookObject = makeHookObject({
115
+ className: 'User',
116
+ operationType: 'beforeCreate',
117
+ newData,
118
+ fields,
119
+ })
120
+ await hashFieldHook(hookObject)
121
+ expect(hookObject.getNewData().password).not.toBe('mysecret')
122
+ expect(hookObject.getNewData().email).toBe('test@example.com')
123
+
124
+ const isValid = await verifyArgon2('mysecret', hookObject.getNewData().password)
125
+ expect(isValid).toBe(true)
126
+ })
127
+
128
+ it('does not double-hash an already hashed value', async () => {
129
+ const hashed = await hashArgon2('alreadyhashed')
130
+ const newData = { password: hashed }
131
+ const hookObject = makeHookObject({
132
+ className: 'User',
133
+ operationType: 'beforeUpdate',
134
+ newData,
135
+ fields,
136
+ })
137
+ await hashFieldHook(hookObject)
138
+ expect(hookObject.getNewData().password).toBe(hashed)
139
+ })
140
+
141
+ it('does nothing if not beforeCreate or beforeUpdate', async () => {
142
+ const newData = { password: 'shouldnotchange' }
143
+ const hookObject = makeHookObject({
144
+ className: 'User',
145
+ operationType: 'afterRead',
146
+ newData,
147
+ fields,
148
+ })
149
+ await hashFieldHook(hookObject)
150
+ expect(hookObject.getNewData().password).toBe('shouldnotchange')
151
+ })
152
+ })
@@ -0,0 +1,89 @@
1
+ import type { HookObject } from './HookObject'
2
+ import type { WabeTypes } from '../server'
3
+ import { getNestedProperty, getNewObjectAfterUpdateNestedProperty } from '../utils'
4
+ import { OperationType } from '.'
5
+ import { hashArgon2, isArgon2Hash } from 'src/utils/crypto'
6
+
7
+ const hashField = ({ value }: { value: ReturnType<typeof HookObject.prototype.getNewData> }) => {
8
+ if (!value || typeof value !== 'string' || isArgon2Hash(value)) return value
9
+
10
+ return hashArgon2(value)
11
+ }
12
+
13
+ export async function hashFieldHook<T extends WabeTypes, K extends keyof WabeTypes['types']>(
14
+ hookObject: HookObject<T, K>,
15
+ ) {
16
+ if (
17
+ hookObject.operationType !== OperationType.BeforeCreate &&
18
+ hookObject.operationType !== OperationType.BeforeUpdate
19
+ )
20
+ return
21
+
22
+ const fields = hookObject.context.wabe.config.schema?.classes?.find(
23
+ (cls: any) => cls.name === hookObject.className,
24
+ )?.fields
25
+
26
+ if (!fields) return
27
+
28
+ const computeHashForFields = ({
29
+ fieldsToCompute,
30
+ path = '',
31
+ }: {
32
+ fieldsToCompute: typeof fields
33
+ path?: string
34
+ }) =>
35
+ Promise.all(
36
+ Object.entries(fieldsToCompute).map(async ([fieldName, fieldDef]) => {
37
+ if (fieldDef.type === 'Object') {
38
+ await computeHashForFields({
39
+ // @ts-expect-error
40
+ fieldsToCompute: fieldDef.object.fields,
41
+ path: `${path || ''}${path ? '.' : ''}${fieldName}`,
42
+ })
43
+
44
+ return
45
+ }
46
+
47
+ if (fieldDef.type !== 'Hash') return
48
+
49
+ // If we don't have nested object
50
+ if (!path) {
51
+ const hashed = await hashField({
52
+ // @ts-expect-error
53
+ value: hookObject.getNewData()[fieldName],
54
+ })
55
+
56
+ // @ts-expect-error
57
+ hookObject.upsertNewData(fieldName, hashed)
58
+
59
+ return
60
+ }
61
+
62
+ const objectNameToUpdate = path.split('.')[0] || ''
63
+ // @ts-expect-error
64
+ const objectToUpdate = hookObject.getNewData()[objectNameToUpdate]
65
+ const valueToUpdate = getNestedProperty(hookObject.getNewData(), path)
66
+
67
+ if (!valueToUpdate?.[fieldName]) return
68
+
69
+ const hashed = await hashField({
70
+ value: valueToUpdate[fieldName],
71
+ })
72
+
73
+ const newObject = getNewObjectAfterUpdateNestedProperty(
74
+ { [objectNameToUpdate]: objectToUpdate },
75
+ `${path}.${fieldName}`,
76
+ hashed,
77
+ )
78
+
79
+ if (hashed)
80
+ hookObject.upsertNewData(
81
+ // @ts-expect-error
82
+ objectNameToUpdate,
83
+ newObject[objectNameToUpdate],
84
+ )
85
+ }),
86
+ )
87
+
88
+ await computeHashForFields({ fieldsToCompute: fields })
89
+ }
@@ -0,0 +1,258 @@
1
+ import { afterEach, describe, expect, it, spyOn, mock } from 'bun:test'
2
+ import * as index from './index'
3
+ import { OperationType } from './index'
4
+
5
+ describe('Hooks', () => {
6
+ const mockGetObject = mock(async (..._args: any[]) => ({}) as any)
7
+ const mockGetObjects = mock(() => {})
8
+ const mockCallback1 = mock(() => {})
9
+ const mockCallback2 = mock(() => {})
10
+ const mockCallback3 = mock(() => {})
11
+ const mockCallback4 = mock(() => {})
12
+ const mockCallback5 = mock(() => {})
13
+
14
+ const controllers = {
15
+ database: {
16
+ getObject: mockGetObject,
17
+ getObjects: mockGetObjects,
18
+ },
19
+ } as any
20
+
21
+ const config = {
22
+ hooks: [
23
+ {
24
+ callback: mockCallback1,
25
+ operationType: OperationType.BeforeRead,
26
+ priority: 1,
27
+ },
28
+ {
29
+ callback: mockCallback2,
30
+ operationType: OperationType.BeforeRead,
31
+ priority: 2,
32
+ },
33
+ {
34
+ callback: mockCallback3,
35
+ operationType: OperationType.BeforeCreate,
36
+ priority: 1,
37
+ },
38
+ {
39
+ callback: mockCallback4,
40
+ operationType: OperationType.BeforeRead,
41
+ priority: 1,
42
+ className: 'ClassTest',
43
+ },
44
+ {
45
+ callback: mockCallback5,
46
+ operationType: OperationType.BeforeRead,
47
+ priority: 1,
48
+ className: 'AnotherClass',
49
+ },
50
+ ],
51
+ } as any
52
+
53
+ afterEach(() => {
54
+ mockGetObjects.mockClear()
55
+ mockGetObject.mockClear()
56
+ mockCallback1.mockClear()
57
+ mockCallback2.mockClear()
58
+ mockCallback3.mockClear()
59
+ mockCallback4.mockClear()
60
+ mockCallback5.mockClear()
61
+ })
62
+
63
+ it('should only run hook on specified className', async () => {
64
+ const hooks = index.initializeHook({
65
+ className: 'ClassTest',
66
+ context: {
67
+ isRoot: true,
68
+ wabe: { controllers, config } as any,
69
+ },
70
+ newData: { name: 'test' },
71
+ select: {},
72
+ objectLoader: (id) =>
73
+ mockGetObject({
74
+ className: 'ClassTest',
75
+ context: {
76
+ isRoot: true,
77
+ wabe: { controllers, config },
78
+ },
79
+ id,
80
+ _skipHooks: true,
81
+ }),
82
+ })
83
+
84
+ await hooks.runOnSingleObject({
85
+ operationType: OperationType.BeforeRead,
86
+ id: 'id',
87
+ })
88
+
89
+ expect(mockCallback4).toHaveBeenCalledTimes(1)
90
+ expect(mockCallback5).toHaveBeenCalledTimes(0)
91
+ })
92
+
93
+ it('should run hook on BeforeRead with one object impacted', async () => {
94
+ mockGetObject.mockResolvedValueOnce({ id: 'id', name: 'name' } as never)
95
+
96
+ const hooks = index.initializeHook({
97
+ className: 'ClassName',
98
+ context: {
99
+ isRoot: true,
100
+ wabe: { controllers, config } as any,
101
+ },
102
+ newData: { name: 'test' },
103
+ select: {},
104
+ objectLoader: (id) =>
105
+ mockGetObject({
106
+ className: 'ClassName',
107
+ context: {
108
+ isRoot: true,
109
+ wabe: { controllers, config },
110
+ isGraphQLCall: false,
111
+ },
112
+ id,
113
+ _skipHooks: true,
114
+ }),
115
+ })
116
+
117
+ await hooks.runOnSingleObject({
118
+ operationType: OperationType.BeforeRead,
119
+ id: 'id',
120
+ })
121
+
122
+ expect(mockGetObject).toHaveBeenCalledTimes(1)
123
+ expect(mockGetObject).toHaveBeenCalledWith({
124
+ className: 'ClassName',
125
+ context: {
126
+ isRoot: true,
127
+ wabe: { controllers, config },
128
+ isGraphQLCall: false,
129
+ },
130
+ id: 'id',
131
+ _skipHooks: true,
132
+ })
133
+
134
+ expect(mockCallback1).toHaveBeenCalledTimes(1)
135
+
136
+ // @ts-expect-error
137
+ const hookObject = mockCallback1.mock.calls[0][0] as any
138
+
139
+ expect(hookObject.object).toEqual({
140
+ id: 'id',
141
+ name: 'name',
142
+ })
143
+
144
+ expect(hookObject.context).toEqual({
145
+ isRoot: true,
146
+ wabe: {
147
+ controllers: expect.any(Object),
148
+ config,
149
+ },
150
+ isGraphQLCall: false,
151
+ })
152
+ })
153
+
154
+ it('should run hook on BeforeCreate', async () => {
155
+ const hooks = index.initializeHook({
156
+ className: 'ClassName',
157
+ context: {
158
+ isRoot: true,
159
+ wabe: { controllers, config } as any,
160
+ },
161
+ newData: { name: 'test' },
162
+ select: {},
163
+ objectLoader: (id) =>
164
+ mockGetObject({
165
+ className: 'ClassTest',
166
+ context: {
167
+ isRoot: true,
168
+ wabe: { controllers, config },
169
+ },
170
+ id,
171
+ _skipHooks: true,
172
+ }),
173
+ })
174
+
175
+ await hooks.runOnSingleObject({
176
+ operationType: OperationType.BeforeCreate,
177
+ })
178
+
179
+ expect(mockGetObjects).toHaveBeenCalledTimes(0)
180
+
181
+ // @ts-expect-error
182
+ const hookObject = mockCallback3.mock.calls[0][0] as any
183
+
184
+ // Before create the object is empty
185
+ expect(hookObject.object).toEqual({})
186
+
187
+ expect(hookObject.context).toEqual({
188
+ isRoot: true,
189
+ wabe: { controllers: expect.any(Object), config },
190
+ isGraphQLCall: false,
191
+ })
192
+ })
193
+
194
+ it('should run callback in priorities order', async () => {
195
+ const spy_findHooksByPriority = spyOn(index, '_findHooksByPriority')
196
+
197
+ mockGetObjects.mockResolvedValueOnce([{ id: 'id', name: 'name' }] as never)
198
+
199
+ const hooks = index.initializeHook({
200
+ className: 'ClassName',
201
+ context: {
202
+ isRoot: true,
203
+ wabe: { controllers, config } as any,
204
+ },
205
+ newData: { name: 'test' },
206
+ select: {},
207
+ objectLoader: (id) =>
208
+ mockGetObject({
209
+ className: 'ClassTest',
210
+ context: {
211
+ isRoot: true,
212
+ wabe: { controllers, config },
213
+ },
214
+ id,
215
+ _skipHooks: true,
216
+ }),
217
+ })
218
+
219
+ await hooks.runOnSingleObject({
220
+ operationType: OperationType.BeforeRead,
221
+ id: 'id',
222
+ })
223
+
224
+ expect(spy_findHooksByPriority.mock.calls[0]?.[0].priority).toEqual(1)
225
+ expect(spy_findHooksByPriority.mock.calls[1]?.[0].priority).toEqual(2)
226
+ })
227
+
228
+ it('should call hook that is trigger by operation type', async () => {
229
+ mockGetObjects.mockResolvedValueOnce([{ id: 'id', name: 'name' }] as never)
230
+
231
+ const hooks = index.initializeHook({
232
+ className: 'ClassName',
233
+ context: {
234
+ isRoot: true,
235
+ wabe: { controllers, config } as any,
236
+ },
237
+ newData: { name: 'test' },
238
+ select: {},
239
+ objectLoader: (id) =>
240
+ mockGetObject({
241
+ className: 'ClassTest',
242
+ context: {
243
+ isRoot: true,
244
+ wabe: { controllers, config },
245
+ },
246
+ id,
247
+ _skipHooks: true,
248
+ }),
249
+ })
250
+
251
+ await hooks.runOnSingleObject({
252
+ operationType: OperationType.BeforeRead,
253
+ id: 'id',
254
+ })
255
+
256
+ expect(mockCallback3).toHaveBeenCalledTimes(0)
257
+ })
258
+ })