@nile-squad/nylonpay-ts 1.0.3 → 1.0.5

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.ts CHANGED
@@ -273,6 +273,12 @@ type NylonPayConfig = {
273
273
  maxPollIntervalMs?: number;
274
274
  maxPollDurationMs?: number;
275
275
  maxPollAttempts?: number;
276
+ /**
277
+ * Receive status updates over an SSE stream (default `true`), falling back to
278
+ * polling automatically when the stream is unavailable. Set `false` to poll
279
+ * only.
280
+ */
281
+ streaming?: boolean;
276
282
  fetch?: typeof globalThis.fetch;
277
283
  /** Force a new instance even if one already exists for this key+url pair. */
278
284
  force?: boolean;
@@ -280,16 +286,31 @@ type NylonPayConfig = {
280
286
  hooks?: SdkHooks;
281
287
  };
282
288
  /**
283
- * Structured error returned by SDK operations. `code` is machine-readable
289
+ * Well-known failure categories. The SDK derives these from the server's
290
+ * tagged error (or from the transport for `network`/`timeout`) so merchants can
291
+ * branch on a stable category instead of parsing messages or HTTP status codes.
292
+ *
293
+ * - `auth` — invalid/missing/revoked/expired key, bad signature, replay, scope.
294
+ * - `validation` — input the server rejected.
295
+ * - `limit` — account/KYC transaction limits exceeded.
296
+ * - `rate_limit` — too many requests.
297
+ * - `account` — merchant account missing or not active.
298
+ * - `provider` — payment provider/engine rejected the operation.
299
+ * - `not_found` — referenced transaction does not exist.
300
+ * - `internal` — unexpected server-side failure.
301
+ * - `network` — request never reached the server (DNS, TLS, connection).
302
+ * - `timeout` — request exceeded the configured timeout.
303
+ */
304
+ type SdkErrorCategory = "auth" | "validation" | "limit" | "rate_limit" | "account" | "provider" | "not_found" | "internal" | "network" | "timeout";
305
+ /**
306
+ * Structured error returned by SDK operations. `category` is machine-readable
284
307
  * for branching logic; `message` is human-readable for logs and alerts.
285
308
  * `retryable` tells the merchant whether the same request may succeed
286
309
  * on re-invocation.
287
- * @internal
288
310
  */
289
311
  type SdkError = {
290
- code: string;
312
+ category: SdkErrorCategory;
291
313
  message: string;
292
- statusCode?: number;
293
314
  retryable?: boolean;
294
315
  };
295
316
  /**
@@ -503,8 +524,8 @@ interface NylonPaySdk {
503
524
  interface PaymentInstance {
504
525
  /** The transaction reference. Immutable after creation. */
505
526
  readonly reference: string;
506
- /** Current transaction status. `null` until the first poll resolves. */
507
- readonly status: TransactionStatus | null;
527
+ /** Current transaction status, set from the initiation response and updated on each status change. */
528
+ readonly status: TransactionStatus;
508
529
  /**
509
530
  * Register a handler for a payment event. Fires every time the event
510
531
  * occurs. Returns the instance for chaining.
@@ -535,20 +556,20 @@ interface PaymentInstance {
535
556
  off(event: PaymentEvent, handler: PaymentEventHandler): PaymentInstance;
536
557
  /**
537
558
  * Block until the transaction reaches a terminal state. Resolves with
538
- * the full {@link Transaction} on success. Rejects on failure,
539
- * cancellation, or polling error.
559
+ * the full {@link Transaction} on success, or `null` on failure,
560
+ * cancellation, or polling error. Never rejects.
540
561
  *
541
562
  * @example
542
563
  * ```ts
543
- * try {
544
- * const tx = await payment.wait();
564
+ * const tx = await payment.wait();
565
+ * if (tx) {
545
566
  * console.log("Paid:", tx.amount, tx.currency);
546
- * } catch (err) {
547
- * console.error("Payment did not succeed:", err.message);
567
+ * } else {
568
+ * console.error("Payment did not succeed");
548
569
  * }
549
570
  * ```
550
571
  */
551
- wait(): Promise<Transaction>;
572
+ wait(): Promise<Transaction | null>;
552
573
  }
553
574
 
554
575
  /**
@@ -593,15 +614,24 @@ declare function createNylonPay(config: NylonPayConfig): NylonPaySdk;
593
614
  */
594
615
 
595
616
  /**
596
- * Parse an error string into an SdkError object.
597
- * Tries JSON.parse first; falls back to a generic UNKNOWN error.
617
+ * Convert a structured SdkError into a throwable Error that still carries the
618
+ * category and retryable flag. Used by async operations that throw on
619
+ * initiation failure (invalid key, etc.) so merchants can `catch (e)` and read
620
+ * `e.category`.
621
+ */
622
+ declare function createSdkError(error: SdkError): Error & SdkError;
623
+ /**
624
+ * Parse an error string into a structured SdkError with a `category`.
625
+ * Tries the JSON envelope first; otherwise pulls the server's
626
+ * ` -- error-type: <category>` suffix off a raw message, falling back to
627
+ * category `internal` when untagged.
598
628
  *
599
629
  * @example
600
630
  * ```ts
601
631
  * const result = await sdk.getStatus({ reference: "ORDER-123" });
602
632
  * if (!result.isOk) {
603
633
  * const error = parseError(result.error);
604
- * console.log(error.code, error.message);
634
+ * console.log(error.category, error.message);
605
635
  * }
606
636
  * ```
607
637
  */
@@ -624,4 +654,4 @@ declare function parseError(error: string): SdkError;
624
654
  */
625
655
  declare function verifyWebhookSignature(input: VerifyWebhookInput): boolean;
626
656
 
627
- export { type AfterCollectHook, type AfterPayoutHook, type BankDetails, type BeforeCollectHook, type BeforePayoutHook, type CollectPaymentInput, type CreateInvoiceInput, type Currency, type Customer, type Destination, type EventData, type GetStatusInput, type GetTransactionInput, type InvoiceItem, type InvoiceResponse, type MakePayoutInput, type NylonPayConfig, type NylonPaySdk, type PaymentEvent, type PaymentEventHandler, type PaymentInstance, type PaymentMethod, type PhoneVerification, type SdkError, type SdkHooks, type StatusResponse, type Transaction, type TransactionMode, type TransactionStatus, type TransactionType, type VerifyPhoneInput, type VerifyWebhookInput, type WebhookEventType, type WebhookPayload, createNylonPay, parseError, verifyWebhookSignature };
657
+ export { type AfterCollectHook, type AfterPayoutHook, type BankDetails, type BeforeCollectHook, type BeforePayoutHook, type CollectPaymentInput, type CreateInvoiceInput, type Currency, type Customer, type Destination, type EventData, type GetStatusInput, type GetTransactionInput, type InvoiceItem, type InvoiceResponse, type MakePayoutInput, type NylonPayConfig, type NylonPaySdk, type PaymentEvent, type PaymentEventHandler, type PaymentInstance, type PaymentMethod, type PhoneVerification, type SdkError, type SdkErrorCategory, type SdkHooks, type StatusResponse, type Transaction, type TransactionMode, type TransactionStatus, type TransactionType, type VerifyPhoneInput, type VerifyWebhookInput, type WebhookEventType, type WebhookPayload, createNylonPay, createSdkError, parseError, verifyWebhookSignature };