@deflectbot/deflect-sdk 1.0.4 → 1.0.6
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.js +42 -0
- package/dist/types/index.d.ts +10 -0
- package/package.json +7 -5
- package/src/index.ts +57 -0
- package/src/types/window.d.ts +3 -0
- package/tsconfig.json +22 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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.6",
|
|
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,57 @@
|
|
|
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
|
+
}
|
|
12
|
+
|
|
13
|
+
configure(params: DeflectConfig): void {
|
|
14
|
+
if (!params.siteKey) {
|
|
15
|
+
throw new Error("siteKey is required in configuration");
|
|
16
|
+
}
|
|
17
|
+
window.Deflect.siteKey = params.siteKey;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
solveChallenge(): Promise<string> {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
if (!window.Deflect.siteKey) {
|
|
23
|
+
return reject("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 script = document.createElement("script");
|
|
29
|
+
script.src = scriptUrl;
|
|
30
|
+
|
|
31
|
+
script.onload = () => {
|
|
32
|
+
if (
|
|
33
|
+
typeof window.Deflect === "undefined" ||
|
|
34
|
+
typeof window.Deflect.getToken !== "function"
|
|
35
|
+
) {
|
|
36
|
+
return reject("Deflect script did not load properly");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
window.Deflect.getToken()
|
|
40
|
+
.then((token: string) => {
|
|
41
|
+
resolve(token);
|
|
42
|
+
})
|
|
43
|
+
.catch((err: Error) => {
|
|
44
|
+
reject(`Failed to get token: ${err.message}`);
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
script.onerror = () => {
|
|
49
|
+
reject("Failed to load the Deflect script");
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
document.head.appendChild(script);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
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
|
+
}
|