wabe 0.6.9 → 0.6.10

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 (158) hide show
  1. package/README.md +138 -32
  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/email/interface.d.ts +1 -1
  7. package/dist/graphql/resolvers.d.ts +4 -2
  8. package/dist/hooks/index.d.ts +1 -0
  9. package/dist/index.d.ts +0 -1
  10. package/dist/index.js +8713 -8867
  11. package/dist/server/index.d.ts +4 -2
  12. package/dist/utils/crypto.d.ts +7 -0
  13. package/dist/utils/helper.d.ts +4 -1
  14. package/generated/schema.graphql +16 -14
  15. package/generated/wabe.ts +4 -4
  16. package/package.json +15 -15
  17. package/src/authentication/OTP.test.ts +69 -0
  18. package/src/authentication/OTP.ts +66 -0
  19. package/src/authentication/Session.test.ts +665 -0
  20. package/src/authentication/Session.ts +529 -0
  21. package/src/authentication/defaultAuthentication.ts +214 -0
  22. package/src/authentication/index.ts +3 -0
  23. package/src/authentication/interface.ts +157 -0
  24. package/src/authentication/oauth/GitHub.test.ts +105 -0
  25. package/src/authentication/oauth/GitHub.ts +133 -0
  26. package/src/authentication/oauth/Google.test.ts +105 -0
  27. package/src/authentication/oauth/Google.ts +110 -0
  28. package/src/authentication/oauth/Oauth2Client.test.ts +225 -0
  29. package/src/authentication/oauth/Oauth2Client.ts +140 -0
  30. package/src/authentication/oauth/index.ts +2 -0
  31. package/src/authentication/oauth/utils.test.ts +35 -0
  32. package/src/authentication/oauth/utils.ts +28 -0
  33. package/src/authentication/providers/EmailOTP.test.ts +138 -0
  34. package/src/authentication/providers/EmailOTP.ts +93 -0
  35. package/src/authentication/providers/EmailPassword.test.ts +187 -0
  36. package/src/authentication/providers/EmailPassword.ts +130 -0
  37. package/src/authentication/providers/EmailPasswordSRP.test.ts +206 -0
  38. package/src/authentication/providers/EmailPasswordSRP.ts +184 -0
  39. package/src/authentication/providers/GitHub.ts +30 -0
  40. package/src/authentication/providers/Google.ts +30 -0
  41. package/src/authentication/providers/OAuth.test.ts +185 -0
  42. package/src/authentication/providers/OAuth.ts +112 -0
  43. package/src/authentication/providers/PhonePassword.test.ts +187 -0
  44. package/src/authentication/providers/PhonePassword.ts +129 -0
  45. package/src/authentication/providers/QRCodeOTP.test.ts +79 -0
  46. package/src/authentication/providers/QRCodeOTP.ts +65 -0
  47. package/src/authentication/providers/index.ts +6 -0
  48. package/src/authentication/resolvers/refreshResolver.test.ts +37 -0
  49. package/src/authentication/resolvers/refreshResolver.ts +20 -0
  50. package/src/authentication/resolvers/signInWithResolver.inte.test.ts +59 -0
  51. package/src/authentication/resolvers/signInWithResolver.test.ts +307 -0
  52. package/src/authentication/resolvers/signInWithResolver.ts +102 -0
  53. package/src/authentication/resolvers/signOutResolver.test.ts +41 -0
  54. package/src/authentication/resolvers/signOutResolver.ts +22 -0
  55. package/src/authentication/resolvers/signUpWithResolver.test.ts +186 -0
  56. package/src/authentication/resolvers/signUpWithResolver.ts +69 -0
  57. package/src/authentication/resolvers/verifyChallenge.test.ts +136 -0
  58. package/src/authentication/resolvers/verifyChallenge.ts +69 -0
  59. package/src/authentication/roles.test.ts +59 -0
  60. package/src/authentication/roles.ts +40 -0
  61. package/src/authentication/utils.test.ts +99 -0
  62. package/src/authentication/utils.ts +43 -0
  63. package/src/cache/InMemoryCache.test.ts +62 -0
  64. package/src/cache/InMemoryCache.ts +45 -0
  65. package/src/cron/index.test.ts +17 -0
  66. package/src/cron/index.ts +46 -0
  67. package/src/database/DatabaseController.test.ts +625 -0
  68. package/src/database/DatabaseController.ts +983 -0
  69. package/src/database/index.test.ts +1230 -0
  70. package/src/database/index.ts +9 -0
  71. package/src/database/interface.ts +312 -0
  72. package/src/email/DevAdapter.ts +8 -0
  73. package/src/email/EmailController.test.ts +29 -0
  74. package/src/email/EmailController.ts +13 -0
  75. package/src/email/index.ts +2 -0
  76. package/src/email/interface.ts +36 -0
  77. package/src/email/templates/sendOtpCode.ts +120 -0
  78. package/src/file/FileController.ts +28 -0
  79. package/src/file/FileDevAdapter.ts +54 -0
  80. package/src/file/hookDeleteFile.ts +27 -0
  81. package/src/file/hookReadFile.ts +70 -0
  82. package/src/file/hookUploadFile.ts +53 -0
  83. package/src/file/index.test.ts +979 -0
  84. package/src/file/index.ts +2 -0
  85. package/src/file/interface.ts +42 -0
  86. package/src/graphql/GraphQLSchema.test.ts +4399 -0
  87. package/src/graphql/GraphQLSchema.ts +928 -0
  88. package/src/graphql/index.ts +2 -0
  89. package/src/graphql/parseGraphqlSchema.ts +94 -0
  90. package/src/graphql/parser.test.ts +217 -0
  91. package/src/graphql/parser.ts +566 -0
  92. package/src/graphql/pointerAndRelationFunction.ts +200 -0
  93. package/src/graphql/resolvers.ts +467 -0
  94. package/src/graphql/tests/aggregation.test.ts +1123 -0
  95. package/src/graphql/tests/e2e.test.ts +596 -0
  96. package/src/graphql/tests/scalars.test.ts +250 -0
  97. package/src/graphql/types.ts +219 -0
  98. package/src/hooks/HookObject.test.ts +122 -0
  99. package/src/hooks/HookObject.ts +168 -0
  100. package/src/hooks/authentication.ts +76 -0
  101. package/src/hooks/createUser.test.ts +77 -0
  102. package/src/hooks/createUser.ts +10 -0
  103. package/src/hooks/defaultFields.test.ts +187 -0
  104. package/src/hooks/defaultFields.ts +40 -0
  105. package/src/hooks/deleteSession.test.ts +181 -0
  106. package/src/hooks/deleteSession.ts +20 -0
  107. package/src/hooks/hashFieldHook.test.ts +163 -0
  108. package/src/hooks/hashFieldHook.ts +97 -0
  109. package/src/hooks/index.test.ts +207 -0
  110. package/src/hooks/index.ts +430 -0
  111. package/src/hooks/permissions.test.ts +424 -0
  112. package/src/hooks/permissions.ts +113 -0
  113. package/src/hooks/protected.test.ts +551 -0
  114. package/src/hooks/protected.ts +72 -0
  115. package/src/hooks/searchableFields.test.ts +166 -0
  116. package/src/hooks/searchableFields.ts +98 -0
  117. package/src/hooks/session.test.ts +138 -0
  118. package/src/hooks/session.ts +78 -0
  119. package/src/hooks/setEmail.test.ts +216 -0
  120. package/src/hooks/setEmail.ts +35 -0
  121. package/src/hooks/setupAcl.test.ts +589 -0
  122. package/src/hooks/setupAcl.ts +29 -0
  123. package/src/index.ts +9 -0
  124. package/src/schema/Schema.test.ts +484 -0
  125. package/src/schema/Schema.ts +795 -0
  126. package/src/schema/defaultResolvers.ts +94 -0
  127. package/src/schema/index.ts +1 -0
  128. package/src/schema/resolvers/meResolver.test.ts +62 -0
  129. package/src/schema/resolvers/meResolver.ts +14 -0
  130. package/src/schema/resolvers/newFile.ts +0 -0
  131. package/src/schema/resolvers/resetPassword.test.ts +345 -0
  132. package/src/schema/resolvers/resetPassword.ts +64 -0
  133. package/src/schema/resolvers/sendEmail.test.ts +118 -0
  134. package/src/schema/resolvers/sendEmail.ts +21 -0
  135. package/src/schema/resolvers/sendOtpCode.test.ts +153 -0
  136. package/src/schema/resolvers/sendOtpCode.ts +52 -0
  137. package/src/security.test.ts +3461 -0
  138. package/src/server/defaultSessionHandler.test.ts +66 -0
  139. package/src/server/defaultSessionHandler.ts +115 -0
  140. package/src/server/generateCodegen.ts +476 -0
  141. package/src/server/index.test.ts +552 -0
  142. package/src/server/index.ts +354 -0
  143. package/src/server/interface.ts +11 -0
  144. package/src/server/routes/authHandler.ts +187 -0
  145. package/src/server/routes/index.ts +40 -0
  146. package/src/utils/crypto.test.ts +41 -0
  147. package/src/utils/crypto.ts +121 -0
  148. package/src/utils/export.ts +13 -0
  149. package/src/utils/helper.ts +195 -0
  150. package/src/utils/index.test.ts +11 -0
  151. package/src/utils/index.ts +201 -0
  152. package/src/utils/preload.ts +8 -0
  153. package/src/utils/testHelper.ts +117 -0
  154. package/tsconfig.json +32 -0
  155. package/bunfig.toml +0 -4
  156. package/dist/ai/index.d.ts +0 -1
  157. package/dist/ai/interface.d.ts +0 -9
  158. /package/dist/server/{defaultHandlers.d.ts → defaultSessionHandler.d.ts} +0 -0
