@keverdjs/fraud-sdk-react 1.1.0 → 1.1.2

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.mjs CHANGED
@@ -655,7 +655,6 @@ var KeverdSDK = class {
655
655
  throw new Error("Keverd SDK: apiKey is required");
656
656
  }
657
657
  this.config = {
658
- endpoint: config.endpoint || this.getDefaultEndpoint(),
659
658
  debug: false,
660
659
  ...config
661
660
  };
@@ -663,10 +662,10 @@ var KeverdSDK = class {
663
662
  this.sessionId = this.generateSessionId();
664
663
  this.isInitialized = true;
665
664
  if (this.config.debug) {
666
- console.log("[Keverd SDK] Initialized successfully", {
667
- endpoint: this.config.endpoint
668
- });
665
+ console.log("[Keverd SDK] Initialized successfully");
669
666
  }
667
+ this.startSession().catch(() => {
668
+ });
670
669
  }
671
670
  /**
672
671
  * Get visitor data (fingerprint and risk assessment)
@@ -701,6 +700,17 @@ var KeverdSDK = class {
701
700
  throw keverdError;
702
701
  }
703
702
  }
703
+ /**
704
+ * Extract origin and referrer from browser
705
+ */
706
+ getOriginHeaders() {
707
+ if (typeof window === "undefined") {
708
+ return {};
709
+ }
710
+ const origin = window.location.origin || void 0;
711
+ const referrer = document.referrer || void 0;
712
+ return { origin, referrer };
713
+ }
704
714
  /**
705
715
  * Send fingerprint request to backend
706
716
  */
