@@ -13,15 +13,23 @@ import {
1313 getCountryCode ,
1414 getCountryCoordinates ,
1515} 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 ,
2533} from './types.js' ;
2634
2735const { version } = packageJson ;
@@ -70,6 +78,8 @@ export function createSupabaseCloudPlatform(
7078 ) ;
7179 } ,
7280 async executeSql < T > ( projectId : string , options : ExecuteSqlOptions ) {
81+ const { query, read_only } = executeSqlOptionsSchema . parse ( options ) ;
82+
7383 const response = await managementApiClient . POST (
7484 '/v1/projects/{ref}/database/query' ,
7585 {
@@ -78,7 +88,10 @@ export function createSupabaseCloudPlatform(
7888 ref : projectId ,
7989 } ,
8090 } ,
81- body : options ,
91+ body : {
92+ query,
93+ read_only,
94+ } ,
8295 }
8396 ) ;
8497
@@ -103,6 +116,8 @@ export function createSupabaseCloudPlatform(
103116 return response . data ;
104117 } ,
105118 async applyMigration < T > ( projectId : string , options : ApplyMigrationOptions ) {
119+ const { name, query } = applyMigrationOptionsSchema . parse ( options ) ;
120+
106121 const response = await managementApiClient . POST (
107122 '/v1/projects/{ref}/database/migrations' ,
108123 {
@@ -111,7 +126,10 @@ export function createSupabaseCloudPlatform(
111126 ref : projectId ,
112127 } ,
113128 } ,
114- body : options ,
129+ body : {
130+ name,
131+ query,
132+ } ,
115133 }
116134 ) ;
117135
@@ -161,13 +179,16 @@ export function createSupabaseCloudPlatform(
161179 return response . data ;
162180 } ,
163181 async createProject ( options : CreateProjectOptions ) {
182+ const { name, organization_id, region, db_pass } =
183+ createProjectOptionsSchema . parse ( options ) ;
184+
164185 const response = await managementApiClient . POST ( '/v1/projects' , {
165186 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,
169190 db_pass :
170- options . db_pass ??
191+ db_pass ??
171192 generatePassword ( {
172193 length : 16 ,
173194 numbers : true ,
@@ -313,20 +334,24 @@ export function createSupabaseCloudPlatform(
313334 projectId : string ,
314335 options : DeployEdgeFunctionOptions
315336 ) {
337+ let {
338+ name,
339+ entrypoint_path,
340+ import_map_path,
341+ files : inputFiles ,
342+ } = deployEdgeFunctionOptionsSchema . parse ( options ) ;
343+
316344 let existingEdgeFunction : EdgeFunction | undefined ;
317345 try {
318- existingEdgeFunction = await platform . getEdgeFunction (
319- projectId ,
320- options . name
321- ) ;
346+ existingEdgeFunction = await platform . getEdgeFunction ( projectId , name ) ;
322347 } catch ( error ) { }
323348
324- const import_map_file = options . files . find ( ( file ) =>
349+ const import_map_file = inputFiles . find ( ( file ) =>
325350 [ 'deno.json' , 'import_map.json' ] . includes ( file . name )
326351 ) ;
327352
328353 // Use existing import map path or file name heuristic if not provided
329- options . import_map_path ??=
354+ import_map_path ??=
330355 existingEdgeFunction ?. import_map_path ?? import_map_file ?. name ;
331356
332357 const response = await managementApiClient . POST (
@@ -336,15 +361,15 @@ export function createSupabaseCloudPlatform(
336361 path : {
337362 ref : projectId ,
338363 } ,
339- query : { slug : options . name } ,
364+ query : { slug : name } ,
340365 } ,
341366 body : {
342367 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,
346371 } ,
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
348373 } ,
349374 bodySerializer ( body ) {
350375 const formData = new FormData ( ) ;
@@ -371,14 +396,10 @@ export function createSupabaseCloudPlatform(
371396
372397 return response . data ;
373398 } ,
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+
382403 const response = await managementApiClient . GET (
383404 '/v1/projects/{ref}/analytics/endpoints/logs.all' ,
384405 {
@@ -387,9 +408,9 @@ export function createSupabaseCloudPlatform(
387408 ref : projectId ,
388409 } ,
389410 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,
393414 } ,
394415 } ,
395416 }
@@ -463,6 +484,8 @@ export function createSupabaseCloudPlatform(
463484 return response . data ;
464485 } ,
465486 async createBranch ( projectId : string , options : CreateBranchOptions ) {
487+ const { name } = createBranchOptionsSchema . parse ( options ) ;
488+
466489 const createBranchResponse = await managementApiClient . POST (
467490 '/v1/projects/{ref}/branches' ,
468491 {
@@ -472,7 +495,7 @@ export function createSupabaseCloudPlatform(
472495 } ,
473496 } ,
474497 body : {
475- branch_name : options . name ,
498+ branch_name : name ,
476499 } ,
477500 }
478501 ) ;
@@ -502,7 +525,7 @@ export function createSupabaseCloudPlatform(
502525 } ,
503526 } ,
504527 body : {
505- branch_name : options . name ,
528+ branch_name : name ,
506529 } ,
507530 }
508531 ) ;
@@ -544,6 +567,8 @@ export function createSupabaseCloudPlatform(
544567 assertSuccess ( response , 'Failed to merge branch' ) ;
545568 } ,
546569 async resetBranch ( branchId : string , options : ResetBranchOptions ) {
570+ const { migration_version } = resetBranchOptionsSchema . parse ( options ) ;
571+
547572 const response = await managementApiClient . POST (
548573 '/v1/branches/{branch_id}/reset' ,
549574 {
@@ -552,7 +577,9 @@ export function createSupabaseCloudPlatform(
552577 branch_id : branchId ,
553578 } ,
554579 } ,
555- body : options ,
580+ body : {
581+ migration_version,
582+ } ,
556583 }
557584 ) ;
558585
0 commit comments