Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

build

Blue Mouse0a2488170cea
diff --git a/dist/captcha-validator.cjs.js b/dist/captcha-validator.cjs.js
new file mode 100644
index 000000000..80a65bb13
--- /dev/null
+++ b/dist/captcha-validator.cjs.js
@@ -0,0 +1,127 @@
+'use strict';
+
+Object.defineProperty(exports, '__esModule', { value: true });
+
+/******************************************************************************
+Copyright (c) Microsoft Corporation.
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
+***************************************************************************** */
+
+function __awaiter(thisArg, _arguments, P, generator) {
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
+ return new (P || (P = Promise))(function (resolve, reject) {
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
+ });
+}
+
+function __generator(thisArg, body) {
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
+ function verb(n) { return function (v) { return step([n, v]); }; }
+ function step(op) {
+ if (f) throw new TypeError("Generator is already executing.");
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
+ if (y = 0, t) op = [op[0] & 2, t.value];
+ switch (op[0]) {
+ case 0: case 1: t = op; break;
+ case 4: _.label++; return { value: op[1], done: false };
+ case 5: _.label++; y = op[1]; op = [0]; continue;
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
+ default:
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
+ if (t[2]) _.ops.pop();
+ _.trys.pop(); continue;
+ }
+ op = body.call(thisArg, _);
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
+ }
+}
+
+var DEFAULT_API_HOST = 'https://api.swetrix.com/captcha';
+var ENDPOINTS = {
+ VALIDATE: '/validate',
+};
+var makeAPIRequest = function (path, method, body, apiURL) { return __awaiter(void 0, void 0, void 0, function () {
+ var res, e_1;
+ return __generator(this, function (_a) {
+ switch (_a.label) {
+ case 0:
+ _a.trys.push([0, 2, , 3]);
+ return [4 /*yield*/, fetch("".concat(apiURL || DEFAULT_API_HOST).concat(path), {
+ method: method,
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ })];
+ case 1:
+ res = _a.sent();
+ return [3 /*break*/, 3];
+ case 2:
+ e_1 = _a.sent();
+ throw "Unable to make API request, error: ".concat(e_1);
+ case 3: return [4 /*yield*/, res.json()];
+ case 4: return [2 /*return*/, _a.sent()];
+ }
+ });
+}); };
+
+/**
+ * Function to validate the CAPTCHA token.
+ *
+ * @param {string} token The CAPTCHA token.
+ * @param {string} secret The secret API key.
+ * @param {number} timestamp The timestamp of the CAPTCHA pass.
+ * @param {string | null} hash The hash of the CAPTCHA pass.
+ * @param {string | null} apiURL The API URL to use (default: https://api.swetrix.com/captcha).
+ * @returns {Promise<[boolean, object | null]>} The result of the validation.
+ */
+function validateToken(token, secret, timestamp, hash, apiURL) {
+ return __awaiter(this, void 0, void 0, function () {
+ var res, e_1, success, data;
+ return __generator(this, function (_a) {
+ switch (_a.label) {
+ case 0:
+ _a.trys.push([0, 2, , 3]);
+ return [4 /*yield*/, makeAPIRequest(ENDPOINTS.VALIDATE, 'POST', {
+ token: token,
+ secret: secret,
+ timestamp: timestamp,
+ hash: hash,
+ }, apiURL)];
+ case 1:
+ res = _a.sent();
+ return [3 /*break*/, 3];
+ case 2:
+ e_1 = _a.sent();
+ return [2 /*return*/, [false, e_1]];
+ case 3:
+ success = res.success, data = res.data;
+ if (!success) {
+ return [2 /*return*/, [false, 'CAPTCHA validation failed']];
+ }
+ return [2 /*return*/, [true, data]];
+ }
+ });
+ });
+}
+
+exports.validateToken = validateToken;
diff --git a/dist/captcha-validator.es5.js b/dist/captcha-validator.es5.js
new file mode 100644
index 000000000..1ab359dd5
--- /dev/null
+++ b/dist/captcha-validator.es5.js
@@ -0,0 +1,123 @@
+/******************************************************************************
+Copyright (c) Microsoft Corporation.
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
+***************************************************************************** */
+
+function __awaiter(thisArg, _arguments, P, generator) {
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
+ return new (P || (P = Promise))(function (resolve, reject) {
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
+ });
+}
+
+function __generator(thisArg, body) {
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
+ function verb(n) { return function (v) { return step([n, v]); }; }
+ function step(op) {
+ if (f) throw new TypeError("Generator is already executing.");
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
+ if (y = 0, t) op = [op[0] & 2, t.value];
+ switch (op[0]) {
+ case 0: case 1: t = op; break;
+ case 4: _.label++; return { value: op[1], done: false };
+ case 5: _.label++; y = op[1]; op = [0]; continue;
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
+ default:
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
+ if (t[2]) _.ops.pop();
+ _.trys.pop(); continue;
+ }
+ op = body.call(thisArg, _);
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
+ }
+}
+
+var DEFAULT_API_HOST = 'https://api.swetrix.com/captcha';
+var ENDPOINTS = {
+ VALIDATE: '/validate',
+};
+var makeAPIRequest = function (path, method, body, apiURL) { return __awaiter(void 0, void 0, void 0, function () {
+ var res, e_1;
+ return __generator(this, function (_a) {
+ switch (_a.label) {
+ case 0:
+ _a.trys.push([0, 2, , 3]);
+ return [4 /*yield*/, fetch("".concat(apiURL || DEFAULT_API_HOST).concat(path), {
+ method: method,
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ })];
+ case 1:
+ res = _a.sent();
+ return [3 /*break*/, 3];
+ case 2:
+ e_1 = _a.sent();
+ throw "Unable to make API request, error: ".concat(e_1);
+ case 3: return [4 /*yield*/, res.json()];
+ case 4: return [2 /*return*/, _a.sent()];
+ }
+ });
+}); };
+
+/**
+ * Function to validate the CAPTCHA token.
+ *
+ * @param {string} token The CAPTCHA token.
+ * @param {string} secret The secret API key.
+ * @param {number} timestamp The timestamp of the CAPTCHA pass.
+ * @param {string | null} hash The hash of the CAPTCHA pass.
+ * @param {string | null} apiURL The API URL to use (default: https://api.swetrix.com/captcha).
+ * @returns {Promise<[boolean, object | null]>} The result of the validation.
+ */
+function validateToken(token, secret, timestamp, hash, apiURL) {
+ return __awaiter(this, void 0, void 0, function () {
+ var res, e_1, success, data;
+ return __generator(this, function (_a) {
+ switch (_a.label) {
+ case 0:
+ _a.trys.push([0, 2, , 3]);
+ return [4 /*yield*/, makeAPIRequest(ENDPOINTS.VALIDATE, 'POST', {
+ token: token,
+ secret: secret,
+ timestamp: timestamp,
+ hash: hash,
+ }, apiURL)];
+ case 1:
+ res = _a.sent();
+ return [3 /*break*/, 3];
+ case 2:
+ e_1 = _a.sent();
+ return [2 /*return*/, [false, e_1]];
+ case 3:
+ success = res.success, data = res.data;
+ if (!success) {
+ return [2 /*return*/, [false, 'CAPTCHA validation failed']];
+ }
+ return [2 /*return*/, [true, data]];
+ }
+ });
+ });
+}
+
+export { validateToken };
diff --git a/dist/captcha-validator.js b/dist/captcha-validator.js
new file mode 100644
index 000000000..dc0ac17f0
--- /dev/null
+++ b/dist/captcha-validator.js
@@ -0,0 +1 @@
+!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["captcha-validator"]={})}(this,function(e){"use strict";function h(e,i,c,s){return new(c=c||Promise)(function(n,t){function r(e){try{a(s.next(e))}catch(e){t(e)}}function o(e){try{a(s.throw(e))}catch(e){t(e)}}function a(e){var t;e.done?n(e.value):((t=e.value)instanceof c?t:new c(function(e){e(t)})).then(r,o)}a((s=s.apply(e,i||[])).next())})}function d(r,o){var a,i,c,s={label:0,sent:function(){if(1&c[0])throw c[1];return c[1]},trys:[],ops:[]},u={next:e(0),throw:e(1),return:e(2)};return"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function e(n){return function(e){var t=[n,e];if(a)throw new TypeError("Generator is already executing.");for(;s=u&&t[u=0]?0:s;)try{if(a=1,i&&(c=2&t[0]?i.return:t[0]?i.throw||((c=i.return)&&c.call(i),0):i.next)&&!(c=c.call(i,t[1])).done)return c;switch(i=0,(t=c?[2&t[0],c.value]:t)[0]){case 0:case 1:c=t;break;case 4:return s.label++,{value:t[1],done:!1};case 5:s.label++,i=t[1],t=[0];continue;case 7:t=s.ops.pop(),s.trys.pop();continue;default:if(!(c=0<(c=s.trys).length&&c[c.length-1])&&(6===t[0]||2===t[0])){s=0;continue}if(3===t[0]&&(!c||t[1]>c[0]&&t[1]<c[3]))s.label=t[1];else if(6===t[0]&&s.label<c[1])s.label=c[1],c=t;else{if(!(c&&s.label<c[2])){c[2]&&s.ops.pop(),s.trys.pop();continue}s.label=c[2],s.ops.push(t)}}t=o.call(r,s)}catch(e){t=[6,e],i=0}finally{a=c=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}}}var y="https://api.swetrix.com/captcha",v="/validate";e.validateToken=function(s,u,l,f,p){return h(this,void 0,void 0,function(){var t,n,c;return d(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,(r=v,o="POST",a={token:s,secret:u,timestamp:l,hash:f},i=p,h(void 0,void 0,void 0,function(){var t,n;return d(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,fetch("".concat(i||y).concat(r),{method:o,headers:{"Content-Type":"application/json"},body:JSON.stringify(a)})];case 1:return t=e.sent(),[3,3];case 2:throw n=e.sent(),"Unable to make API request, error: ".concat(n);case 3:return[4,t.json()];case 4:return[2,e.sent()]}})}))];case 1:return t=e.sent(),[3,3];case 2:return[2,[!1,e.sent()]];case 3:return(n=t.success,c=t.data,n)?[2,[!0,c]]:[2,[!1,"CAPTCHA validation failed"]]}var r,o,a,i})})},Object.defineProperty(e,"__esModule",{value:!0})});
diff --git a/dist/captcha-validator.orig.js b/dist/captcha-validator.orig.js
new file mode 100644
index 000000000..a6c696750
--- /dev/null
+++ b/dist/captcha-validator.orig.js
@@ -0,0 +1,133 @@
+(function (global, factory) {
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["captcha-validator"] = {}));
+})(this, (function (exports) { 'use strict';
+
+ /******************************************************************************
+ Copyright (c) Microsoft Corporation.
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted.
+
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ PERFORMANCE OF THIS SOFTWARE.
+ ***************************************************************************** */
+
+ function __awaiter(thisArg, _arguments, P, generator) {
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
+ return new (P || (P = Promise))(function (resolve, reject) {
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
+ });
+ }
+
+ function __generator(thisArg, body) {
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
+ function verb(n) { return function (v) { return step([n, v]); }; }
+ function step(op) {
+ if (f) throw new TypeError("Generator is already executing.");
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
+ if (y = 0, t) op = [op[0] & 2, t.value];
+ switch (op[0]) {
+ case 0: case 1: t = op; break;
+ case 4: _.label++; return { value: op[1], done: false };
+ case 5: _.label++; y = op[1]; op = [0]; continue;
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
+ default:
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
+ if (t[2]) _.ops.pop();
+ _.trys.pop(); continue;
+ }
+ op = body.call(thisArg, _);
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
+ }
+ }
+
+ var DEFAULT_API_HOST = 'https://api.swetrix.com/captcha';
+ var ENDPOINTS = {
+ VALIDATE: '/validate',
+ };
+ var makeAPIRequest = function (path, method, body, apiURL) { return __awaiter(void 0, void 0, void 0, function () {
+ var res, e_1;
+ return __generator(this, function (_a) {
+ switch (_a.label) {
+ case 0:
+ _a.trys.push([0, 2, , 3]);
+ return [4 /*yield*/, fetch("".concat(apiURL || DEFAULT_API_HOST).concat(path), {
+ method: method,
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ })];
+ case 1:
+ res = _a.sent();
+ return [3 /*break*/, 3];
+ case 2:
+ e_1 = _a.sent();
+ throw "Unable to make API request, error: ".concat(e_1);
+ case 3: return [4 /*yield*/, res.json()];
+ case 4: return [2 /*return*/, _a.sent()];
+ }
+ });
+ }); };
+
+ /**
+ * Function to validate the CAPTCHA token.
+ *
+ * @param {string} token The CAPTCHA token.
+ * @param {string} secret The secret API key.
+ * @param {number} timestamp The timestamp of the CAPTCHA pass.
+ * @param {string | null} hash The hash of the CAPTCHA pass.
+ * @param {string | null} apiURL The API URL to use (default: https://api.swetrix.com/captcha).
+ * @returns {Promise<[boolean, object | null]>} The result of the validation.
+ */
+ function validateToken(token, secret, timestamp, hash, apiURL) {
+ return __awaiter(this, void 0, void 0, function () {
+ var res, e_1, success, data;
+ return __generator(this, function (_a) {
+ switch (_a.label) {
+ case 0:
+ _a.trys.push([0, 2, , 3]);
+ return [4 /*yield*/, makeAPIRequest(ENDPOINTS.VALIDATE, 'POST', {
+ token: token,
+ secret: secret,
+ timestamp: timestamp,
+ hash: hash,
+ }, apiURL)];
+ case 1:
+ res = _a.sent();
+ return [3 /*break*/, 3];
+ case 2:
+ e_1 = _a.sent();
+ return [2 /*return*/, [false, e_1]];
+ case 3:
+ success = res.success, data = res.data;
+ if (!success) {
+ return [2 /*return*/, [false, 'CAPTCHA validation failed']];
+ }
+ return [2 /*return*/, [true, data]];
+ }
+ });
+ });
+ }
+
+ exports.validateToken = validateToken;
+
+ Object.defineProperty(exports, '__esModule', { value: true });
+
+}));
diff --git a/dist/esnext/index.d.ts b/dist/esnext/index.d.ts
index 9c77fcbb3..79ecc7515 100644
--- a/dist/esnext/index.d.ts
+++ b/dist/esnext/index.d.ts
@@ -1,27 +1,11 @@
-import { Lib, LibOptions, TrackEventOptions, PageViewsOptions, PageActions } from './Lib';
-export declare let LIB_INSTANCE: Lib | null;
/**
- * Initialise the tracking library instance (other methods won't work if the library is not initialised).
+ * Function to validate the CAPTCHA token.
*
- * @param {string} pid The Project ID to link the instance of Swetrix.js to.
- * @param {LibOptions} options Options related to the tracking.
- * @returns {Lib} Instance of the Swetrix.js.
+ * @param {string} token The CAPTCHA token.
+ * @param {string} secret The secret API key.
+ * @param {number} timestamp The timestamp of the CAPTCHA pass.
+ * @param {string | null} hash The hash of the CAPTCHA pass.
+ * @param {string | null} apiURL The API URL to use (default: https://api.swetrix.com/captcha).
+ * @returns {Promise<[boolean, object | null]>} The result of the validation.
*/
-export declare function init(pid: string, options?: LibOptions): Lib;
-/**
- * With this function you are able to track any custom events you want.
- * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
- * The total number of track calls and their conversion rate will be saved.
- *
- * @param {TrackEventOptions} event The options related to the custom event.
- */
-export declare function track(event: TrackEventOptions): void;
-/**
- * With this function you are able to track any custom events you want.
- * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
- * The total number of track calls and their conversion rate will be saved.
- *
- * @param {PageViewsOptions} options The options related to the custom event.
- * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
- */
-export declare function trackViews(options?: PageViewsOptions): Promise<PageActions>;
+export declare function validateToken(token: string, secret: string, timestamp: number, hash?: string, apiURL?: string): Promise<[boolean, object | string]>;
diff --git a/dist/esnext/index.js b/dist/esnext/index.js
index f96627acd..42faa390b 100644
--- a/dist/esnext/index.js
+++ b/dist/esnext/index.js
@@ -1,54 +1,31 @@
-import { Lib, defaultPageActions, } from './Lib';
-export let LIB_INSTANCE = null;
+import { makeAPIRequest, ENDPOINTS } from './utils';
/**
- * Initialise the tracking library instance (other methods won't work if the library is not initialised).
+ * Function to validate the CAPTCHA token.
*
- * @param {string} pid The Project ID to link the instance of Swetrix.js to.
- * @param {LibOptions} options Options related to the tracking.
- * @returns {Lib} Instance of the Swetrix.js.
+ * @param {string} token The CAPTCHA token.
+ * @param {string} secret The secret API key.
+ * @param {number} timestamp The timestamp of the CAPTCHA pass.
+ * @param {string | null} hash The hash of the CAPTCHA pass.
+ * @param {string | null} apiURL The API URL to use (default: https://api.swetrix.com/captcha).
+ * @returns {Promise<[boolean, object | null]>} The result of the validation.
*/
-export function init(pid, options) {
- if (!LIB_INSTANCE) {
- LIB_INSTANCE = new Lib(pid, options);
+export async function validateToken(token, secret, timestamp, hash, apiURL) {
+ let res;
+ try {
+ res = await makeAPIRequest(ENDPOINTS.VALIDATE, 'POST', {
+ token,
+ secret,
+ timestamp,
+ hash,
+ }, apiURL);
}
- return LIB_INSTANCE;
-}
-/**
- * With this function you are able to track any custom events you want.
- * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
- * The total number of track calls and their conversion rate will be saved.
- *
- * @param {TrackEventOptions} event The options related to the custom event.
- */
-export function track(event) {
- if (!LIB_INSTANCE)
- return;
- LIB_INSTANCE.track(event);
-}
-/**
- * With this function you are able to track any custom events you want.
- * You should never send any identifiable data (like User ID, email, session cookie, etc.) as an event name.
- * The total number of track calls and their conversion rate will be saved.
- *
- * @param {PageViewsOptions} options The options related to the custom event.
- * @returns {PageActions} The actions related to the tracking. Used to stop tracking pages.
- */
-export function trackViews(options) {
- return new Promise((resolve) => {
- if (!LIB_INSTANCE) {
- resolve(defaultPageActions);
- return;
- }
- // We need to verify that document.readyState is complete for the performance stats to be collected correctly.
- if (typeof document === 'undefined' || document.readyState === 'complete') {
- resolve(LIB_INSTANCE.trackPageViews(options));
- }
- else {
- window.addEventListener('load', () => {
- // @ts-ignore
- resolve(LIB_INSTANCE.trackPageViews(options));
- });
- }
- });
+ catch (e) {
+ return [false, e];
+ }
+ const { success, data } = res;
+ if (!success) {
+ return [false, 'CAPTCHA validation failed'];
+ }
+ return [true, data];
}
//# sourceMappingURL=index.js.map
\ No newline at end of file
diff --git a/dist/esnext/index.js.map b/dist/esnext/index.js.map
index 26ac360fa..3c329d731 100644
--- a/dist/esnext/index.js.map
+++ b/dist/esnext/index.js.map
@@ -1 +1 @@
-{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EAAgE,kBAAkB,GACtF,MAAM,OAAO,CAAA;AAEd,MAAM,CAAC,IAAI,YAAY,GAAe,IAAI,CAAA;AAE1C;;;;;;GAMG;AACH,MAAM,UAAU,IAAI,CAAC,GAAW,EAAE,OAAoB;IACpD,IAAI,CAAC,YAAY,EAAE;QACjB,YAAY,GAAG,IAAI,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;KACrC;IAED,OAAO,YAAY,CAAA;AACrB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CAAC,KAAwB;IAC5C,IAAI,CAAC,YAAY;QAAE,OAAM;IAEzB,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;AAC3B,CAAC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C;IAED,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;AACrB,CAAC"}
\ No newline at end of file
diff --git a/dist/esnext/utils.d.ts b/dist/esnext/utils.d.ts
index c65fe5217..904abfe5d 100644
--- a/dist/esnext/utils.d.ts
+++ b/dist/esnext/utils.d.ts
@@ -1,10 +1,4 @@
-export declare const isInBrowser: () => boolean;
-export declare const isLocalhost: () => boolean;
-export declare const isAutomated: () => boolean;
-export declare const getLocale: () => string;
-export declare const getTimezone: () => string | undefined;
-export declare const getReferrer: () => string | undefined;
-export declare const getUTMSource: () => string | undefined;
-export declare const getUTMMedium: () => string | undefined;
-export declare const getUTMCampaign: () => string | undefined;
-export declare const getPath: () => string;
+export declare const ENDPOINTS: {
+ VALIDATE: string;
+};
+export declare const makeAPIRequest: (path: string, method: string, body: object, apiURL?: string) => Promise<any>;
diff --git a/dist/esnext/utils.js b/dist/esnext/utils.js
index f376b120c..1eb172dee 100644
--- a/dist/esnext/utils.js
+++ b/dist/esnext/utils.js
@@ -1,38 +1,21 @@
-const findInSearch = (exp) => {
- const res = location.search.match(exp);
- return (res && res[2]) || undefined;
+const DEFAULT_API_HOST = 'https://api.swetrix.com/captcha';
+export const ENDPOINTS = {
+ VALIDATE: '/validate',
};
-const utmSourceRegex = /[?&](ref|source|utm_source)=([^?&]+)/;
-const utmCampaignRegex = /[?&](utm_campaign)=([^?&]+)/;
-const utmMediumRegex = /[?&](utm_medium)=([^?&]+)/;
-export const isInBrowser = () => {
- return typeof window !== 'undefined';
-};
-export const isLocalhost = () => {
- return location?.hostname === 'localhost' || location?.hostname === '127.0.0.1' || location?.hostname === '';
-};
-export const isAutomated = () => {
- return navigator?.webdriver;
-};
-export const getLocale = () => {
- return typeof navigator.languages !== 'undefined' ? navigator.languages[0] : navigator.language;
-};
-export const getTimezone = () => {
+export const makeAPIRequest = async (path, method, body, apiURL) => {
+ let res;
try {
- return Intl.DateTimeFormat().resolvedOptions().timeZone;
+ res = await fetch(`${apiURL || DEFAULT_API_HOST}${path}`, {
+ method,
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(body),
+ });
}
catch (e) {
- return;
+ throw `Unable to make API request, error: ${e}`;
}
-};
-export const getReferrer = () => {
- return document.referrer || undefined;
-};
-export const getUTMSource = () => findInSearch(utmSourceRegex);
-export const getUTMMedium = () => findInSearch(utmMediumRegex);
-export const getUTMCampaign = () => findInSearch(utmCampaignRegex);
-export const getPath = () => {
- // TODO: Maybe we should also include such data as location.hash or location.search
- return location.pathname || '';
+ return await res.json();
};
//# sourceMappingURL=utils.js.map
\ No newline at end of file
diff --git a/dist/esnext/utils.js.map b/dist/esnext/utils.js.map
index 78db14da6..a102aa324 100644
--- a/dist/esnext/utils.js.map
+++ b/dist/esnext/utils.js.map
@@ -1 +1 @@
-{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,YAAY,GAAG,CAAC,GAAW,EAAsB,EAAE;IACvD,MAAM,GAAG,GAAG,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IACtC,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,SAAS,CAAA;AACrC,CAAC,CAAA;AAED,MAAM,cAAc,GAAG,sCAAsC,CAAA;AAC7D,MAAM,gBAAgB,GAAG,6BAA6B,CAAA;AACtD,MAAM,cAAc,GAAG,2BAA2B,CAAA;AAElD,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,EAAE;IAC9B,OAAO,OAAO,MAAM,KAAK,WAAW,CAAA;AACtC,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,EAAE;IAC9B,OAAO,QAAQ,EAAE,QAAQ,KAAK,WAAW,IAAI,QAAQ,EAAE,QAAQ,KAAK,WAAW,IAAI,QAAQ,EAAE,QAAQ,KAAK,EAAE,CAAA;AAC9G,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,WAAW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AAE,MAAc,EAAE,IAAY,EAAE,MAAe,EAAgB,EAAE;IAChH,IAAI,GAAG,CAAA;IAEP,IAAI;QACF,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,MAAM,IAAI,gBAAgB,GAAG,IAAI,EAAE,EAAE;YACxD,MAAM;YACN,OAAO,EAAE;gBACP,cAAc,EAAE,kBAAkB;aACnC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAA;KACH;IAAC,OAAO,CAAC,EAAE;QACV,MAAM,sCAAsC,CAAC,EAAE,CAAA;KAChD;IAED,OAAO,MAAM,GAAG,CAAC,IAAI,EAAE,CAAA;AACzB,CAAC,CAAA"}
\ No newline at end of file