slates 0.0.1 → 1.0.0-rc.1

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 (34) hide show
  1. package/example/google-sheets/auth.ts +317 -0
  2. package/example/google-sheets/config.ts +9 -0
  3. package/example/google-sheets/docs/abc.md +1 -0
  4. package/example/google-sheets/docs/readme.md +1 -0
  5. package/example/google-sheets/index.ts +42 -0
  6. package/example/google-sheets/lib/client.ts +717 -0
  7. package/example/google-sheets/lib/types.ts +1674 -0
  8. package/example/google-sheets/readme.md +1 -0
  9. package/example/google-sheets/slate.json +4 -0
  10. package/example/google-sheets/spec.ts +13 -0
  11. package/example/google-sheets/tools/append-values.ts +74 -0
  12. package/example/google-sheets/tools/clear-values.ts +45 -0
  13. package/example/google-sheets/tools/copy-paste.ts +99 -0
  14. package/example/google-sheets/tools/create-spreadsheet.ts +72 -0
  15. package/example/google-sheets/tools/find-replace.ts +68 -0
  16. package/example/google-sheets/tools/format-cells.ts +137 -0
  17. package/example/google-sheets/tools/get-spreadsheet.ts +108 -0
  18. package/example/google-sheets/tools/index.ts +15 -0
  19. package/example/google-sheets/tools/manage-rows-columns.ts +160 -0
  20. package/example/google-sheets/tools/manage-sheets.ts +183 -0
  21. package/example/google-sheets/tools/merge-cells.ts +76 -0
  22. package/example/google-sheets/tools/named-ranges.ts +82 -0
  23. package/example/google-sheets/tools/protect-range.ts +100 -0
  24. package/example/google-sheets/tools/read-values.ts +94 -0
  25. package/example/google-sheets/tools/sort-range.ts +78 -0
  26. package/example/google-sheets/tools/write-values.ts +91 -0
  27. package/example/google-sheets/triggers/index.ts +1 -0
  28. package/example/google-sheets/triggers/spreadsheet-changed.ts +108 -0
  29. package/example/out.zip.b64 +1 -0
  30. package/package.json +30 -20
  31. package/src/index.ts +1 -0
  32. package/tsconfig.json +8 -0
  33. package/bin/slates.js +0 -8
  34. package/readme.md +0 -5
