wabe 0.6.12 → 0.6.14

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 (156) hide show
  1. package/dist/database/DatabaseController.d.ts +2 -0
  2. package/dist/file/FileDevAdapter.d.ts +1 -0
  3. package/dist/graphql/pointerAndRelationFunction.d.ts +6 -0
  4. package/dist/index.js +3827 -3541
  5. package/dist/schema/Schema.d.ts +2 -2
  6. package/dist/server/generateCodegen.d.ts +10 -0
  7. package/dist/server/index.d.ts +2 -1
  8. package/dist/utils/objectKeys.d.ts +1 -0
  9. package/package.json +7 -4
  10. package/dev/index.ts +0 -215
  11. package/dist/schema/resolvers/sendEmail.d.ts +0 -1
  12. package/generated/schema.graphql +0 -1945
  13. package/generated/wabe.ts +0 -448
  14. package/src/authentication/OTP.test.ts +0 -69
  15. package/src/authentication/OTP.ts +0 -64
  16. package/src/authentication/Session.test.ts +0 -629
  17. package/src/authentication/Session.ts +0 -517
  18. package/src/authentication/cookies.ts +0 -10
  19. package/src/authentication/defaultAuthentication.ts +0 -209
  20. package/src/authentication/index.ts +0 -4
  21. package/src/authentication/interface.ts +0 -177
  22. package/src/authentication/oauth/GitHub.test.ts +0 -91
  23. package/src/authentication/oauth/GitHub.ts +0 -121
  24. package/src/authentication/oauth/Google.test.ts +0 -91
  25. package/src/authentication/oauth/Google.ts +0 -101
  26. package/src/authentication/oauth/Oauth2Client.test.ts +0 -219
  27. package/src/authentication/oauth/Oauth2Client.ts +0 -135
  28. package/src/authentication/oauth/index.ts +0 -2
  29. package/src/authentication/oauth/utils.test.ts +0 -33
  30. package/src/authentication/oauth/utils.ts +0 -27
  31. package/src/authentication/providers/EmailOTP.test.ts +0 -127
  32. package/src/authentication/providers/EmailOTP.ts +0 -95
  33. package/src/authentication/providers/EmailPassword.test.ts +0 -263
  34. package/src/authentication/providers/EmailPassword.ts +0 -138
  35. package/src/authentication/providers/EmailPasswordSRP.test.ts +0 -208
  36. package/src/authentication/providers/EmailPasswordSRP.ts +0 -191
  37. package/src/authentication/providers/GitHub.ts +0 -24
  38. package/src/authentication/providers/Google.ts +0 -24
  39. package/src/authentication/providers/OAuth.test.ts +0 -185
  40. package/src/authentication/providers/OAuth.ts +0 -106
  41. package/src/authentication/providers/PhonePassword.test.ts +0 -221
  42. package/src/authentication/providers/PhonePassword.ts +0 -136
  43. package/src/authentication/providers/QRCodeOTP.test.ts +0 -77
  44. package/src/authentication/providers/QRCodeOTP.ts +0 -69
  45. package/src/authentication/providers/index.ts +0 -6
  46. package/src/authentication/resolvers/refreshResolver.test.ts +0 -30
  47. package/src/authentication/resolvers/refreshResolver.ts +0 -19
  48. package/src/authentication/resolvers/signInWithResolver.inte.test.ts +0 -59
  49. package/src/authentication/resolvers/signInWithResolver.test.ts +0 -306
  50. package/src/authentication/resolvers/signInWithResolver.ts +0 -106
  51. package/src/authentication/resolvers/signOutResolver.test.ts +0 -38
  52. package/src/authentication/resolvers/signOutResolver.ts +0 -18
  53. package/src/authentication/resolvers/signUpWithResolver.test.ts +0 -180
  54. package/src/authentication/resolvers/signUpWithResolver.ts +0 -68
  55. package/src/authentication/resolvers/verifyChallenge.test.ts +0 -230
  56. package/src/authentication/resolvers/verifyChallenge.ts +0 -78
  57. package/src/authentication/roles.test.ts +0 -49
  58. package/src/authentication/roles.ts +0 -40
  59. package/src/authentication/security.ts +0 -278
  60. package/src/authentication/utils.test.ts +0 -97
  61. package/src/authentication/utils.ts +0 -39
  62. package/src/cache/InMemoryCache.test.ts +0 -62
  63. package/src/cache/InMemoryCache.ts +0 -45
  64. package/src/cron/index.test.ts +0 -17
  65. package/src/cron/index.ts +0 -43
  66. package/src/database/DatabaseController.test.ts +0 -613
  67. package/src/database/DatabaseController.ts +0 -1415
  68. package/src/database/index.test.ts +0 -1551
  69. package/src/database/index.ts +0 -9
  70. package/src/database/interface.ts +0 -308
  71. package/src/email/DevAdapter.ts +0 -7
  72. package/src/email/EmailController.test.ts +0 -29
  73. package/src/email/EmailController.ts +0 -13
  74. package/src/email/index.ts +0 -2
  75. package/src/email/interface.ts +0 -36
  76. package/src/email/templates/sendOtpCode.ts +0 -120
  77. package/src/file/FileController.ts +0 -28
  78. package/src/file/FileDevAdapter.ts +0 -51
  79. package/src/file/hookDeleteFile.ts +0 -25
  80. package/src/file/hookReadFile.ts +0 -66
  81. package/src/file/hookUploadFile.ts +0 -52
  82. package/src/file/index.test.ts +0 -1031
  83. package/src/file/index.ts +0 -2
  84. package/src/file/interface.ts +0 -63
  85. package/src/file/security.ts +0 -156
  86. package/src/graphql/GraphQLSchema.test.ts +0 -5099
  87. package/src/graphql/GraphQLSchema.ts +0 -886
  88. package/src/graphql/index.ts +0 -2
  89. package/src/graphql/parseGraphqlSchema.ts +0 -85
  90. package/src/graphql/parser.test.ts +0 -203
  91. package/src/graphql/parser.ts +0 -707
  92. package/src/graphql/pointerAndRelationFunction.ts +0 -191
  93. package/src/graphql/resolvers.ts +0 -464
  94. package/src/graphql/tests/aggregation.test.ts +0 -1115
  95. package/src/graphql/tests/e2e.test.ts +0 -590
  96. package/src/graphql/tests/scalars.test.ts +0 -250
  97. package/src/graphql/types.ts +0 -227
  98. package/src/hooks/HookObject.test.ts +0 -122
  99. package/src/hooks/HookObject.ts +0 -165
  100. package/src/hooks/authentication.ts +0 -67
  101. package/src/hooks/createUser.test.ts +0 -77
  102. package/src/hooks/createUser.ts +0 -10
  103. package/src/hooks/defaultFields.test.ts +0 -176
  104. package/src/hooks/defaultFields.ts +0 -32
  105. package/src/hooks/deleteSession.test.ts +0 -181
  106. package/src/hooks/deleteSession.ts +0 -20
  107. package/src/hooks/hashFieldHook.test.ts +0 -152
  108. package/src/hooks/hashFieldHook.ts +0 -89
  109. package/src/hooks/index.test.ts +0 -258
  110. package/src/hooks/index.ts +0 -420
  111. package/src/hooks/permissions.test.ts +0 -412
  112. package/src/hooks/permissions.ts +0 -93
  113. package/src/hooks/protected.test.ts +0 -551
  114. package/src/hooks/protected.ts +0 -74
  115. package/src/hooks/searchableFields.test.ts +0 -147
  116. package/src/hooks/searchableFields.ts +0 -86
  117. package/src/hooks/session.test.ts +0 -134
  118. package/src/hooks/session.ts +0 -76
  119. package/src/hooks/setEmail.test.ts +0 -216
  120. package/src/hooks/setEmail.ts +0 -33
  121. package/src/hooks/setupAcl.test.ts +0 -618
  122. package/src/hooks/setupAcl.ts +0 -25
  123. package/src/hooks/virtualFields.test.ts +0 -228
  124. package/src/hooks/virtualFields.ts +0 -48
  125. package/src/index.ts +0 -9
  126. package/src/schema/Schema.test.ts +0 -482
  127. package/src/schema/Schema.ts +0 -839
  128. package/src/schema/defaultResolvers.ts +0 -93
  129. package/src/schema/index.ts +0 -1
  130. package/src/schema/resolvers/meResolver.test.ts +0 -62
  131. package/src/schema/resolvers/meResolver.ts +0 -10
  132. package/src/schema/resolvers/resetPassword.test.ts +0 -341
  133. package/src/schema/resolvers/resetPassword.ts +0 -63
  134. package/src/schema/resolvers/sendEmail.test.ts +0 -118
  135. package/src/schema/resolvers/sendEmail.ts +0 -21
  136. package/src/schema/resolvers/sendOtpCode.test.ts +0 -141
  137. package/src/schema/resolvers/sendOtpCode.ts +0 -52
  138. package/src/security.test.ts +0 -4136
  139. package/src/server/defaultSessionHandler.test.ts +0 -62
  140. package/src/server/defaultSessionHandler.ts +0 -104
  141. package/src/server/generateCodegen.ts +0 -433
  142. package/src/server/index.test.ts +0 -843
  143. package/src/server/index.ts +0 -336
  144. package/src/server/interface.ts +0 -11
  145. package/src/server/routes/authHandler.ts +0 -171
  146. package/src/server/routes/index.ts +0 -48
  147. package/src/utils/crypto.test.ts +0 -41
  148. package/src/utils/crypto.ts +0 -105
  149. package/src/utils/database.ts +0 -8
  150. package/src/utils/export.ts +0 -12
  151. package/src/utils/helper.ts +0 -204
  152. package/src/utils/index.test.ts +0 -11
  153. package/src/utils/index.ts +0 -196
  154. package/src/utils/preload.ts +0 -8
  155. package/src/utils/testHelper.ts +0 -124
  156. package/tsconfig.json +0 -32
