rox-ssr
Version:
Rollout.io ROX JS SDK Client for SSR
173 lines • 7.78 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Rox = exports._RoxClient = exports.RoxOverridesPosition = exports.RoxFetcherStatus = exports.RoxNumber = exports.RoxString = exports.Flag = void 0;
const utils_1 = require("./utils");
var flag_1 = require("./flag");
Object.defineProperty(exports, "Flag", { enumerable: true, get: function () { return flag_1.Flag; } });
var string_1 = require("./string");
Object.defineProperty(exports, "RoxString", { enumerable: true, get: function () { return string_1.RoxString; } });
var number_1 = require("./number");
Object.defineProperty(exports, "RoxNumber", { enumerable: true, get: function () { return number_1.RoxNumber; } });
var RoxFetcherStatus;
(function (RoxFetcherStatus) {
RoxFetcherStatus["AppliedFromEmbedded"] = "APPLIED_FROM_EMBEDDED";
RoxFetcherStatus["AppliedFromCache"] = "APPLIED_FROM_CACHE";
RoxFetcherStatus["AppliedFromNetwork"] = "APPLIED_FROM_NETWORK";
RoxFetcherStatus["ErrorFetchFailed"] = "ERROR_FETCH_FAILED";
})(RoxFetcherStatus = exports.RoxFetcherStatus || (exports.RoxFetcherStatus = {}));
var RoxOverridesPosition;
(function (RoxOverridesPosition) {
RoxOverridesPosition["TopLeft"] = "top left";
RoxOverridesPosition["TopRight"] = "top right";
RoxOverridesPosition["BottomLeft"] = "bottom left";
RoxOverridesPosition["BottomRight"] = "bottom right";
})(RoxOverridesPosition = exports.RoxOverridesPosition || (exports.RoxOverridesPosition = {}));
class _RoxClient {
constructor() {
this.dynamicApi = utils_1.internalRox.dynamicApi;
this.overrides = utils_1.internalRox.overrides;
this.alreadyInitialized = false;
this._configurationFetchedHandlers = [];
this._rolloutData = {};
this._confFetchedHandlers = [];
this._impressionHandlers = [];
}
register(namespace, roxContainer) {
try {
utils_1.internalRox.register(namespace, roxContainer);
}
catch (e) {
if (!e.message || !e.message.includes('A namespace must be unique')) {
throw e;
}
}
if (this.flags && this.flags.length > 0) {
this.flags.forEach((f) => {
if (f === null || f === void 0 ? void 0 : f.name) {
roxContainer[f.name.replace(`${namespace}.`, '')] = f;
}
});
}
}
setup(apiKey, options) {
return __awaiter(this, void 0, void 0, function* () {
if (this._initialSetup) {
if (this._initialSetup.apiKey !== apiKey ||
this._initialSetup.devModeSecret !== (options === null || options === void 0 ? void 0 : options.devModeSecret)) {
throw new Error('Rox cannot be initialized more than once');
}
if (options === null || options === void 0 ? void 0 : options.configurationFetchedHandler) {
this._confFetchedHandlers.push(options.configurationFetchedHandler);
}
if (options === null || options === void 0 ? void 0 : options.impressionHandler) {
this._impressionHandlers.push(options.impressionHandler);
}
return;
}
const _options = Object.assign({}, options);
if (options === null || options === void 0 ? void 0 : options.configurationFetchedHandler) {
this._confFetchedHandlers.push(options.configurationFetchedHandler);
}
_options.configurationFetchedHandler = this._confFetchedHandler;
_options.impressionHandler = this._impressionsHandler;
yield this._setup(apiKey, options);
this._initialSetup = {
apiKey,
devModeSecret: options === null || options === void 0 ? void 0 : options.devModeSecret,
};
});
}
_confFetchedHandler() {
const handlers = this._confFetchedHandlers;
return (fetcherResults) => {
handlers.forEach((cfh) => cfh(fetcherResults));
};
}
_impressionsHandler() {
const handlers = this._impressionHandlers;
return (reporting, context) => {
handlers.forEach((imh) => imh(reporting, context));
};
}
_setup(apiKey, options) {
return __awaiter(this, void 0, void 0, function* () {
if (this.alreadyInitialized) {
throw new Error('Rox cannot be initialized more than once');
}
const _options = Object.assign({}, options);
if ((0, utils_1.isBrowser)() && window && window.rolloutData) {
Object.assign(_options, options, {
embedded: window.rolloutData,
disableNetworkFetch: true,
});
if (_options.disablePushUpdateListener !== false) {
_options.disablePushUpdateListener = true;
}
}
if (!_options.platform) {
_options.platform = 'SSR';
}
if (options === null || options === void 0 ? void 0 : options.configurationFetchedHandler) {
this._configurationFetchedHandlers.push(options.configurationFetchedHandler);
}
_options.configurationFetchedHandler = this._configurationFetchedHandler();
yield utils_1.internalRox.setup(apiKey, _options);
this.alreadyInitialized = true;
});
}
setContext(globalContext) {
utils_1.internalRox.setContext(globalContext);
}
setCustomNumberProperty(name, value) {
utils_1.internalRox.setCustomNumberProperty(name, value);
}
setCustomStringProperty(name, value) {
utils_1.internalRox.setCustomStringProperty(name, value);
}
setCustomBooleanProperty(name, value) {
utils_1.internalRox.setCustomBooleanProperty(name, value);
}
setCustomDateProperty(name, value) {
utils_1.internalRox.setCustomDateProperty(name, value);
}
setCustomSemverProperty(name, value) {
utils_1.internalRox.setCustomSemverProperty(name, value);
}
fetch() {
utils_1.internalRox.fetch();
}
showOverrides(position) {
if ((0, utils_1.isBrowser)()) {
utils_1.internalRox.showOverrides(position);
return;
}
throw new Error('showOverrides is only available in a browser context');
}
_configurationFetchedHandler() {
const roxClient = this;
return (fetcherResults) => {
if (fetcherResults.hasChanges) {
roxClient._rolloutData = fetcherResults.clientData;
}
roxClient._configurationFetchedHandlers.forEach((cfh) => cfh(fetcherResults));
};
}
get rolloutData() {
return JSON.stringify(this._rolloutData);
}
get flags() {
return utils_1.internalRox.flags;
}
}
exports._RoxClient = _RoxClient;
exports.Rox = new _RoxClient();
//# sourceMappingURL=index.js.map