Skip to content

feat: add customizable pagination and ordering parameter names#316

Merged
G4brym merged 2 commits intomainfrom
feat/custom-pagination-params
Feb 27, 2026
Merged

feat: add customizable pagination and ordering parameter names#316
G4brym merged 2 commits intomainfrom
feat/custom-pagination-params

Conversation

@G4brym
Copy link
Member

@G4brym G4brym commented Feb 25, 2026

Summary

Adds 4 new class properties to ListEndpoint that allow overriding the default query parameter names for pagination and ordering:

Property Default Description
pageFieldName "page" Query param name for page number
perPageFieldName "per_page" Query param name for items per page
orderByFieldName "order_by" Query param name for sort column
orderByDirectionFieldName "order_by_direction" Query param name for sort direction

This follows the existing pattern of searchFieldName which was already customizable.

Breaking change note

ListFilters.options type changed from a specific shape ({ page?: number; per_page?: number; ... }) to Record<string, unknown>. This is technically a breaking change for TypeScript consumers who relied on the specific property types, but the runtime behavior is identical and existing code accessing options.page etc. will still work (just typed as unknown now).

Example

class MyListEndpoint extends ListEndpoint {
  _meta = { model: { schema: MySchema, tableName: "items", primaryKeys: ["id"] } };

  // Custom parameter names
  pageFieldName = "p";
  perPageFieldName = "limit";
  orderByFieldName = "sort";
  orderByDirectionFieldName = "dir";
  orderByFields = ["name", "created_at"];
}
// GET /items?p=2&limit=10&sort=name&dir=desc

Changes

  • src/endpoints/list.ts: Added 4 field name properties, converted optionFields from static array to a getter, updated getSchema() to use dynamic field names
  • src/endpoints/d1/list.ts: Updated list() to read options using the custom field names
  • src/endpoints/types.ts: Changed ListFilters.options to Record<string, unknown> to support arbitrary option keys
  • 4 integration tests covering custom pagination, ordering, OpenAPI schema output, and option field routing

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 25, 2026

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/chanfana@316

commit: 04f775a

@github-actions
Copy link
Contributor

github-actions bot commented Feb 25, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 94.38% 740 / 784
🔵 Statements 94.32% 764 / 810
🔵 Functions 91.76% 156 / 170
🔵 Branches 86.71% 444 / 512
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/endpoints/list.ts 95.55% 82.6% 83.33% 95.55% 48, 157-159
src/endpoints/types.ts 100% 100% 100% 100%
src/endpoints/d1/list.ts 95% 64% 100% 94.73% 86, 117
Generated in workflow #670 for commit 04f775a by the Vitest Coverage Report Action

Add pageFieldName, perPageFieldName, orderByFieldName, and
orderByDirectionFieldName class properties to ListEndpoint. These allow
overriding the default query parameter names (page, per_page, order_by,
order_by_direction) while maintaining full backward compatibility.

Also updates D1ListEndpoint to read options using the custom field names,
and changes ListFilters.options to Record<string, unknown> to support
arbitrary option key names.
@G4brym G4brym force-pushed the feat/custom-pagination-params branch from 1270c9e to 7187370 Compare February 27, 2026 10:25
…and test coverage

Restore ListFilters.options to a backward-compatible type with named default
fields plus an index signature for custom names. Use ListFilters["options"]
in D1ListEndpoint instead of raw Record. Add inline comment on the as-any cast
in getSchema(). Add D1 integration tests for custom pagination field names,
tests for partial field name customization, and document the optionFields
getter change in the changeset.
@G4brym G4brym merged commit d30cc36 into main Feb 27, 2026
8 checks passed
@G4brym G4brym deleted the feat/custom-pagination-params branch February 27, 2026 10:58
@github-actions github-actions bot mentioned this pull request Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant