@nile-squad/nylonpay-ts 1.0.4 → 1.0.6

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
  /**
@@ -313,6 +334,17 @@ type EventData = {
313
334
  transaction?: Transaction;
314
335
  /** Error message. Present for the `"error"` event. */
315
336
  error?: string;
337
+ /**
338
+ * Machine-readable failure category. Present for the `"error"` event when the
339
+ * failure carries one (initiation rejection, polling/stream failure) — lets
340
+ * merchants branch on a stable category instead of parsing the message.
341
+ */
342
+ category?: SdkErrorCategory;
343
+ /**
344
+ * Whether re-invoking the same operation may succeed. Present for the
345
+ * `"error"` event when known.
346
+ */
347
+ retryable?: boolean;
316
348
  /** ISO 8601 timestamp of when the event was emitted. */
317
349
  timestamp: string;
318
350
  };
@@ -336,8 +368,12 @@ interface NylonPaySdk {
336
368
  * Returns a {@link PaymentInstance} that polls for status updates and emits
337
369
  * events (`processing`, `success`, `failed`, `cancelled`, `error`).
338
370
  *
339
- * Auto-generates an idempotency `reference` if omitted. Throws on invalid
340
- * input (zero amount, empty phone, bank method without bank details).
371
+ * Auto-generates an idempotency `reference` if omitted. Throws *synchronously*
372
+ * only on invalid input (zero amount, empty phone, bank method without bank
373
+ * details) — programmer errors caught before any network call. A server-side
374
+ * initiation rejection (auth, limit, provider, network, timeout) does **not**
375
+ * throw: the returned instance emits an `"error"` event carrying `category`
376
+ * and `retryable`, and `wait()` resolves `null`.
341
377
  *
342
378
  * @example
343
379
  * ```ts
@@ -350,6 +386,7 @@ interface NylonPaySdk {
350
386
  *
351
387
  * payment.on("success", ({ transaction }) => fulfillOrder(transaction));
352
388
  * payment.on("failed", ({ error }) => notifyCustomer(error));
389
+ * payment.on("error", ({ error, category }) => log.error(category, error));
353
390
  * ```
354
391
  */
355
392
  collectPayment(input: CollectPaymentInput): Promise<PaymentInstance>;
@@ -377,7 +414,10 @@ interface NylonPaySdk {
377
414
  * Returns a {@link PaymentInstance} that polls for status updates and emits
378
415
  * events as the payout progresses.
379
416
  *
380
- * Auto-generates an idempotency `reference` if omitted.
417
+ * Auto-generates an idempotency `reference` if omitted. Same error semantics
418
+ * as {@link collectPayment}: throws synchronously on invalid input, but a
419
+ * server-side initiation rejection surfaces as an `"error"` event (with
420
+ * `category`/`retryable`) rather than a throw.
381
421
  *
382
422
  * @example
383
423
  * ```ts
@@ -503,8 +543,8 @@ interface NylonPaySdk {
503
543
  interface PaymentInstance {
504
544
  /** The transaction reference. Immutable after creation. */
505
545
  readonly reference: string;
506
- /** Current transaction status. `null` until the first poll resolves. */
507
- readonly status: TransactionStatus | null;
546
+ /** Current transaction status, set from the initiation response and updated on each status change. */
547
+ readonly status: TransactionStatus;
508
548
  /**
509
549
  * Register a handler for a payment event. Fires every time the event
510
550
  * occurs. Returns the instance for chaining.
@@ -593,15 +633,24 @@ declare function createNylonPay(config: NylonPayConfig): NylonPaySdk;
593
633
  */
594
634
 
595
635
  /**
596
- * Parse an error string into an SdkError object.
597
- * Tries JSON.parse first; falls back to a generic UNKNOWN error.
636
+ * Convert a structured SdkError into a throwable Error that still carries the
637
+ * category and retryable flag. Used by async operations that throw on
638
+ * initiation failure (invalid key, etc.) so merchants can `catch (e)` and read
639
+ * `e.category`.
640
+ */
641
+ declare function createSdkError(error: SdkError): Error & SdkError;
642
+ /**
643
+ * Parse an error string into a structured SdkError with a `category`.
644
+ * Tries the JSON envelope first; otherwise pulls the server's
645
+ * ` -- error-type: <category>` suffix off a raw message, falling back to
646
+ * category `internal` when untagged.
598
647
  *
599
648
  * @example
600
649
  * ```ts
601
650
  * const result = await sdk.getStatus({ reference: "ORDER-123" });
602
651
  * if (!result.isOk) {
603
652
  * const error = parseError(result.error);
604
- * console.log(error.code, error.message);
653
+ * console.log(error.category, error.message);
605
654
  * }
606
655
  * ```
607
656
  */
@@ -624,4 +673,4 @@ declare function parseError(error: string): SdkError;
624
673
  */
625
674
  declare function verifyWebhookSignature(input: VerifyWebhookInput): boolean;
626
675
 
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 };
676
+ 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 };