diff --git a/lib/services/pax8-client.ts b/lib/services/pax8-client.ts index e578d3a..316a68f 100644 --- a/lib/services/pax8-client.ts +++ b/lib/services/pax8-client.ts @@ -4,7 +4,7 @@ * https://devx.pax8.com */ -import type { Pax8Company, Pax8PageEnvelope } from '@/lib/types/pax8'; +import type { Pax8Company, Pax8PageEnvelope, Pax8Subscription, Pax8Product } from '@/lib/types/pax8'; export interface Pax8ClientConfig { clientId: string; @@ -76,4 +76,48 @@ export class Pax8Client { async listCompanies(page = 0, size = 10): Promise> { return this.fetchJson>(`/companies?page=${page}&size=${size}`); } + + /** + * Generic read-only paginate-until-exhausted helper. Loops the given + * page-fetcher (each call inherits fetchJson's 429/Retry-After backoff), + * requesting size=200, and stops once page.number >= page.totalPages - 1. + * GET-only — no method override is ever set, satisfying PAX8-08. + */ + private async paginateAll( + fetchPage: (page: number, size: number) => Promise>, + ): Promise { + const size = 200; + const items: T[] = []; + let page = 0; + + while (true) { + const envelope = await fetchPage(page, size); + items.push(...envelope.content); + if (envelope.page.number >= envelope.page.totalPages - 1) break; + page++; + } + + return items; + } + + /** Read-only: page through every company (PAX8-08 — GET only). */ + async listAllCompanies(): Promise { + return this.paginateAll((page, size) => + this.fetchJson>(`/companies?page=${page}&size=${size}`), + ); + } + + /** Read-only: page through every subscription (PAX8-08 — GET only). */ + async listAllSubscriptions(): Promise { + return this.paginateAll((page, size) => + this.fetchJson>(`/subscriptions?page=${page}&size=${size}`), + ); + } + + /** Read-only: page through every product (PAX8-08 — GET only). */ + async listAllProducts(): Promise { + return this.paginateAll((page, size) => + this.fetchJson>(`/products?page=${page}&size=${size}`), + ); + } }