@deflectbot/deflect-sdk 1.0.5 → 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/{src → dist}/index.js +42 -46
- package/dist/types/index.d.ts +10 -0
- package/package.json +7 -5
- package/src/index.ts +56 -0
- package/src/types/window.d.ts +3 -0
- package/tsconfig.json +22 -0
- package/src/types/deflect-sdk.d.ts +0 -13
package/{src → dist}/index.js
RENAMED
|
@@ -1,46 +1,42 @@
|
|
|
1
|
-
class Deflect {
|
|
2
|
-
constructor() {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const scriptUrl = `https://js.deflect.bot/main.js?site=${window.Deflect.siteKey}`;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
script.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return reject("Deflect script did not load properly");
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
module.exports = new Deflect();
|
|
1
|
+
class Deflect {
|
|
2
|
+
constructor() {
|
|
3
|
+
if (typeof window.Deflect === "undefined") {
|
|
4
|
+
window.Deflect = {};
|
|
5
|
+
}
|
|
6
|
+
window.Deflect.siteKey = "";
|
|
7
|
+
}
|
|
8
|
+
configure(params) {
|
|
9
|
+
if (!params.siteKey) {
|
|
10
|
+
throw new Error("siteKey is required in configuration");
|
|
11
|
+
}
|
|
12
|
+
window.Deflect.siteKey = params.siteKey;
|
|
13
|
+
}
|
|
14
|
+
solveChallenge() {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
if (!window.Deflect.siteKey) {
|
|
17
|
+
return reject("API key (siteKey) is missing in configuration");
|
|
18
|
+
}
|
|
19
|
+
const scriptUrl = `https://js.deflect.bot/main.js?site=${window.Deflect.siteKey}`;
|
|
20
|
+
const script = document.createElement("script");
|
|
21
|
+
script.src = scriptUrl;
|
|
22
|
+
script.onload = () => {
|
|
23
|
+
if (typeof window.Deflect === "undefined" ||
|
|
24
|
+
typeof window.Deflect.getToken !== "function") {
|
|
25
|
+
return reject("Deflect script did not load properly");
|
|
26
|
+
}
|
|
27
|
+
window.Deflect.getToken()
|
|
28
|
+
.then((token) => {
|
|
29
|
+
resolve(token);
|
|
30
|
+
})
|
|
31
|
+
.catch((err) => {
|
|
32
|
+
reject(`Failed to get token: ${err.message}`);
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
script.onerror = () => {
|
|
36
|
+
reject("Failed to load the Deflect script");
|
|
37
|
+
};
|
|
38
|
+
document.head.appendChild(script);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export default new Deflect();
|
package/package.json
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deflectbot/deflect-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/types/index.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
|
-
"
|
|
8
|
+
"build": "tsc"
|
|
8
9
|
},
|
|
9
10
|
"author": "Fredrik Rafn",
|
|
10
11
|
"license": "MIT",
|
|
11
|
-
"
|
|
12
|
-
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"typescript": "^5.7.3"
|
|
14
|
+
},
|
|
13
15
|
"publishConfig": {
|
|
14
16
|
"access": "public"
|
|
15
17
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
interface DeflectConfig {
|
|
2
|
+
siteKey: string;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
class Deflect {
|
|
6
|
+
constructor() {
|
|
7
|
+
if (typeof window.Deflect === "undefined") {
|
|
8
|
+
window.Deflect = {};
|
|
9
|
+
}
|
|
10
|
+
window.Deflect.siteKey = "";
|
|
11
|
+
window.Deflect.sessionId = "";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
configure(params: DeflectConfig): void {
|
|
15
|
+
if (!params.siteKey) {
|
|
16
|
+
throw new Error("siteKey is required in configuration");
|
|
17
|
+
}
|
|
18
|
+
window.Deflect.siteKey = params.siteKey;
|
|
19
|
+
}
|
|
20
|
+
|
|
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;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default new Deflect();
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2015", // Set target to ES2015 or later
|
|
4
|
+
"module": "ESNext", // Use ES module syntax
|
|
5
|
+
"moduleResolution": "node", // Resolve modules like Node.js
|
|
6
|
+
"strict": true, // Enable strict type-checking
|
|
7
|
+
"declaration": true, // Enable generation of .d.ts files
|
|
8
|
+
"declarationDir": "./dist/types", // Directory for .d.ts files
|
|
9
|
+
"esModuleInterop": true, // Allow importing non-ESM modules
|
|
10
|
+
"skipLibCheck": true, // Skip type checking of declaration files
|
|
11
|
+
"outDir": "./dist", // Output directory for JavaScript files
|
|
12
|
+
"lib": ["ES2015", "DOM"], // Add ES2015 and DOM libraries
|
|
13
|
+
"jsx": "preserve" // For JSX compatibility in Vue
|
|
14
|
+
},
|
|
15
|
+
"include": [
|
|
16
|
+
"src/**/*"
|
|
17
|
+
],
|
|
18
|
+
"exclude": [
|
|
19
|
+
"node_modules",
|
|
20
|
+
"dist"
|
|
21
|
+
]
|
|
22
|
+
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
declare module "@deflectbot/deflect-sdk" {
|
|
2
|
-
interface DeflectConfig {
|
|
3
|
-
siteKey: string;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
interface Deflect {
|
|
7
|
-
configure(params: DeflectConfig): void;
|
|
8
|
-
solveChallenge(): Promise<string>;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const Deflect: Deflect;
|
|
12
|
-
export default Deflect;
|
|
13
|
-
}
|