wabe 0.6.11 → 0.6.13
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/dist/authentication/Session.d.ts +10 -12
- package/dist/authentication/cookies.d.ts +1 -0
- package/dist/authentication/index.d.ts +1 -0
- package/dist/authentication/interface.d.ts +20 -0
- package/dist/authentication/security.d.ts +18 -0
- package/dist/database/DatabaseController.d.ts +57 -3
- package/dist/database/interface.d.ts +5 -0
- package/dist/file/interface.d.ts +23 -0
- package/dist/file/security.d.ts +6 -0
- package/dist/graphql/GraphQLSchema.d.ts +1 -1
- package/dist/graphql/resolvers.d.ts +1 -0
- package/dist/hooks/protected.d.ts +3 -0
- package/dist/hooks/virtualFields.d.ts +3 -0
- package/dist/index.js +1757 -818
- package/dist/schema/Schema.d.ts +66 -7
- package/dist/server/index.d.ts +2 -1
- package/dist/server/routes/index.d.ts +4 -1
- package/dist/utils/database.d.ts +1 -0
- package/dist/utils/export.d.ts +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/package.json +7 -4
- package/bucket/b.txt +0 -1
- package/dev/index.ts +0 -215
- package/generated/schema.graphql +0 -1892
- package/generated/wabe.ts +0 -439
- package/src/authentication/OTP.test.ts +0 -69
- package/src/authentication/OTP.ts +0 -64
- package/src/authentication/Session.test.ts +0 -629
- package/src/authentication/Session.ts +0 -493
- package/src/authentication/defaultAuthentication.ts +0 -209
- package/src/authentication/index.ts +0 -3
- package/src/authentication/interface.ts +0 -155
- package/src/authentication/oauth/GitHub.test.ts +0 -91
- package/src/authentication/oauth/GitHub.ts +0 -121
- package/src/authentication/oauth/Google.test.ts +0 -91
- package/src/authentication/oauth/Google.ts +0 -101
- package/src/authentication/oauth/Oauth2Client.test.ts +0 -219
- package/src/authentication/oauth/Oauth2Client.ts +0 -135
- package/src/authentication/oauth/index.ts +0 -2
- package/src/authentication/oauth/utils.test.ts +0 -33
- package/src/authentication/oauth/utils.ts +0 -27
- package/src/authentication/providers/EmailOTP.test.ts +0 -127
- package/src/authentication/providers/EmailOTP.ts +0 -84
- package/src/authentication/providers/EmailPassword.test.ts +0 -176
- package/src/authentication/providers/EmailPassword.ts +0 -116
- package/src/authentication/providers/EmailPasswordSRP.test.ts +0 -208
- package/src/authentication/providers/EmailPasswordSRP.ts +0 -179
- package/src/authentication/providers/GitHub.ts +0 -24
- package/src/authentication/providers/Google.ts +0 -24
- package/src/authentication/providers/OAuth.test.ts +0 -185
- package/src/authentication/providers/OAuth.ts +0 -106
- package/src/authentication/providers/PhonePassword.test.ts +0 -176
- package/src/authentication/providers/PhonePassword.ts +0 -115
- package/src/authentication/providers/QRCodeOTP.test.ts +0 -77
- package/src/authentication/providers/QRCodeOTP.ts +0 -58
- package/src/authentication/providers/index.ts +0 -6
- package/src/authentication/resolvers/refreshResolver.test.ts +0 -30
- package/src/authentication/resolvers/refreshResolver.ts +0 -19
- package/src/authentication/resolvers/signInWithResolver.inte.test.ts +0 -59
- package/src/authentication/resolvers/signInWithResolver.test.ts +0 -293
- package/src/authentication/resolvers/signInWithResolver.ts +0 -92
- package/src/authentication/resolvers/signOutResolver.test.ts +0 -38
- package/src/authentication/resolvers/signOutResolver.ts +0 -18
- package/src/authentication/resolvers/signUpWithResolver.test.ts +0 -180
- package/src/authentication/resolvers/signUpWithResolver.ts +0 -65
- package/src/authentication/resolvers/verifyChallenge.test.ts +0 -133
- package/src/authentication/resolvers/verifyChallenge.ts +0 -62
- package/src/authentication/roles.test.ts +0 -49
- package/src/authentication/roles.ts +0 -40
- package/src/authentication/utils.test.ts +0 -97
- package/src/authentication/utils.ts +0 -39
- package/src/cache/InMemoryCache.test.ts +0 -62
- package/src/cache/InMemoryCache.ts +0 -45
- package/src/cron/index.test.ts +0 -17
- package/src/cron/index.ts +0 -43
- package/src/database/DatabaseController.test.ts +0 -613
- package/src/database/DatabaseController.ts +0 -1007
- package/src/database/index.test.ts +0 -1372
- package/src/database/index.ts +0 -9
- package/src/database/interface.ts +0 -302
- package/src/email/DevAdapter.ts +0 -7
- package/src/email/EmailController.test.ts +0 -29
- package/src/email/EmailController.ts +0 -13
- package/src/email/index.ts +0 -2
- package/src/email/interface.ts +0 -36
- package/src/email/templates/sendOtpCode.ts +0 -120
- package/src/file/FileController.ts +0 -28
- package/src/file/FileDevAdapter.ts +0 -51
- package/src/file/hookDeleteFile.ts +0 -25
- package/src/file/hookReadFile.ts +0 -66
- package/src/file/hookUploadFile.ts +0 -50
- package/src/file/index.test.ts +0 -932
- package/src/file/index.ts +0 -2
- package/src/file/interface.ts +0 -39
- package/src/graphql/GraphQLSchema.test.ts +0 -4408
- package/src/graphql/GraphQLSchema.ts +0 -880
- package/src/graphql/index.ts +0 -2
- package/src/graphql/parseGraphqlSchema.ts +0 -85
- package/src/graphql/parser.test.ts +0 -203
- package/src/graphql/parser.ts +0 -542
- package/src/graphql/pointerAndRelationFunction.ts +0 -191
- package/src/graphql/resolvers.ts +0 -442
- package/src/graphql/tests/aggregation.test.ts +0 -1115
- package/src/graphql/tests/e2e.test.ts +0 -590
- package/src/graphql/tests/scalars.test.ts +0 -250
- package/src/graphql/types.ts +0 -227
- package/src/hooks/HookObject.test.ts +0 -122
- package/src/hooks/HookObject.ts +0 -165
- package/src/hooks/authentication.ts +0 -67
- package/src/hooks/createUser.test.ts +0 -77
- package/src/hooks/createUser.ts +0 -10
- package/src/hooks/defaultFields.test.ts +0 -176
- package/src/hooks/defaultFields.ts +0 -32
- package/src/hooks/deleteSession.test.ts +0 -181
- package/src/hooks/deleteSession.ts +0 -20
- package/src/hooks/hashFieldHook.test.ts +0 -152
- package/src/hooks/hashFieldHook.ts +0 -89
- package/src/hooks/index.test.ts +0 -258
- package/src/hooks/index.ts +0 -414
- package/src/hooks/permissions.test.ts +0 -412
- package/src/hooks/permissions.ts +0 -93
- package/src/hooks/protected.test.ts +0 -551
- package/src/hooks/protected.ts +0 -60
- package/src/hooks/searchableFields.test.ts +0 -147
- package/src/hooks/searchableFields.ts +0 -86
- package/src/hooks/session.test.ts +0 -134
- package/src/hooks/session.ts +0 -76
- package/src/hooks/setEmail.test.ts +0 -216
- package/src/hooks/setEmail.ts +0 -33
- package/src/hooks/setupAcl.test.ts +0 -618
- package/src/hooks/setupAcl.ts +0 -25
- package/src/index.ts +0 -9
- package/src/schema/Schema.test.ts +0 -482
- package/src/schema/Schema.ts +0 -757
- package/src/schema/defaultResolvers.ts +0 -93
- package/src/schema/index.ts +0 -1
- package/src/schema/resolvers/meResolver.test.ts +0 -62
- package/src/schema/resolvers/meResolver.ts +0 -10
- package/src/schema/resolvers/resetPassword.test.ts +0 -341
- package/src/schema/resolvers/resetPassword.ts +0 -63
- package/src/schema/resolvers/sendEmail.test.ts +0 -118
- package/src/schema/resolvers/sendEmail.ts +0 -21
- package/src/schema/resolvers/sendOtpCode.test.ts +0 -141
- package/src/schema/resolvers/sendOtpCode.ts +0 -52
- package/src/security.test.ts +0 -3434
- package/src/server/defaultSessionHandler.test.ts +0 -62
- package/src/server/defaultSessionHandler.ts +0 -105
- package/src/server/generateCodegen.ts +0 -433
- package/src/server/index.test.ts +0 -532
- package/src/server/index.ts +0 -334
- package/src/server/interface.ts +0 -11
- package/src/server/routes/authHandler.ts +0 -169
- package/src/server/routes/index.ts +0 -39
- package/src/utils/crypto.test.ts +0 -41
- package/src/utils/crypto.ts +0 -105
- package/src/utils/export.ts +0 -11
- package/src/utils/helper.ts +0 -204
- package/src/utils/index.test.ts +0 -11
- package/src/utils/index.ts +0 -189
- package/src/utils/preload.ts +0 -8
- package/src/utils/testHelper.ts +0 -116
- 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,105 +0,0 @@
|
|
|
1
|
-
import type { Wabe, WobeCustomContext } from '.'
|
|
2
|
-
import { timingSafeEqual } from 'node:crypto'
|
|
3
|
-
import { Session } from '../authentication/Session'
|
|
4
|
-
import { getCookieInRequestHeaders } 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
|
-
const headerRootKey = Buffer.from(headers.get('Wabe-Root-Key') || '')
|
|
13
|
-
const rootKey = Buffer.from(wabe.config.rootKey)
|
|
14
|
-
|
|
15
|
-
if (headerRootKey.length === rootKey.length && timingSafeEqual(rootKey, headerRootKey)) {
|
|
16
|
-
ctx.wabe = {
|
|
17
|
-
isRoot: true,
|
|
18
|
-
wabe,
|
|
19
|
-
response: ctx.res,
|
|
20
|
-
isGraphQLCall,
|
|
21
|
-
}
|
|
22
|
-
return
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const getAccessToken = () => {
|
|
26
|
-
if (headers.get('Wabe-Access-Token')) return { accessToken: headers.get('Wabe-Access-Token') }
|
|
27
|
-
|
|
28
|
-
const isCookieSession = !!wabe.config.authentication?.session?.cookieSession
|
|
29
|
-
|
|
30
|
-
if (isCookieSession)
|
|
31
|
-
return {
|
|
32
|
-
accessToken: getCookieInRequestHeaders('accessToken', ctx.request.headers),
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return { accessToken: null }
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const { accessToken } = getAccessToken()
|
|
39
|
-
|
|
40
|
-
if (!accessToken) {
|
|
41
|
-
ctx.wabe = {
|
|
42
|
-
isRoot: false,
|
|
43
|
-
wabe,
|
|
44
|
-
response: ctx.res,
|
|
45
|
-
isGraphQLCall,
|
|
46
|
-
}
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const getCsrfToken = () => {
|
|
51
|
-
if (headers.get('Wabe-Csrf-Token')) return { csrfToken: headers.get('Wabe-Csrf-Token') || '' }
|
|
52
|
-
|
|
53
|
-
return { csrfToken: '' }
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const { csrfToken } = getCsrfToken()
|
|
57
|
-
|
|
58
|
-
const session = new Session()
|
|
59
|
-
|
|
60
|
-
const {
|
|
61
|
-
user,
|
|
62
|
-
sessionId,
|
|
63
|
-
accessToken: newAccessToken,
|
|
64
|
-
refreshToken: newRefreshToken,
|
|
65
|
-
} = await session.meFromAccessToken(
|
|
66
|
-
{ accessToken, csrfToken },
|
|
67
|
-
{
|
|
68
|
-
wabe,
|
|
69
|
-
isRoot: true,
|
|
70
|
-
isGraphQLCall,
|
|
71
|
-
},
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
ctx.wabe = {
|
|
75
|
-
isRoot: false,
|
|
76
|
-
sessionId,
|
|
77
|
-
user,
|
|
78
|
-
wabe,
|
|
79
|
-
response: ctx.res,
|
|
80
|
-
isGraphQLCall,
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (
|
|
84
|
-
wabe.config.authentication?.session?.cookieSession &&
|
|
85
|
-
newAccessToken &&
|
|
86
|
-
newRefreshToken &&
|
|
87
|
-
newAccessToken !== accessToken
|
|
88
|
-
) {
|
|
89
|
-
ctx.res.setCookie('accessToken', newAccessToken, {
|
|
90
|
-
httpOnly: true,
|
|
91
|
-
path: '/',
|
|
92
|
-
expires: session.getAccessTokenExpireAt(wabe.config),
|
|
93
|
-
sameSite: 'None',
|
|
94
|
-
secure: true,
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
ctx.res.setCookie('refreshToken', newRefreshToken, {
|
|
98
|
-
httpOnly: true,
|
|
99
|
-
path: '/',
|
|
100
|
-
expires: session.getRefreshTokenExpireAt(wabe.config),
|
|
101
|
-
sameSite: 'None',
|
|
102
|
-
secure: true,
|
|
103
|
-
})
|
|
104
|
-
}
|
|
105
|
-
}
|
|
@@ -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
|
-
}
|