@@ -13,15 +13,23 @@ import {
13
13
getCountryCode ,
14
14
getCountryCoordinates ,
15
15
} from '../regions.js' ;
16
- import type {
17
- ApplyMigrationOptions ,
18
- CreateBranchOptions ,
19
- CreateProjectOptions ,
20
- DeployEdgeFunctionOptions ,
21
- EdgeFunction ,
22
- ExecuteSqlOptions ,
23
- ResetBranchOptions ,
24
- SupabasePlatform ,
16
+ import {
17
+ applyMigrationOptionsSchema ,
18
+ createBranchOptionsSchema ,
19
+ createProjectOptionsSchema ,
20
+ deployEdgeFunctionOptionsSchema ,
21
+ executeSqlOptionsSchema ,
22
+ getLogsOptionsSchema ,
23
+ resetBranchOptionsSchema ,
24
+ type ApplyMigrationOptions ,
25
+ type CreateBranchOptions ,
26
+ type CreateProjectOptions ,
27
+ type DeployEdgeFunctionOptions ,
28
+ type EdgeFunction ,
29
+ type ExecuteSqlOptions ,
30
+ type GetLogsOptions ,
31
+ type ResetBranchOptions ,
32
+ type SupabasePlatform ,
25
33
} from './types.js' ;
26
34
27
35
const { version } = packageJson ;
@@ -70,6 +78,8 @@ export function createSupabaseCloudPlatform(
70
78
) ;
71
79
} ,
72
80
async executeSql < T > ( projectId : string , options : ExecuteSqlOptions ) {
81
+ const { query, read_only } = executeSqlOptionsSchema . parse ( options ) ;
82
+
73
83
const response = await managementApiClient . POST (
74
84
'/v1/projects/{ref}/database/query' ,
75
85
{
@@ -78,7 +88,10 @@ export function createSupabaseCloudPlatform(
78
88
ref : projectId ,
79
89
} ,
80
90
} ,
81
- body : options ,
91
+ body : {
92
+ query,
93
+ read_only,
94
+ } ,
82
95
}
83
96
) ;
84
97
@@ -103,6 +116,8 @@ export function createSupabaseCloudPlatform(
103
116
return response . data ;
104
117
} ,
105
118
async applyMigration < T > ( projectId : string , options : ApplyMigrationOptions ) {
119
+ const { name, query } = applyMigrationOptionsSchema . parse ( options ) ;
120
+
106
121
const response = await managementApiClient . POST (
107
122
'/v1/projects/{ref}/database/migrations' ,
108
123
{
@@ -111,7 +126,10 @@ export function createSupabaseCloudPlatform(
111
126
ref : projectId ,
112
127
} ,
113
128
} ,
114
- body : options ,
129
+ body : {
130
+ name,
131
+ query,
132
+ } ,
115
133
}
116
134
) ;
117
135
@@ -161,13 +179,16 @@ export function createSupabaseCloudPlatform(
161
179
return response . data ;
162
180
} ,
163
181
async createProject ( options : CreateProjectOptions ) {
182
+ const { name, organization_id, region, db_pass } =
183
+ createProjectOptionsSchema . parse ( options ) ;
184
+
164
185
const response = await managementApiClient . POST ( '/v1/projects' , {
165
186
body : {
166
- name : options . name ,
167
- region : options . region ?? ( await getClosestRegion ( ) ) ,
168
- organization_id : options . organization_id ,
187
+ name,
188
+ region : region ?? ( await getClosestRegion ( ) ) ,
189
+ organization_id,
169
190
db_pass :
170
- options . db_pass ??
191
+ db_pass ??
171
192
generatePassword ( {
172
193
length : 16 ,
173
194
numbers : true ,
@@ -313,20 +334,24 @@ export function createSupabaseCloudPlatform(
313
334
projectId : string ,
314
335
options : DeployEdgeFunctionOptions
315
336
) {
337
+ let {
338
+ name,
339
+ entrypoint_path,
340
+ import_map_path,
341
+ files : inputFiles ,
342
+ } = deployEdgeFunctionOptionsSchema . parse ( options ) ;
343
+
316
344
let existingEdgeFunction : EdgeFunction | undefined ;
317
345
try {
318
- existingEdgeFunction = await platform . getEdgeFunction (
319
- projectId ,
320
- options . name
321
- ) ;
346
+ existingEdgeFunction = await platform . getEdgeFunction ( projectId , name ) ;
322
347
} catch ( error ) { }
323
348
324
- const import_map_file = options . files . find ( ( file ) =>
349
+ const import_map_file = inputFiles . find ( ( file ) =>
325
350
[ 'deno.json' , 'import_map.json' ] . includes ( file . name )
326
351
) ;
327
352
328
353
// Use existing import map path or file name heuristic if not provided
329
- options . import_map_path ??=
354
+ import_map_path ??=
330
355
existingEdgeFunction ?. import_map_path ?? import_map_file ?. name ;
331
356
332
357
const response = await managementApiClient . POST (
@@ -336,15 +361,15 @@ export function createSupabaseCloudPlatform(
336
361
path : {
337
362
ref : projectId ,
338
363
} ,
339
- query : { slug : options . name } ,
364
+ query : { slug : name } ,
340
365
} ,
341
366
body : {
342
367
metadata : {
343
- name : options . name ,
344
- entrypoint_path : options . entrypoint_path ,
345
- import_map_path : options . import_map_path ,
368
+ name,
369
+ entrypoint_path,
370
+ import_map_path,
346
371
} ,
347
- file : options . files as any , // We need to pass file name and content to our serializer
372
+ file : inputFiles as any , // We need to pass file name and content to our serializer
348
373
} ,
349
374
bodySerializer ( body ) {
350
375
const formData = new FormData ( ) ;
@@ -371,14 +396,10 @@ export function createSupabaseCloudPlatform(
371
396
372
397
return response . data ;
373
398
} ,
374
- async getLogs (
375
- projectId : string ,
376
- options : {
377
- isoTimestampStart : string ;
378
- isoTimestampEnd : string ;
379
- sql : string ;
380
- }
381
- ) {
399
+ async getLogs ( projectId : string , options : GetLogsOptions ) {
400
+ const { sql, iso_timestamp_start, iso_timestamp_end } =
401
+ getLogsOptionsSchema . parse ( options ) ;
402
+
382
403
const response = await managementApiClient . GET (
383
404
'/v1/projects/{ref}/analytics/endpoints/logs.all' ,
384
405
{
@@ -387,9 +408,9 @@ export function createSupabaseCloudPlatform(
387
408
ref : projectId ,
388
409
} ,
389
410
query : {
390
- sql : options . sql ,
391
- iso_timestamp_start : options . isoTimestampStart ,
392
- iso_timestamp_end : options . isoTimestampEnd ,
411
+ sql,
412
+ iso_timestamp_start,
413
+ iso_timestamp_end,
393
414
} ,
394
415
} ,
395
416
}
@@ -463,6 +484,8 @@ export function createSupabaseCloudPlatform(
463
484
return response . data ;
464
485
} ,
465
486
async createBranch ( projectId : string , options : CreateBranchOptions ) {
487
+ const { name } = createBranchOptionsSchema . parse ( options ) ;
488
+
466
489
const createBranchResponse = await managementApiClient . POST (
467
490
'/v1/projects/{ref}/branches' ,
468
491
{
@@ -472,7 +495,7 @@ export function createSupabaseCloudPlatform(
472
495
} ,
473
496
} ,
474
497
body : {
475
- branch_name : options . name ,
498
+ branch_name : name ,
476
499
} ,
477
500
}
478
501
) ;
@@ -502,7 +525,7 @@ export function createSupabaseCloudPlatform(
502
525
} ,
503
526
} ,
504
527
body : {
505
- branch_name : options . name ,
528
+ branch_name : name ,
506
529
} ,
507
530
}
508
531
) ;
@@ -544,6 +567,8 @@ export function createSupabaseCloudPlatform(
544
567
assertSuccess ( response , 'Failed to merge branch' ) ;
545
568
} ,
546
569
async resetBranch ( branchId : string , options : ResetBranchOptions ) {
570
+ const { migration_version } = resetBranchOptionsSchema . parse ( options ) ;
571
+
547
572
const response = await managementApiClient . POST (
548
573
'/v1/branches/{branch_id}/reset' ,
549
574
{
@@ -552,7 +577,9 @@ export function createSupabaseCloudPlatform(
552
577
branch_id : branchId ,
553
578
} ,
554
579
} ,
555
- body : options ,
580
+ body : {
581
+ migration_version,
582
+ } ,
556
583
}
557
584
) ;
558
585
0 commit comments