@deflectbot/deflect-sdk 1.0.6 → 1.0.7
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/package.json +1 -1
- package/src/index.ts +33 -34
- package/src/index.js +0 -46
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -8,6 +8,7 @@ class Deflect {
|
|
|
8
8
|
window.Deflect = {};
|
|
9
9
|
}
|
|
10
10
|
window.Deflect.siteKey = "";
|
|
11
|
+
window.Deflect.sessionId = "";
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
configure(params: DeflectConfig): void {
|
|
@@ -17,40 +18,38 @@ class Deflect {
|
|
|
17
18
|
window.Deflect.siteKey = params.siteKey;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
solveChallenge(): Promise<string> {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
document.head.appendChild(script);
|
|
53
|
-
});
|
|
21
|
+
async solveChallenge(): Promise<string> {
|
|
22
|
+
if (!window.Deflect.siteKey) {
|
|
23
|
+
throw new Error("API key (siteKey) is missing in configuration");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const scriptUrl = `https://js.deflect.bot/main.js?site=${window.Deflect.siteKey}`;
|
|
27
|
+
|
|
28
|
+
const response = await fetch(scriptUrl);
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
throw new Error("Failed to fetch the Deflect script");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const sessionId = response.headers.get("session_id");
|
|
34
|
+
if (sessionId) {
|
|
35
|
+
window.Deflect.sessionId = sessionId;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const scriptText = await response.text();
|
|
39
|
+
|
|
40
|
+
const scriptEl = document.createElement("script");
|
|
41
|
+
scriptEl.textContent = scriptText;
|
|
42
|
+
document.head.appendChild(scriptEl);
|
|
43
|
+
|
|
44
|
+
if (
|
|
45
|
+
typeof window.Deflect === "undefined" ||
|
|
46
|
+
typeof window.Deflect.getToken !== "function"
|
|
47
|
+
) {
|
|
48
|
+
throw new Error("Deflect script did not load properly");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const token = await window.Deflect.getToken();
|
|
52
|
+
return token;
|
|
54
53
|
}
|
|
55
54
|
}
|
|
56
55
|
|
package/src/index.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
class Deflect {
|
|
2
|
-
constructor() {
|
|
3
|
-
window.Deflect = window.Deflect || {};
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
configure(params) {
|
|
7
|
-
if (!params.siteKey) {
|
|
8
|
-
throw new Error('siteKey is required in configuration');
|
|
9
|
-
}
|
|
10
|
-
window.Deflect.siteKey = params.siteKey;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
solveChallenge() {
|
|
14
|
-
return new Promise((resolve, reject) => {
|
|
15
|
-
if (!window.Deflect.siteKey) {
|
|
16
|
-
return reject("siteKey is missing in configuration");
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const scriptUrl = `https://js.deflect.bot/main.js?site=${window.Deflect.siteKey}`;
|
|
20
|
-
|
|
21
|
-
const script = document.createElement("script");
|
|
22
|
-
script.src = scriptUrl;
|
|
23
|
-
script.onload = () => {
|
|
24
|
-
if (typeof window.Deflect === 'undefined' || typeof window.Deflect.getToken !== 'function') {
|
|
25
|
-
return reject("Deflect script did not load properly");
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
window.Deflect.getToken()
|
|
29
|
-
.then((token) => {
|
|
30
|
-
resolve(token);
|
|
31
|
-
})
|
|
32
|
-
.catch((err) => {
|
|
33
|
-
reject(`Failed to get token: ${err}`);
|
|
34
|
-
});
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
script.onerror = () => {
|
|
38
|
-
reject("Failed to load the Deflect script");
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
document.head.appendChild(script);
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
module.exports = new Deflect();
|