@quadible/web-sdk
Version:
The web sdk for Quadible's behavioral authentication service.
125 lines • 4.08 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fflate_1 = require("fflate");
const sleep_1 = __importDefault(require("./common/sleep"));
// Hack for clients that wrap fetch in a way that makes it incompatible
// with this SDK.
// tslint:disable-next-line: no-string-literal
const fetchFn = (...args) => (window['__quadible_fetch'] || fetch)(...args);
class API {
config;
cid;
constructor(config) {
this.config = config;
}
async postFile(path, payload) {
const headers = this.getHeaders();
delete headers['content-type'];
const response = await fetchFn(`${this.config.serviceUrl}${path}`, {
method: 'post',
body: payload,
headers
});
return response;
}
async deleteUser() {
await this.request({ url: `/v1/user`, method: 'DELETE' });
}
async authenticate() {
return await this.request({
url: '/v1/auth',
retries: 3
});
}
async getRemoteConfig() {
return await this.request({
url: `/v1/user/config?cp=${encodeURIComponent(JSON.stringify({ platform: 'Web' }))}`,
retries: 3
});
}
async pushEvents(events) {
const headers = {};
let payload = JSON.stringify({ events });
if (this.config.compression) {
payload = await new Promise((resolve, reject) => {
(0, fflate_1.gzip)(new TextEncoder().encode(payload), (err, data) => {
if (err) {
reject(err);
}
else {
resolve(data);
}
});
});
headers['content-type'] = 'application/octet-stream';
}
await this.request({
url: `/v1/events`,
method: 'POST',
payload,
headers,
retries: 3
});
}
async getConfiguredSdkVersion() {
return await this.request({
url: '/v1/websdk/version',
retries: 3
});
}
async loadDependency(url) {
return await this.loadScript(`${this.config.serviceUrl}/v1/websdk/dependency?url=${encodeURIComponent(url)}`);
}
async loadSdk(version) {
return await this.loadScript(`${this.config.serviceUrl}/v1/websdk/version/${version}`);
}
setCid(cid) {
this.cid = cid;
}
async request({ url, method = 'GET', headers = {}, payload, retries }) {
let tries = 0;
while (true) {
tries++;
const response = await fetchFn(`${this.config.serviceUrl.replace(/\/+$/, '')}${url}`, {
method,
headers: { ...this.getHeaders(), ...headers },
body: payload
});
if (!response.ok) {
if (tries > retries) {
throw new Error(`Request failed with status ${response.status}`);
}
await (0, sleep_1.default)(500 * tries);
continue;
}
if (response.headers.get('content-type')?.includes('application/json')) {
return await response.json();
}
else {
return await response.text();
}
}
}
async loadScript(url) {
const script = document.createElement('script');
script.src = url;
script.async = true;
document.head.appendChild(script);
return new Promise((resolve, reject) => {
script.onload = resolve;
script.onerror = reject;
});
}
getHeaders() {
return {
'content-type': 'application/json',
authorization: `Bearer ${this.config.apiKey}`,
'x-app-cid': this.cid
};
}
}
exports.default = API;
//# sourceMappingURL=API.js.map