diff --git a/lib/class/cryptor.d.ts b/lib/class/cryptor.d.ts new file mode 100644 index 0000000..540ac96 --- /dev/null +++ b/lib/class/cryptor.d.ts @@ -0,0 +1,11 @@ +import { CryptorOption, KeyOption, ReturnOption, CryptorModel, VectorIV } from './interface'; +export declare const CryptorDefaults: CryptorOption; +export declare class Cryptor implements CryptorModel { + #private; + constructor(options?: CryptorOption, ivHex?: VectorIV); + get settings(): CryptorOption; + get key(): KeyOption; + get ivHex(): KeyOption; + encrypt(subject: string): ReturnOption; + decrypt(encrypted: string): ReturnOption; +} diff --git a/lib/class/encoded-storage.d.ts b/lib/class/encoded-storage.d.ts new file mode 100644 index 0000000..2976725 --- /dev/null +++ b/lib/class/encoded-storage.d.ts @@ -0,0 +1,12 @@ +import { KeyPath, StorageValue, Storage } from './interface'; +import { WebStore } from "./storage"; +export declare class EncodedWebStore extends WebStore { + #private; + /** + * + * @param storage Storage interface to be used and initialized. + */ + constructor(storage: Storage, delimiter?: string); + getItem(key: KeyPath): StorageValue; + setItem(key: KeyPath, value: StorageValue): boolean | Error; +} diff --git a/lib/class/encrypted-storage.d.ts b/lib/class/encrypted-storage.d.ts new file mode 100644 index 0000000..49959c8 --- /dev/null +++ b/lib/class/encrypted-storage.d.ts @@ -0,0 +1,15 @@ +import { KeyPath, StorageValue, Storage, EncryptedWebStorage } from './interface'; +import { WebStore } from "./storage"; +import { Cryptor } from "./cryptor"; +export declare class EncryptedWebStore extends WebStore implements EncryptedWebStorage { + #private; + /** + * + * @param storage Storage interface to be used and initialized. + */ + constructor(storage: Storage, cryptor: Cryptor, delimiter?: string); + getItem(key: KeyPath): StorageValue; + setItem(key: KeyPath, value: StorageValue): boolean | Error; + getEncryptedRawItem(key: string): any; + setEncryptedRawItem(key: string, value: any): boolean | Error; +} diff --git a/lib/class/interface.d.ts b/lib/class/interface.d.ts new file mode 100644 index 0000000..a867781 --- /dev/null +++ b/lib/class/interface.d.ts @@ -0,0 +1,143 @@ +/// +export type KeyPath = string; +export type StorageValue = any; +export type StorageItem = { + key: KeyPath; + value: StorageValue; +}; +/** + * Attribute compare will work for a collection of items where values match or will replace a value of the matched key for data objects. + */ +export type AttributeCompare = { + name: string; + value: string | number; +}; +export interface Storage { + /** + * keypath delimeter. defaults to '.'. + */ + delimiter: string; + /** + * Returns an integer representing the number of data items stored in the Storage object. + */ + get length(): number; + /** + * Returns an integer representing the number of data items stored in the Storage object. + * @param n When passed a number n, this method will return the name of the nth key in the storage. + */ + key(n: number): number; + /** + * When passed a key name, will return that key's value. + * @param {KeyPath} key key name. + */ + getItem(key: KeyPath): StorageValue; + /** + * When passed a key name and value, will add that key to the storage, or update that key's value if it already exists. + * @param {KeyPath} key A string containing the name of the key you want to create/update. + * @param {StorageValue} value A string containing the value you want to give the key you are creating/updating. + */ + setItem(key: KeyPath, value: StorageValue): boolean | Error; + /** + * When passed a key name, will remove that key from the storage. + * @param {KeyPath} key A string containing the name of the key you want to remove. + */ + removeItem(key: KeyPath): void; + /** + * When invoked, will empty all keys out of the storage. + */ + clear(): void; +} +export interface WebStorage extends Storage { + /** + * Add multiple entries of key value pairs to the storage. + * @param {StorageItem[]} items Items to add individually in the storage. + */ + setMultipleItems(items: StorageItem[]): boolean | Error; + /** + * Remove multiple entries found in the specified keypaths. + * Will only work on top level keypaths and will not utilize an `AttributeCompare`. + * Use `removeItemInItem` to utilize an `AttributeCompare`. + * @param {KeyPath[]} keys + */ + removeMultipleItems(keys: KeyPath[]): void; + /** + * Returns multiple entries found in the specified keypaths. + * Will only work on top level keypaths and will not utilize an `AttributeCompare`. + * Use `getItemInItem` to utilize an `AttributeCompare`. + * @param {KeyPath[]} keys + */ + getMultipleItems(keys: KeyPath[]): StorageValue[]; + /** + * Append item to an existing item on the storage. Works for object and array type data. + * @param {KeyPath} key keypath of the data you want to append to. + * @param {StorageValue} value data value you want to append to. + */ + appendItemInItem(key: KeyPath, value: StorageValue): boolean | Error; + /** + * Updates an item in the specified keypath. + * @param {KeyPath} key keypath of the data. + * @param {AttributeCompare} attrCompare data key attribute to be updated. + */ + updateItemInItem(key: KeyPath, attrCompare: AttributeCompare | null, newValue: StorageValue): boolean | Error; + /** + * Removes an item in the specified keypath. + * @param {KeyPath} key keypath of the data. + * @param {AttributeCompare} attrCompare data key attribute to be updated. + */ + removeItemInItem(key: KeyPath, attrCompare?: AttributeCompare): boolean | Error; + /** + * Returns data found in the specified keypath. + * @param {KeyPath} key keypath of the data. + * @param {AttributeCompare} attrCompare data key attribute to be updated. + */ + getItemInItem(key: KeyPath, attrCompare?: AttributeCompare): StorageValue; +} +export interface EncryptedWebStorage extends Storage { + /** + * When passed a key name, will return that key's value. + * @param {KeyPath} key key name. + */ + getEncryptedRawItem(key: KeyPath): StorageValue; + /** + * When passed a key name and value, will add that key to the storage, or update that key's value if it already exists. + * @param {KeyPath} key A string containing the name of the key you want to create/update. + * @param {StorageValue} value A string containing the value you want to give the key you are creating/updating. + */ + setEncryptedRawItem(key: KeyPath, value: StorageValue): boolean | Error; +} +/** + * Cryptor interface + */ +export interface CryptorOption { + salt: string | Buffer; + keyLength: number; + algorithm: string; + password: string | Buffer; + byteLength: number; +} +export type KeyOption = string | null; +export type ReturnOption = string | null; +export type VectorIV = string | null; +export interface CryptorModel { + /** + * Returns the cryptor's configs + */ + get settings(): CryptorOption; + /** + * Returns the cryptor's generated encryption key + */ + get key(): KeyOption; + /** + * Returns the cryptor's initialization vector. + * NOTE: This is important for decryption. Make sure you store it somewhere for reuse. + */ + get ivHex(): KeyOption; + /** + * Encrypt the data and save to storage. + */ + encrypt(subject: string): ReturnOption; + /** + * Returns the decrypted data from storage. + */ + decrypt(encrypted: string): ReturnOption; +} diff --git a/lib/class/storage.d.ts b/lib/class/storage.d.ts new file mode 100644 index 0000000..d3c1632 --- /dev/null +++ b/lib/class/storage.d.ts @@ -0,0 +1,23 @@ +import { AttributeCompare, KeyPath, Storage, StorageItem, StorageValue, WebStorage } from './interface'; +export declare class WebStore implements WebStorage { + #private; + delimiter: string; + /** + * + * @param storage Storage interface to be used and initialized. + */ + constructor(storage: Storage, delimiter?: string); + get length(): number; + key(n: number): number; + getItem(key: KeyPath): StorageValue; + setItem(key: KeyPath, value: StorageValue): boolean | Error; + removeItem(key: KeyPath): void; + clear(): void; + setMultipleItems(items: StorageItem[]): boolean | Error; + removeMultipleItems(keys: KeyPath[]): void; + getMultipleItems(keys: KeyPath[]): StorageValue[]; + appendItemInItem(key: KeyPath, value: any): boolean | Error; + updateItemInItem(key: KeyPath, attrCompare: AttributeCompare | null, newValue: StorageValue): boolean | Error; + removeItemInItem(key: KeyPath, attrCompare?: AttributeCompare): boolean | Error; + getItemInItem(key: KeyPath, attrCompare?: AttributeCompare): StorageValue; +} diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..bcecf8a --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,4 @@ +export interface Options { + delimiter?: string; + isEncoded: boolean; +} diff --git a/package-lock.json b/package-lock.json index e99067a..46a71bb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "web-storage-manager", - "version": "4.0.3", + "version": "4.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "web-storage-manager", - "version": "4.0.3", + "version": "4.0.4", "license": "MIT", "devDependencies": { "@babel/cli": "^7.22.5", diff --git a/package.json b/package.json index f2cf399..93e8e6e 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,18 @@ { "name": "web-storage-manager", - "version": "4.0.4", + "version": "4.1.0", "description": "Web utility storage manager for handling data encryption, save and persist, update and data purge in your local and session storage", "main": "lib/index.js", "type": "commonjs", + "types": "lib/index.d.ts", "directories": { "lib": "lib" }, "scripts": { "test": "jest", - "test:build": "rm -rf lib && npm run build:js && jest", - "test:silent-build": "rm -rf lib && npm run build:js && jest --verbose false", - "build": "rm -rf lib && npm run build:js", + "test:build": "rm -rf lib && npm run build && jest", + "test:silent-build": "rm -rf lib && npm run build && jest --verbose false", + "build": "rm -rf lib && npm run build:js && npm run build:types", "build:types": "tsc --emitDeclarationOnly", "build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline", "publish": "npm run build && npm publish" diff --git a/tsconfig.json b/tsconfig.json index a4035d3..627c72d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "noEmit": true, + "noEmit": false, "allowJs": true, "alwaysStrict": true, "module": "ESNext",