Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/js-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type {
SandboxLifecycle,
SandboxInfoLifecycle,
SnapshotInfo,
CreateSnapshotOpts,
SnapshotListOpts,
SnapshotPaginator,
} from './sandbox/sandboxApi'
Expand Down
9 changes: 5 additions & 4 deletions packages/js-sdk/src/sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
SandboxPaginator,
SandboxBetaCreateOpts,
SandboxApiOpts,
CreateSnapshotOpts,
SnapshotListOpts,
SnapshotInfo,
SnapshotPaginator,
Expand Down Expand Up @@ -614,17 +615,17 @@ export class Sandbox extends SandboxApi {
* const sandbox = await Sandbox.create()
* await sandbox.files.write('/app/state.json', '{"step": 1}')
*
* // Create a snapshot
* const snapshot = await sandbox.createSnapshot()
* // Create a named snapshot
* const snapshot = await sandbox.createSnapshot({ name: 'my-snapshot' })
*
* // Create a new sandbox from the snapshot
* const newSandbox = await Sandbox.create(snapshot.snapshotId)
* ```
*/
async createSnapshot(opts?: SandboxApiOpts): Promise<SnapshotInfo> {
async createSnapshot(opts?: CreateSnapshotOpts): Promise<SnapshotInfo> {
return await SandboxApi.createSnapshot(
this.sandboxId,
this.resolveApiOpts(opts)
{ ...this.resolveApiOpts(opts), ...opts }
)
}

Expand Down
25 changes: 23 additions & 2 deletions packages/js-sdk/src/sandbox/sandboxApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ export interface SandboxMetricsOpts extends SandboxApiOpts {
/**
* Options for listing snapshots.
*/
/**
* Options for creating a snapshot.
*/
export interface CreateSnapshotOpts extends SandboxApiOpts {
/**
* Optional name for the snapshot template.
* If a snapshot template with this name already exists,
* a new build will be assigned to the existing template.
*/
name?: string
}

export interface SnapshotListOpts extends SandboxApiOpts {
/**
* Filter snapshots by source sandbox ID.
Expand Down Expand Up @@ -276,6 +288,11 @@ export interface SnapshotInfo {
* Can be used with Sandbox.create() to create a new sandbox from this snapshot.
*/
snapshotId: string
/**
* Full names of the snapshot template including team namespace and tag
* (e.g. team-slug/my-snapshot:v2).
*/
names: string[]
}

/**
Expand Down Expand Up @@ -687,7 +704,7 @@ export class SandboxApi {
*/
static async createSnapshot(
sandboxId: string,
opts?: SandboxApiOpts
opts?: CreateSnapshotOpts
): Promise<SnapshotInfo> {
const config = new ConnectionConfig(opts)
const client = new ApiClient(config)
Expand All @@ -698,7 +715,9 @@ export class SandboxApi {
sandboxID: sandboxId,
},
},
body: {},
body: {
...(opts?.name && { name: opts.name }),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Forward empty snapshot names to API

The request body currently uses a truthy guard (opts?.name && { name: opts.name }), which drops name when it is an empty string. In that case createSnapshot({ name: '' }) is silently converted into an unnamed snapshot request instead of preserving caller intent and letting the API validate/reject the value, which can create unexpected anonymous snapshots when names come from user input. Check explicitly for undefined so every provided value is forwarded.

Useful? React with 👍 / 👎.

},
signal: config.getSignal(opts?.requestTimeoutMs),
})

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

return {
snapshotId: res.data!.snapshotID,
names: res.data!.names ?? [],
}
}

Expand Down Expand Up @@ -1038,6 +1058,7 @@ export class SnapshotPaginator extends BasePaginator<SnapshotInfo> {
return (res.data ?? []).map(
(snapshot: components['schemas']['SnapshotInfo']) => ({
snapshotId: snapshot.snapshotID,
names: snapshot.names ?? [],
})
)
}
Expand Down