@keverdjs/fraud-sdk-react 1.1.0 → 2.0.0

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/README.md CHANGED
@@ -85,7 +85,6 @@ interface KeverdProviderProps {
85
85
  ```typescript
86
86
  interface KeverdLoadOptions {
87
87
  apiKey: string; // Required: Your Keverd API key
88
- endpoint?: string; // Optional: Custom API endpoint (default: https://api.keverd.com)
89
88
  debug?: boolean; // Optional: Enable debug logging (default: false)
90
89
  }
91
90
  ```
@@ -151,21 +150,6 @@ const sdk = useKeverdContext();
151
150
 
152
151
  ## Configuration
153
152
 
154
- ### Custom Endpoint
155
-
156
- If you're using a custom backend endpoint:
157
-
158
- ```tsx
159
- <KeverdProvider
160
- loadOptions={{
161
- apiKey: 'your-api-key',
162
- endpoint: 'https://api.yourdomain.com/v1/fingerprint/score',
163
- }}
164
- >
165
- {children}
166
- </KeverdProvider>
167
- ```
168
-
169
153
  ### Debug Mode
170
154
 
171
155
  Enable debug logging for development:
package/dist/index.d.mts CHANGED
@@ -82,7 +82,6 @@ interface KeverdVisitorData {
82
82
  }
83
83
  interface KeverdConfig {
84
84
  apiKey: string;
85
- endpoint?: string;
86
85
  userId?: string;
87
86
  debug?: boolean;
88
87
  extendedResult?: boolean;
@@ -90,7 +89,6 @@ interface KeverdConfig {
90
89
  }
91
90
  interface KeverdLoadOptions {
92
91
  apiKey: string;
93
- endpoint?: string;
94
92
  debug?: boolean;
95
93
  }
96
94
  interface KeverdVisitorDataOptions {
@@ -151,10 +149,44 @@ declare class KeverdSDK {
151
149
  * Generate a session ID
152
150
  */
153
151
  private generateSessionId;
152
+ /**
153
+ * Start a new session (called automatically on init, but can be called manually)
154
+ */
155
+ startSession(userId?: string, deviceHash?: string, metadata?: Record<string, unknown>): Promise<void>;
156
+ /**
157
+ * End the current session
158
+ */
159
+ endSession(): Promise<void>;
160
+ /**
161
+ * Pause the current session (e.g., when app goes to background)
162
+ */
163
+ pauseSession(): Promise<void>;
164
+ /**
165
+ * Resume a paused session (e.g., when app comes to foreground)
166
+ */
167
+ resumeSession(): Promise<void>;
168
+ /**
169
+ * Get current session status
170
+ */
171
+ getSessionStatus(): Promise<{
172
+ session_id: string;
173
+ status: string;
174
+ is_active: boolean;
175
+ is_paused: boolean;
176
+ event_count: number;
177
+ started_at: string;
178
+ last_activity_at: string;
179
+ duration_seconds: number | null;
180
+ } | null>;
181
+ /**
182
+ * Helper methods for browser/OS detection
183
+ */
184
+ private _detectBrowser;
185
+ private _detectOS;
154
186
  /**
155
187
  * Destroy the SDK instance
156
188
  */
157
- destroy(): void;
189
+ destroy(): Promise<void>;
158
190
  /**
159
191
  * Get current configuration
160
192
  */
package/dist/index.d.ts CHANGED
@@ -82,7 +82,6 @@ interface KeverdVisitorData {
82
82
  }
83
83
  interface KeverdConfig {
84
84
  apiKey: string;
85
- endpoint?: string;
86
85
  userId?: string;
87
86
  debug?: boolean;
88
87
  extendedResult?: boolean;
@@ -90,7 +89,6 @@ interface KeverdConfig {
90
89
  }
91
90
  interface KeverdLoadOptions {
92
91
  apiKey: string;
93
- endpoint?: string;
94
92
  debug?: boolean;
95
93
  }
96
94
  interface KeverdVisitorDataOptions {
@@ -151,10 +149,44 @@ declare class KeverdSDK {
151
149
  * Generate a session ID
152
150
  */
153
151
  private generateSessionId;
152
+ /**
153
+ * Start a new session (called automatically on init, but can be called manually)
154
+ */
155
+ startSession(userId?: string, deviceHash?: string, metadata?: Record<string, unknown>): Promise<void>;
156
+ /**
157
+ * End the current session
158
+ */
159
+ endSession(): Promise<void>;
160
+ /**
161
+ * Pause the current session (e.g., when app goes to background)
162
+ */
163
+ pauseSession(): Promise<void>;
164
+ /**
165
+ * Resume a paused session (e.g., when app comes to foreground)
166
+ */
167
+ resumeSession(): Promise<void>;
168
+ /**
169
+ * Get current session status
170
+ */
171
+ getSessionStatus(): Promise<{
172
+ session_id: string;
173
+ status: string;
174
+ is_active: boolean;
175
+ is_paused: boolean;
176
+ event_count: number;
177
+ started_at: string;
178
+ last_activity_at: string;
179
+ duration_seconds: number | null;
180
+ } | null>;
181
+ /**
182
+ * Helper methods for browser/OS detection
183
+ */
184
+ private _detectBrowser;
185
+ private _detectOS;
154
186
  /**
155
187
  * Destroy the SDK instance
156
188
  */
157
- destroy(): void;
189
+ destroy(): Promise<void>;
158
190
  /**
159
191
  * Get current configuration
160
192
  */
package/dist/index.js CHANGED
@@ -657,7 +657,6 @@ var KeverdSDK = class {
657
657
  throw new Error("Keverd SDK: apiKey is required");
658
658
  }
659
659
  this.config = {
660
- endpoint: config.endpoint || this.getDefaultEndpoint(),
661
660
  debug: false,
662
661
  ...config
663
662
  };
@@ -665,10 +664,10 @@ var KeverdSDK = class {
665
664
  this.sessionId = this.generateSessionId();
666
665
  this.isInitialized = true;
667
666
  if (this.config.debug) {
668
- console.log("[Keverd SDK] Initialized successfully", {
669
- endpoint: this.config.endpoint
670
- });
667
+ console.log("[Keverd SDK] Initialized successfully");
671
668
  }
669
+ this.startSession().catch(() => {
670
+ });
672
671
  }
673
672
  /**
674
673
  * Get visitor data (fingerprint and risk assessment)
@@ -710,8 +709,7 @@ var KeverdSDK = class {
710
709
  if (!this.config) {
711
710
  throw new Error("SDK not initialized");
712
711
  }
713
- const endpoint = this.config.endpoint || this.getDefaultEndpoint();
714
- const url = `${endpoint}/fingerprint/score`;
712
+ const url = `${this.getDefaultEndpoint()}/fingerprint/score`;
715
713
  const headers = {
716
714
  "Content-Type": "application/json",
717
715
  "X-SDK-Source": "react"
@@ -726,10 +724,17 @@ var KeverdSDK = class {
726
724
  if (options?.tag === "sandbox") {
727
725
  headers["X-Sandbox"] = "true";
728
726
  }
727
+ const requestBody = {
728
+ ...request,
729
+ session: {
730
+ ...request.session || {},
731
+ sessionId: this.sessionId || request.session?.sessionId
732
+ }
733
+ };
729
734
  const response = await fetch(url, {
730
735
  method: "POST",
731
736
  headers,
732
- body: JSON.stringify(request)
737
+ body: JSON.stringify(requestBody)
733
738
  });
734
739
  if (!response.ok) {
735
740
  const errorText = await response.text().catch(() => "Unknown error");
@@ -775,10 +780,204 @@ var KeverdSDK = class {
775
780
  generateSessionId() {
776
781
  return `${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
777
782
  }
783
+ /**
784
+ * Start a new session (called automatically on init, but can be called manually)
785
+ */
786
+ async startSession(userId, deviceHash, metadata) {
787
+ if (!this.isInitialized || !this.config) {
788
+ throw new Error("Keverd SDK not initialized. Call init() first.");
789
+ }
790
+ if (!this.sessionId) {
791
+ this.sessionId = this.generateSessionId();
792
+ }
793
+ try {
794
+ const url = `${this.getDefaultEndpoint()}/dashboard/sessions/start`;
795
+ const headers = {
796
+ "Content-Type": "application/json"
797
+ };
798
+ const apiKey = this.config.apiKey;
799
+ if (apiKey) {
800
+ headers["x-keverd-key"] = apiKey;
801
+ headers["X-API-KEY"] = apiKey;
802
+ headers["Authorization"] = `Bearer ${apiKey}`;
803
+ }
804
+ const deviceInfo = this.deviceCollector.collect();
805
+ await fetch(url, {
806
+ method: "POST",
807
+ headers,
808
+ body: JSON.stringify({
809
+ session_id: this.sessionId,
810
+ user_id: userId || this.config.userId,
811
+ device_hash: deviceHash || deviceInfo.fingerprint,
812
+ session_metadata: metadata || {},
813
+ user_agent: navigator.userAgent,
814
+ browser: this._detectBrowser(),
815
+ os: this._detectOS(),
816
+ platform: "web",
817
+ sdk_type: "web",
818
+ sdk_source: "react"
819
+ })
820
+ });
821
+ if (this.config.debug) {
822
+ console.log(`[Keverd SDK] Session started: ${this.sessionId}`);
823
+ }
824
+ } catch (error) {
825
+ if (this.config.debug) {
826
+ console.warn("[Keverd SDK] Failed to start session on server:", error);
827
+ }
828
+ }
829
+ }
830
+ /**
831
+ * End the current session
832
+ */
833
+ async endSession() {
834
+ if (!this.isInitialized || !this.config || !this.sessionId) {
835
+ return;
836
+ }
837
+ try {
838
+ const url = `${this.getDefaultEndpoint()}/dashboard/sessions/${this.sessionId}/end`;
839
+ const headers = {
840
+ "Content-Type": "application/json"
841
+ };
842
+ const apiKey = this.config.apiKey;
843
+ if (apiKey) {
844
+ headers["x-keverd-key"] = apiKey;
845
+ headers["X-API-KEY"] = apiKey;
846
+ headers["Authorization"] = `Bearer ${apiKey}`;
847
+ }
848
+ await fetch(url, {
849
+ method: "POST",
850
+ headers
851
+ });
852
+ if (this.config.debug) {
853
+ console.log(`[Keverd SDK] Session ended: ${this.sessionId}`);
854
+ }
855
+ } catch (error) {
856
+ if (this.config.debug) {
857
+ console.warn("[Keverd SDK] Failed to end session on server:", error);
858
+ }
859
+ }
860
+ }
861
+ /**
862
+ * Pause the current session (e.g., when app goes to background)
863
+ */
864
+ async pauseSession() {
865
+ if (!this.isInitialized || !this.config || !this.sessionId) {
866
+ return;
867
+ }
868
+ try {
869
+ const url = `${this.getDefaultEndpoint()}/dashboard/sessions/${this.sessionId}/pause`;
870
+ const headers = {
871
+ "Content-Type": "application/json"
872
+ };
873
+ const apiKey = this.config.apiKey;
874
+ if (apiKey) {
875
+ headers["x-keverd-key"] = apiKey;
876
+ headers["X-API-KEY"] = apiKey;
877
+ headers["Authorization"] = `Bearer ${apiKey}`;
878
+ }
879
+ await fetch(url, {
880
+ method: "POST",
881
+ headers
882
+ });
883
+ if (this.config.debug) {
884
+ console.log(`[Keverd SDK] Session paused: ${this.sessionId}`);
885
+ }
886
+ } catch (error) {
887
+ if (this.config.debug) {
888
+ console.warn("[Keverd SDK] Failed to pause session on server:", error);
889
+ }
890
+ }
891
+ }
892
+ /**
893
+ * Resume a paused session (e.g., when app comes to foreground)
894
+ */
895
+ async resumeSession() {
896
+ if (!this.isInitialized || !this.config || !this.sessionId) {
897
+ return;
898
+ }
899
+ try {
900
+ const url = `${this.getDefaultEndpoint()}/dashboard/sessions/${this.sessionId}/resume`;
901
+ const headers = {
902
+ "Content-Type": "application/json"
903
+ };
904
+ const apiKey = this.config.apiKey;
905
+ if (apiKey) {
906
+ headers["x-keverd-key"] = apiKey;
907
+ headers["X-API-KEY"] = apiKey;
908
+ headers["Authorization"] = `Bearer ${apiKey}`;
909
+ }
910
+ await fetch(url, {
911
+ method: "POST",
912
+ headers
913
+ });
914
+ if (this.config.debug) {
915
+ console.log(`[Keverd SDK] Session resumed: ${this.sessionId}`);
916
+ }
917
+ } catch (error) {
918
+ if (this.config.debug) {
919
+ console.warn("[Keverd SDK] Failed to resume session on server:", error);
920
+ }
921
+ }
922
+ }
923
+ /**
924
+ * Get current session status
925
+ */
926
+ async getSessionStatus() {
927
+ if (!this.isInitialized || !this.config || !this.sessionId) {
928
+ return null;
929
+ }
930
+ try {
931
+ const url = `${this.getDefaultEndpoint()}/dashboard/sessions/${this.sessionId}/status`;
932
+ const headers = {};
933
+ const apiKey = this.config.apiKey;
934
+ if (apiKey) {
935
+ headers["x-keverd-key"] = apiKey;
936
+ headers["X-API-KEY"] = apiKey;
937
+ headers["Authorization"] = `Bearer ${apiKey}`;
938
+ }
939
+ const response = await fetch(url, {
940
+ method: "GET",
941
+ headers
942
+ });
943
+ if (!response.ok) {
944
+ return null;
945
+ }
946
+ return await response.json();
947
+ } catch (error) {
948
+ if (this.config.debug) {
949
+ console.warn("[Keverd SDK] Failed to get session status:", error);
950
+ }
951
+ return null;
952
+ }
953
+ }
954
+ /**
955
+ * Helper methods for browser/OS detection
956
+ */
957
+ _detectBrowser() {
958
+ const ua = navigator.userAgent;
959
+ if (ua.includes("Chrome") && !ua.includes("Edg")) return "Chrome";
960
+ if (ua.includes("Firefox")) return "Firefox";
961
+ if (ua.includes("Safari") && !ua.includes("Chrome")) return "Safari";
962
+ if (ua.includes("Edg")) return "Edge";
963
+ return "Unknown";
964
+ }
965
+ _detectOS() {
966
+ const ua = navigator.userAgent;
967
+ if (ua.includes("Windows")) return "Windows";
968
+ if (ua.includes("Mac")) return "macOS";
969
+ if (ua.includes("Linux")) return "Linux";
970
+ if (ua.includes("Android")) return "Android";
971
+ if (ua.includes("iOS") || ua.includes("iPhone") || ua.includes("iPad")) return "iOS";
972
+ return "Unknown";
973
+ }
778
974
  /**
779
975
  * Destroy the SDK instance
780
976
  */
781
- destroy() {
977
+ async destroy() {
978
+ if (this.sessionId) {
979
+ await this.endSession();
980
+ }
782
981
  const wasDebug = this.config?.debug;
783
982
  this.behavioralCollector.stop();
784
983
  this.isInitialized = false;
@@ -810,14 +1009,14 @@ function KeverdProvider({ loadOptions, children }) {
810
1009
  if (!sdk.isReady()) {
811
1010
  sdk.init({
812
1011
  apiKey: loadOptions.apiKey,
813
- endpoint: loadOptions.endpoint,
814
1012
  debug: loadOptions.debug || false
815
1013
  });
816
1014
  setIsReady(true);
817
1015
  }
818
1016
  return () => {
819
1017
  if (sdk.isReady()) {
820
- sdk.destroy();
1018
+ sdk.destroy().catch(() => {
1019
+ });
821
1020
  setIsReady(false);
822
1021
  }
823
1022
  };