@@ -0,0 +1,250 @@
1
+ import { afterAll, beforeAll, describe, expect, it } from 'bun:test'
2
+ import type { Wabe } from '../../server'
3
+ import { type GraphQLClient, gql } from 'graphql-request'
4
+ import { type DevWabeTypes, getGraphqlClient } from '../../utils/helper'
5
+ import { setupTests, closeTests } from '../../utils/testHelper'
6
+
7
+ describe('GraphQL : Scalars', () => {
8
+ let wabe: Wabe<DevWabeTypes>
9
+ let port: number
10
+ let client: GraphQLClient
11
+
12
+ const now = new Date()
13
+
14
+ beforeAll(async () => {
15
+ const setup = await setupTests()
16
+ wabe = setup.wabe
17
+ port = setup.port
18
+ client = getGraphqlClient(port)
19
+ })
20
+
21
+ afterAll(async () => {
22
+ await closeTests(wabe)
23
+ })
24
+
25
+ describe('Date', () => {
26
+ it('should create a date with JavaScript Date', async () => {
27
+ const { createUsers } = await client.request<any>(graphql.createUsers, {
28
+ input: {
29
+ fields: [
30
+ {
31
+ name: 'Jean',
32
+ birthDate: now,
33
+ email: 'email@test.fr',
34
+ },
35
+ ],
36
+ },
37
+ })
38
+
39
+ expect(createUsers.edges[0].node.birthDate).toEqual(now.toISOString())
40
+ })
41
+
42
+ it('should create a date with timestamp in number', async () => {
43
+ const { createUsers } = await client.request<any>(graphql.createUsers, {
44
+ input: {
45
+ fields: [
46
+ {
47
+ name: 'Jean2',
48
+ birthDate: now.getTime(),
49
+ email: 'email@test.fr',
50
+ },
51
+ ],
52
+ },
53
+ })
54
+
55
+ expect(createUsers.edges[0].node.birthDate).toEqual(now.toISOString())
56
+ })
57
+
58
+ it('should create a date with iso string', async () => {
59
+ const { createUsers } = await client.request<any>(graphql.createUsers, {
60
+ input: {
61
+ fields: [
62
+ {
63
+ name: 'Jean3',
64
+ birthDate: now.toISOString(),
65
+ email: 'email@test.fr',
66
+ },
67
+ ],
68
+ },
69
+ })
70
+
71
+ expect(createUsers.edges[0].node.birthDate).toEqual(now.toISOString())
72
+ })
73
+
74
+ it('should create a date with partial date', async () => {
75
+ const { createUsers } = await client.request<any>(graphql.createUsers, {
76
+ input: {
77
+ fields: [
78
+ {
79
+ name: 'Jean4',
80
+ birthDate: '2023-12-20',
81
+ email: 'email@test.fr',
82
+ },
83
+ ],
84
+ },
85
+ })
86
+
87
+ const birthDate = new Date(createUsers.edges[0].node.birthDate)
88
+ const date = new Date('2023-12-20')
89
+
90
+ expect(date.getFullYear()).toEqual(birthDate.getFullYear())
91
+ expect(date.getMonth()).toEqual(birthDate.getMonth())
92
+ expect(date.getDate()).toEqual(birthDate.getDate())
93
+ })
94
+
95
+ it('should not create a date with invalid string', () => {
96
+ expect(
97
+ client.request(graphql.createUsers, {
98
+ input: {
99
+ fields: [
100
+ {
101
+ name: 'Jean5',
102
+ birthDate: now.getTime().toString(),
103
+ },
104
+ ],
105
+ },
106
+ }),
107
+ ).rejects.toThrow('Invalid date')
108
+
109
+ expect(
110
+ client.request(graphql.createUsers, {
111
+ input: {
112
+ fields: [
113
+ {
114
+ name: 'Jean5',
115
+ birthDate: 'tata',
116
+ },
117
+ ],
118
+ },
119
+ }),
120
+ ).rejects.toThrow('Invalid date')
121
+ })
122
+ })
123
+
124
+ describe('Email', () => {
125
+ it('should create an email', async () => {
126
+ const { createUsers } = await client.request<any>(graphql.createUsers, {
127
+ input: {
128
+ fields: [
129
+ {
130
+ name: 'Jean',
131
+ email: 'jean.doe@gmail.com',
132
+ },
133
+ ],
134
+ },
135
+ })
136
+
137
+ expect(createUsers.edges[0].node.email).toEqual('jean.doe@gmail.com')
138
+
139
+ expect(
140
+ client.request(graphql.createUsers, {
141
+ input: {
142
+ fields: [
143
+ {
144
+ name: 'Jean',
145
+ email: 'oe@gmail.com',
146
+ },
147
+ ],
148
+ },
149
+ }),
150
+ ).resolves.toEqual(expect.anything())
151
+
152
+ expect(
153
+ client.request(graphql.createUsers, {
154
+ input: {
155
+ fields: [
156
+ {
157
+ name: 'Jean',
158
+ email: 'test.@gmail.fr',
159
+ },
160
+ ],
161
+ },
162
+ }),
163
+ ).resolves.toEqual(expect.anything())
164
+ })
165
+
166
+ it('should not create an invalid email', () => {
167
+ expect(
168
+ client.request(graphql.createUsers, {
169
+ input: {
170
+ fields: [
171
+ {
172
+ name: 'Jean',
173
+ email: 'jean.doe',
174
+ },
175
+ ],
176
+ },
177
+ }),
178
+ ).rejects.toThrow('Invalid email')
179
+
180
+ expect(
181
+ client.request(graphql.createUsers, {
182
+ input: {
183
+ fields: [
184
+ {
185
+ name: 'Jean',
186
+ email: 'jean.doe@gmail',
187
+ },
188
+ ],
189
+ },
190
+ }),
191
+ ).rejects.toThrow('Invalid email')
192
+
193
+ expect(
194
+ client.request(graphql.createUsers, {
195
+ input: {
196
+ fields: [
197
+ {
198
+ name: 'Jean',
199
+ email: '@gmail.com',
200
+ },
201
+ ],
202
+ },
203
+ }),
204
+ ).rejects.toThrow('Invalid email')
205
+
206
+ expect(
207
+ client.request(graphql.createUsers, {
208
+ input: {
209
+ fields: [
210
+ {
211
+ name: 'Jean',
212
+ email: '@gmail',
213
+ },
214
+ ],
215
+ },
216
+ }),
217
+ ).rejects.toThrow('Invalid email')
218
+ })
219
+ })
220
+ })
221
+
222
+ const graphql = {
223
+ users: gql`
224
+ query users($where: UserWhereInput) {
225
+ users(where: $where) {
226
+ edges {
227
+ node {
228
+ id
229
+ name
230
+ birthDate
231
+ email
232
+ }
233
+ }
234
+ }
235
+ }
236
+ `,
237
+ createUsers: gql`
238
+ mutation createUsers($input: CreateUsersInput!) {
239
+ createUsers(input: $input) {
240
+ edges {
241
+ node {
242
+ name
243
+ birthDate
244
+ email
245
+ }
246
+ }
247
+ }
248
+ }
249
+ `,
250
+ }
@@ -0,0 +1,219 @@
1
+ import {
2
+ GraphQLBoolean,
3
+ GraphQLFloat,
4
+ GraphQLInputObjectType,
5
+ GraphQLInt,
6
+ GraphQLList,
7
+ GraphQLScalarType,
8
+ GraphQLString,
9
+ GraphQLID,
10
+ } from 'graphql'
11
+ import { isValidPhoneNumber } from 'libphonenumber-js'
12
+ import { tokenize } from '../utils'
13
+
14
+ export const AnyScalarType = new GraphQLScalarType({
15
+ name: 'Any',
16
+ description:
17
+ 'The Any scalar type is used in operations and types that involve any type of value.',
18
+ parseValue: (value) => value,
19
+ serialize: (value) => value,
20
+ })
21
+
22
+ export const DateScalarType = new GraphQLScalarType({
23
+ name: 'Date',
24
+ description: 'Date scalar type',
25
+ parseValue(value: any): Date {
26
+ const date = new Date(value)
27
+
28
+ if (Number.isNaN(date.getTime())) throw new Error('Invalid date')
29
+
30
+ return date
31
+ },
32
+ serialize(value: any) {
33
+ if (value instanceof Date) return value.toISOString()
34
+
35
+ return new Date(value).toISOString()
36
+ },
37
+ })
38
+
39
+ export const EmailScalarType = new GraphQLScalarType({
40
+ name: 'Email',
41
+ description: 'Email scalar type',
42
+ parseValue(value: any): string {
43
+ if (typeof value !== 'string') throw new Error('Invalid email')
44
+
45
+ if (!value.match(/^[^@\s]+@[^@\s]+\.[^@\s]+$/))
46
+ throw new Error('Invalid email')
47
+
48
+ return value
49
+ },
50
+ })
51
+
52
+ export const PhoneScalarType = new GraphQLScalarType({
53
+ name: 'Phone',
54
+ description: 'Phone scalar type',
55
+ parseValue(value: any): string {
56
+ if (typeof value !== 'string') throw new Error('Invalid phone')
57
+
58
+ if (!isValidPhoneNumber(value)) throw new Error('Invalid phone number')
59
+
60
+ return value
61
+ },
62
+ })
63
+
64
+ const parseFileValue = (value: any) => {
65
+ if (value instanceof Blob) return value
66
+
67
+ throw new Error('Invalid file')
68
+ }
69
+
70
+ export const FileScalarType = new GraphQLScalarType({
71
+ name: 'File',
72
+ description: 'File scalar type',
73
+ parseValue: parseFileValue,
74
+ serialize: (value: any) => {
75
+ return value
76
+ },
77
+ parseLiteral: () => {
78
+ throw new Error('Invalid file')
79
+ },
80
+ })
81
+
82
+ export const SearchScalarType = new GraphQLScalarType({
83
+ name: 'Search',
84
+ description: 'Search scalar to tokenize and search for all searchable fields',
85
+ parseValue: (value: any) => {
86
+ if (typeof value !== 'string') throw new Error('Invalid search term')
87
+
88
+ if (value === '') return ''
89
+
90
+ return tokenize(value).split(' ')
91
+ },
92
+ })
93
+
94
+ export const SearchWhereInput = new GraphQLInputObjectType({
95
+ name: 'SearchWhereInput',
96
+ fields: {
97
+ contains: { type: SearchScalarType },
98
+ },
99
+ })
100
+
101
+ export const AnyWhereInput = new GraphQLInputObjectType({
102
+ name: 'AnyWhereInput',
103
+ fields: {
104
+ equalTo: { type: AnyScalarType },
105
+ notEqualTo: { type: AnyScalarType },
106
+ },
107
+ })
108
+
109
+ export const FileWhereInput = new GraphQLInputObjectType({
110
+ name: 'FileWhereInput',
111
+ fields: {
112
+ equalTo: { type: FileScalarType },
113
+ notEqualTo: { type: FileScalarType },
114
+ in: { type: new GraphQLList(FileScalarType) },
115
+ notInt: { type: new GraphQLList(FileScalarType) },
116
+ },
117
+ })
118
+
119
+ export const ArrayWhereInput = new GraphQLInputObjectType({
120
+ name: 'ArrayWhereInput',
121
+ fields: {
122
+ equalTo: { type: AnyScalarType },
123
+ notEqualTo: { type: AnyScalarType },
124
+ contains: { type: AnyScalarType },
125
+ notContains: { type: AnyScalarType },
126
+ },
127
+ })
128
+
129
+ export const DateWhereInput = new GraphQLInputObjectType({
130
+ name: 'DateWhereInput',
131
+ fields: {
132
+ equalTo: { type: DateScalarType },
133
+ notEqualTo: { type: DateScalarType },
134
+ in: { type: new GraphQLList(DateScalarType) },
135
+ notIn: { type: new GraphQLList(DateScalarType) },
136
+ lessThan: { type: DateScalarType },
137
+ lessThanOrEqualTo: { type: DateScalarType },
138
+ greaterThan: { type: DateScalarType },
139
+ greaterThanOrEqualTo: { type: DateScalarType },
140
+ },
141
+ })
142
+
143
+ export const EmailWhereInput = new GraphQLInputObjectType({
144
+ name: 'EmailWhereInput',
145
+ fields: {
146
+ equalTo: { type: EmailScalarType },
147
+ notEqualTo: { type: EmailScalarType },
148
+ in: { type: new GraphQLList(EmailScalarType) },
149
+ notIn: { type: new GraphQLList(EmailScalarType) },
150
+ },
151
+ })
152
+
153
+ export const PhoneWhereInput = new GraphQLInputObjectType({
154
+ name: 'PhoneWhereInput',
155
+ fields: {
156
+ equalTo: { type: PhoneScalarType },
157
+ notEqualTo: { type: PhoneScalarType },
158
+ in: { type: new GraphQLList(PhoneScalarType) },
159
+ notIn: { type: new GraphQLList(PhoneScalarType) },
160
+ },
161
+ })
162
+
163
+ export const IdWhereInput = new GraphQLInputObjectType({
164
+ name: 'IdWhereInput',
165
+ fields: {
166
+ equalTo: { type: GraphQLID },
167
+ notEqualTo: { type: GraphQLID },
168
+ in: { type: new GraphQLList(GraphQLID) },
169
+ notIn: { type: new GraphQLList(GraphQLID) },
170
+ },
171
+ })
172
+
173
+ export const StringWhereInput = new GraphQLInputObjectType({
174
+ name: 'StringWhereInput',
175
+ fields: {
176
+ equalTo: { type: GraphQLString },
177
+ notEqualTo: { type: GraphQLString },
178
+ in: { type: new GraphQLList(GraphQLString) },
179
+ notIn: { type: new GraphQLList(GraphQLString) },
180
+ },
181
+ })
182
+
183
+ export const IntWhereInput = new GraphQLInputObjectType({
184
+ name: 'IntWhereInput',
185
+ fields: {
186
+ equalTo: { type: GraphQLInt },
187
+ notEqualTo: { type: GraphQLInt },
188
+ lessThan: { type: GraphQLInt },
189
+ lessThanOrEqualTo: { type: GraphQLInt },
190
+ greaterThan: { type: GraphQLInt },
191
+ greaterThanOrEqualTo: { type: GraphQLInt },
192
+ in: { type: new GraphQLList(GraphQLInt) },
193
+ notIn: { type: new GraphQLList(GraphQLInt) },
194
+ },
195
+ })
196
+
197
+ export const FloatWhereInput = new GraphQLInputObjectType({
198
+ name: 'FloatWhereInput',
199
+ fields: {
200
+ equalTo: { type: GraphQLFloat },
201
+ notEqualTo: { type: GraphQLFloat },
202
+ lessThan: { type: GraphQLFloat },
203
+ lessThanOrEqualTo: { type: GraphQLFloat },
204
+ greaterThan: { type: GraphQLFloat },
205
+ greaterThanOrEqualTo: { type: GraphQLFloat },
206
+ in: { type: new GraphQLList(GraphQLFloat) },
207
+ notIn: { type: new GraphQLList(GraphQLFloat) },
208
+ },
209
+ })
210
+
211
+ export const BooleanWhereInput = new GraphQLInputObjectType({
212
+ name: 'BooleanWhereInput',
213
+ fields: {
214
+ equalTo: { type: GraphQLBoolean },
215
+ notEqualTo: { type: GraphQLBoolean },
216
+ in: { type: new GraphQLList(GraphQLBoolean) },
217
+ notIn: { type: new GraphQLList(GraphQLBoolean) },
218
+ },
219
+ })
@@ -0,0 +1,122 @@
1
+ import { describe, expect, it, beforeAll, afterAll } from 'bun:test'
2
+ import { HookObject } from './HookObject'
3
+ import { OperationType } from '.'
4
+ import type { DevWabeTypes } from '../utils/helper'
5
+ import { setupTests, closeTests } from '../utils/testHelper'
6
+ import type { Wabe } from '../server'
7
+
8
+ describe('HookObject', () => {
9
+ let wabe: Wabe<DevWabeTypes>
10
+
11
+ beforeAll(async () => {
12
+ const setup = await setupTests()
13
+ wabe = setup.wabe
14
+ })
15
+
16
+ afterAll(async () => {
17
+ await closeTests(wabe)
18
+ })
19
+
20
+ it('should fetch all the fields of an object', async () => {
21
+ const res = await wabe.controllers.database.createObject({
22
+ className: 'User',
23
+ data: { age: 30, name: 'John Doe' },
24
+ context: {
25
+ wabe,
26
+ isRoot: true,
27
+ },
28
+ select: { id: true },
29
+ })
30
+
31
+ const hookObject = new HookObject<DevWabeTypes, 'User'>({
32
+ className: 'User',
33
+ // @ts-expect-error
34
+ newData: { age: 30, name: 'John Doe' },
35
+ context: {
36
+ wabe,
37
+ } as any,
38
+ operationType: OperationType.BeforeCreate,
39
+ object: {
40
+ id: res?.id || 'id',
41
+ },
42
+ select: {},
43
+ })
44
+
45
+ const fetchResult = await hookObject.fetch()
46
+
47
+ expect(fetchResult).toEqual(
48
+ expect.objectContaining({
49
+ id: res?.id || 'id',
50
+ age: 30,
51
+ name: 'John Doe',
52
+ }),
53
+ )
54
+ })
55
+
56
+ it('should return correctly value depends on the update state of the field', () => {
57
+ const userData = { name: 'John Doe' }
58
+
59
+ const hookObject = new HookObject<DevWabeTypes, 'User'>({
60
+ className: 'User',
61
+ // @ts-expect-error
62
+ newData: userData,
63
+ context: {} as any,
64
+ operationType: OperationType.BeforeUpdate,
65
+ object: {
66
+ id: '1',
67
+ },
68
+ select: {},
69
+ })
70
+
71
+ expect(hookObject.isFieldUpdated('name')).toBeTrue()
72
+ expect(hookObject.isFieldUpdated('age')).toBeFalse()
73
+ })
74
+
75
+ it('should create a clone of the data', () => {
76
+ const userData = { name: 'John Doe', age: 30 }
77
+
78
+ const hookObject = new HookObject<DevWabeTypes, 'User'>({
79
+ className: 'User',
80
+ newData: userData as any,
81
+ operationType: OperationType.BeforeCreate,
82
+ context: {} as any,
83
+ object: {
84
+ id: '1',
85
+ },
86
+ select: {},
87
+ })
88
+
89
+ hookObject.upsertNewData('name', 'tata')
90
+
91
+ expect(hookObject.getNewData()).toEqual(
92
+ expect.objectContaining({
93
+ name: 'tata',
94
+ age: 30,
95
+ }),
96
+ )
97
+ })
98
+
99
+ it('should not set data for an after hook', () => {
100
+ const userData = { name: 'John Doe', age: 30 }
101
+
102
+ const hookObject = new HookObject({
103
+ className: 'User',
104
+ newData: userData as any,
105
+ operationType: OperationType.AfterCreate,
106
+ context: {} as any,
107
+ object: {
108
+ id: '1',
109
+ },
110
+ select: {},
111
+ })
112
+
113
+ expect(() => hookObject.upsertNewData('name', 'tata')).toThrow(
114
+ 'Cannot set data in a hook that is not a before hook',
115
+ )
116
+
117
+ expect(hookObject.getNewData()).toEqual({
118
+ name: 'John Doe',
119
+ age: 30,
120
+ })
121
+ })
122
+ })