1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
import { z } from "zod"
import { ConnectionProviderEnum } from "./schemas"
export const providers = ConnectionProviderEnum
export type Provider = z.infer<typeof providers>
const BaseMetadataSchema = <T extends z.ZodTypeAny>(provider: T) =>
z.object({
provider,
})
export const NotionMetadataSchema = BaseMetadataSchema(
z.literal("notion"),
).extend({
email: z.string().email().optional(),
ownerId: z.string(),
workspaceIcon: z.string().optional(),
workspaceId: z.string(),
workspaceName: z.string(),
})
export type NotionMetadata = z.infer<typeof NotionMetadataSchema>
export const GoogleDriveMetadataSchema = BaseMetadataSchema(
z.literal("google-drive"),
).extend({
pageToken: z.number(),
webhookChannelId: z.string().optional(),
webhookExpiration: z.number().optional(),
webhookResourceId: z.string().optional(),
})
export type GoogleDriveMetadata = z.infer<typeof GoogleDriveMetadataSchema>
export const OneDriveMetadataSchema = BaseMetadataSchema(
z.literal("onedrive"),
).extend({
deltaLink: z.string().optional(),
lastRenewalCheck: z.number().optional(),
webhookClientState: z.string().optional(),
webhookExpiration: z.number().optional(),
webhookSubscriptionId: z.string().optional(),
})
export type OneDriveMetadata = z.infer<typeof OneDriveMetadataSchema>
export const ConnectionMetadataSchema = z.discriminatedUnion("provider", [
NotionMetadataSchema,
GoogleDriveMetadataSchema,
OneDriveMetadataSchema,
])
export type ConnectionMetadata<T extends Provider> = T extends "notion"
? NotionMetadata
: T extends "google-drive"
? GoogleDriveMetadata
: T extends "onedrive"
? OneDriveMetadata
: never
export function isNotionMetadata(
metadata: unknown,
): metadata is NotionMetadata {
return NotionMetadataSchema.safeParse(metadata).success
}
export function isGoogleDriveMetadata(
metadata: unknown,
): metadata is GoogleDriveMetadata {
return GoogleDriveMetadataSchema.safeParse(metadata).success
}
export function isOneDriveMetadata(
metadata: unknown,
): metadata is OneDriveMetadata {
return OneDriveMetadataSchema.safeParse(metadata).success
}
export const ConnectionStateSchema = z.object({
createdAt: z.number(),
org: z.string(),
provider: providers,
userId: z.string(),
})
export type ConnectionState = z.infer<typeof ConnectionStateSchema>
export const TokenDataSchema = z.object({
// Only used for Notion connections since they don't support refresh tokens
accessToken: z.string().optional(),
createdAt: z.number(),
documentLimit: z.number().optional(),
email: z.string().email().optional(),
expiresAt: z.number().optional(),
metadata: ConnectionMetadataSchema.optional(),
refreshToken: z.string().optional(),
userId: z.string().optional(),
})
export type TokenData = z.infer<typeof TokenDataSchema>
export const NotionTokenResponseSchema = z.object({
access_token: z.string(),
bot_id: z.string().optional(),
duplicated_template_id: z.string().nullable().optional(),
owner: z.union([
z.object({
type: z.literal("user"),
user: z.object({
avatar_url: z.string().url().optional(),
id: z.string(),
name: z.string().optional(),
object: z.literal("user"),
person: z
.object({
email: z.string().email().optional(),
})
.optional(),
type: z.literal("person"),
}),
}),
z.object({
type: z.literal("workspace"),
workspace: z.literal(true),
}),
]),
request_id: z.string().optional(),
token_type: z.literal("bearer"),
workspace_icon: z.string().optional(),
workspace_id: z.string(),
workspace_name: z.string(),
})
export type NotionTokenResponse = z.infer<typeof NotionTokenResponseSchema>
export const GoogleDriveTokenResponseSchema = z.object({
access_token: z.string(),
expires_in: z.number(),
refresh_token: z.string().optional(),
scope: z.string(),
token_type: z.literal("Bearer"),
})
export type GoogleDriveTokenResponse = z.infer<
typeof GoogleDriveTokenResponseSchema
>
export const OneDriveTokenResponseSchema = z.object({
access_token: z.string(),
expires_in: z.number(),
refresh_token: z.string().optional(),
scope: z.string(),
token_type: z.literal("Bearer"),
})
export type OneDriveTokenResponse = z.infer<typeof OneDriveTokenResponseSchema>
export const NotionConfigSchema = z.object({
clientId: z.string(),
clientSecret: z.string(),
endpoints: z.object({
authorize: z.string().url(),
token: z.string().url(),
}),
scopes: z.array(z.string()),
})
export type NotionConfig = z.infer<typeof NotionConfigSchema>
export const ConnectionQuerySchema = z.object({
id: z.string(),
redirectUrl: z.string().optional(),
})
export const GoogleDrivePageTokenResponseSchema = z.object({
startPageToken: z.union([z.string(), z.number()]),
})
export const GoogleDriveWatchResponseSchema = z.object({
expiration: z.string(),
id: z.string(),
resourceId: z.string(),
})
export const OneDriveSubscriptionResponseSchema = z.object({
changeType: z.string(),
clientState: z.string(),
expirationDateTime: z.string(),
id: z.string(),
notificationUrl: z.string(),
resource: z.string(),
})
export const GoogleUserInfoResponseSchema = z.object({
email: z.string().email(),
})
export const MicrosoftUserInfoResponseSchema = z.object({
mail: z.string().optional(),
userPrincipalName: z.string().optional(),
})
|