@@ -4,6 +4,7 @@ import type { Client } from "@hey-api/client-fetch";
4
4
import type { VmStartResponse , tier } from "./client" ;
5
5
import {
6
6
sandboxFork ,
7
+ sandboxList ,
7
8
vmHibernate ,
8
9
vmShutdown ,
9
10
vmStart ,
@@ -16,6 +17,24 @@ import { handleResponse } from "./utils/handle-response";
16
17
export type SandboxPrivacy = "public" | "unlisted" | "private" ;
17
18
export type SandboxStartData = Required < VmStartResponse > [ "data" ] ;
18
19
20
+ export type SandboxInfo = {
21
+ id : string ;
22
+ createdAt : Date ;
23
+ updatedAt : Date ;
24
+ title ?: string ;
25
+ description ?: string ;
26
+ privacy : SandboxPrivacy ;
27
+ tags : string [ ] ;
28
+ } ;
29
+
30
+ export type SandboxListOpts = {
31
+ tags ?: string [ ] ;
32
+ page ?: number ;
33
+ pageSize ?: number ;
34
+ orderBy ?: "inserted_at" | "updated_at" ;
35
+ direction ?: "asc" | "desc" ;
36
+ } ;
37
+
19
38
export const DEFAULT_SUBSCRIPTIONS = {
20
39
client : {
21
40
status : true ,
@@ -352,6 +371,35 @@ export class SandboxClient {
352
371
handleResponse ( response , `Failed to hibernate sandbox ${ id } ` ) ;
353
372
}
354
373
374
+ /**
375
+ * List sandboxes from the current workspace with optional filters.
376
+ * Results are limited to a maximum of 50 sandboxes per request.
377
+ */
378
+ async list ( opts : SandboxListOpts = { } ) : Promise < SandboxInfo [ ] > {
379
+ const response = await sandboxList ( {
380
+ client : this . apiClient ,
381
+ query : {
382
+ tags : opts . tags ?. join ( "," ) ,
383
+ page : opts . page ,
384
+ page_size : opts . pageSize ,
385
+ order_by : opts . orderBy ,
386
+ direction : opts . direction ,
387
+ } ,
388
+ } ) ;
389
+
390
+ const info = handleResponse ( response , "Failed to list sandboxes" ) ;
391
+
392
+ return info . sandboxes . map ( ( sandbox ) => ( {
393
+ id : sandbox . id ,
394
+ createdAt : new Date ( sandbox . created_at ) ,
395
+ updatedAt : new Date ( sandbox . updated_at ) ,
396
+ title : sandbox . title ?? undefined ,
397
+ description : sandbox . description ?? undefined ,
398
+ privacy : privacyFromNumber ( sandbox . privacy ) ,
399
+ tags : sandbox . tags ,
400
+ } ) ) ;
401
+ }
402
+
355
403
/**
356
404
* Updates the specs that this sandbox runs on. It will dynamically scale the sandbox to the
357
405
* new specs without a reboot. Be careful when scaling specs down, if the VM is using more memory
@@ -467,3 +515,16 @@ function privacyToNumber(privacy: SandboxPrivacy): number {
467
515
return 2 ;
468
516
}
469
517
}
518
+
519
+ function privacyFromNumber ( privacy : number ) : SandboxPrivacy {
520
+ switch ( privacy ) {
521
+ case 0 :
522
+ return "public" ;
523
+ case 1 :
524
+ return "unlisted" ;
525
+ case 2 :
526
+ return "private" ;
527
+ }
528
+
529
+ throw new Error ( `Invalid privacy number: ${ privacy } ` ) ;
530
+ }
0 commit comments