kameleoon-client-javascript
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -121,2 +121,6 @@ /// <reference types="node" /> | ||
} | ||
export interface IntegrationInterface { | ||
configDataFile?: ConfigurationAPIInterface; | ||
fetchCallback?: any; | ||
} | ||
export {}; |
@@ -12,3 +12,3 @@ import Configuration from "./configurations/configuration"; | ||
import CredentialsNotFound from "./exceptions/credentialsNotFound"; | ||
import { CallbackInterface, DataInterface, SDKConfiguration } from "./interfaces/interfaces"; | ||
import { CallbackInterface, DataInterface, SDKConfiguration, IntegrationInterface } from "./interfaces/interfaces"; | ||
declare class KameleoonClient { | ||
@@ -35,3 +35,5 @@ static readonly DEFAULT_TIMEOUT_MILLISECONDS: number; | ||
private variationStorage; | ||
constructor(siteCode: string, configuration?: SDKConfiguration); | ||
private configDataFile?; | ||
private fetchCallback?; | ||
constructor(siteCode: string, configuration?: SDKConfiguration, integration?: IntegrationInterface); | ||
private updateCampaignConfiguration; | ||
@@ -38,0 +40,0 @@ runWhenReady: (successCallback: () => void, errorCallback: () => void, timeout: number) => void; |
@@ -78,2 +78,3 @@ "use strict"; | ||
var variationStorage_1 = require("./storage/variationStorage"); | ||
var instanceVariationStorage_1 = require("./storage/instanceVariationStorage"); | ||
var constants_1 = require("./constants"); | ||
@@ -83,3 +84,3 @@ var targetingEngine_1 = require("./targeting/targetingEngine"); | ||
var KameleoonClient = /** @class */ (function () { | ||
function KameleoonClient(siteCode, configuration) { | ||
function KameleoonClient(siteCode, configuration, integration) { | ||
var _this = this; | ||
@@ -96,3 +97,2 @@ this.ready = false; | ||
this.handlerUpdateConfiguration = null; | ||
this.variationStorage = new variationStorage_1.VariationStorage(); | ||
this.updateCampaignConfiguration = function (timeStamp) { | ||
@@ -159,15 +159,20 @@ var _a; | ||
timeStampStringUrl; | ||
kameleoonUtils_1.default.doRequest(configurationUrl, kameleoonUtils_1.HttpMethod.GET, null, function (response) { | ||
try { | ||
_this.configurations = new configuration_1.default(JSON.parse(response)); | ||
_this.ready = true; | ||
_this.clearCallbacks(true); | ||
_this.saveConfiguration(); | ||
_this.fireUpdateConfigurationHandler(Boolean(timeStamp)); //fire handler after configuration was updated with SSE event | ||
_this.manageConfiguraitonUpdate(_this.configurations.settings.realTimeUpdate); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
} | ||
}, false); | ||
kameleoonUtils_1.default.doRequest({ | ||
url: configurationUrl, | ||
method: kameleoonUtils_1.HttpMethod.GET, | ||
callback: function (response) { | ||
try { | ||
_this.configurations = new configuration_1.default(JSON.parse(response)); | ||
_this.ready = true; | ||
_this.clearCallbacks(true); | ||
_this.saveConfiguration(); | ||
_this.fireUpdateConfigurationHandler(Boolean(timeStamp)); //fire handler after configuration was updated with SSE event | ||
_this.manageConfiguraitonUpdate(_this.configurations.settings.realTimeUpdate); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
} | ||
}, | ||
addKameleoonClientHeader: false, | ||
}); | ||
} | ||
@@ -219,3 +224,9 @@ else { | ||
var body = _this.getAndClearUnsentData(visitorCode); | ||
kameleoonUtils_1.default.doRequest(constants_1.API_SSX_URL + _this.getDataTrackingURL(visitorCode), kameleoonUtils_1.HttpMethod.POST, body, null, true, _this.getUserAgent(visitorCode)); | ||
kameleoonUtils_1.default.doRequest({ | ||
url: constants_1.API_SSX_URL + _this.getDataTrackingURL(visitorCode), | ||
method: kameleoonUtils_1.HttpMethod.POST, | ||
data: body, | ||
userAgent: _this.getUserAgent(visitorCode), | ||
fetchCallback: _this.fetchCallback, | ||
}); | ||
} | ||
@@ -254,4 +265,10 @@ else { | ||
var body = _this.getAndClearUnsentData(visitorCode); | ||
kameleoonUtils_1.default.doRequest(constants_1.API_SSX_URL + | ||
_this.getExperimentRegisterURL(visitorCode, experimentId, variationId, noneVariation), kameleoonUtils_1.HttpMethod.POST, body, undefined, true, _this.getUserAgent(visitorCode)); | ||
kameleoonUtils_1.default.doRequest({ | ||
url: constants_1.API_SSX_URL + | ||
_this.getExperimentRegisterURL(visitorCode, experimentId, variationId, noneVariation), | ||
method: kameleoonUtils_1.HttpMethod.POST, | ||
data: body, | ||
userAgent: _this.getUserAgent(visitorCode), | ||
fetchCallback: _this.fetchCallback, | ||
}); | ||
} | ||
@@ -298,4 +315,10 @@ else { | ||
var body = _this.getAndClearUnsentData(visitorCode); | ||
kameleoonUtils_1.default.doRequest(constants_1.API_SSX_URL + | ||
_this.getExperimentRegisterURL(visitorCode, featureId, variationId, noneVariation), kameleoonUtils_1.HttpMethod.POST, body, undefined, true, _this.getUserAgent(visitorCode)); | ||
kameleoonUtils_1.default.doRequest({ | ||
url: constants_1.API_SSX_URL + | ||
_this.getExperimentRegisterURL(visitorCode, featureId, variationId, noneVariation), | ||
method: kameleoonUtils_1.HttpMethod.POST, | ||
data: body, | ||
userAgent: _this.getUserAgent(visitorCode), | ||
fetchCallback: _this.fetchCallback, | ||
}); | ||
result = variationId != 0; | ||
@@ -399,3 +422,9 @@ } | ||
var trackingURL = _this.getExperimentRegisterURL(visitorCode, rule.experimentId, rule.getVariationIdByKey(variationKey), false); | ||
kameleoonUtils_1.default.doRequest(trackingURL, kameleoonUtils_1.HttpMethod.POST, body, null, true, _this.getUserAgent(visitorCode)); | ||
kameleoonUtils_1.default.doRequest({ | ||
url: trackingURL, | ||
method: kameleoonUtils_1.HttpMethod.POST, | ||
data: body, | ||
userAgent: _this.getUserAgent(visitorCode), | ||
fetchCallback: _this.fetchCallback, | ||
}); | ||
} | ||
@@ -604,4 +633,9 @@ else { | ||
return [2 /*return*/, new Promise(function (resolve) { | ||
kameleoonUtils_1.default.doRequest(_this.getAPIDataRequestURL(key), kameleoonUtils_1.HttpMethod.GET, null, function (response) { | ||
resolve(JSON.parse(response)); | ||
kameleoonUtils_1.default.doRequest({ | ||
url: _this.getAPIDataRequestURL(key), | ||
method: kameleoonUtils_1.HttpMethod.GET, | ||
callback: function (response) { | ||
resolve(JSON.parse(response)); | ||
}, | ||
fetchCallback: _this.fetchCallback, | ||
}); | ||
@@ -705,2 +739,5 @@ })]; | ||
this.siteCode = siteCode; | ||
this.variationStorage = (integration === null || integration === void 0 ? void 0 : integration.configDataFile) ? new instanceVariationStorage_1.InstanceVariationStorage() : new variationStorage_1.VariationStorage(); | ||
this.configDataFile = integration === null || integration === void 0 ? void 0 : integration.configDataFile; | ||
this.fetchCallback = integration === null || integration === void 0 ? void 0 : integration.fetchCallback; | ||
if (configuration) { | ||
@@ -721,4 +758,16 @@ if (configuration.visitor_data_maximum_size) { | ||
constants_1.TARGETING_DATA_MAXIMUM_SIZE_BYTES; | ||
this.updateCampaignConfiguration(); | ||
setInterval(this.emptyTargetingData, constants_1.DEFAULT_MILLISECONDS_BETWEEN_TARGETING_DATA_UPDATE); | ||
if (this.configDataFile) { | ||
try { | ||
this.configurations = new configuration_1.default(this.configDataFile); | ||
this.ready = true; | ||
this.clearCallbacks(true); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
else { | ||
this.updateCampaignConfiguration(); | ||
setInterval(this.emptyTargetingData, constants_1.DEFAULT_MILLISECONDS_BETWEEN_TARGETING_DATA_UPDATE); | ||
} | ||
} | ||
@@ -725,0 +774,0 @@ KameleoonClient.prototype.calculateHash = function (value) { |
@@ -1,4 +0,4 @@ | ||
import { UserAgent } from "./data"; | ||
import { TypeFetchRequestParams } from "./types"; | ||
declare class KameleoonUtils { | ||
doRequest: (url: string, method: HttpMethod, data?: any, callback?: any, addKameleoonClientHeader?: boolean, userAgent?: UserAgent | undefined) => void; | ||
doRequest: ({ url, method, data, callback, addKameleoonClientHeader, userAgent, fetchCallback, }: TypeFetchRequestParams) => void; | ||
computeStringSha256: (string: string) => string; | ||
@@ -5,0 +5,0 @@ encodeUTF8: (string: string) => string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HttpMethod = void 0; | ||
var VERSION = "2.0.1"; | ||
var constants_1 = require("./constants"); | ||
var VERSION = "2.1.0"; | ||
var KameleoonUtils = /** @class */ (function () { | ||
function KameleoonUtils() { | ||
var _this = this; | ||
this.doRequest = function (url, method, data, callback, addKameleoonClientHeader, userAgent) { | ||
if (addKameleoonClientHeader === void 0) { addKameleoonClientHeader = true; } | ||
var xhr = new XMLHttpRequest(); | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState === 4) { | ||
callback && callback(xhr.response); | ||
this.doRequest = function (_a) { | ||
var url = _a.url, method = _a.method, data = _a.data, callback = _a.callback, _b = _a.addKameleoonClientHeader, addKameleoonClientHeader = _b === void 0 ? true : _b, userAgent = _a.userAgent, fetchCallback = _a.fetchCallback; | ||
if (fetchCallback) { | ||
var isTracking = url.includes(constants_1.API_SSX_URL); | ||
var headers = new Headers({ | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}); | ||
if (addKameleoonClientHeader) { | ||
headers.append("Kameleoon-Client", "sdk/js/".concat(VERSION)); | ||
} | ||
}; | ||
xhr.open(method, url, true); | ||
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded"); | ||
addKameleoonClientHeader && | ||
xhr.setRequestHeader("Kameleoon-Client", "sdk/js/" + VERSION); | ||
userAgent && xhr.setRequestHeader("User-Agent", userAgent.value); | ||
xhr.send(data ? data : null); | ||
if (userAgent) { | ||
headers.append("User-Agent", userAgent.value); | ||
} | ||
var eventRequest = new Request(url, { | ||
method: method, | ||
body: data !== null && data !== void 0 ? data : null, | ||
headers: headers, | ||
}); | ||
fetchCallback(eventRequest, { | ||
backend: isTracking ? "kameleoontracking" : "kameleoondata", | ||
}) | ||
.then(function (response) { return response.text(); }) | ||
.then(function (responseText) { return callback && callback(responseText); }); | ||
} | ||
else { | ||
var xhr_1 = new XMLHttpRequest(); | ||
xhr_1.onreadystatechange = function () { | ||
if (xhr_1.readyState === 4) { | ||
callback && callback(xhr_1.response); | ||
} | ||
}; | ||
xhr_1.open(method, url, true); | ||
xhr_1.setRequestHeader("content-type", "application/x-www-form-urlencoded"); | ||
addKameleoonClientHeader && | ||
xhr_1.setRequestHeader("Kameleoon-Client", "sdk/js/" + VERSION); | ||
userAgent && xhr_1.setRequestHeader("User-Agent", userAgent.value); | ||
xhr_1.send(data ? data : null); | ||
} | ||
}; | ||
@@ -23,0 +48,0 @@ this.computeStringSha256 = function (string) { |
@@ -0,1 +1,3 @@ | ||
import { HttpMethod } from "./kameleoonUtils"; | ||
import { UserAgent } from "./data"; | ||
declare type JSONValue = string | number | boolean | { | ||
@@ -5,2 +7,11 @@ [x: string]: JSONValue; | ||
export declare type CustomJsonType = Record<string | number | symbol, JSONValue> | null; | ||
export declare type TypeFetchRequestParams = { | ||
url: string; | ||
method: HttpMethod; | ||
data?: any; | ||
callback?: any; | ||
addKameleoonClientHeader?: boolean; | ||
userAgent?: UserAgent; | ||
fetchCallback?: any; | ||
}; | ||
export {}; |
{ | ||
"name": "kameleoon-client-javascript", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "JavaScript SDK client", | ||
@@ -5,0 +5,0 @@ "main": "dist/kameleoonClient.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
140966
84
3130