@@ -708,13 +718,19 @@ var KeverdSDK = class {
708
718
  if (!this.config) {
709
719
  throw new Error("SDK not initialized");
710
720
  }
711
- const endpoint = this.config.endpoint || this.getDefaultEndpoint();
712
- const url = `${endpoint}/fingerprint/score`;
721
+ const url = `${this.getDefaultEndpoint()}/fingerprint/score`;
713
722
  const headers = {
714
723
  "Content-Type": "application/json",
715
724
  "X-SDK-Source": "react"
716
725
  // Identify SDK source for backend analytics
717
726
  };
727
+ const { origin, referrer } = this.getOriginHeaders();
728
+ if (origin) {
729
+ headers["X-Origin-URL"] = origin;
730
+ }
731
+ if (referrer) {
732
+ headers["X-Referrer-URL"] = referrer;
733
+ }
718
734
  const apiKey = this.config.apiKey;
719
735
  if (apiKey) {
720
736
  headers["x-keverd-key"] = apiKey;
@@ -724,10 +740,17 @@ var KeverdSDK = class {
724
740
  if (options?.tag === "sandbox") {
725
741
  headers["X-Sandbox"] = "true";
726
742
  }
743
+ const requestBody = {
744
+ ...request,
745
+ session: {
746
+ ...request.session || {},
747
+ sessionId: this.sessionId || request.session?.sessionId
748
+ }
749
+ };
727
750
  const response = await fetch(url, {
728
751
  method: "POST",
729
752
  headers,
730
- body: JSON.stringify(request)
753
+ body: JSON.stringify(requestBody)
731
754
  });
732
755
  if (!response.ok) {
733
756
  const errorText = await response.text().catch(() => "Unknown error");
@@ -773,10 +796,204 @@ var KeverdSDK = class {
773
796
  generateSessionId() {
774
797
  return `${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
775
798
  }
799
+ /**
800
+ * Start a new session (called automatically on init, but can be called manually)
801
+ */
802
+ async startSession(userId, deviceHash, metadata) {
803
+ if (!this.isInitialized || !this.config) {
804
+ throw new Error("Keverd SDK not initialized. Call init() first.");
805
+ }
806
+ if (!this.sessionId) {
807
+ this.sessionId = this.generateSessionId();
808
+ }
809
+ try {
810
+ const url = `${this.getDefaultEndpoint()}/dashboard/sessions/start`;
811
+ const headers = {
812
+ "Content-Type": "application/json"
813
+ };
814
+ const apiKey = this.config.apiKey;
815
+ if (apiKey) {
816
+ headers["x-keverd-key"] = apiKey;
817
+ headers["X-API-KEY"] = apiKey;
818
+ headers["Authorization"] = `Bearer ${apiKey}`;
819
+ }
820
+ const deviceInfo = this.deviceCollector.collect();
821
+ await fetch(url, {
822
+ method: "POST",
823
+ headers,
824
+ body: JSON.stringify({
825
+ session_id: this.sessionId,
826
+ user_id: userId || this.config.userId,
827
+ device_hash: deviceHash || deviceInfo.fingerprint,
828
+ session_metadata: metadata || {},
829
+ user_agent: navigator.userAgent,
830
+ browser: this._detectBrowser(),
831
+ os: this._detectOS(),
832
+ platform: "web",
833
+ sdk_type: "web",
834
+ sdk_source: "react"
835
+ })
836
+ });
837
+ if (this.config.debug) {
838
+ console.log(`[Keverd SDK] Session started: ${this.sessionId}`);
839
+ }
840
+ } catch (error) {
841
+ if (this.config.debug) {
842
+ console.warn("[Keverd SDK] Failed to start session on server:", error);
843
+ }
844
+ }
845
+ }
846
+ /**
847
+ * End the current session
848
+ */
849
+ async endSession() {
850
+ if (!this.isInitialized || !this.config || !this.sessionId) {
851
+ return;
852
+ }
853
+ try {
854
+ const url = `${this.getDefaultEndpoint()}/dashboard/sessions/${this.sessionId}/end`;
855
+ const headers = {
856
+ "Content-Type": "application/json"
857
+ };
858
+ const apiKey = this.config.apiKey;
859
+ if (apiKey) {
860
+ headers["x-keverd-key"] = apiKey;
861
+ headers["X-API-KEY"] = apiKey;
862
+ headers["Authorization"] = `Bearer ${apiKey}`;
863
+ }
864
+ await fetch(url, {
865
+ method: "POST",
866
+ headers
867
+ });
868
+ if (this.config.debug) {
869
+ console.log(`[Keverd SDK] Session ended: ${this.sessionId}`);
870
+ }
871
+ } catch (error) {
872
+ if (this.config.debug) {
873
+ console.warn("[Keverd SDK] Failed to end session on server:", error);
874
+ }
875
+ }
876
+ }
877
+ /**
878
+ * Pause the current session (e.g., when app goes to background)
879
+ */
880
+ async pauseSession() {
881
+ if (!this.isInitialized || !this.config || !this.sessionId) {
882
+ return;
883
+ }
884
+ try {
885
+ const url = `${this.getDefaultEndpoint()}/dashboard/sessions/${this.sessionId}/pause`;
886
+ const headers = {
887
+ "Content-Type": "application/json"
888
+ };
889
+ const apiKey = this.config.apiKey;
890
+ if (apiKey) {
891
+ headers["x-keverd-key"] = apiKey;
892
+ headers["X-API-KEY"] = apiKey;
893
+ headers["Authorization"] = `Bearer ${apiKey}`;
894
+ }
895
+ await fetch(url, {
896
+ method: "POST",
897
+ headers
898
+ });
899
+ if (this.config.debug) {
900
+ console.log(`[Keverd SDK] Session paused: ${this.sessionId}`);
901
+ }
902
+ } catch (error) {
903
+ if (this.config.debug) {
904
+ console.warn("[Keverd SDK] Failed to pause session on server:", error);
905
+ }
906
+ }
907
+ }
908
+ /**
909
+ * Resume a paused session (e.g., when app comes to foreground)
910
+ */
911
+ async resumeSession() {
912
+ if (!this.isInitialized || !this.config || !this.sessionId) {
913
+ return;
914
+ }
915
+ try {
916
+ const url = `${this.getDefaultEndpoint()}/dashboard/sessions/${this.sessionId}/resume`;
917
+ const headers = {
918
+ "Content-Type": "application/json"
919
+ };
920
+ const apiKey = this.config.apiKey;
921
+ if (apiKey) {
922
+ headers["x-keverd-key"] = apiKey;
923
+ headers["X-API-KEY"] = apiKey;
924
+ headers["Authorization"] = `Bearer ${apiKey}`;
925
+ }
926
+ await fetch(url, {
927
+ method: "POST",
928
+ headers
929
+ });
930
+ if (this.config.debug) {
931
+ console.log(`[Keverd SDK] Session resumed: ${this.sessionId}`);
932
+ }
933
+ } catch (error) {
934
+ if (this.config.debug) {
935
+ console.warn("[Keverd SDK] Failed to resume session on server:", error);
936
+ }
937
+ }
938
+ }
939
+ /**
940
+ * Get current session status
941
+ */
942
+ async getSessionStatus() {
943
+ if (!this.isInitialized || !this.config || !this.sessionId) {
944
+ return null;
945
+ }
946
+ try {
947
+ const url = `${this.getDefaultEndpoint()}/dashboard/sessions/${this.sessionId}/status`;
948
+ const headers = {};
949
+ const apiKey = this.config.apiKey;
950
+ if (apiKey) {
951
+ headers["x-keverd-key"] = apiKey;
952
+ headers["X-API-KEY"] = apiKey;
953
+ headers["Authorization"] = `Bearer ${apiKey}`;
954
+ }
955
+ const response = await fetch(url, {
956
+ method: "GET",
957
+ headers
958
+ });
959
+ if (!response.ok) {
960
+ return null;
961
+ }
962
+ return await response.json();
963
+ } catch (error) {
964
+ if (this.config.debug) {
965
+ console.warn("[Keverd SDK] Failed to get session status:", error);
966
+ }
967
+ return null;
968
+ }
969
+ }
970
+ /**
971
+ * Helper methods for browser/OS detection
972
+ */
973
+ _detectBrowser() {
974
+ const ua = navigator.userAgent;
975
+ if (ua.includes("Chrome") && !ua.includes("Edg")) return "Chrome";
976
+ if (ua.includes("Firefox")) return "Firefox";
977
+ if (ua.includes("Safari") && !ua.includes("Chrome")) return "Safari";
978
+ if (ua.includes("Edg")) return "Edge";
979
+ return "Unknown";
980
+ }
981
+ _detectOS() {
982
+ const ua = navigator.userAgent;
983
+ if (ua.includes("Windows")) return "Windows";
984
+ if (ua.includes("Mac")) return "macOS";
985
+ if (ua.includes("Linux")) return "Linux";
986
+ if (ua.includes("Android")) return "Android";
987
+ if (ua.includes("iOS") || ua.includes("iPhone") || ua.includes("iPad")) return "iOS";
988
+ return "Unknown";
989
+ }
776
990
  /**
777
991
  * Destroy the SDK instance
778
992
  */
779
- destroy() {
993
+ async destroy() {
994
+ if (this.sessionId) {
995
+ await this.endSession();
996
+ }
780
997
  const wasDebug = this.config?.debug;
781
998
  this.behavioralCollector.stop();
782
999
  this.isInitialized = false;
@@ -808,14 +1025,14 @@ function KeverdProvider({ loadOptions, children }) {
808
1025
  if (!sdk.isReady()) {
809
1026
  sdk.init({
810
1027
  apiKey: loadOptions.apiKey,
811
- endpoint: loadOptions.endpoint,
812
1028
  debug: loadOptions.debug || false
813
1029
  });
814
1030
  setIsReady(true);
815
1031
  }
816
1032
  return () => {
817
1033
  if (sdk.isReady()) {
818
- sdk.destroy();
1034
+ sdk.destroy().catch(() => {
1035
+ });
819
1036
  setIsReady(false);
820
1037
  }
821
1038
  };