@moneymq/sdk 0.9.1 → 0.9.4

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.mts CHANGED
@@ -41,7 +41,7 @@ interface ExperimentConfig {
41
41
  exposure: number;
42
42
  }
43
43
  /** Product feature definition */
44
- interface ProductFeature {
44
+ interface ProductFeature$1 {
45
45
  name?: string;
46
46
  description?: string;
47
47
  value?: unknown;
@@ -80,7 +80,7 @@ interface Product {
80
80
  /** Selected experiment variant ID (e.g., "surfnet-lite#a" while id is "surfnet-lite") */
81
81
  experimentId?: string;
82
82
  /** Product features */
83
- features?: Record<string, ProductFeature>;
83
+ features?: Record<string, ProductFeature$1>;
84
84
  /** Default price for this product */
85
85
  defaultPrice?: ProductPrice;
86
86
  }
@@ -305,6 +305,45 @@ interface AddEventListenerOptions {
305
305
  interface EventListenerOptions {
306
306
  capture?: boolean;
307
307
  }
308
+ /**
309
+ * Product feature definition
310
+ */
311
+ interface ProductFeature {
312
+ /** Feature display name */
313
+ name?: string;
314
+ /** Feature description */
315
+ description?: string;
316
+ /** Feature value (can be bool, number, string, etc.) */
317
+ value?: unknown;
318
+ }
319
+ /**
320
+ * Basket item representing a product in a transaction
321
+ */
322
+ interface BasketItem$1 {
323
+ /** Product ID from catalog */
324
+ productId: string;
325
+ /** Experiment variant ID (e.g., "surfnet-lite#a") */
326
+ experimentId?: string;
327
+ /** Product features (capabilities and limits purchased) */
328
+ features?: Record<string, ProductFeature> | unknown;
329
+ /** Quantity of items */
330
+ quantity?: number;
331
+ }
332
+ /**
333
+ * Payment details from x402 payment
334
+ */
335
+ interface PaymentDetails$1 {
336
+ /** Payer address/wallet */
337
+ payer: string;
338
+ /** Transaction ID/signature */
339
+ transactionId: string;
340
+ /** Payment amount as string */
341
+ amount: string;
342
+ /** Currency code (e.g., "USDC") */
343
+ currency: string;
344
+ /** Network name (e.g., "solana") */
345
+ network: string;
346
+ }
308
347
  /**
309
348
  * Channel event envelope
310
349
  */
@@ -377,12 +416,10 @@ interface Transaction {
377
416
  id: string;
378
417
  /** Channel ID for this transaction */
379
418
  channelId: string;
380
- /** Payment amount in smallest unit */
381
- amount: number;
382
- /** Currency code */
383
- currency: string;
384
- /** Product ID if available */
385
- productId?: string;
419
+ /** Basket items (products being purchased with features) */
420
+ basket: BasketItem$1[];
421
+ /** Payment details from x402 */
422
+ payment?: PaymentDetails$1;
386
423
  /** Custom metadata */
387
424
  metadata?: Record<string, unknown>;
388
425
  }
@@ -393,6 +430,76 @@ declare class ChannelError extends Error {
393
430
  code: string;
394
431
  constructor(message: string, code: string);
395
432
  }
433
+ /**
434
+ * Event message from the stream
435
+ */
436
+ interface EventStreamMessage<T = unknown> {
437
+ type: 'event';
438
+ event: ChannelEvent<T>;
439
+ }
440
+ /**
441
+ * Connection state change message
442
+ */
443
+ interface StateStreamMessage {
444
+ type: 'state';
445
+ state: ConnectionState;
446
+ }
447
+ /**
448
+ * Error message from the stream
449
+ */
450
+ interface ErrorStreamMessage {
451
+ type: 'error';
452
+ error: ChannelError;
453
+ }
454
+ /**
455
+ * Transaction with actor factory method and convenience methods
456
+ */
457
+ interface TransactionWithActor extends Transaction {
458
+ /** Create an actor scoped to this transaction's channel */
459
+ actor(options?: Omit<ActorOptions, 'secret'>): EventActor;
460
+ /**
461
+ * Stream events for this transaction
462
+ *
463
+ * Creates an actor internally and yields events from the transaction's channel.
464
+ */
465
+ events(options?: {
466
+ signal?: AbortSignal;
467
+ }): AsyncGenerator<ChannelEvent>;
468
+ /**
469
+ * Attach data to this transaction's channel
470
+ *
471
+ * Creates an actor internally if not already created.
472
+ * The server will create a signed receipt JWT and emit a transaction:completed event.
473
+ */
474
+ attach<T = unknown>(data: T): Promise<ChannelEvent<T>>;
475
+ }
476
+ /**
477
+ * Transaction message (for receivers)
478
+ * Note: The transaction has an actor() method to create a scoped EventActor
479
+ */
480
+ interface TransactionStreamMessage {
481
+ type: 'transaction';
482
+ transaction: TransactionWithActor;
483
+ }
484
+ /**
485
+ * Union of all channel stream message types
486
+ */
487
+ type ChannelStreamMessage<T = unknown> = EventStreamMessage<T> | StateStreamMessage | ErrorStreamMessage;
488
+ /**
489
+ * Union of all receiver stream message types
490
+ */
491
+ type ReceiverStreamMessage = TransactionStreamMessage | StateStreamMessage | ErrorStreamMessage;
492
+ /**
493
+ * Options for the stream() method
494
+ */
495
+ interface StreamOptions {
496
+ /** AbortSignal to cancel the stream */
497
+ signal?: AbortSignal;
498
+ /** Include state change messages (default: true) */
499
+ includeState?: boolean;
500
+ /** Include error messages (default: true) */
501
+ includeErrors?: boolean;
502
+ }
396
503
  /**
397
504
  * Base class for channel connections (shared by Reader and Actor)
398
505
  */
@@ -443,6 +550,47 @@ declare abstract class BaseChannel {
443
550
  * Disconnect from the channel
444
551
  */
445
552
  disconnect(): void;
553
+ /**
554
+ * Stream events as an async iterable
555
+ *
556
+ * Automatically connects and yields messages until disconnected or aborted.
557
+ *
558
+ * @example
559
+ * ```typescript
560
+ * const reader = moneymq.payment.listener('tx-123');
561
+ *
562
+ * for await (const message of reader.stream()) {
563
+ * if (message.type === 'event') {
564
+ * console.log('Event:', message.event.type, message.event.data);
565
+ * } else if (message.type === 'state') {
566
+ * console.log('State:', message.state);
567
+ * } else if (message.type === 'error') {
568
+ * console.error('Error:', message.error);
569
+ * }
570
+ * }
571
+ * ```
572
+ */
573
+ stream<T = unknown>(options?: StreamOptions): AsyncGenerator<ChannelStreamMessage<T>>;
574
+ /**
575
+ * Stream events directly as an async iterable
576
+ *
577
+ * Simpler API that only yields events, ignoring state/error messages.
578
+ * Automatically connects and yields until disconnected or aborted.
579
+ *
580
+ * @example
581
+ * ```typescript
582
+ * const listener = moneymq.payment.listener('tx-123');
583
+ *
584
+ * for await (const event of listener.events()) {
585
+ * if (event.type === 'payment:settled') {
586
+ * console.log('Payment settled:', event.data.amount);
587
+ * }
588
+ * }
589
+ * ```
590
+ */
591
+ events<T = unknown>(options?: {
592
+ signal?: AbortSignal;
593
+ }): AsyncGenerator<ChannelEvent<T>>;
446
594
  protected abstract buildUrl(): string;
447
595
  protected handleMessage(event: MessageEvent): void;
448
596
  protected setState(state: ConnectionState): void;
@@ -518,22 +666,81 @@ declare class EventActor extends BaseChannel {
518
666
  /**
519
667
  * Transaction wrapper for receiver callbacks
520
668
  */
521
- declare class TransactionContext implements Transaction {
669
+ declare class TransactionContext implements TransactionWithActor {
522
670
  id: string;
523
671
  channelId: string;
524
- amount: number;
525
- currency: string;
526
- productId?: string;
672
+ basket: BasketItem$1[];
673
+ payment?: PaymentDetails$1;
527
674
  metadata?: Record<string, unknown>;
528
675
  private endpoint;
529
676
  private secret;
677
+ private _actor;
530
678
  constructor(data: Transaction, endpoint: string, secret: string);
679
+ /**
680
+ * Get or create the internal actor (lazy initialization)
681
+ */
682
+ private getOrCreateActor;
683
+ /**
684
+ * Get the payment amount as string (from payment details)
685
+ */
686
+ get amount(): string;
687
+ /**
688
+ * Get the currency (from payment details)
689
+ */
690
+ get currency(): string;
691
+ /**
692
+ * Get the first product ID from basket (convenience method)
693
+ */
694
+ get productId(): string | undefined;
695
+ /**
696
+ * Get the payer address (from payment details)
697
+ */
698
+ get payer(): string | undefined;
699
+ /**
700
+ * Get the network (from payment details)
701
+ */
702
+ get network(): string | undefined;
703
+ /**
704
+ * Get features for the first product in basket (convenience method)
705
+ */
706
+ get features(): Record<string, ProductFeature> | unknown | undefined;
531
707
  /**
532
708
  * Create an actor scoped to this transaction's channel
533
709
  *
534
710
  * The actor is automatically connected.
711
+ * Note: If you use events() or send() methods, they share the same internal actor.
535
712
  */
536
713
  actor(options?: Omit<ActorOptions, 'secret'>): EventActor;
714
+ /**
715
+ * Stream events for this transaction
716
+ *
717
+ * Creates an actor internally and yields events from the transaction's channel.
718
+ *
719
+ * @example
720
+ * ```typescript
721
+ * for await (const event of tx.events()) {
722
+ * if (event.type === 'payment:settled') {
723
+ * await tx.send('order:completed', { orderId: tx.id });
724
+ * break;
725
+ * }
726
+ * }
727
+ * ```
728
+ */
729
+ events(options?: {
730
+ signal?: AbortSignal;
731
+ }): AsyncGenerator<ChannelEvent>;
732
+ /**
733
+ * Attach data to this transaction's channel
734
+ *
735
+ * Creates an actor internally if not already created.
736
+ * The server will create a signed receipt JWT and emit a transaction:completed event.
737
+ *
738
+ * @example
739
+ * ```typescript
740
+ * await tx.attach({ orderId: tx.id, trackingNumber: '...' });
741
+ * ```
742
+ */
743
+ attach<T = unknown>(data: T): Promise<ChannelEvent<T>>;
537
744
  }
538
745
  /**
539
746
  * Transaction handler callback
@@ -606,6 +813,57 @@ declare class EventReceiver {
606
813
  * Disconnect from the transaction stream
607
814
  */
608
815
  disconnect(): void;
816
+ /**
817
+ * Stream transactions as an async iterable
818
+ *
819
+ * Automatically connects and yields messages until disconnected or aborted.
820
+ *
821
+ * @example
822
+ * ```typescript
823
+ * const receiver = moneymq.payment.processor();
824
+ *
825
+ * for await (const message of receiver.stream()) {
826
+ * if (message.type === 'transaction') {
827
+ * const tx = message.transaction;
828
+ * console.log('New transaction:', tx.id);
829
+ *
830
+ * const actor = tx.actor();
831
+ * // Handle the transaction...
832
+ * } else if (message.type === 'state') {
833
+ * console.log('State:', message.state);
834
+ * } else if (message.type === 'error') {
835
+ * console.error('Error:', message.error);
836
+ * }
837
+ * }
838
+ * ```
839
+ */
840
+ stream(options?: StreamOptions): AsyncGenerator<ReceiverStreamMessage>;
841
+ /**
842
+ * Stream transactions directly as an async iterable
843
+ *
844
+ * Simpler API that only yields transactions, ignoring state/error messages.
845
+ * Automatically connects and yields until disconnected or aborted.
846
+ *
847
+ * @example
848
+ * ```typescript
849
+ * const processor = moneymq.payment.processor();
850
+ *
851
+ * for await (const tx of processor.transactions()) {
852
+ * console.log('New transaction:', tx.id, tx.payment?.amount);
853
+ *
854
+ * const actor = tx.actor();
855
+ * for await (const event of actor.events()) {
856
+ * if (event.type === 'payment:settled') {
857
+ * await actor.send('order:completed', { orderId: tx.id });
858
+ * break;
859
+ * }
860
+ * }
861
+ * }
862
+ * ```
863
+ */
864
+ transactions(options?: {
865
+ signal?: AbortSignal;
866
+ }): AsyncGenerator<TransactionWithActor>;
609
867
  private setState;
610
868
  private shouldReconnect;
611
869
  private scheduleReconnect;
@@ -1827,4 +2085,4 @@ declare class CheckoutReceipt {
1827
2085
  };
1828
2086
  }
1829
2087
 
1830
- export { type ActorOptions, type Attachments, type BasketItem, ChannelError, type ChannelErrorHandler, type ChannelEvent, type ChannelEventHandler, type CheckoutCreateParams, type CheckoutLineItem, CheckoutReceipt, type CheckoutSession, type CloudEventEnvelope, type ConnectionHandler, type ConnectionState, type Customer, type CustomerCreateParams, type CustomerUpdateParams, type ErrorHandler, EventActor, type EventHandler, EventReader, EventReceiver, EventStream, type EventStreamOptions, type EventStreamState, type ExperimentConfig, type GetSignerParams, MoneyMQ, type MoneyMQConfig, type MoneyMQEventMap, type MoneyMQEventType, type PayParams, type PayResult, type Payment, type PaymentDetails, type PaymentEvent, type PaymentFlow, type PaymentIntent, type PaymentIntentCreateParams, type PaymentLink, type PaymentLinkCreateParams, type PaymentListParams, PaymentRequiredError, type PaymentRequirements, type PaymentSettlementEvent, type PaymentSettlementFailedData, type PaymentSettlementSucceededData, type PaymentVerificationEvent, type PaymentVerificationFailedData, type PaymentVerificationSucceededData, type Payout, type PayoutCreateParams, type PayoutListParams, type PayoutSettings, type PayoutSettingsUpdateParams, type Price, type PriceCreateParams, type PriceRecurring, type ProcessorData, type Product, type ProductAccessParams, type ProductAccessResponse, type ProductCreateParams, type ProductFeature, type ProductListParams, type ProductPrice, type ReaderOptions, type ReceiptClaims, type ReceiverOptions, type ServerConfig, type StateHandler, type Transaction, type TransactionHandler, type X402ClientConfig, buildEventStreamUrl, computeChannelId, createEventActor, createEventReader, createEventReceiver, createEventStream, fetchConfig, getRpcUrl, isPaymentSettlementFailed, isPaymentSettlementSucceeded, isPaymentVerificationFailed, isPaymentVerificationSucceeded, parseCloudEvent };
2088
+ export { type ActorOptions, type Attachments, type BasketItem, ChannelError, type ChannelErrorHandler, type ChannelEvent, type ChannelEventHandler, type ChannelStreamMessage, type CheckoutCreateParams, type CheckoutLineItem, CheckoutReceipt, type CheckoutSession, type CloudEventEnvelope, type ConnectionHandler, type ConnectionState, type Customer, type CustomerCreateParams, type CustomerUpdateParams, type ErrorHandler, type ErrorStreamMessage, EventActor, type EventHandler, EventReader, EventReceiver, EventStream, type EventStreamMessage, type EventStreamOptions, type EventStreamState, type ExperimentConfig, type GetSignerParams, MoneyMQ, type MoneyMQConfig, type MoneyMQEventMap, type MoneyMQEventType, type PayParams, type PayResult, type Payment, type PaymentDetails, type PaymentEvent, type PaymentFlow, type PaymentIntent, type PaymentIntentCreateParams, type PaymentLink, type PaymentLinkCreateParams, type PaymentListParams, PaymentRequiredError, type PaymentRequirements, type PaymentSettlementEvent, type PaymentSettlementFailedData, type PaymentSettlementSucceededData, type PaymentVerificationEvent, type PaymentVerificationFailedData, type PaymentVerificationSucceededData, type Payout, type PayoutCreateParams, type PayoutListParams, type PayoutSettings, type PayoutSettingsUpdateParams, type Price, type PriceCreateParams, type PriceRecurring, type ProcessorData, type Product, type ProductAccessParams, type ProductAccessResponse, type ProductCreateParams, type ProductFeature$1 as ProductFeature, type ProductListParams, type ProductPrice, type ReaderOptions, type ReceiptClaims, type ReceiverOptions, type ReceiverStreamMessage, type ServerConfig, type StateHandler, type StateStreamMessage, type StreamOptions, type Transaction, type TransactionHandler, type TransactionStreamMessage, type TransactionWithActor, type X402ClientConfig, buildEventStreamUrl, computeChannelId, createEventActor, createEventReader, createEventReceiver, createEventStream, fetchConfig, getRpcUrl, isPaymentSettlementFailed, isPaymentSettlementSucceeded, isPaymentVerificationFailed, isPaymentVerificationSucceeded, parseCloudEvent };