ibm-appconfiguration-js-client-sdk
Version:
IBM Cloud App Configuration JavaScript Client SDK
79 lines (78 loc) • 2.74 kB
JavaScript
;
/**
* Copyright 2022 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.storage = void 0;
var prefix = 'ibmappconfig:';
var InMemoryStorage = /** @class */ (function () {
function InMemoryStorage() {
this.map = new Map();
}
InMemoryStorage.prototype.save = function (name, data) {
var key = ''.concat(prefix, name);
this.map.set(key, data);
};
InMemoryStorage.prototype.get = function (name) {
var key = ''.concat(prefix, name);
return this.map.get(key);
};
InMemoryStorage.prototype.remove = function (name) {
var key = ''.concat(prefix, name);
this.map.delete(key);
};
InMemoryStorage.prototype.clear = function () {
this.map.clear();
};
return InMemoryStorage;
}());
var LocalStorage = /** @class */ (function () {
function LocalStorage() {
}
LocalStorage.prototype.save = function (name, data) {
var key = ''.concat(prefix, name);
window.localStorage.setItem(key, JSON.stringify(data));
};
LocalStorage.prototype.get = function (name) {
var key = ''.concat(prefix, name);
var data = window.localStorage.getItem(key);
return data ? JSON.parse(data) : undefined;
};
LocalStorage.prototype.remove = function (name) {
var key = ''.concat(prefix, name);
window.localStorage.removeItem(key);
};
LocalStorage.prototype.clear = function () {
window.localStorage.clear();
};
return LocalStorage;
}());
function isLocalStorageAvailable() {
var storage_1;
try {
storage_1 = window.localStorage;
var x = '__ibm_appconfig_storage_test__';
storage_1.setItem(x, x);
storage_1.removeItem(x);
return true;
}
catch (e) {
return (e instanceof DOMException &&
(e.code === 22 || e.code === 1014 || e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
storage_1 &&
storage_1.length !== 0);
}
}
exports.storage = isLocalStorageAvailable() ? new LocalStorage() : new InMemoryStorage();