@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/README.md +0 -16
- package/dist/index.d.mts +39 -3
- package/dist/index.d.ts +39 -3
- package/dist/index.js +227 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +227 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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 {
|
|
@@ -135,6 +133,10 @@ declare class KeverdSDK {
|
|
|
135
133
|
* Get visitor data (fingerprint and risk assessment)
|
|
136
134
|
*/
|
|
137
135
|
getVisitorData(options?: KeverdVisitorDataOptions): Promise<KeverdVisitorData>;
|
|
136
|
+
/**
|
|
137
|
+
* Extract origin and referrer from browser
|
|
138
|
+
*/
|
|
139
|
+
private getOriginHeaders;
|
|
138
140
|
/**
|
|
139
141
|
* Send fingerprint request to backend
|
|
140
142
|
*/
|
|
@@ -151,10 +153,44 @@ declare class KeverdSDK {
|
|
|
151
153
|
* Generate a session ID
|
|
152
154
|
*/
|
|
153
155
|
private generateSessionId;
|
|
156
|
+
/**
|
|
157
|
+
* Start a new session (called automatically on init, but can be called manually)
|
|
158
|
+
*/
|
|
159
|
+
startSession(userId?: string, deviceHash?: string, metadata?: Record<string, unknown>): Promise<void>;
|
|
160
|
+
/**
|
|
161
|
+
* End the current session
|
|
162
|
+
*/
|
|
163
|
+
endSession(): Promise<void>;
|
|
164
|
+
/**
|
|
165
|
+
* Pause the current session (e.g., when app goes to background)
|
|
166
|
+
*/
|
|
167
|
+
pauseSession(): Promise<void>;
|
|
168
|
+
/**
|
|
169
|
+
* Resume a paused session (e.g., when app comes to foreground)
|
|
170
|
+
*/
|
|
171
|
+
resumeSession(): Promise<void>;
|
|
172
|
+
/**
|
|
173
|
+
* Get current session status
|
|
174
|
+
*/
|
|
175
|
+
getSessionStatus(): Promise<{
|
|
176
|
+
session_id: string;
|
|
177
|
+
status: string;
|
|
178
|
+
is_active: boolean;
|
|
179
|
+
is_paused: boolean;
|
|
180
|
+
event_count: number;
|
|
181
|
+
started_at: string;
|
|
182
|
+
last_activity_at: string;
|
|
183
|
+
duration_seconds: number | null;
|
|
184
|
+
} | null>;
|
|
185
|
+
/**
|
|
186
|
+
* Helper methods for browser/OS detection
|
|
187
|
+
*/
|
|
188
|
+
private _detectBrowser;
|
|
189
|
+
private _detectOS;
|
|
154
190
|
/**
|
|
155
191
|
* Destroy the SDK instance
|
|
156
192
|
*/
|
|
157
|
-
destroy(): void
|
|
193
|
+
destroy(): Promise<void>;
|
|
158
194
|
/**
|
|
159
195
|
* Get current configuration
|
|
160
196
|
*/
|
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 {
|
|
@@ -135,6 +133,10 @@ declare class KeverdSDK {
|
|
|
135
133
|
* Get visitor data (fingerprint and risk assessment)
|
|
136
134
|
*/
|
|
137
135
|
getVisitorData(options?: KeverdVisitorDataOptions): Promise<KeverdVisitorData>;
|
|
136
|
+
/**
|
|
137
|
+
* Extract origin and referrer from browser
|
|
138
|
+
*/
|
|
139
|
+
private getOriginHeaders;
|
|
138
140
|
/**
|
|
139
141
|
* Send fingerprint request to backend
|
|
140
142
|
*/
|
|
@@ -151,10 +153,44 @@ declare class KeverdSDK {
|
|
|
151
153
|
* Generate a session ID
|
|
152
154
|
*/
|
|
153
155
|
private generateSessionId;
|
|
156
|
+
/**
|
|
157
|
+
* Start a new session (called automatically on init, but can be called manually)
|
|
158
|
+
*/
|
|
159
|
+
startSession(userId?: string, deviceHash?: string, metadata?: Record<string, unknown>): Promise<void>;
|
|
160
|
+
/**
|
|
161
|
+
* End the current session
|
|
162
|
+
*/
|
|
163
|
+
endSession(): Promise<void>;
|
|
164
|
+
/**
|
|
165
|
+
* Pause the current session (e.g., when app goes to background)
|
|
166
|
+
*/
|
|
167
|
+
pauseSession(): Promise<void>;
|
|
168
|
+
/**
|
|
169
|
+
* Resume a paused session (e.g., when app comes to foreground)
|
|
170
|
+
*/
|
|
171
|
+
resumeSession(): Promise<void>;
|
|
172
|
+
/**
|
|
173
|
+
* Get current session status
|
|
174
|
+
*/
|
|
175
|
+
getSessionStatus(): Promise<{
|
|
176
|
+
session_id: string;
|
|
177
|
+
status: string;
|
|
178
|
+
is_active: boolean;
|
|
179
|
+
is_paused: boolean;
|
|
180
|
+
event_count: number;
|
|
181
|
+
started_at: string;
|
|
182
|
+
last_activity_at: string;
|
|
183
|
+
duration_seconds: number | null;
|
|
184
|
+
} | null>;
|
|
185
|
+
/**
|
|
186
|
+
* Helper methods for browser/OS detection
|
|
187
|
+
*/
|
|
188
|
+
private _detectBrowser;
|
|
189
|
+
private _detectOS;
|
|
154
190
|
/**
|
|
155
191
|
* Destroy the SDK instance
|
|
156
192
|
*/
|
|
157
|
-
destroy(): void
|
|
193
|
+
destroy(): Promise<void>;
|
|
158
194
|
/**
|
|
159
195
|
* Get current configuration
|
|
160
196
|
*/
|
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)
|
|
@@ -703,6 +702,17 @@ var KeverdSDK = class {
|
|
|
703
702
|
throw keverdError;
|
|
704
703
|
}
|
|
705
704
|
}
|
|
705
|
+
/**
|
|
706
|
+
* Extract origin and referrer from browser
|
|
707
|
+
*/
|
|
708
|
+
getOriginHeaders() {
|
|
709
|
+
if (typeof window === "undefined") {
|
|
710
|
+
return {};
|
|
711
|
+
}
|
|
712
|
+
const origin = window.location.origin || void 0;
|
|
713
|
+
const referrer = document.referrer || void 0;
|
|
714
|
+
return { origin, referrer };
|
|
715
|
+
}
|
|
706
716
|
/**
|
|
707
717
|
* Send fingerprint request to backend
|
|
708
718
|
*/
|
|
@@ -710,13 +720,19 @@ var KeverdSDK = class {
|
|
|
710
720
|
if (!this.config) {
|
|
711
721
|
throw new Error("SDK not initialized");
|
|
712
722
|
}
|
|
713
|
-
const
|
|
714
|
-
const url = `${endpoint}/fingerprint/score`;
|
|
723
|
+
const url = `${this.getDefaultEndpoint()}/fingerprint/score`;
|
|
715
724
|
const headers = {
|
|
716
725
|
"Content-Type": "application/json",
|
|
717
726
|
"X-SDK-Source": "react"
|
|
718
727
|
// Identify SDK source for backend analytics
|
|
719
728
|
};
|
|
729
|
+
const { origin, referrer } = this.getOriginHeaders();
|
|
730
|
+
if (origin) {
|
|
731
|
+
headers["X-Origin-URL"] = origin;
|
|
732
|
+
}
|
|
733
|
+
if (referrer) {
|
|
734
|
+
headers["X-Referrer-URL"] = referrer;
|
|
735
|
+
}
|
|
720
736
|
const apiKey = this.config.apiKey;
|
|
721
737
|
if (apiKey) {
|
|
722
738
|
headers["x-keverd-key"] = apiKey;
|
|
@@ -726,10 +742,17 @@ var KeverdSDK = class {
|
|
|
726
742
|
if (options?.tag === "sandbox") {
|
|
727
743
|
headers["X-Sandbox"] = "true";
|
|
728
744
|
}
|
|
745
|
+
const requestBody = {
|
|
746
|
+
...request,
|
|
747
|
+
session: {
|
|
748
|
+
...request.session || {},
|
|
749
|
+
sessionId: this.sessionId || request.session?.sessionId
|
|
750
|
+
}
|
|
751
|
+
};
|
|
729
752
|
const response = await fetch(url, {
|
|
730
753
|
method: "POST",
|
|
731
754
|
headers,
|
|
732
|
-
body: JSON.stringify(
|
|
755
|
+
body: JSON.stringify(requestBody)
|
|
733
756
|
});
|
|
734
757
|
if (!response.ok) {
|
|
735
758
|
const errorText = await response.text().catch(() => "Unknown error");
|
|
@@ -775,10 +798,204 @@ var KeverdSDK = class {
|
|
|
775
798
|
generateSessionId() {
|
|
776
799
|
return `${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
777
800
|
}
|
|
801
|
+
/**
|
|
802
|
+
* Start a new session (called automatically on init, but can be called manually)
|
|
803
|
+
*/
|
|
804
|
+
async startSession(userId, deviceHash, metadata) {
|
|
805
|
+
if (!this.isInitialized || !this.config) {
|
|
806
|
+
throw new Error("Keverd SDK not initialized. Call init() first.");
|
|
807
|
+
}
|
|
808
|
+
if (!this.sessionId) {
|
|
809
|
+
this.sessionId = this.generateSessionId();
|
|
810
|
+
}
|
|
811
|
+
try {
|
|
812
|
+
const url = `${this.getDefaultEndpoint()}/dashboard/sessions/start`;
|
|
813
|
+
const headers = {
|
|
814
|
+
"Content-Type": "application/json"
|
|
815
|
+
};
|
|
816
|
+
const apiKey = this.config.apiKey;
|
|
817
|
+
if (apiKey) {
|
|
818
|
+
headers["x-keverd-key"] = apiKey;
|
|
819
|
+
headers["X-API-KEY"] = apiKey;
|
|
820
|
+
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
821
|
+
}
|
|
822
|
+
const deviceInfo = this.deviceCollector.collect();
|
|
823
|
+
await fetch(url, {
|
|
824
|
+
method: "POST",
|
|
825
|
+
headers,
|
|
826
|
+
body: JSON.stringify({
|
|
827
|
+
session_id: this.sessionId,
|
|
828
|
+
user_id: userId || this.config.userId,
|
|
829
|
+
device_hash: deviceHash || deviceInfo.fingerprint,
|
|
830
|
+
session_metadata: metadata || {},
|
|
831
|
+
user_agent: navigator.userAgent,
|
|
832
|
+
browser: this._detectBrowser(),
|
|
833
|
+
os: this._detectOS(),
|
|
834
|
+
platform: "web",
|
|
835
|
+
sdk_type: "web",
|
|
836
|
+
sdk_source: "react"
|
|
837
|
+
})
|
|
838
|
+
});
|
|
839
|
+
if (this.config.debug) {
|
|
840
|
+
console.log(`[Keverd SDK] Session started: ${this.sessionId}`);
|
|
841
|
+
}
|
|
842
|
+
} catch (error) {
|
|
843
|
+
if (this.config.debug) {
|
|
844
|
+
console.warn("[Keverd SDK] Failed to start session on server:", error);
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* End the current session
|
|
850
|
+
*/
|
|
851
|
+
async endSession() {
|
|
852
|
+
if (!this.isInitialized || !this.config || !this.sessionId) {
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
try {
|
|
856
|
+
const url = `${this.getDefaultEndpoint()}/dashboard/sessions/${this.sessionId}/end`;
|
|
857
|
+
const headers = {
|
|
858
|
+
"Content-Type": "application/json"
|
|
859
|
+
};
|
|
860
|
+
const apiKey = this.config.apiKey;
|
|
861
|
+
if (apiKey) {
|
|
862
|
+
headers["x-keverd-key"] = apiKey;
|
|
863
|
+
headers["X-API-KEY"] = apiKey;
|
|
864
|
+
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
865
|
+
}
|
|
866
|
+
await fetch(url, {
|
|
867
|
+
method: "POST",
|
|
868
|
+
headers
|
|
869
|
+
});
|
|
870
|
+
if (this.config.debug) {
|
|
871
|
+
console.log(`[Keverd SDK] Session ended: ${this.sessionId}`);
|
|
872
|
+
}
|
|
873
|
+
} catch (error) {
|
|
874
|
+
if (this.config.debug) {
|
|
875
|
+
console.warn("[Keverd SDK] Failed to end session on server:", error);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
880
|
+
* Pause the current session (e.g., when app goes to background)
|
|
881
|
+
*/
|
|
882
|
+
async pauseSession() {
|
|
883
|
+
if (!this.isInitialized || !this.config || !this.sessionId) {
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
try {
|
|
887
|
+
const url = `${this.getDefaultEndpoint()}/dashboard/sessions/${this.sessionId}/pause`;
|
|
888
|
+
const headers = {
|
|
889
|
+
"Content-Type": "application/json"
|
|
890
|
+
};
|
|
891
|
+
const apiKey = this.config.apiKey;
|
|
892
|
+
if (apiKey) {
|
|
893
|
+
headers["x-keverd-key"] = apiKey;
|
|
894
|
+
headers["X-API-KEY"] = apiKey;
|
|
895
|
+
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
896
|
+
}
|
|
897
|
+
await fetch(url, {
|
|
898
|
+
method: "POST",
|
|
899
|
+
headers
|
|
900
|
+
});
|
|
901
|
+
if (this.config.debug) {
|
|
902
|
+
console.log(`[Keverd SDK] Session paused: ${this.sessionId}`);
|
|
903
|
+
}
|
|
904
|
+
} catch (error) {
|
|
905
|
+
if (this.config.debug) {
|
|
906
|
+
console.warn("[Keverd SDK] Failed to pause session on server:", error);
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
/**
|
|
911
|
+
* Resume a paused session (e.g., when app comes to foreground)
|
|
912
|
+
*/
|
|
913
|
+
async resumeSession() {
|
|
914
|
+
if (!this.isInitialized || !this.config || !this.sessionId) {
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
try {
|
|
918
|
+
const url = `${this.getDefaultEndpoint()}/dashboard/sessions/${this.sessionId}/resume`;
|
|
919
|
+
const headers = {
|
|
920
|
+
"Content-Type": "application/json"
|
|
921
|
+
};
|
|
922
|
+
const apiKey = this.config.apiKey;
|
|
923
|
+
if (apiKey) {
|
|
924
|
+
headers["x-keverd-key"] = apiKey;
|
|
925
|
+
headers["X-API-KEY"] = apiKey;
|
|
926
|
+
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
927
|
+
}
|
|
928
|
+
await fetch(url, {
|
|
929
|
+
method: "POST",
|
|
930
|
+
headers
|
|
931
|
+
});
|
|
932
|
+
if (this.config.debug) {
|
|
933
|
+
console.log(`[Keverd SDK] Session resumed: ${this.sessionId}`);
|
|
934
|
+
}
|
|
935
|
+
} catch (error) {
|
|
936
|
+
if (this.config.debug) {
|
|
937
|
+
console.warn("[Keverd SDK] Failed to resume session on server:", error);
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
/**
|
|
942
|
+
* Get current session status
|
|
943
|
+
*/
|
|
944
|
+
async getSessionStatus() {
|
|
945
|
+
if (!this.isInitialized || !this.config || !this.sessionId) {
|
|
946
|
+
return null;
|
|
947
|
+
}
|
|
948
|
+
try {
|
|
949
|
+
const url = `${this.getDefaultEndpoint()}/dashboard/sessions/${this.sessionId}/status`;
|
|
950
|
+
const headers = {};
|
|
951
|
+
const apiKey = this.config.apiKey;
|
|
952
|
+
if (apiKey) {
|
|
953
|
+
headers["x-keverd-key"] = apiKey;
|
|
954
|
+
headers["X-API-KEY"] = apiKey;
|
|
955
|
+
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
956
|
+
}
|
|
957
|
+
const response = await fetch(url, {
|
|
958
|
+
method: "GET",
|
|
959
|
+
headers
|
|
960
|
+
});
|
|
961
|
+
if (!response.ok) {
|
|
962
|
+
return null;
|
|
963
|
+
}
|
|
964
|
+
return await response.json();
|
|
965
|
+
} catch (error) {
|
|
966
|
+
if (this.config.debug) {
|
|
967
|
+
console.warn("[Keverd SDK] Failed to get session status:", error);
|
|
968
|
+
}
|
|
969
|
+
return null;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* Helper methods for browser/OS detection
|
|
974
|
+
*/
|
|
975
|
+
_detectBrowser() {
|
|
976
|
+
const ua = navigator.userAgent;
|
|
977
|
+
if (ua.includes("Chrome") && !ua.includes("Edg")) return "Chrome";
|
|
978
|
+
if (ua.includes("Firefox")) return "Firefox";
|
|
979
|
+
if (ua.includes("Safari") && !ua.includes("Chrome")) return "Safari";
|
|
980
|
+
if (ua.includes("Edg")) return "Edge";
|
|
981
|
+
return "Unknown";
|
|
982
|
+
}
|
|
983
|
+
_detectOS() {
|
|
984
|
+
const ua = navigator.userAgent;
|
|
985
|
+
if (ua.includes("Windows")) return "Windows";
|
|
986
|
+
if (ua.includes("Mac")) return "macOS";
|
|
987
|
+
if (ua.includes("Linux")) return "Linux";
|
|
988
|
+
if (ua.includes("Android")) return "Android";
|
|
989
|
+
if (ua.includes("iOS") || ua.includes("iPhone") || ua.includes("iPad")) return "iOS";
|
|
990
|
+
return "Unknown";
|
|
991
|
+
}
|
|
778
992
|
/**
|
|
779
993
|
* Destroy the SDK instance
|
|
780
994
|
*/
|
|
781
|
-
destroy() {
|
|
995
|
+
async destroy() {
|
|
996
|
+
if (this.sessionId) {
|
|
997
|
+
await this.endSession();
|
|
998
|
+
}
|
|
782
999
|
const wasDebug = this.config?.debug;
|
|
783
1000
|
this.behavioralCollector.stop();
|
|
784
1001
|
this.isInitialized = false;
|
|
@@ -810,14 +1027,14 @@ function KeverdProvider({ loadOptions, children }) {
|
|
|
810
1027
|
if (!sdk.isReady()) {
|
|
811
1028
|
sdk.init({
|
|
812
1029
|
apiKey: loadOptions.apiKey,
|
|
813
|
-
endpoint: loadOptions.endpoint,
|
|
814
1030
|
debug: loadOptions.debug || false
|
|
815
1031
|
});
|
|
816
1032
|
setIsReady(true);
|
|
817
1033
|
}
|
|
818
1034
|
return () => {
|
|
819
1035
|
if (sdk.isReady()) {
|
|
820
|
-
sdk.destroy()
|
|
1036
|
+
sdk.destroy().catch(() => {
|
|
1037
|
+
});
|
|
821
1038
|
setIsReady(false);
|
|
822
1039
|
}
|
|
823
1040
|
};
|