Repository navigation
fix: paginate worker deployments from nested result #2819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
airshakur88
wants to merge
1
commit into
cloudflare:main
Choose a base branch
from
airshakur88:fix/deployment-list-pagination
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| 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, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
tests/api-resources/workers/deployments-pagination.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 whenresultis missing or falsy. A body like{ result: {}, result_info: { page: 1 } }is truthy, sogetPaginatedItems()returnsundefinedand afor awaitover the list will throw. I'd usebody.result?.deployments ?? [](and keep a definite array onthis.result).nextPageRequestOptionsalso never returnsnulland incrementsquery.page(defaulting a missing param to 1) instead ofthis.result_info.page. That is fine ifAbstractPagestops on an empty items array, which the test relies on by serving a second page withdeployments: []. If a last page still has items, or if the helper only stops when this method returnsnull, iteration would request page n+1 forever. I'd returnnullwhen the current page has fewer thanper_pageitems, or whenresult_info.pageis already the last page (if the API exposes a total), and add a test whereresultis{}and one where the first page is the last page.