From 918ba7946d7d0458f8e1fd8ed442ab5227ce918b Mon Sep 17 00:00:00 2001 From: airshakur88 <272401523+airshakur88@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:29:14 -0300 Subject: [PATCH] fix: paginate nested worker deployments --- src/lib/worker-deployments-page.ts | 46 +++++++++++++++++++ src/resources/workers/scripts/deployments.ts | 9 ++-- .../workers/deployments-pagination.test.ts | 42 +++++++++++++++++ 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/lib/worker-deployments-page.ts create mode 100644 tests/api-resources/workers/deployments-pagination.test.ts diff --git a/src/lib/worker-deployments-page.ts b/src/lib/worker-deployments-page.ts new file mode 100644 index 00000000000..6e84a2afb90 --- /dev/null +++ b/src/lib/worker-deployments-page.ts @@ -0,0 +1,46 @@ +import { AbstractPage, type PageRequestOptions, type V4PagePaginationParams } from '../core/pagination'; +import { type BaseCloudflare } from '../client'; +import { type FinalRequestOptions } from '../internal/request-options'; +import { maybeObj } from '../internal/utils/values'; + +export interface WorkerDeploymentsPageResponse { + result: { deployments: Array }; + result_info: { page?: number; per_page?: number }; +} + +export class WorkerDeploymentsPage + extends AbstractPage + implements WorkerDeploymentsPageResponse +{ + result: WorkerDeploymentsPageResponse['result']; + result_info: WorkerDeploymentsPageResponse['result_info']; + + constructor( + client: BaseCloudflare, + response: Response, + body: WorkerDeploymentsPageResponse, + options: FinalRequestOptions, + ) { + super(client, response, body, options); + + this.result = body.result || { deployments: [] }; + this.result_info = body.result_info || {}; + } + + getPaginatedItems(): Item[] { + return this.result.deployments; + } + + nextPageRequestOptions(): PageRequestOptions | null { + const query = this.options.query as V4PagePaginationParams; + const currentPage = query?.page ?? 1; + + return { + ...this.options, + query: { + ...maybeObj(this.options.query), + page: currentPage + 1, + }, + }; + } +} diff --git a/src/resources/workers/scripts/deployments.ts b/src/resources/workers/scripts/deployments.ts index 489294d5754..80be6f2da1e 100644 --- a/src/resources/workers/scripts/deployments.ts +++ b/src/resources/workers/scripts/deployments.ts @@ -2,7 +2,8 @@ import { APIResource } from '../../../core/resource'; import { APIPromise } from '../../../core/api-promise'; -import { PagePromise, V4PagePagination, type V4PagePaginationParams } from '../../../core/pagination'; +import { PagePromise, type V4PagePaginationParams } from '../../../core/pagination'; +import { WorkerDeploymentsPage } from '../../../lib/worker-deployments-page'; import { RequestOptions } from '../../../internal/request-options'; import { path } from '../../../internal/utils/path'; @@ -72,11 +73,11 @@ export class BaseDeployments extends APIResource { scriptName: string, params: DeploymentListParams, options?: RequestOptions, - ): PagePromise { + ): PagePromise { const { account_id, ...query } = params; return this._client.getAPIList( path`/accounts/${account_id}/workers/scripts/${scriptName}/deployments`, - V4PagePagination, + WorkerDeploymentsPage, { query, ...options }, ); } @@ -136,7 +137,7 @@ export class BaseDeployments extends APIResource { } export class Deployments extends BaseDeployments {} -export type DeploymentListResponsesV4PagePagination = V4PagePagination; +export type DeploymentListResponsesV4PagePagination = WorkerDeploymentsPage; export interface Deployment { id: string; diff --git a/tests/api-resources/workers/deployments-pagination.test.ts b/tests/api-resources/workers/deployments-pagination.test.ts new file mode 100644 index 00000000000..be8dc26a8de --- /dev/null +++ b/tests/api-resources/workers/deployments-pagination.test.ts @@ -0,0 +1,42 @@ +import Cloudflare from 'cloudflare'; + +const deployment = { + id: 'deployment-1', + created_on: '2026-01-01T00:00:00Z', + source: 'api', + strategy: 'percentage' as const, + versions: [{ version_id: 'version-1', percentage: 100 }], +}; + +test('list iterates deployments from the nested result field', async () => { + const requestedURLs: string[] = []; + const pages = [ + { result: { deployments: [deployment] }, result_info: { page: 1, per_page: 1 } }, + { result: { deployments: [] }, result_info: { page: 2, per_page: 1 } }, + ]; + const client = new Cloudflare({ + apiToken: 'test-token', + fetch: async (input) => { + requestedURLs.push(String(input)); + const body = pages.shift(); + if (!body) throw new Error('Unexpected pagination request'); + return new Response(JSON.stringify({ success: true, errors: [], messages: [], ...body }), { + headers: { 'content-type': 'application/json' }, + }); + }, + }); + + const deployments = []; + for await (const currentDeployment of client.workers.scripts.deployments.list('worker', { + account_id: 'account', + per_page: 1, + })) { + deployments.push(currentDeployment); + } + + expect(deployments).toEqual([deployment]); + expect(requestedURLs).toHaveLength(2); + const secondURL = requestedURLs[1]; + if (!secondURL) throw new Error('Expected a second-page request'); + expect(new URL(secondURL).searchParams.get('page')).toBe('2'); +});