@@ -1,62 +0,0 @@
1
- import { describe, expect, it, mock, spyOn } from 'bun:test'
2
- import { defaultSessionHandler } from './defaultSessionHandler'
3
- import { Session } from '../authentication/Session'
4
-
5
- describe('defaultSessionHandler', () => {
6
- it('should set refreshToken cookie expiration to refresh expiry', async () => {
7
- const refreshExpiry = new Date('2030-01-01T00:00:00.000Z')
8
- const accessExpiry = new Date('2029-01-01T00:00:00.000Z')
9
-
10
- const spyMeFromAccessToken = spyOn(Session.prototype, 'meFromAccessToken').mockResolvedValue({
11
- user: { id: 'userId' } as any,
12
- sessionId: 'sessionId',
13
- accessToken: 'newAccess',
14
- refreshToken: 'newRefresh',
15
- })
16
-
17
- const spyGetAccessTokenExpireAt = spyOn(
18
- Session.prototype,
19
- 'getAccessTokenExpireAt',
20
- ).mockReturnValue(accessExpiry)
21
- const spyGetRefreshTokenExpireAt = spyOn(
22
- Session.prototype,
23
- 'getRefreshTokenExpireAt',
24
- ).mockReturnValue(refreshExpiry)
25
-
26
- const setCookie = mock(() => {})
27
-
28
- const wabe: any = {
29
- config: {
30
- rootKey: 'root',
31
- authentication: {
32
- session: { cookieSession: true, jwtSecret: 'dev' },
33
- },
34
- },
35
- }
36
-
37
- const handler = defaultSessionHandler(wabe)
38
-
39
- const ctx: any = {
40
- request: new Request('http://localhost/graphql', {
41
- headers: new Headers({
42
- 'Wabe-Access-Token': 'oldAccess',
43
- }),
44
- }),
45
- res: {
46
- setCookie,
47
- },
48
- }
49
-
50
- await handler(ctx)
51
-
52
- const refreshCookie = setCookie.mock.calls.find(
53
- (call: unknown) => Array.isArray(call) && call.length > 0 && call[0] === 'refreshToken',
54
- ) as [string, string, { expires?: Date }] | undefined
55
-
56
- expect(refreshCookie?.[2]?.expires).toEqual(refreshExpiry)
57
-
58
- spyMeFromAccessToken.mockRestore()
59
- spyGetAccessTokenExpireAt.mockRestore()
60
- spyGetRefreshTokenExpireAt.mockRestore()
61
- })
62
- })
@@ -1,104 +0,0 @@
1
- import type { Wabe, WobeCustomContext } from '.'
2
- import { getSessionCookieSameSite } from '../authentication/cookies'
3
- import { Session } from '../authentication/Session'
4
- import { getCookieInRequestHeaders, isValidRootKey } from '../utils'
5
- import type { DevWabeTypes } from '../utils/helper'
6
-
7
- export const defaultSessionHandler =
8
- (wabe: Wabe<DevWabeTypes>) => async (ctx: WobeCustomContext<DevWabeTypes>) => {
9
- const headers = ctx.request.headers
10
- const isGraphQLCall = ctx.request.url.includes('/graphql')
11
-
12
- if (isValidRootKey(headers, wabe.config.rootKey)) {
13
- ctx.wabe = {
14
- isRoot: true,
15
- wabe,
16
- response: ctx.res,
17
- isGraphQLCall,
18
- }
19
- return
20
- }
21
-
22
- const getAccessToken = () => {
23
- if (headers.get('Wabe-Access-Token')) return { accessToken: headers.get('Wabe-Access-Token') }
24
-
25
- const isCookieSession = !!wabe.config.authentication?.session?.cookieSession
26
-
27
- if (isCookieSession)
28
- return {
29
- accessToken: getCookieInRequestHeaders('accessToken', ctx.request.headers),
30
- }
31
-
32
- return { accessToken: null }
33
- }
34
-
35
- const { accessToken } = getAccessToken()
36
-
37
- if (!accessToken) {
38
- ctx.wabe = {
39
- isRoot: false,
40
- wabe,
41
- response: ctx.res,
42
- isGraphQLCall,
43
- }
44
- return
45
- }
46
-
47
- const getCsrfToken = () => {
48
- if (headers.get('Wabe-Csrf-Token')) return { csrfToken: headers.get('Wabe-Csrf-Token') || '' }
49
-
50
- return { csrfToken: '' }
51
- }
52
-
53
- const { csrfToken } = getCsrfToken()
54
-
55
- const session = new Session<DevWabeTypes>()
56
-
57
- const {
58
- user,
59
- sessionId,
60
- accessToken: newAccessToken,
61
- refreshToken: newRefreshToken,
62
- } = await session.meFromAccessToken(
63
- { accessToken, csrfToken },
64
- {
65
- wabe,
66
- isRoot: true,
67
- isGraphQLCall,
68
- },
69
- )
70
-
71
- ctx.wabe = {
72
- isRoot: false,
73
- sessionId,
74
- user,
75
- wabe,
76
- response: ctx.res,
77
- isGraphQLCall,
78
- }
79
-
80
- if (
81
- wabe.config.authentication?.session?.cookieSession &&
82
- newAccessToken &&
83
- newRefreshToken &&
84
- newAccessToken !== accessToken
85
- ) {
86
- const sameSite = getSessionCookieSameSite(wabe.config)
87
-
88
- ctx.res.setCookie('accessToken', newAccessToken, {
89
- httpOnly: true,
90
- path: '/',
91
- expires: session.getAccessTokenExpireAt(wabe.config),
92
- sameSite,
93
- secure: true,
94
- })
95
-
96
- ctx.res.setCookie('refreshToken', newRefreshToken, {
97
- httpOnly: true,
98
- path: '/',
99
- expires: session.getRefreshTokenExpireAt(wabe.config),
100
- sameSite,
101
- secure: true,
102
- })
103
- }
104
- }
@@ -1,433 +0,0 @@
1
- import { type GraphQLSchema, printSchema } from 'graphql'
2
- import { writeFile, readFile } from 'node:fs/promises'
3
- import type {
4
- ClassInterface,
5
- EnumInterface,
6
- MutationResolver,
7
- QueryResolver,
8
- ScalarInterface,
9
- SchemaInterface,
10
- TypeField,
11
- TypeResolver,
12
- WabeObject,
13
- WabePrimaryTypes,
14
- } from '../schema'
15
- import { firstLetterUpperCase, type DevWabeTypes } from '../utils/helper'
16
- import { firstLetterInUpperCase } from '../utils'
17
-
18
- const wabePrimaryTypesToTypescriptTypes: Record<WabePrimaryTypes, string> = {
19
- Boolean: 'boolean',
20
- Int: 'number',
21
- Float: 'number',
22
- String: 'string',
23
- Email: 'string',
24
- Phone: 'string',
25
- Date: 'Date',
26
- File: '{url: string, name: string}',
27
- Hash: 'string',
28
- }
29
-
30
- const wabeTypesToTypescriptTypes = ({
31
- field,
32
- isInput = false,
33
- }: {
34
- field: TypeField<DevWabeTypes>
35
- isInput?: boolean
36
- }) => {
37
- switch (field.type) {
38
- case 'Date':
39
- if (isInput) return 'Date'
40
- return 'string'
41
- case 'Boolean':
42
- case 'Int':
43
- case 'Float':
44
- case 'String':
45
- case 'Email':
46
- case 'Phone':
47
- case 'File':
48
- case 'Hash':
49
- return wabePrimaryTypesToTypescriptTypes[field.type]
50
- case 'Array':
51
- if (field.typeValue === 'Object') return `Array<${field.object.name}>`
52
- return `Array<${wabePrimaryTypesToTypescriptTypes[field.typeValue]}>`
53
- case 'Pointer':
54
- return field.class
55
- case 'Relation':
56
- return `Array<${field.class}>`
57
- case 'Object':
58
- return `${field.object.name}`
59
- default:
60
- return field.type
61
- }
62
- }
63
-
64
- const generateWabeObject = ({
65
- object,
66
- isInput = false,
67
- prefix = '',
68
- }: {
69
- object: WabeObject<DevWabeTypes>
70
- prefix?: string
71
- isInput?: boolean
72
- }): Record<string, Record<string, string>> => {
73
- const objectName = object.name
74
-
75
- return Object.entries(object.fields).reduce(
76
- (acc, [fieldName, field]) => {
77
- const type = wabeTypesToTypescriptTypes({ field, isInput })
78
-
79
- const objectNameWithPrefix = `${prefix}${firstLetterUpperCase(objectName)}`
80
-
81
- if (field.type === 'Object' || (field.type === 'Array' && field.typeValue === 'Object')) {
82
- const subObject = generateWabeObject({
83
- object: field.object,
84
- isInput,
85
- prefix: objectNameWithPrefix,
86
- })
87
-
88
- const isArray = field.type === 'Array'
89
-
90
- return {
91
- ...acc,
92
- ...subObject,
93
- [objectNameWithPrefix]: {
94
- ...acc[objectNameWithPrefix],
95
- [`${fieldName}${field.required ? '' : 'undefined'}`]: `${
96
- isArray ? 'Array<' : ''
97
- }${objectNameWithPrefix}${firstLetterUpperCase(field.object.name)}${
98
- isArray ? '>' : ''
99
- }`,
100
- },
101
- }
102
- }
103
-
104
- return {
105
- ...acc,
106
- [objectNameWithPrefix]: {
107
- ...acc[objectNameWithPrefix],
108
- [`${fieldName}${field.required ? '' : 'undefined'}`]: `${type}`,
109
- },
110
- }
111
- },
112
- {} as Record<string, Record<string, string>>,
113
- )
114
- }
115
-
116
- const generateWabeTypes = (classes: ClassInterface<DevWabeTypes>[]) => {
117
- const wabeTypes = classes.reduce(
118
- (acc, classType) => {
119
- const { name, fields } = classType
120
-
121
- const objectsToLoad: Array<Record<string, Record<string, string>>> = []
122
-
123
- const currentClass = Object.entries(fields).reduce(
124
- (acc2, [name, field]) => {
125
- const type = wabeTypesToTypescriptTypes({ field })
126
-
127
- if (field.type === 'Object' || (field.type === 'Array' && field.typeValue === 'Object')) {
128
- const wabeObject = generateWabeObject({ object: field.object })
129
-
130
- objectsToLoad.push(wabeObject)
131
- }
132
-
133
- return {
134
- ...acc2,
135
- [`${name}${field.required ? '' : 'undefined'}`]: type,
136
- }
137
- },
138
- {} as Record<string, string>,
139
- )
140
-
141
- const objects = objectsToLoad.reduce((acc2, object) => {
142
- return {
143
- ...acc2,
144
- ...object,
145
- }
146
- }, {})
147
-
148
- return {
149
- ...acc,
150
- ...objects,
151
- [name]: { id: 'string', ...currentClass },
152
- }
153
- },
154
- {} as Record<string, Record<string, string>>,
155
- )
156
-
157
- return wabeTypes
158
- }
159
-
160
- const generateWabeWhereTypes = (classes: ClassInterface<DevWabeTypes>[]) => {
161
- const wabeTypes = classes.reduce(
162
- (acc, classType) => {
163
- const { name, fields } = classType
164
-
165
- const completeName = `Where${firstLetterUpperCase(name)}`
166
-
167
- const objectsToLoad: Array<Record<string, Record<string, string>>> = []
168
-
169
- const currentClass = Object.entries(fields).reduce(
170
- (acc2, [name, field]) => {
171
- const type = wabeTypesToTypescriptTypes({ field, isInput: true })
172
-
173
- if (field.type === 'Object' || (field.type === 'Array' && field.typeValue === 'Object')) {
174
- const wabeObject = generateWabeObject({
175
- object: field.object,
176
- isInput: true,
177
- })
178
-
179
- objectsToLoad.push(wabeObject)
180
- }
181
-
182
- return {
183
- ...acc2,
184
- [`${name}${field.required ? '' : 'undefined'}`]: type,
185
- }
186
- },
187
- {} as Record<string, string>,
188
- )
189
-
190
- const objects = objectsToLoad.reduce((acc2, object) => {
191
- return {
192
- ...acc2,
193
- ...object,
194
- }
195
- }, {})
196
-
197
- return {
198
- ...acc,
199
- ...objects,
200
- [completeName]: { id: 'string', ...currentClass },
201
- }
202
- },
203
- {} as Record<string, Record<string, string>>,
204
- )
205
-
206
- return wabeTypes
207
- }
208
-
209
- const generateWabeEnumTypes = (enums: EnumInterface[]) => {
210
- return Object.values(enums).reduce(
211
- (acc, { name, values }) => {
212
- return {
213
- ...acc,
214
- [name]: values,
215
- }
216
- },
217
- {} as Record<string, Record<string, string>>,
218
- )
219
- }
220
-
221
- const generateWabeScalarTypes = (scalars: ScalarInterface[]) => {
222
- return Object.values(scalars).reduce(
223
- (acc, { name }) => {
224
- return {
225
- ...acc,
226
- // For the moment we will just use string as the type
227
- // Suppose all scalars are string
228
- [name]: 'string',
229
- }
230
- },
231
- {} as Record<string, string>,
232
- )
233
- }
234
-
235
- const generateWabeMutationOrQueryInput = (
236
- mutationOrQueryName: string,
237
- resolver: MutationResolver<any> | QueryResolver<any>,
238
- isMutation: boolean,
239
- ) => {
240
- const objectsToLoad: Array<Record<string, Record<string, string>>> = []
241
-
242
- const mutationNameWithFirstLetterUpperCase = firstLetterUpperCase(mutationOrQueryName)
243
-
244
- const mutationObject = Object.entries(
245
- (isMutation ? resolver.args?.input : resolver.args) || {},
246
- ).reduce(
247
- (acc, [name, field]) => {
248
- let type = wabeTypesToTypescriptTypes({ field, isInput: true })
249
-
250
- if (field.type === 'Object') {
251
- type = firstLetterInUpperCase(name)
252
-
253
- const wabeObject = generateWabeObject({
254
- object: {
255
- ...field.object,
256
- name: type,
257
- },
258
- prefix: mutationNameWithFirstLetterUpperCase,
259
- })
260
-
261
- objectsToLoad.push(wabeObject)
262
-
263
- return {
264
- ...acc,
265
- [`${name}${field.required ? '' : 'undefined'}`]: `${mutationNameWithFirstLetterUpperCase}${type}`,
266
- }
267
- }
268
-
269
- return {
270
- ...acc,
271
- [`${name}${field.required ? '' : 'undefined'}`]: type,
272
- }
273
- },
274
- {} as Record<string, string>,
275
- )
276
-
277
- const objects = objectsToLoad.reduce((acc2, object) => {
278
- return {
279
- ...acc2,
280
- ...object,
281
- }
282
- }, {})
283
-
284
- return {
285
- ...(isMutation
286
- ? {
287
- [`${firstLetterInUpperCase(mutationOrQueryName)}Input`]: mutationObject,
288
- }
289
- : {}),
290
- [`${isMutation ? 'Mutation' : 'Query'}${firstLetterInUpperCase(mutationOrQueryName)}Args`]:
291
- isMutation
292
- ? {
293
- input: `${firstLetterInUpperCase(mutationOrQueryName)}Input`,
294
- }
295
- : mutationObject,
296
- ...objects,
297
- }
298
- }
299
-
300
- const generateWabeMutationsAndQueriesTypes = (resolver: TypeResolver<any>) => {
301
- const mutationsObject = Object.entries(resolver.mutations || {}).reduce(
302
- (acc, [mutationName, mutation]) => {
303
- return {
304
- ...acc,
305
- ...generateWabeMutationOrQueryInput(mutationName, mutation, true),
306
- }
307
- },
308
- {},
309
- )
310
-
311
- const queriesObject = Object.entries(resolver.queries || {}).reduce((acc, [queryName, query]) => {
312
- return {
313
- ...acc,
314
- ...generateWabeMutationOrQueryInput(queryName, query, false),
315
- }
316
- }, {})
317
-
318
- return {
319
- ...mutationsObject,
320
- ...queriesObject,
321
- }
322
- }
323
-
324
- const wabeClassRecordToString = (wabeClass: Record<string, Record<string, string>>) => {
325
- return Object.entries(wabeClass).reduce((acc, [className, fields]) => {
326
- return `${acc}export type ${className} = {\n${Object.entries(fields)
327
- .map(([fieldName, fieldType]) => `\t${fieldName.replace('undefined', '?')}: ${fieldType}`)
328
- .join(',\n')}\n}\n\n`
329
- }, '')
330
- }
331
-
332
- const wabeEnumRecordToString = (wabeEnum: Record<string, Record<string, string>>) => {
333
- return Object.entries(wabeEnum).reduce((acc, [enumName, values]) => {
334
- return `${acc}export enum ${enumName} {\n${Object.entries(values)
335
- .map(([valueName, value]) => `\t${valueName} = "${value}"`)
336
- .join(',\n')}\n}\n\n`
337
- }, '')
338
- }
339
-
340
- const wabeScalarRecordToString = (wabeScalar: Record<string, string>) => {
341
- return Object.entries(wabeScalar).reduce((acc, [scalarName, scalarType]) => {
342
- return `${acc}export type ${scalarName} = ${scalarType}\n\n`
343
- }, '')
344
- }
345
-
346
- const generateWabeDevTypes = ({
347
- scalars,
348
- enums,
349
- classes,
350
- }: {
351
- enums?: EnumInterface[]
352
- scalars?: ScalarInterface[]
353
- classes: ClassInterface<DevWabeTypes>[]
354
- }) => {
355
- // Scalars
356
- const listOfScalars = scalars?.map((scalar) => `"${scalar.name}"`) || []
357
-
358
- const wabeScalarType =
359
- listOfScalars.length > 0
360
- ? `export type WabeSchemaScalars = ${listOfScalars.join(' | ')}`
361
- : 'export type WabeSchemaScalars = ""'
362
-
363
- // Enums
364
- const wabeEnumsGlobalTypes = enums?.map((wabeEnum) => `${wabeEnum.name}: ${wabeEnum.name}`) || []
365
-
366
- const wabeEnumsGlobalTypesString =
367
- wabeEnumsGlobalTypes.length > 0
368
- ? `export type WabeSchemaEnums = {\n\t${wabeEnumsGlobalTypes.join(',\n\t')}\n}`
369
- : ''
370
-
371
- // Classes
372
- const allNames = classes
373
- .map((schema) => `${schema.name}: ${schema.name}`)
374
- .filter((schema) => schema)
375
-
376
- const globalWabeTypeString = `export type WabeSchemaTypes = {\n\t${allNames.join(',\n\t')}\n}`
377
-
378
- // Where
379
- const allWhereNames = classes
380
- .map((schema) => `${schema.name}: Where${firstLetterUpperCase(schema.name)}`)
381
- .filter((schema) => schema)
382
-
383
- const globalWabeWhereTypeString = `export type WabeSchemaWhereTypes = {\n\t${allWhereNames.join(
384
- ',\n\t',
385
- )}\n}`
386
-
387
- return `${wabeScalarType}\n\n${wabeEnumsGlobalTypesString}\n\n${globalWabeTypeString}\n\n${globalWabeWhereTypeString}`
388
- }
389
-
390
- export const generateCodegen = async ({
391
- schema,
392
- path,
393
- graphqlSchema,
394
- }: {
395
- schema: SchemaInterface<any>
396
- path: string
397
- graphqlSchema: GraphQLSchema
398
- }) => {
399
- const graphqlSchemaContent = printSchema(graphqlSchema)
400
-
401
- const wabeClasses = generateWabeTypes(schema.classes || [])
402
- const wabeWhereTypes = generateWabeWhereTypes(schema.classes || [])
403
- const mutationsAndQueries = generateWabeMutationsAndQueriesTypes(schema.resolvers || {})
404
-
405
- const wabeEnumsInString = wabeEnumRecordToString(generateWabeEnumTypes(schema.enums || []))
406
- const wabeScalarsInString = wabeScalarRecordToString(
407
- generateWabeScalarTypes(schema.scalars || []),
408
- )
409
- const wabeObjectsInString = wabeClassRecordToString({
410
- ...wabeClasses,
411
- ...wabeWhereTypes,
412
- ...mutationsAndQueries,
413
- })
414
-
415
- const wabeDevTypes = generateWabeDevTypes({
416
- scalars: schema.scalars,
417
- enums: schema.enums,
418
- classes: schema.classes || [],
419
- })
420
-
421
- const wabeTsContent = `${wabeEnumsInString}${wabeScalarsInString}${wabeObjectsInString}${wabeDevTypes}`
422
-
423
- try {
424
- const contentOfGraphqlSchema = (await readFile(`${path}/schema.graphql`)).toString()
425
-
426
- // We will need to find a better way to avoid infinite loop of loading
427
- // Better solution will be that bun implements watch ignores)
428
- if (!process.env.CODEGEN && contentOfGraphqlSchema === graphqlSchemaContent.toString()) return
429
- } catch {}
430
-
431
- await writeFile(`${path}/wabe.ts`, wabeTsContent)
432
- await writeFile(`${path}/schema.graphql`, graphqlSchemaContent)
433
- }