@giancarl021/cli-core-vault-extension
Version:
Plain and secure storage extension for the @giancarl021/cli-core npm package
98 lines (95 loc) • 3.76 kB
JavaScript
import { setProperty, getProperty, deleteProperty, deepKeys } from 'dot-prop';
import constants from '../util/constants.js';
/**
* JSON object storage service.
*
* @template Schema The schema of the object to be stored.
* @param storage The underlying storage engine to use.
* @param initialData Initial data to populate the storage with if empty.
* @returns An object with methods to interact with the stored object.
*/
function ObjectStorage(storage, initialData) {
/**
* Set a property (including nested) of the object according to the Schema.
*
* @template P A property path of the Schema.
*
* @param prop The property path to set.
* @param value The value to set at the specified property path.
* @returns A promise that resolves when the property has been set.
*/
async function set(prop, value) {
const data = await storage.readObject(constants.workspace.dataPath, initialData);
setProperty(data, String(prop), value);
await storage.writeObject(constants.workspace.dataPath, data);
}
async function get(prop, defaultValue) {
const data = await storage.readObject(constants.workspace.dataPath, initialData);
const result = getProperty(data, String(prop), defaultValue);
return result;
}
/**
* Remove a property (including nested) of the object according to the Schema.
*
* @template P A property path of the Schema.
*
* @param prop The property path to remove.
* @returns A promise that resolves when the property has been removed.
*/
async function remove(prop) {
const data = await storage.readObject(constants.workspace.dataPath, initialData);
deleteProperty(data, String(prop));
await storage.writeObject(constants.workspace.dataPath, data);
}
/**
* List all keys (including nested) in the stored object.
*
* @returns A promise that resolves to an array of all keys in the object.
*/
async function listKeys() {
const data = await storage.readObject(constants.workspace.dataPath, initialData);
return deepKeys(data);
}
return {
/**
* Set a property (including nested) of the object according to the Schema.
*
* @template P A property path of the Schema.
*
* @param prop The property path to set.
* @param value The value to set at the specified property path.
* @returns A promise that resolves when the property has been set.
*/
set,
/**
* Get a property (including nested) of the object according to the Schema.
*
* @template P A property path of the Schema.
*
* @param prop The property path to get.
* @param defaultValue An optional default value to return if the property is not found.
* @returns A promise that resolves to the value at the specified property path, or the default value if provided and the property is not found.
*/
get,
/**
* Remove a property (including nested) of the object according to the Schema.
*
* @template P A property path of the Schema.
*
* @param prop The property path to remove.
* @returns A promise that resolves when the property has been removed.
*/
remove,
/**
* List all keys (including nested) in the stored object.
*
* @returns A promise that resolves to an array of all keys in the object.
*/
listKeys,
/**
* The underlying storage engine used by the ObjectStorage.
*/
storage
};
}
export { ObjectStorage as default };