@naturalpay/sdk 0.0.2 → 0.0.3

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.cjs CHANGED
@@ -585,6 +585,32 @@ var BaseResource = class {
585
585
 
586
586
  // src/resources/payments.ts
587
587
  var IDEMPOTENCY_WINDOW_SECONDS = 300;
588
+ function unwrapPayment(response) {
589
+ if (!response?.data) {
590
+ throw new NaturalError('Unexpected response format: missing "data" field in payment response');
591
+ }
592
+ const { data } = response;
593
+ if (data.type !== "payment" || !data.attributes) {
594
+ throw new NaturalError(
595
+ `Unexpected resource format: expected type "payment", got "${data.type}"`
596
+ );
597
+ }
598
+ const { id, attributes, relationships } = data;
599
+ const description = attributes.description != null ? String(attributes.description) : void 0;
600
+ return {
601
+ transferId: id,
602
+ status: String(attributes.status),
603
+ amount: String(attributes.amount),
604
+ currency: String(attributes.currency),
605
+ description,
606
+ memo: description,
607
+ createdAt: attributes.createdAt != null ? String(attributes.createdAt) : void 0,
608
+ updatedAt: attributes.updatedAt != null ? String(attributes.updatedAt) : void 0,
609
+ claimLink: attributes.claimLink != null ? String(attributes.claimLink) : void 0,
610
+ counterpartyPartyId: relationships?.counterparty?.data?.id,
611
+ customerPartyId: relationships?.customerParty?.data?.id
612
+ };
613
+ }
588
614
  var PaymentsResource = class extends BaseResource {
589
615
  /**
590
616
  * Generate idempotency key based on payment details + time window.
@@ -615,20 +641,19 @@ var PaymentsResource = class extends BaseResource {
615
641
  }
616
642
  const recipient = params.recipientEmail ?? params.recipientPhone ?? params.recipientPartyId;
617
643
  const idempotencyKey = params.idempotencyKey ?? this.generateIdempotencyKey(recipient, params.amount, params.memo);
618
- const body = {
619
- amount: params.amount
644
+ const attributes = {
645
+ amount: String(params.amount),
646
+ currency: "USD",
647
+ counterparty: recipient,
648
+ customerPartyId: params.customerPartyId
620
649
  };
621
- if (params.recipientEmail) body["recipientEmail"] = params.recipientEmail;
622
- if (params.recipientPhone) body["recipientPhone"] = params.recipientPhone;
623
- if (params.recipientPartyId) body["recipientPartyId"] = params.recipientPartyId;
624
- if (params.memo) body["memo"] = params.memo;
625
- if (params.agentId) body["agentId"] = params.agentId;
626
- if (params.customerPartyId) body["customerPartyId"] = params.customerPartyId;
627
- if (params.instanceId) body["instanceId"] = params.instanceId;
628
- return this.http.post("/payments/initiate", {
650
+ if (params.memo) attributes["description"] = params.memo;
651
+ const body = { data: { attributes } };
652
+ const response = await this.http.post("/payments", {
629
653
  body,
630
654
  headers: { "Idempotency-Key": idempotencyKey }
631
655
  });
656
+ return unwrapPayment(response);
632
657
  }
633
658
  /**
634
659
  * Get payment status by transfer ID.
@@ -637,20 +662,32 @@ var PaymentsResource = class extends BaseResource {
637
662
  * @returns Payment object with current status
638
663
  */
639
664
  async retrieve(transferId) {
640
- return this.http.get(`/payments/${transferId}`);
641
- }
642
- /**
643
- * Cancel a pending payment.
644
- *
645
- * @param transferId - The transfer ID to cancel
646
- * @returns Cancellation result with status and message
647
- */
648
- async cancel(transferId) {
649
- return this.http.post(`/payments/${transferId}/cancel`);
665
+ const response = await this.http.get(`/payments/${transferId}`);
666
+ return unwrapPayment(response);
650
667
  }
651
668
  };
652
669
 
653
670
  // src/resources/wallet.ts
671
+ function unwrapBalance(response) {
672
+ if (!response?.data) {
673
+ throw new NaturalError(
674
+ 'Unexpected response format: missing "data" field in wallet balance response'
675
+ );
676
+ }
677
+ const { data } = response;
678
+ if (data.type !== "wallet_balance" || !data.attributes) {
679
+ throw new NaturalError(
680
+ `Unexpected resource format: expected type "wallet_balance", got "${data.type}"`
681
+ );
682
+ }
683
+ const { id, attributes } = data;
684
+ return {
685
+ walletId: id,
686
+ balances: attributes.balances,
687
+ pendingClaimAmountMinor: attributes.pendingClaimAmountMinor != null ? Number(attributes.pendingClaimAmountMinor) : void 0,
688
+ pendingClaimCount: attributes.pendingClaimCount != null ? Number(attributes.pendingClaimCount) : void 0
689
+ };
690
+ }
654
691
  var WalletResource = class extends BaseResource {
655
692
  /**
656
693
  * Get current wallet balance.
@@ -658,7 +695,8 @@ var WalletResource = class extends BaseResource {
658
695
  * @returns AccountBalance with available, current, pending amounts
659
696
  */
660
697
  async balance() {
661
- return this.http.get("/wallet/balance");
698
+ const response = await this.http.get("/wallet/balance");
699
+ return unwrapBalance(response);
662
700
  }
663
701
  /**
664
702
  * Initiate a deposit from a linked bank account.
@@ -695,6 +733,9 @@ var WalletResource = class extends BaseResource {
695
733
  if (params.description) {
696
734
  body["description"] = params.description;
697
735
  }
736
+ if (params.walletId) {
737
+ body["walletId"] = params.walletId;
738
+ }
698
739
  return this.http.post("/wallet/withdraw", {
699
740
  body,
700
741
  headers: { "Idempotency-Key": params.idempotencyKey }
@@ -703,12 +744,42 @@ var WalletResource = class extends BaseResource {
703
744
  };
704
745
 
705
746
  // src/resources/transactions.ts
747
+ function unwrapTransactionResource(resource) {
748
+ if (resource.type !== "transaction" || !resource.attributes) {
749
+ throw new NaturalError(
750
+ `Unexpected resource format: expected type "transaction", got "${resource.type}"`
751
+ );
752
+ }
753
+ const { id, attributes, relationships } = resource;
754
+ return {
755
+ transactionId: id,
756
+ amount: String(attributes.amount),
757
+ currency: String(attributes.currency),
758
+ status: String(attributes.status),
759
+ description: attributes.description != null ? String(attributes.description) : void 0,
760
+ memo: attributes.memo != null ? String(attributes.memo) : void 0,
761
+ createdAt: String(attributes.createdAt),
762
+ updatedAt: attributes.updatedAt != null ? String(attributes.updatedAt) : void 0,
763
+ isDelegated: Boolean(attributes.isDelegated),
764
+ customerName: attributes.customerName != null ? String(attributes.customerName) : void 0,
765
+ customerAgentId: attributes.customerAgentId != null ? String(attributes.customerAgentId) : void 0,
766
+ senderName: attributes.senderName != null ? String(attributes.senderName) : void 0,
767
+ recipientName: attributes.recipientName != null ? String(attributes.recipientName) : void 0,
768
+ transactionType: String(attributes.transactionType),
769
+ category: String(attributes.category),
770
+ direction: String(attributes.direction),
771
+ sourcePartyId: relationships?.sourceParty?.data?.id,
772
+ destinationPartyId: relationships?.destinationParty?.data?.id,
773
+ sourceWalletId: relationships?.sourceWallet?.data?.id,
774
+ destinationWalletId: relationships?.destinationWallet?.data?.id
775
+ };
776
+ }
706
777
  var TransactionsResource = class extends BaseResource {
707
778
  /**
708
779
  * List recent transactions.
709
780
  *
710
781
  * @param params - List parameters including agent context
711
- * @returns List of Transaction objects
782
+ * @returns TransactionListResponse with transactions and pagination info
712
783
  */
713
784
  async list(params) {
714
785
  const headers = {};
@@ -718,20 +789,69 @@ var TransactionsResource = class extends BaseResource {
718
789
  if (params?.customerPartyId) {
719
790
  headers["X-On-Behalf-Of"] = params.customerPartyId;
720
791
  }
721
- const data = await this.http.get("/transactions", {
792
+ const response = await this.http.get("/transactions", {
722
793
  params: {
723
- limit: params?.limit ?? 10,
724
- offset: params?.offset ?? 0,
725
- customerFilter: params?.customerFilter,
794
+ limit: params?.limit ?? 50,
795
+ cursor: params?.cursor,
726
796
  type: params?.type
727
797
  },
728
798
  headers: Object.keys(headers).length > 0 ? headers : void 0
729
799
  });
730
- return data.transfers ?? data.transactions ?? [];
800
+ if (!response?.data || !Array.isArray(response.data)) {
801
+ throw new NaturalError(
802
+ 'Unexpected response format: missing "data" array in transaction list response'
803
+ );
804
+ }
805
+ const transactions = response.data.map(unwrapTransactionResource);
806
+ const pagination = response.meta?.pagination ?? {};
807
+ return {
808
+ transactions,
809
+ hasMore: pagination.hasMore ?? false,
810
+ nextCursor: pagination.nextCursor ?? null
811
+ };
731
812
  }
732
813
  };
733
814
 
734
815
  // src/resources/agents.ts
816
+ function unwrapAgentResource(resource) {
817
+ if (resource.type !== "agent" || !resource.attributes) {
818
+ throw new NaturalError(
819
+ `Unexpected resource format: expected type "agent", got "${resource.type}"`
820
+ );
821
+ }
822
+ const { id, attributes, relationships } = resource;
823
+ return {
824
+ id,
825
+ name: String(attributes.name),
826
+ description: attributes.description != null ? String(attributes.description) : void 0,
827
+ status: String(attributes.status),
828
+ partyId: relationships?.party?.data?.id ?? "",
829
+ createdAt: attributes.createdAt != null ? String(attributes.createdAt) : void 0,
830
+ createdBy: attributes.createdBy != null ? String(attributes.createdBy) : void 0,
831
+ updatedAt: attributes.updatedAt != null ? String(attributes.updatedAt) : void 0,
832
+ updatedBy: attributes.updatedBy != null ? String(attributes.updatedBy) : void 0
833
+ };
834
+ }
835
+ function unwrapAgent(response) {
836
+ if (!response?.data) {
837
+ throw new NaturalError('Unexpected response format: missing "data" field in agent response');
838
+ }
839
+ return unwrapAgentResource(response.data);
840
+ }
841
+ function unwrapAgentList(response) {
842
+ if (!response?.data || !Array.isArray(response.data)) {
843
+ throw new NaturalError(
844
+ 'Unexpected response format: missing "data" array in agent list response'
845
+ );
846
+ }
847
+ const agents = response.data.map(unwrapAgentResource);
848
+ const pagination = response.meta?.pagination ?? {};
849
+ return {
850
+ agents,
851
+ hasMore: pagination.hasMore ?? false,
852
+ nextCursor: pagination.nextCursor ?? null
853
+ };
854
+ }
735
855
  var AgentsResource = class extends BaseResource {
736
856
  /**
737
857
  * List agents for the partner.
@@ -740,14 +860,15 @@ var AgentsResource = class extends BaseResource {
740
860
  * @returns AgentListResponse with list of agents
741
861
  */
742
862
  async list(params) {
743
- return this.http.get("/agents", {
863
+ const response = await this.http.get("/agents", {
744
864
  params: {
745
865
  status: params?.status,
746
866
  partyId: params?.partyId,
747
867
  limit: params?.limit ?? 50,
748
- offset: params?.offset ?? 0
868
+ cursor: params?.cursor
749
869
  }
750
870
  });
871
+ return unwrapAgentList(response);
751
872
  }
752
873
  /**
753
874
  * Get agent by ID.
@@ -756,7 +877,8 @@ var AgentsResource = class extends BaseResource {
756
877
  * @returns Agent details
757
878
  */
758
879
  async get(agentId) {
759
- return this.http.get(`/agents/${agentId}`);
880
+ const response = await this.http.get(`/agents/${agentId}`);
881
+ return unwrapAgent(response);
760
882
  }
761
883
  /**
762
884
  * Create a new agent.
@@ -765,21 +887,25 @@ var AgentsResource = class extends BaseResource {
765
887
  * @returns AgentCreateResponse with created agent details
766
888
  */
767
889
  async create(params) {
768
- const body = {
769
- name: params.name,
770
- partyId: params.partyId
890
+ const attributes = {
891
+ name: params.name
771
892
  };
772
893
  if (params.description) {
773
- body["description"] = params.description;
894
+ attributes["description"] = params.description;
895
+ }
896
+ if (params.limits) {
897
+ attributes["limits"] = params.limits;
774
898
  }
899
+ const body = { data: { attributes } };
775
900
  const headers = {};
776
901
  if (params.idempotencyKey) {
777
902
  headers["Idempotency-Key"] = params.idempotencyKey;
778
903
  }
779
- return this.http.post("/agents", {
904
+ const response = await this.http.post("/agents", {
780
905
  body,
781
906
  headers: Object.keys(headers).length > 0 ? headers : void 0
782
907
  });
908
+ return unwrapAgent(response);
783
909
  }
784
910
  /**
785
911
  * Update an existing agent.
@@ -789,18 +915,20 @@ var AgentsResource = class extends BaseResource {
789
915
  * @returns AgentUpdateResponse with updated agent details
790
916
  */
791
917
  async update(agentId, params) {
792
- const body = {};
793
- if (params.name !== void 0) body["name"] = params.name;
794
- if (params.description !== void 0) body["description"] = params.description;
795
- if (params.status !== void 0) body["status"] = params.status;
918
+ const attributes = {};
919
+ if (params.name !== void 0) attributes["name"] = params.name;
920
+ if (params.description !== void 0) attributes["description"] = params.description;
921
+ if (params.status !== void 0) attributes["status"] = params.status;
922
+ const body = { data: { attributes } };
796
923
  const headers = {};
797
924
  if (params.idempotencyKey) {
798
925
  headers["Idempotency-Key"] = params.idempotencyKey;
799
926
  }
800
- return this.http.put(`/agents/${agentId}`, {
927
+ const response = await this.http.put(`/agents/${agentId}`, {
801
928
  body,
802
929
  headers: Object.keys(headers).length > 0 ? headers : void 0
803
930
  });
931
+ return unwrapAgent(response);
804
932
  }
805
933
  /**
806
934
  * Delete an agent.
@@ -825,7 +953,9 @@ var DelegationsResource = class extends BaseResource {
825
953
  params: {
826
954
  status: params?.status,
827
955
  delegatingPartyId: params?.delegatingPartyId,
828
- delegatedPartyId: params?.delegatedPartyId
956
+ delegatedPartyId: params?.delegatedPartyId,
957
+ limit: params?.limit ?? 50,
958
+ cursor: params?.cursor
829
959
  }
830
960
  });
831
961
  }
@@ -838,30 +968,6 @@ var DelegationsResource = class extends BaseResource {
838
968
  async get(delegationId) {
839
969
  return this.http.get(`/delegations/${delegationId}`);
840
970
  }
841
- /**
842
- * Create a new delegation (party-to-party trust relationship).
843
- *
844
- * @param params - Delegation creation parameters
845
- * @returns Created Delegation
846
- */
847
- async create(params) {
848
- const body = {
849
- delegatingPartyId: params.delegatingPartyId,
850
- delegatedPartyId: params.delegatedPartyId,
851
- permissions: params.permissions
852
- };
853
- if (params.expiresAt) {
854
- body["expiresAt"] = params.expiresAt;
855
- }
856
- const headers = {};
857
- if (params.idempotencyKey) {
858
- headers["Idempotency-Key"] = params.idempotencyKey;
859
- }
860
- return this.http.post("/delegations", {
861
- body,
862
- headers: Object.keys(headers).length > 0 ? headers : void 0
863
- });
864
- }
865
971
  /**
866
972
  * Update an existing delegation.
867
973
  *
@@ -883,7 +989,7 @@ var DelegationsResource = class extends BaseResource {
883
989
  * @returns Revoked Delegation
884
990
  */
885
991
  async revoke(delegationId) {
886
- return this.http.put(`/delegations/${delegationId}/revoke`);
992
+ return this.update(delegationId, { status: "REVOKED" });
887
993
  }
888
994
  };
889
995
 
@@ -892,10 +998,16 @@ var CustomersResource = class extends BaseResource {
892
998
  /**
893
999
  * List customers onboarded by the partner via delegation.
894
1000
  *
895
- * @returns List of Customer objects with party info and delegation details
1001
+ * @param params - Pagination parameters
1002
+ * @returns CustomerListResponse with items, hasMore, nextCursor
896
1003
  */
897
- async list() {
898
- return this.http.get("/customers");
1004
+ async list(params) {
1005
+ return this.http.get("/customers", {
1006
+ params: {
1007
+ limit: params?.limit,
1008
+ cursor: params?.cursor
1009
+ }
1010
+ });
899
1011
  }
900
1012
  };
901
1013