@delopay/sdk 0.30.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -185,6 +185,8 @@ interface PaymentCreateRequest {
185
185
  allowed_payment_method_types?: PaymentMethodType[] | null;
186
186
  /** Browser metadata for 3-D Secure fingerprinting. */
187
187
  browser_info?: Record<string, unknown> | null;
188
+ /** Set to `true` to generate a hosted payment link for this payment. */
189
+ payment_link?: boolean | null;
188
190
  }
189
191
  interface PaymentUpdateRequest {
190
192
  amount?: number | null;
@@ -217,6 +219,8 @@ interface PaymentUpdateRequest {
217
219
  phone_country_code?: string | null;
218
220
  }
219
221
  interface PaymentConfirmRequest {
222
+ /** Client secret from the created payment, required for client-side confirmation with a publishable key. */
223
+ client_secret?: string | null;
220
224
  payment_method?: PaymentMethod | null;
221
225
  payment_method_type?: PaymentMethodType | null;
222
226
  payment_method_data?: Record<string, unknown> | null;
@@ -323,6 +327,8 @@ interface PaymentResponse {
323
327
  disputes?: DisputeResponse[] | null;
324
328
  /** Instructions for completing additional customer actions (3DS redirect, etc.). */
325
329
  next_action?: Record<string, unknown> | null;
330
+ /** Hosted payment link details, present when the payment was created with `payment_link: true`. */
331
+ payment_link?: PaymentLinkResponse | null;
326
332
  /** Reason provided when the payment was cancelled. */
327
333
  cancellation_reason?: string | null;
328
334
  /** Bank statement descriptor (name portion). */
@@ -541,6 +547,8 @@ interface PaymentMethodResponse {
541
547
  client_secret?: string | null;
542
548
  status?: string | null;
543
549
  billing?: Address | null;
550
+ /** Token used to charge this saved method on a later payment (pass as `payment_token` to `payments.create`). */
551
+ payment_token?: string | null;
544
552
  }
545
553
  interface PaymentMethodListParams {
546
554
  client_secret?: string | null;
@@ -668,6 +676,8 @@ interface ShopUpdateRequest {
668
676
  return_url?: string | null;
669
677
  webhook_url?: string | null;
670
678
  is_active?: boolean | null;
679
+ /** Webhook signing secret for this shop. Outgoing webhooks are HMAC-SHA512 signed with this key. */
680
+ payment_response_hash_key?: string | null;
671
681
  payment_link_config?: BusinessPaymentLinkConfig | null;
672
682
  /**
673
683
  * Origins permitted to embed this shop's hosted checkout in an iframe.
@@ -2492,18 +2502,20 @@ declare class Payments {
2492
2502
  * Create a new payment intent.
2493
2503
  *
2494
2504
  * @param params - Payment creation parameters including amount and currency.
2505
+ * @param options - Optional per-call extras: extra `headers` (e.g. an
2506
+ * `Idempotency-Key` to make the create safe to retry), a `timeout` override,
2507
+ * and an `AbortSignal`.
2495
2508
  * @returns The created payment intent.
2496
2509
  *
2497
2510
  * @example
2498
2511
  * ```typescript
2499
- * const payment = await delopay.payments.create({
2500
- * amount: 5000,
2501
- * currency: 'EUR',
2502
- * customer_id: 'cus_123',
2503
- * });
2512
+ * const payment = await delopay.payments.create(
2513
+ * { amount: 5000, currency: 'EUR', customer_id: 'cus_123' },
2514
+ * { headers: { 'Idempotency-Key': 'order_1001' } },
2515
+ * );
2504
2516
  * ```
2505
2517
  */
2506
- create(params: PaymentCreateRequest): Promise<PaymentResponse>;
2518
+ create(params: PaymentCreateRequest, options?: RequestExtras): Promise<PaymentResponse>;
2507
2519
  /**
2508
2520
  * Retrieve a payment by its ID.
2509
2521
  *
@@ -2562,6 +2574,8 @@ declare class Payments {
2562
2574
  * List payment intents, optionally filtered by customer or date range.
2563
2575
  *
2564
2576
  * @param params - Optional filter and pagination parameters.
2577
+ * @param options - Optional per-call extras: extra `headers`, a `timeout`
2578
+ * override, and an `AbortSignal` for cancellation.
2565
2579
  * @returns Paginated list of payment intents.
2566
2580
  *
2567
2581
  * @example
@@ -2569,7 +2583,7 @@ declare class Payments {
2569
2583
  * const { data } = await delopay.payments.list({ customer_id: 'cus_123', limit: 25 });
2570
2584
  * ```
2571
2585
  */
2572
- list(params?: PaymentListParams): Promise<PaymentListResponse>;
2586
+ list(params?: PaymentListParams, options?: RequestExtras): Promise<PaymentListResponse>;
2573
2587
  /** Generate session tokens. `POST /payments/session-tokens` */
2574
2588
  sessionTokens(params: Record<string, unknown>): Promise<Record<string, unknown>>;
2575
2589
  /** Retrieve payment with gateway credentials. `POST /payments/sync` */
@@ -3031,7 +3045,7 @@ declare class Shops {
3031
3045
  *
3032
3046
  * @example
3033
3047
  * ```typescript
3034
- * const shop = await delopay.shops.create('merch_123', { profile_name: 'EU Store' });
3048
+ * const shop = await delopay.shops.create('merch_123', { shop_name: 'EU Store' });
3035
3049
  * ```
3036
3050
  */
3037
3051
  create(merchantId: string, params: ShopCreateRequest): Promise<ShopResponse>;
@@ -3508,6 +3522,12 @@ interface RequestOptions {
3508
3522
  signal?: AbortSignal;
3509
3523
  }
3510
3524
  type RequestFn = <T>(method: string, path: string, options?: RequestOptions) => Promise<T>;
3525
+ /**
3526
+ * Per-call options that resource methods accept as an optional final argument:
3527
+ * extra HTTP headers (e.g. `Idempotency-Key`), a per-request timeout override,
3528
+ * and an `AbortSignal` for cancellation.
3529
+ */
3530
+ type RequestExtras = Pick<RequestOptions, 'headers' | 'timeout' | 'signal'>;
3511
3531
  /**
3512
3532
  * Delopay API client.
3513
3533
  *
@@ -3517,7 +3537,7 @@ type RequestFn = <T>(method: string, path: string, options?: RequestOptions) =>
3517
3537
  *
3518
3538
  * @example
3519
3539
  * ```typescript
3520
- * const delopay = new Delopay('sk_live_...', { sandbox: false });
3540
+ * const delopay = new Delopay('prd_...', { sandbox: false });
3521
3541
  * const payment = await delopay.payments.create({ amount: 5000, currency: 'EUR' });
3522
3542
  * ```
3523
3543
  */
@@ -3575,7 +3595,7 @@ declare class Delopay {
3575
3595
  /**
3576
3596
  * Create a new Delopay client.
3577
3597
  *
3578
- * @param apiKey - Your Delopay API key (e.g. `sk_live_...` or `sk_test_...`).
3598
+ * @param apiKey - Your Delopay API key (e.g. `prd_...` or `snd_...`).
3579
3599
  * Pass an empty string or omit for JWT-only usage (e.g. dashboard apps).
3580
3600
  * @param options - Optional configuration (sandbox mode, base URL override, timeout).
3581
3601
  */
@@ -3608,14 +3628,32 @@ declare class Delopay {
3608
3628
  * Auto-paginate a list endpoint. Yields items one by one, fetching
3609
3629
  * the next page automatically when the current one is exhausted.
3610
3630
  *
3611
- * @param listFn - A function that takes `{ limit, offset }` and returns `{ data: T[] }` or `T[]`.
3631
+ * Delopay list endpoints use one of two pagination styles, so this helper
3632
+ * supports both:
3633
+ * - **Offset** (default) — for endpoints like `customers.list` that accept
3634
+ * `offset`/`limit`. Each page advances `offset` by the number of items returned.
3635
+ * - **Cursor** — for endpoints like `payments.list` and `payouts.list` that page
3636
+ * with `starting_after`/`limit` (they ignore `offset`). Pass a `cursor` extractor
3637
+ * that returns the id of an item; the next page is requested with
3638
+ * `starting_after` set to the last item's id.
3639
+ *
3640
+ * @param listFn - A function that takes the paging params and returns `{ data: T[] }` or `T[]`.
3612
3641
  * @param params - Additional parameters to pass to every page request.
3613
- * @param pageSize - Number of items per page. Defaults to `50`.
3642
+ * @param options - Page size (number) for offset mode, or `{ pageSize?, cursor? }`.
3643
+ * Provide `cursor` to switch to cursor pagination.
3614
3644
  *
3615
3645
  * @example
3616
3646
  * ```typescript
3647
+ * // Offset endpoint (customers):
3648
+ * for await (const c of delopay.paginate((p) => delopay.customers.list(p))) {
3649
+ * console.log(c.customer_id);
3650
+ * }
3651
+ *
3652
+ * // Cursor endpoint (payments): extract the id used as the next cursor.
3617
3653
  * for await (const payment of delopay.paginate(
3618
3654
  * (p) => delopay.payments.list(p),
3655
+ * undefined,
3656
+ * { cursor: (p) => p.payment_id },
3619
3657
  * )) {
3620
3658
  * console.log(payment.payment_id);
3621
3659
  * }
@@ -3623,10 +3661,14 @@ declare class Delopay {
3623
3661
  */
3624
3662
  paginate<T, P extends Record<string, unknown>>(listFn: (params: P & {
3625
3663
  limit: number;
3626
- offset: number;
3664
+ offset?: number;
3665
+ starting_after?: string;
3627
3666
  }) => Promise<{
3628
3667
  data: T[];
3629
- } | T[]>, params?: P, pageSize?: number): AsyncGenerator<T>;
3668
+ } | T[]>, params?: P, options?: number | {
3669
+ pageSize?: number;
3670
+ cursor?: (item: T) => string | undefined;
3671
+ }): AsyncGenerator<T>;
3630
3672
  }
3631
3673
 
3632
3674
  /**
package/dist/index.d.ts CHANGED
@@ -185,6 +185,8 @@ interface PaymentCreateRequest {
185
185
  allowed_payment_method_types?: PaymentMethodType[] | null;
186
186
  /** Browser metadata for 3-D Secure fingerprinting. */
187
187
  browser_info?: Record<string, unknown> | null;
188
+ /** Set to `true` to generate a hosted payment link for this payment. */
189
+ payment_link?: boolean | null;
188
190
  }
189
191
  interface PaymentUpdateRequest {
190
192
  amount?: number | null;
@@ -217,6 +219,8 @@ interface PaymentUpdateRequest {
217
219
  phone_country_code?: string | null;
218
220
  }
219
221
  interface PaymentConfirmRequest {
222
+ /** Client secret from the created payment, required for client-side confirmation with a publishable key. */
223
+ client_secret?: string | null;
220
224
  payment_method?: PaymentMethod | null;
221
225
  payment_method_type?: PaymentMethodType | null;
222
226
  payment_method_data?: Record<string, unknown> | null;
@@ -323,6 +327,8 @@ interface PaymentResponse {
323
327
  disputes?: DisputeResponse[] | null;
324
328
  /** Instructions for completing additional customer actions (3DS redirect, etc.). */
325
329
  next_action?: Record<string, unknown> | null;
330
+ /** Hosted payment link details, present when the payment was created with `payment_link: true`. */
331
+ payment_link?: PaymentLinkResponse | null;
326
332
  /** Reason provided when the payment was cancelled. */
327
333
  cancellation_reason?: string | null;
328
334
  /** Bank statement descriptor (name portion). */
@@ -541,6 +547,8 @@ interface PaymentMethodResponse {
541
547
  client_secret?: string | null;
542
548
  status?: string | null;
543
549
  billing?: Address | null;
550
+ /** Token used to charge this saved method on a later payment (pass as `payment_token` to `payments.create`). */
551
+ payment_token?: string | null;
544
552
  }
545
553
  interface PaymentMethodListParams {
546
554
  client_secret?: string | null;
@@ -668,6 +676,8 @@ interface ShopUpdateRequest {
668
676
  return_url?: string | null;
669
677
  webhook_url?: string | null;
670
678
  is_active?: boolean | null;
679
+ /** Webhook signing secret for this shop. Outgoing webhooks are HMAC-SHA512 signed with this key. */
680
+ payment_response_hash_key?: string | null;
671
681
  payment_link_config?: BusinessPaymentLinkConfig | null;
672
682
  /**
673
683
  * Origins permitted to embed this shop's hosted checkout in an iframe.
@@ -2492,18 +2502,20 @@ declare class Payments {
2492
2502
  * Create a new payment intent.
2493
2503
  *
2494
2504
  * @param params - Payment creation parameters including amount and currency.
2505
+ * @param options - Optional per-call extras: extra `headers` (e.g. an
2506
+ * `Idempotency-Key` to make the create safe to retry), a `timeout` override,
2507
+ * and an `AbortSignal`.
2495
2508
  * @returns The created payment intent.
2496
2509
  *
2497
2510
  * @example
2498
2511
  * ```typescript
2499
- * const payment = await delopay.payments.create({
2500
- * amount: 5000,
2501
- * currency: 'EUR',
2502
- * customer_id: 'cus_123',
2503
- * });
2512
+ * const payment = await delopay.payments.create(
2513
+ * { amount: 5000, currency: 'EUR', customer_id: 'cus_123' },
2514
+ * { headers: { 'Idempotency-Key': 'order_1001' } },
2515
+ * );
2504
2516
  * ```
2505
2517
  */
2506
- create(params: PaymentCreateRequest): Promise<PaymentResponse>;
2518
+ create(params: PaymentCreateRequest, options?: RequestExtras): Promise<PaymentResponse>;
2507
2519
  /**
2508
2520
  * Retrieve a payment by its ID.
2509
2521
  *
@@ -2562,6 +2574,8 @@ declare class Payments {
2562
2574
  * List payment intents, optionally filtered by customer or date range.
2563
2575
  *
2564
2576
  * @param params - Optional filter and pagination parameters.
2577
+ * @param options - Optional per-call extras: extra `headers`, a `timeout`
2578
+ * override, and an `AbortSignal` for cancellation.
2565
2579
  * @returns Paginated list of payment intents.
2566
2580
  *
2567
2581
  * @example
@@ -2569,7 +2583,7 @@ declare class Payments {
2569
2583
  * const { data } = await delopay.payments.list({ customer_id: 'cus_123', limit: 25 });
2570
2584
  * ```
2571
2585
  */
2572
- list(params?: PaymentListParams): Promise<PaymentListResponse>;
2586
+ list(params?: PaymentListParams, options?: RequestExtras): Promise<PaymentListResponse>;
2573
2587
  /** Generate session tokens. `POST /payments/session-tokens` */
2574
2588
  sessionTokens(params: Record<string, unknown>): Promise<Record<string, unknown>>;
2575
2589
  /** Retrieve payment with gateway credentials. `POST /payments/sync` */
@@ -3031,7 +3045,7 @@ declare class Shops {
3031
3045
  *
3032
3046
  * @example
3033
3047
  * ```typescript
3034
- * const shop = await delopay.shops.create('merch_123', { profile_name: 'EU Store' });
3048
+ * const shop = await delopay.shops.create('merch_123', { shop_name: 'EU Store' });
3035
3049
  * ```
3036
3050
  */
3037
3051
  create(merchantId: string, params: ShopCreateRequest): Promise<ShopResponse>;
@@ -3508,6 +3522,12 @@ interface RequestOptions {
3508
3522
  signal?: AbortSignal;
3509
3523
  }
3510
3524
  type RequestFn = <T>(method: string, path: string, options?: RequestOptions) => Promise<T>;
3525
+ /**
3526
+ * Per-call options that resource methods accept as an optional final argument:
3527
+ * extra HTTP headers (e.g. `Idempotency-Key`), a per-request timeout override,
3528
+ * and an `AbortSignal` for cancellation.
3529
+ */
3530
+ type RequestExtras = Pick<RequestOptions, 'headers' | 'timeout' | 'signal'>;
3511
3531
  /**
3512
3532
  * Delopay API client.
3513
3533
  *
@@ -3517,7 +3537,7 @@ type RequestFn = <T>(method: string, path: string, options?: RequestOptions) =>
3517
3537
  *
3518
3538
  * @example
3519
3539
  * ```typescript
3520
- * const delopay = new Delopay('sk_live_...', { sandbox: false });
3540
+ * const delopay = new Delopay('prd_...', { sandbox: false });
3521
3541
  * const payment = await delopay.payments.create({ amount: 5000, currency: 'EUR' });
3522
3542
  * ```
3523
3543
  */
@@ -3575,7 +3595,7 @@ declare class Delopay {
3575
3595
  /**
3576
3596
  * Create a new Delopay client.
3577
3597
  *
3578
- * @param apiKey - Your Delopay API key (e.g. `sk_live_...` or `sk_test_...`).
3598
+ * @param apiKey - Your Delopay API key (e.g. `prd_...` or `snd_...`).
3579
3599
  * Pass an empty string or omit for JWT-only usage (e.g. dashboard apps).
3580
3600
  * @param options - Optional configuration (sandbox mode, base URL override, timeout).
3581
3601
  */
@@ -3608,14 +3628,32 @@ declare class Delopay {
3608
3628
  * Auto-paginate a list endpoint. Yields items one by one, fetching
3609
3629
  * the next page automatically when the current one is exhausted.
3610
3630
  *
3611
- * @param listFn - A function that takes `{ limit, offset }` and returns `{ data: T[] }` or `T[]`.
3631
+ * Delopay list endpoints use one of two pagination styles, so this helper
3632
+ * supports both:
3633
+ * - **Offset** (default) — for endpoints like `customers.list` that accept
3634
+ * `offset`/`limit`. Each page advances `offset` by the number of items returned.
3635
+ * - **Cursor** — for endpoints like `payments.list` and `payouts.list` that page
3636
+ * with `starting_after`/`limit` (they ignore `offset`). Pass a `cursor` extractor
3637
+ * that returns the id of an item; the next page is requested with
3638
+ * `starting_after` set to the last item's id.
3639
+ *
3640
+ * @param listFn - A function that takes the paging params and returns `{ data: T[] }` or `T[]`.
3612
3641
  * @param params - Additional parameters to pass to every page request.
3613
- * @param pageSize - Number of items per page. Defaults to `50`.
3642
+ * @param options - Page size (number) for offset mode, or `{ pageSize?, cursor? }`.
3643
+ * Provide `cursor` to switch to cursor pagination.
3614
3644
  *
3615
3645
  * @example
3616
3646
  * ```typescript
3647
+ * // Offset endpoint (customers):
3648
+ * for await (const c of delopay.paginate((p) => delopay.customers.list(p))) {
3649
+ * console.log(c.customer_id);
3650
+ * }
3651
+ *
3652
+ * // Cursor endpoint (payments): extract the id used as the next cursor.
3617
3653
  * for await (const payment of delopay.paginate(
3618
3654
  * (p) => delopay.payments.list(p),
3655
+ * undefined,
3656
+ * { cursor: (p) => p.payment_id },
3619
3657
  * )) {
3620
3658
  * console.log(payment.payment_id);
3621
3659
  * }
@@ -3623,10 +3661,14 @@ declare class Delopay {
3623
3661
  */
3624
3662
  paginate<T, P extends Record<string, unknown>>(listFn: (params: P & {
3625
3663
  limit: number;
3626
- offset: number;
3664
+ offset?: number;
3665
+ starting_after?: string;
3627
3666
  }) => Promise<{
3628
3667
  data: T[];
3629
- } | T[]>, params?: P, pageSize?: number): AsyncGenerator<T>;
3668
+ } | T[]>, params?: P, options?: number | {
3669
+ pageSize?: number;
3670
+ cursor?: (item: T) => string | undefined;
3671
+ }): AsyncGenerator<T>;
3630
3672
  }
3631
3673
 
3632
3674
  /**
package/dist/index.js CHANGED
@@ -41,7 +41,7 @@ import {
41
41
  shadowFor,
42
42
  surfacePadValue,
43
43
  verticalGapValue
44
- } from "./chunk-INP6UXMJ.js";
44
+ } from "./chunk-5C42YTV2.js";
45
45
  export {
46
46
  Analytics,
47
47
  AnalyticsDashboard,
package/dist/internal.cjs CHANGED
@@ -1014,19 +1014,21 @@ var Payments = class {
1014
1014
  * Create a new payment intent.
1015
1015
  *
1016
1016
  * @param params - Payment creation parameters including amount and currency.
1017
+ * @param options - Optional per-call extras: extra `headers` (e.g. an
1018
+ * `Idempotency-Key` to make the create safe to retry), a `timeout` override,
1019
+ * and an `AbortSignal`.
1017
1020
  * @returns The created payment intent.
1018
1021
  *
1019
1022
  * @example
1020
1023
  * ```typescript
1021
- * const payment = await delopay.payments.create({
1022
- * amount: 5000,
1023
- * currency: 'EUR',
1024
- * customer_id: 'cus_123',
1025
- * });
1024
+ * const payment = await delopay.payments.create(
1025
+ * { amount: 5000, currency: 'EUR', customer_id: 'cus_123' },
1026
+ * { headers: { 'Idempotency-Key': 'order_1001' } },
1027
+ * );
1026
1028
  * ```
1027
1029
  */
1028
- async create(params) {
1029
- return this.request("POST", "/payments", { body: params });
1030
+ async create(params, options) {
1031
+ return this.request("POST", "/payments", { body: params, ...options });
1030
1032
  }
1031
1033
  /**
1032
1034
  * Retrieve a payment by its ID.
@@ -1110,6 +1112,8 @@ var Payments = class {
1110
1112
  * List payment intents, optionally filtered by customer or date range.
1111
1113
  *
1112
1114
  * @param params - Optional filter and pagination parameters.
1115
+ * @param options - Optional per-call extras: extra `headers`, a `timeout`
1116
+ * override, and an `AbortSignal` for cancellation.
1113
1117
  * @returns Paginated list of payment intents.
1114
1118
  *
1115
1119
  * @example
@@ -1117,9 +1121,10 @@ var Payments = class {
1117
1121
  * const { data } = await delopay.payments.list({ customer_id: 'cus_123', limit: 25 });
1118
1122
  * ```
1119
1123
  */
1120
- async list(params) {
1124
+ async list(params, options) {
1121
1125
  return this.request("GET", "/payments/list", {
1122
- query: params
1126
+ query: params,
1127
+ ...options
1123
1128
  });
1124
1129
  }
1125
1130
  // --- Advanced operations (Task 3.2) ---
@@ -1840,7 +1845,7 @@ var Shops = class {
1840
1845
  *
1841
1846
  * @example
1842
1847
  * ```typescript
1843
- * const shop = await delopay.shops.create('merch_123', { profile_name: 'EU Store' });
1848
+ * const shop = await delopay.shops.create('merch_123', { shop_name: 'EU Store' });
1844
1849
  * ```
1845
1850
  */
1846
1851
  async create(merchantId, params) {
@@ -2733,7 +2738,7 @@ var Delopay = class {
2733
2738
  /**
2734
2739
  * Create a new Delopay client.
2735
2740
  *
2736
- * @param apiKey - Your Delopay API key (e.g. `sk_live_...` or `sk_test_...`).
2741
+ * @param apiKey - Your Delopay API key (e.g. `prd_...` or `snd_...`).
2737
2742
  * Pass an empty string or omit for JWT-only usage (e.g. dashboard apps).
2738
2743
  * @param options - Optional configuration (sandbox mode, base URL override, timeout).
2739
2744
  */
@@ -2990,34 +2995,64 @@ var Delopay = class {
2990
2995
  * Auto-paginate a list endpoint. Yields items one by one, fetching
2991
2996
  * the next page automatically when the current one is exhausted.
2992
2997
  *
2993
- * @param listFn - A function that takes `{ limit, offset }` and returns `{ data: T[] }` or `T[]`.
2998
+ * Delopay list endpoints use one of two pagination styles, so this helper
2999
+ * supports both:
3000
+ * - **Offset** (default) — for endpoints like `customers.list` that accept
3001
+ * `offset`/`limit`. Each page advances `offset` by the number of items returned.
3002
+ * - **Cursor** — for endpoints like `payments.list` and `payouts.list` that page
3003
+ * with `starting_after`/`limit` (they ignore `offset`). Pass a `cursor` extractor
3004
+ * that returns the id of an item; the next page is requested with
3005
+ * `starting_after` set to the last item's id.
3006
+ *
3007
+ * @param listFn - A function that takes the paging params and returns `{ data: T[] }` or `T[]`.
2994
3008
  * @param params - Additional parameters to pass to every page request.
2995
- * @param pageSize - Number of items per page. Defaults to `50`.
3009
+ * @param options - Page size (number) for offset mode, or `{ pageSize?, cursor? }`.
3010
+ * Provide `cursor` to switch to cursor pagination.
2996
3011
  *
2997
3012
  * @example
2998
3013
  * ```typescript
3014
+ * // Offset endpoint (customers):
3015
+ * for await (const c of delopay.paginate((p) => delopay.customers.list(p))) {
3016
+ * console.log(c.customer_id);
3017
+ * }
3018
+ *
3019
+ * // Cursor endpoint (payments): extract the id used as the next cursor.
2999
3020
  * for await (const payment of delopay.paginate(
3000
3021
  * (p) => delopay.payments.list(p),
3022
+ * undefined,
3023
+ * { cursor: (p) => p.payment_id },
3001
3024
  * )) {
3002
3025
  * console.log(payment.payment_id);
3003
3026
  * }
3004
3027
  * ```
3005
3028
  */
3006
- async *paginate(listFn, params, pageSize = 50) {
3029
+ async *paginate(listFn, params, options) {
3030
+ const pageSize = typeof options === "number" ? options : options?.pageSize ?? 50;
3031
+ const cursorOf = typeof options === "object" ? options.cursor : void 0;
3007
3032
  let offset = 0;
3033
+ let after;
3008
3034
  while (true) {
3009
- const result = await listFn({
3010
- ...params ?? {},
3011
- limit: pageSize,
3012
- offset
3013
- });
3035
+ const page = { ...params ?? {}, limit: pageSize };
3036
+ if (cursorOf) {
3037
+ if (after !== void 0) page.starting_after = after;
3038
+ } else {
3039
+ page.offset = offset;
3040
+ }
3041
+ const result = await listFn(page);
3014
3042
  const items = Array.isArray(result) ? result : result.data;
3015
3043
  if (items.length === 0) break;
3016
3044
  for (const item of items) {
3017
3045
  yield item;
3018
3046
  }
3019
3047
  if (items.length < pageSize) break;
3020
- offset += items.length;
3048
+ if (cursorOf) {
3049
+ const last = items[items.length - 1];
3050
+ if (last === void 0) break;
3051
+ after = cursorOf(last);
3052
+ if (after === void 0) break;
3053
+ } else {
3054
+ offset += items.length;
3055
+ }
3021
3056
  }
3022
3057
  }
3023
3058
  };