Skip to content

Commit 0937011

Browse files
committed
fix(js-sdk): forward name param in createSnapshot and return names
createSnapshot() sends an empty body, ignoring the name parameter from the API. The SnapshotInfo interface also omits the names array from the API response. This means SDK users cannot create named snapshots. - Add CreateSnapshotOpts with optional name field - Forward name to POST /sandboxes/{sandboxID}/snapshots body - Add names to SnapshotInfo interface - Return names from both createSnapshot and list paginator - Export CreateSnapshotOpts from package index Fixes #1249
1 parent fcb95c3 commit 0937011

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

packages/js-sdk/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export type {
6161
SandboxLifecycle,
6262
SandboxInfoLifecycle,
6363
SnapshotInfo,
64+
CreateSnapshotOpts,
6465
SnapshotListOpts,
6566
SnapshotPaginator,
6667
} from './sandbox/sandboxApi'

packages/js-sdk/src/sandbox/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
SandboxPaginator,
2222
SandboxBetaCreateOpts,
2323
SandboxApiOpts,
24+
CreateSnapshotOpts,
2425
SnapshotListOpts,
2526
SnapshotInfo,
2627
SnapshotPaginator,
@@ -614,17 +615,17 @@ export class Sandbox extends SandboxApi {
614615
* const sandbox = await Sandbox.create()
615616
* await sandbox.files.write('/app/state.json', '{"step": 1}')
616617
*
617-
* // Create a snapshot
618-
* const snapshot = await sandbox.createSnapshot()
618+
* // Create a named snapshot
619+
* const snapshot = await sandbox.createSnapshot({ name: 'my-snapshot' })
619620
*
620621
* // Create a new sandbox from the snapshot
621622
* const newSandbox = await Sandbox.create(snapshot.snapshotId)
622623
* ```
623624
*/
624-
async createSnapshot(opts?: SandboxApiOpts): Promise<SnapshotInfo> {
625+
async createSnapshot(opts?: CreateSnapshotOpts): Promise<SnapshotInfo> {
625626
return await SandboxApi.createSnapshot(
626627
this.sandboxId,
627-
this.resolveApiOpts(opts)
628+
{ ...this.resolveApiOpts(opts), ...opts }
628629
)
629630
}
630631

packages/js-sdk/src/sandbox/sandboxApi.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ export interface SandboxMetricsOpts extends SandboxApiOpts {
248248
/**
249249
* Options for listing snapshots.
250250
*/
251+
/**
252+
* Options for creating a snapshot.
253+
*/
254+
export interface CreateSnapshotOpts extends SandboxApiOpts {
255+
/**
256+
* Optional name for the snapshot template.
257+
* If a snapshot template with this name already exists,
258+
* a new build will be assigned to the existing template.
259+
*/
260+
name?: string
261+
}
262+
251263
export interface SnapshotListOpts extends SandboxApiOpts {
252264
/**
253265
* Filter snapshots by source sandbox ID.
@@ -276,6 +288,11 @@ export interface SnapshotInfo {
276288
* Can be used with Sandbox.create() to create a new sandbox from this snapshot.
277289
*/
278290
snapshotId: string
291+
/**
292+
* Full names of the snapshot template including team namespace and tag
293+
* (e.g. team-slug/my-snapshot:v2).
294+
*/
295+
names: string[]
279296
}
280297

281298
/**
@@ -687,7 +704,7 @@ export class SandboxApi {
687704
*/
688705
static async createSnapshot(
689706
sandboxId: string,
690-
opts?: SandboxApiOpts
707+
opts?: CreateSnapshotOpts
691708
): Promise<SnapshotInfo> {
692709
const config = new ConnectionConfig(opts)
693710
const client = new ApiClient(config)
@@ -698,7 +715,9 @@ export class SandboxApi {
698715
sandboxID: sandboxId,
699716
},
700717
},
701-
body: {},
718+
body: {
719+
...(opts?.name && { name: opts.name }),
720+
},
702721
signal: config.getSignal(opts?.requestTimeoutMs),
703722
})
704723

@@ -713,6 +732,7 @@ export class SandboxApi {
713732

714733
return {
715734
snapshotId: res.data!.snapshotID,
735+
names: res.data!.names ?? [],
716736
}
717737
}
718738

@@ -1038,6 +1058,7 @@ export class SnapshotPaginator extends BasePaginator<SnapshotInfo> {
10381058
return (res.data ?? []).map(
10391059
(snapshot: components['schemas']['SnapshotInfo']) => ({
10401060
snapshotId: snapshot.snapshotID,
1061+
names: snapshot.names ?? [],
10411062
})
10421063
)
10431064
}

0 commit comments

Comments
 (0)