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
46 changes: 46 additions & 0 deletions src/lib/worker-deployments-page.ts
Original file line number Diff line number Diff line change
@@ -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<Item> {
result: { deployments: Array<Item> };
result_info: { page?: number; per_page?: number };
}

export class WorkerDeploymentsPage<Item>
extends AbstractPage<Item>
implements WorkerDeploymentsPageResponse<Item>
{
result: WorkerDeploymentsPageResponse<Item>['result'];
result_info: WorkerDeploymentsPageResponse<Item>['result_info'];

constructor(
client: BaseCloudflare,
response: Response,
body: WorkerDeploymentsPageResponse<Item>,
options: FinalRequestOptions,
) {
super(client, response, body, options);

this.result = body.result || { deployments: [] };
this.result_info = body.result_info || {};
}

getPaginatedItems(): Item[] {
return this.result.deployments;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this.result = body.result || { deployments: [] } only substitutes when result is missing or falsy. A body like { result: {}, result_info: { page: 1 } } is truthy, so getPaginatedItems() returns undefined and a for await over the list will throw. I'd use body.result?.deployments ?? [] (and keep a definite array on this.result).

nextPageRequestOptions also never returns null and increments query.page (defaulting a missing param to 1) instead of this.result_info.page. That is fine if AbstractPage stops on an empty items array, which the test relies on by serving a second page with deployments: []. If a last page still has items, or if the helper only stops when this method returns null, iteration would request page n+1 forever. I'd return null when the current page has fewer than per_page items, or when result_info.page is already the last page (if the API exposes a total), and add a test where result is {} and one where the first page is the last page.

}

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,
},
};
}
}
9 changes: 5 additions & 4 deletions src/resources/workers/scripts/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -72,11 +73,11 @@ export class BaseDeployments extends APIResource {
scriptName: string,
params: DeploymentListParams,
options?: RequestOptions,
): PagePromise<DeploymentListResponsesV4PagePagination, DeploymentListResponse> {
): PagePromise<DeploymentListResponsesV4PagePagination, Deployment> {
const { account_id, ...query } = params;
return this._client.getAPIList(
path`/accounts/${account_id}/workers/scripts/${scriptName}/deployments`,
V4PagePagination<DeploymentListResponse>,
WorkerDeploymentsPage<Deployment>,
{ query, ...options },
);
}
Expand Down Expand Up @@ -136,7 +137,7 @@ export class BaseDeployments extends APIResource {
}
export class Deployments extends BaseDeployments {}

export type DeploymentListResponsesV4PagePagination = V4PagePagination<DeploymentListResponse>;
export type DeploymentListResponsesV4PagePagination = WorkerDeploymentsPage<Deployment>;

export interface Deployment {
id: string;
Expand Down
42 changes: 42 additions & 0 deletions tests/api-resources/workers/deployments-pagination.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});