Skip to content

Commit 116c513

Browse files
authored
tweak: list api (#40)
1 parent f086bb8 commit 116c513

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/bin/commands/sandbox/list.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function listSandboxes(
4545
outputFields?: string,
4646
listOpts: SandboxListOpts & { pagination?: PaginationOpts } = {},
4747
showHeaders = true,
48-
limit?: number
48+
limit = 100
4949
) {
5050
const sdk = new CodeSandbox();
5151
const spinner = ora("Fetching sandboxes...").start();
@@ -63,7 +63,6 @@ export async function listSandboxes(
6363
pagination,
6464
} = await sdk.sandbox.list({
6565
...listOpts,
66-
limit: undefined, // Force pagination so we can show progress
6766
pagination: {
6867
page: currentPage,
6968
pageSize,
@@ -86,11 +85,11 @@ export async function listSandboxes(
8685
})`;
8786

8887
// Stop if we've reached the total count
89-
if (allSandboxes.length >= totalCount) {
88+
if (allSandboxes.length >= limit || pagination.nextPage == null) {
9089
break;
9190
}
9291

93-
currentPage++;
92+
currentPage = pagination.nextPage;
9493
}
9594

9695
// Apply limit after fetching all sandboxes

src/sandbox-client.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export class SandboxClient {
394394
* This method supports two modes of operation:
395395
* 1. Simple limit-based fetching (default):
396396
* ```ts
397-
* // Get up to 100 sandboxes (default)
397+
* // Get up to 50 sandboxes (default)
398398
* const { sandboxes, totalCount } = await client.list();
399399
*
400400
* // Get up to 200 sandboxes
@@ -423,10 +423,10 @@ export class SandboxClient {
423423
pagination?: PaginationOpts;
424424
} = {}
425425
): Promise<SandboxListResponse> {
426-
const limit = opts.limit ?? 100;
426+
const limit = opts.limit ?? 50;
427427
let allSandboxes: SandboxInfo[] = [];
428428
let currentPage = opts.pagination?.page ?? 1;
429-
let pageSize = opts.pagination?.pageSize ?? 50;
429+
let pageSize = opts.pagination?.pageSize ?? limit;
430430
let totalCount = 0;
431431
let nextPage: number | null = null;
432432

@@ -457,11 +457,14 @@ export class SandboxClient {
457457
tags: sandbox.tags,
458458
}));
459459

460-
allSandboxes = [...allSandboxes, ...sandboxes];
460+
const newSandboxes = sandboxes.filter(
461+
(sandbox) =>
462+
!allSandboxes.some((existing) => existing.id === sandbox.id)
463+
);
464+
allSandboxes = [...allSandboxes, ...newSandboxes];
461465

462466
// Stop if we've hit the limit or there are no more pages
463467
if (!nextPage || allSandboxes.length >= limit) {
464-
allSandboxes = allSandboxes.slice(0, limit);
465468
break;
466469
}
467470

0 commit comments

Comments
 (0)