UNPKG

clean-insights-sdk

Version:

A privacy-preserving measurement framework.

66 lines (65 loc) 2.73 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.BrowserStore = void 0; var Store_1 = require("./Store"); var storejs = require('store/dist/store.modern'); var BrowserStore = /** @class */ (function (_super) { __extends(BrowserStore, _super); function BrowserStore() { return _super !== null && _super.apply(this, arguments) || this; } BrowserStore.prototype.load = function (args) { var data = storejs.get(BrowserStore.storeJsKey); if (data && typeof data === 'object' && ((data.consents && typeof data.consents === 'object') || (data.visits && Array.isArray(data.visits)) || (data.events && Array.isArray(data.events)))) { return data; } return undefined; }; BrowserStore.prototype.persist = function (async, callback) { // We're not supporting web workers or the like in the browser environment, yet, // to do async storing. storejs.set(BrowserStore.storeJsKey, this); callback(); }; BrowserStore.prototype.send = function (data, server, timeout, done) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { try { if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.status !== 200 && xhr.status !== 204) { return done(new Error("HTTP Error ".concat(xhr.status, ": ").concat(xhr.responseText))); } done(); } } catch (e) { done(e); } }; xhr.timeout = timeout * 1000; xhr.open('POST', server); xhr.setRequestHeader('Content-type', 'application/json; charset=UTF-8'); xhr.send(data); }; BrowserStore.storeJsKey = 'clean-insights'; return BrowserStore; }(Store_1.Store)); exports.BrowserStore = BrowserStore;