@@ -0,0 +1,317 @@
1
+ import { SlateAuth, createAxios } from 'slates';
2
+ import { z } from 'zod';
3
+
4
+ let axios = createAxios({
5
+ baseURL: 'https://oauth2.googleapis.com'
6
+ });
7
+
8
+ export let auth = SlateAuth.create()
9
+ .output(
10
+ z.object({
11
+ token: z.string(),
12
+ refreshToken: z.string().optional(),
13
+ expiresAt: z.date().optional()
14
+ })
15
+ )
16
+ .addOauth({
17
+ type: 'auth.oauth',
18
+ name: 'Google OAuth2',
19
+ key: 'google_oauth2',
20
+
21
+ scopes: [
22
+ {
23
+ title: 'Google Sheets - Full Access',
24
+ description: 'Full read and write access to Google Sheets',
25
+ scope: 'https://www.googleapis.com/auth/spreadsheets'
26
+ },
27
+ {
28
+ title: 'Google Drive - File Access',
29
+ description: 'Access to files created or opened by the app',
30
+ scope: 'https://www.googleapis.com/auth/drive.file'
31
+ }
32
+ ],
33
+
34
+ inputSchema: z.object({}),
35
+
36
+ getAuthorizationUrl: async ctx => {
37
+ let params = new URLSearchParams({
38
+ client_id: ctx.clientId,
39
+ redirect_uri: ctx.redirectUri,
40
+ response_type: 'code',
41
+ scope: ctx.scopes.join(' '),
42
+ state: ctx.state,
43
+ access_type: 'offline',
44
+ prompt: 'consent'
45
+ });
46
+
47
+ return {
48
+ url: `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`,
49
+ input: ctx.input
50
+ };
51
+ },
52
+
53
+ handleCallback: async ctx => {
54
+ let response = await axios.post(
55
+ '/token',
56
+ new URLSearchParams({
57
+ client_id: ctx.clientId,
58
+ client_secret: ctx.clientSecret,
59
+ code: ctx.code,
60
+ grant_type: 'authorization_code',
61
+ redirect_uri: ctx.redirectUri
62
+ }).toString(),
63
+ {
64
+ headers: {
65
+ 'Content-Type': 'application/x-www-form-urlencoded'
66
+ }
67
+ }
68
+ );
69
+
70
+ let data = response.data as {
71
+ access_token: string;
72
+ refresh_token?: string;
73
+ expires_in?: number;
74
+ };
75
+
76
+ return {
77
+ output: {
78
+ token: data.access_token,
79
+ refreshToken: data.refresh_token,
80
+ expiresAt: data.expires_in
81
+ ? new Date(Date.now() + data.expires_in * 1000)
82
+ : undefined
83
+ },
84
+ input: ctx.input
85
+ };
86
+ },
87
+
88
+ handleTokenRefresh: async ctx => {
89
+ if (!ctx.output.refreshToken) {
90
+ throw new Error('No refresh token available');
91
+ }
92
+
93
+ let response = await axios.post(
94
+ '/token',
95
+ new URLSearchParams({
96
+ client_id: ctx.clientId,
97
+ client_secret: ctx.clientSecret,
98
+ refresh_token: ctx.output.refreshToken,
99
+ grant_type: 'refresh_token'
100
+ }).toString(),
101
+ {
102
+ headers: {
103
+ 'Content-Type': 'application/x-www-form-urlencoded'
104
+ }
105
+ }
106
+ );
107
+
108
+ let data = response.data as {
109
+ access_token: string;
110
+ refresh_token?: string;
111
+ expires_in?: number;
112
+ };
113
+
114
+ return {
115
+ output: {
116
+ token: data.access_token,
117
+ refreshToken: data.refresh_token || ctx.output.refreshToken,
118
+ expiresAt: data.expires_in
119
+ ? new Date(Date.now() + data.expires_in * 1000)
120
+ : undefined
121
+ },
122
+ input: ctx.input
123
+ };
124
+ },
125
+
126
+ getProfile: async (ctx: { output: { token: string }; input: Record<string, never> }) => {
127
+ let profileAxios = createAxios({
128
+ baseURL: 'https://www.googleapis.com'
129
+ });
130
+
131
+ let response = await profileAxios.get('/oauth2/v2/userinfo', {
132
+ headers: {
133
+ Authorization: `Bearer ${ctx.output.token}`
134
+ }
135
+ });
136
+
137
+ let data = response.data as {
138
+ id?: string;
139
+ email?: string;
140
+ name?: string;
141
+ picture?: string;
142
+ };
143
+
144
+ return {
145
+ profile: {
146
+ id: data.id,
147
+ email: data.email,
148
+ name: data.name,
149
+ imageUrl: data.picture
150
+ }
151
+ };
152
+ }
153
+ })
154
+ .addServiceAccountAuth({
155
+ type: 'auth.service_account',
156
+ name: 'Service Account',
157
+ key: 'service_account',
158
+
159
+ inputSchema: z.object({
160
+ serviceAccountJson: z.string().describe('JSON key file contents for the service account')
161
+ }),
162
+
163
+ getOutput: async ctx => {
164
+ let serviceAccount: {
165
+ client_email: string;
166
+ private_key: string;
167
+ token_uri?: string;
168
+ };
169
+
170
+ try {
171
+ serviceAccount = JSON.parse(ctx.input.serviceAccountJson);
172
+ } catch {
173
+ throw new Error('Invalid service account JSON');
174
+ }
175
+
176
+ if (!serviceAccount.client_email || !serviceAccount.private_key) {
177
+ throw new Error('Service account JSON must contain client_email and private_key');
178
+ }
179
+
180
+ let token = await generateServiceAccountToken(serviceAccount, [
181
+ 'https://www.googleapis.com/auth/spreadsheets',
182
+ 'https://www.googleapis.com/auth/drive.file'
183
+ ]);
184
+
185
+ return {
186
+ output: {
187
+ token: token.accessToken,
188
+ expiresAt: token.expiresAt
189
+ }
190
+ };
191
+ },
192
+
193
+ getProfile: async (ctx: {
194
+ output: { token: string };
195
+ input: { serviceAccountJson: string };
196
+ }) => {
197
+ let serviceAccount: { client_email: string };
198
+
199
+ try {
200
+ serviceAccount = JSON.parse(ctx.input.serviceAccountJson);
201
+ } catch {
202
+ throw new Error('Invalid service account JSON');
203
+ }
204
+
205
+ return {
206
+ profile: {
207
+ email: serviceAccount.client_email,
208
+ name: 'Service Account'
209
+ }
210
+ };
211
+ }
212
+ })
213
+ .addTokenAuth({
214
+ type: 'auth.token',
215
+ name: 'API Key',
216
+ key: 'api_key',
217
+
218
+ inputSchema: z.object({
219
+ token: z
220
+ .string()
221
+ .describe('Google Cloud API Key (read-only access to public spreadsheets only)')
222
+ }),
223
+
224
+ getOutput: async ctx => {
225
+ return {
226
+ output: {
227
+ token: ctx.input.token
228
+ }
229
+ };
230
+ }
231
+ });
232
+
233
+ let generateServiceAccountToken = async (
234
+ serviceAccount: {
235
+ client_email: string;
236
+ private_key: string;
237
+ token_uri?: string;
238
+ },
239
+ scopes: string[]
240
+ ): Promise<{ accessToken: string; expiresAt: Date }> => {
241
+ let now = Math.floor(Date.now() / 1000);
242
+ let exp = now + 3600;
243
+
244
+ let header = {
245
+ alg: 'RS256',
246
+ typ: 'JWT'
247
+ };
248
+
249
+ let payload = {
250
+ iss: serviceAccount.client_email,
251
+ scope: scopes.join(' '),
252
+ aud: 'https://oauth2.googleapis.com/token',
253
+ iat: now,
254
+ exp: exp
255
+ };
256
+
257
+ let jwt = await signJwt(header, payload, serviceAccount.private_key);
258
+
259
+ let response = await axios.post(
260
+ '/token',
261
+ new URLSearchParams({
262
+ grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
263
+ assertion: jwt
264
+ }).toString(),
265
+ {
266
+ headers: {
267
+ 'Content-Type': 'application/x-www-form-urlencoded'
268
+ }
269
+ }
270
+ );
271
+
272
+ let data = response.data as {
273
+ access_token: string;
274
+ expires_in: number;
275
+ };
276
+
277
+ return {
278
+ accessToken: data.access_token,
279
+ expiresAt: new Date(Date.now() + data.expires_in * 1000)
280
+ };
281
+ };
282
+
283
+ let signJwt = async (header: object, payload: object, privateKey: string): Promise<string> => {
284
+ let base64UrlEncode = (data: Uint8Array): string => {
285
+ let base64 = btoa(String.fromCharCode(...data));
286
+ return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
287
+ };
288
+
289
+ let textEncoder = new TextEncoder();
290
+ let headerB64 = base64UrlEncode(textEncoder.encode(JSON.stringify(header)));
291
+ let payloadB64 = base64UrlEncode(textEncoder.encode(JSON.stringify(payload)));
292
+ let signingInput = `${headerB64}.${payloadB64}`;
293
+
294
+ let pemContents = privateKey
295
+ .replace(/-----BEGIN PRIVATE KEY-----/g, '')
296
+ .replace(/-----END PRIVATE KEY-----/g, '')
297
+ .replace(/\s/g, '');
298
+ let binaryKey = Uint8Array.from(atob(pemContents), c => c.charCodeAt(0));
299
+
300
+ let cryptoKey = await crypto.subtle.importKey(
301
+ 'pkcs8',
302
+ binaryKey,
303
+ { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' },
304
+ false,
305
+ ['sign']
306
+ );
307
+
308
+ let signature = await crypto.subtle.sign(
309
+ 'RSASSA-PKCS1-v1_5',
310
+ cryptoKey,
311
+ textEncoder.encode(signingInput)
312
+ );
313
+
314
+ let signatureB64 = base64UrlEncode(new Uint8Array(signature));
315
+
316
+ return `${signingInput}.${signatureB64}`;
317
+ };
@@ -0,0 +1,9 @@
1
+ import { SlateConfig } from 'slates';
2
+ import { z } from 'zod';
3
+
4
+ export let config = SlateConfig.create(
5
+ z.object({
6
+ // No global configuration needed for Google Sheets
7
+ // All necessary values are handled through authentication
8
+ })
9
+ );
@@ -0,0 +1 @@
1
+ ABC
@@ -0,0 +1 @@
1
+ please do read me
@@ -0,0 +1,42 @@
1
+ import { Slate } from 'slates';
2
+ import { spec } from './spec';
3
+ import {
4
+ appendValuesTool,
5
+ clearValuesTool,
6
+ copyPasteTool,
7
+ createSpreadsheetTool,
8
+ findReplaceTool,
9
+ formatCellsTool,
10
+ getSpreadsheetTool,
11
+ manageRowsColumnsTool,
12
+ manageSheetsTool,
13
+ mergeCellsTool,
14
+ namedRangesTool,
15
+ protectRangeTool,
16
+ readValuesTool,
17
+ sortRangeTool,
18
+ writeValuesTool
19
+ } from './tools';
20
+ import { spreadsheetChangedTrigger } from './triggers';
21
+
22
+ export let provider = Slate.create({
23
+ spec,
24
+ tools: [
25
+ createSpreadsheetTool,
26
+ getSpreadsheetTool,
27
+ readValuesTool,
28
+ writeValuesTool,
29
+ appendValuesTool,
30
+ clearValuesTool,
31
+ manageSheetsTool,
32
+ formatCellsTool,
33
+ findReplaceTool,
34
+ manageRowsColumnsTool,
35
+ sortRangeTool,
36
+ mergeCellsTool,
37
+ protectRangeTool,
38
+ namedRangesTool,
39
+ copyPasteTool
40
+ ],
41
+ triggers: [spreadsheetChangedTrigger]
42
+ });