trusted
Version:
⚖️ Trustworthy localStorage. Validate against a schema, set default values.
205 lines (170 loc) • 6.09 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
var Trusted = /*#__PURE__*/function () {
function Trusted(options) {
if (options === void 0) {
options = {};
}
var _options = options,
namespace = _options.namespace;
this.namespace = namespace;
this.registeredKeys = new Set();
this.accessor.bind(this);
}
var _proto = Trusted.prototype;
_proto.registerKey = function registerKey(key) {
if (this.registeredKeys.has(key)) {
throw new Error("Key has already been registered: " + key);
}
this.registeredKeys.add(key);
};
_proto.unregisterKey = function unregisterKey(key) {
this.registeredKeys["delete"](key);
};
_proto.accessor = function accessor(options) {
var _this = this;
var key = "" + (this.namespace || '') + options.key;
var yupSchema = options.yupSchema,
skipRegistration = options.skipRegistration,
_options$validate = options.validate,
validate = _options$validate === void 0 ? yupSchema ? function (value) {
return yupSchema.isValidSync(value);
} : undefined : _options$validate,
unmarshal = options.unmarshal,
marshal = options.marshal,
defaultValue = options.defaultValue;
if (defaultValue !== undefined && defaultValue !== null && typeof validate === 'function' && !validate(defaultValue)) {
throw new Error("Invalid default value provided at key: " + key + ". Please check your yupSchema.");
}
if (defaultValue !== undefined && defaultValue !== null && typeof defaultValue !== 'string' && typeof marshal !== 'function') {
throw new Error("Invalid configuration provided at key: " + key + ". Non-string default values must be accompanied by a marshal function.");
}
if (!skipRegistration) {
this.registerKey(key);
}
return {
get: function get() {
var item;
try {
item = localStorage.getItem(key);
if (item === undefined || item === null) {
throw new Error("Item not found at key: " + key + ".");
}
item = typeof unmarshal === 'function' ? unmarshal(item) : item;
if (typeof validate === 'function' && !validate(item)) {
throw new Error("Item is invalid at key: " + key + ".");
}
return item;
} catch (e) {
if (defaultValue !== undefined && defaultValue !== null && typeof marshal === 'function') {
localStorage.setItem(key, marshal(defaultValue));
} else if (typeof defaultValue === 'string') {
localStorage.setItem(key, defaultValue);
} else if (defaultValue === undefined) {
localStorage.removeItem(key);
}
return defaultValue;
}
},
set: function set(value) {
if (validate && value !== undefined && value !== null && !validate(value)) {
console.error("Invalid value provided at key: " + key + ".", value);
return;
}
if (value !== undefined && value !== null && typeof marshal === 'function') {
localStorage.setItem(key, marshal(value));
} else if (typeof value === 'string') {
localStorage.setItem(key, value);
} else {
console.warn("You have attempted to set a non-string value in local storage without marshaling. This operation has been prevented.\nkey: " + key + "\nvalue: " + value);
}
},
remove: function remove() {
localStorage.removeItem(key);
},
unregister: function unregister() {
_this.unregisterKey(key);
},
getDefaultValue: function getDefaultValue() {
return defaultValue;
},
getKey: function getKey() {
return key;
}
};
};
_proto.string = function string(accessorOptions) {
return this.accessor(accessorOptions);
};
_proto["boolean"] = function boolean(accessorOptions) {
return this.accessor(_extends({}, accessorOptions, {
marshal: JSON.stringify,
unmarshal: JSON.parse
}));
};
_proto.object = function object(accessorOptions) {
return this.accessor(_extends({}, accessorOptions, {
marshal: JSON.stringify,
unmarshal: JSON.parse
}));
};
_proto.number = function number(accessorOptions) {
return this.accessor(_extends({}, accessorOptions, {
marshal: JSON.stringify,
unmarshal: JSON.parse
}));
};
_proto.array = function array(accessorOptions) {
return this.accessor(_extends({}, accessorOptions, {
marshal: JSON.stringify,
unmarshal: JSON.parse
}));
};
_proto.map = function map(accessorOptions) {
return this.accessor(_extends({}, accessorOptions, {
marshal: function marshal(map) {
return JSON.stringify(Array.from(map));
},
unmarshal: function unmarshal(localString) {
return new Map(JSON.parse(localString));
}
}));
};
_proto.set = function set(accessorOptions) {
return this.accessor(_extends({}, accessorOptions, {
marshal: function marshal(set) {
return JSON.stringify(Array.from(set));
},
unmarshal: function unmarshal(localString) {
return new Set(JSON.parse(localString));
}
}));
};
_proto.date = function date(accessorOptions) {
return this.accessor(_extends({}, accessorOptions, {
marshal: function marshal(value) {
return value.toISOString();
},
unmarshal: function unmarshal(localString) {
return new Date(localString);
}
}));
};
return Trusted;
}();
exports.Trusted = Trusted;
//# sourceMappingURL=trusted.cjs.development.js.map