Skip to content

feat(backend): standardize pagination and cursor validation across list endpoints#753

Merged
Calebux merged 1 commit into
Calebux:mainfrom
Belzabeem:feature/pagination-standardization
May 28, 2026
Merged

feat(backend): standardize pagination and cursor validation across list endpoints#753
Calebux merged 1 commit into
Calebux:mainfrom
Belzabeem:feature/pagination-standardization

Conversation

@Belzabeem
Copy link
Copy Markdown

Closes #641

Summary

This PR implements shared pagination helpers that enforce consistent cursor validation and error responses across all list endpoints, bringing them in line with the rigor already present in subscription services.

Changes

  • backend/src/utils/pagination.ts (new) - Shared pagination utilities

    • validateCursor() - Validates base64-encoded cursor with created_at field, throws MALFORMED_CURSOR or INVALID_CURSOR errors
    • validateLimit() - Validates limit parameter (1-100 range), throws INVALID_LIMIT error
    • encodeCursor() - Encodes cursor data to base64
    • PaginationError - Custom error class with error codes
    • cursorPaginationSchema - Zod schema for cursor-based pagination validation
  • backend/src/schemas/common.ts - Added cursorPaginationSchema for reusable validation

  • backend/src/services/subscription-service.ts - Updated to use shared validateCursor and encodeCursor helpers

  • backend/src/routes/subscriptions.ts - Uses shared pagination validation with proper error handling

  • backend/src/routes/merchants.ts - Added PaginationError handling for consistent error responses

  • backend/src/routes/audit.ts - Added PaginationError handling for consistent error responses

  • backend/src/routes/gift-card-ledger.ts - Uses validateLimit for limit validation

  • backend/tests/pagination.test.ts (new) - 28 tests covering all pagination utilities

  • backend/tests/subscription-service.test.ts - Updated tests for cursor-based pagination

  • backend/SUBSCRIPTION_API.md - Updated documentation with cursor pagination semantics

Error Codes

All pagination errors now return consistent structured responses:

  • INVALID_CURSOR - The cursor is malformed or cannot be decoded
  • MALFORMED_CURSOR - The cursor is missing required created_at field
  • INVALID_LIMIT - The limit parameter is out of range or not a valid integer

Tests

All 35 pagination-related tests pass:

  • validateCursor: 9 tests
  • validateLimit: 12 tests
  • encodeCursor: 2 tests
  • cursorPaginationSchema: 5 tests
  • listSubscriptions integration: 7 tests

Checklist

  • Shared pagination helpers exist
  • Invalid cursors and limits return consistent errors
  • Tests added/updated and passing
  • Documentation updated
  • No security regressions introduced

…st endpoints (Calebux#641)

- Create shared pagination helpers in src/utils/pagination.ts
- Add validateCursor, validateLimit, encodeCursor, PaginationError utilities
- Add cursorPaginationSchema for Zod validation
- Update subscription-service to use shared cursor validation
- Update subscriptions, merchants, audit, gift-card-ledger routes to use helpers
- Add comprehensive tests for pagination utilities
- Update SUBSCRIPTION_API.md with cursor pagination documentation

Error codes: INVALID_CURSOR, MALFORMED_CURSOR, INVALID_LIMIT
@Belzabeem Belzabeem requested a review from Calebux as a code owner May 28, 2026 15:14
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 28, 2026

@Belzabeem Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Calebux Calebux merged commit b941901 into Calebux:main May 28, 2026
7 of 17 checks passed
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.

[P1] Standardize pagination and cursor validation across list endpoints

3 participants