|
| 1 | +/// <reference types="node" /> |
| 2 | +export type KeyPath = string; |
| 3 | +export type StorageValue = any; |
| 4 | +export type StorageItem = { |
| 5 | + key: KeyPath; |
| 6 | + value: StorageValue; |
| 7 | +}; |
| 8 | +/** |
| 9 | + * Attribute compare will work for a collection of items where values match or will replace a value of the matched key for data objects. |
| 10 | + */ |
| 11 | +export type AttributeCompare = { |
| 12 | + name: string; |
| 13 | + value: string | number; |
| 14 | +}; |
| 15 | +export interface Storage { |
| 16 | + /** |
| 17 | + * keypath delimeter. defaults to '.'. |
| 18 | + */ |
| 19 | + delimiter: string; |
| 20 | + /** |
| 21 | + * Returns an integer representing the number of data items stored in the Storage object. |
| 22 | + */ |
| 23 | + get length(): number; |
| 24 | + /** |
| 25 | + * Returns an integer representing the number of data items stored in the Storage object. |
| 26 | + * @param n When passed a number n, this method will return the name of the nth key in the storage. |
| 27 | + */ |
| 28 | + key(n: number): number; |
| 29 | + /** |
| 30 | + * When passed a key name, will return that key's value. |
| 31 | + * @param {KeyPath} key key name. |
| 32 | + */ |
| 33 | + getItem(key: KeyPath): StorageValue; |
| 34 | + /** |
| 35 | + * When passed a key name and value, will add that key to the storage, or update that key's value if it already exists. |
| 36 | + * @param {KeyPath} key A string containing the name of the key you want to create/update. |
| 37 | + * @param {StorageValue} value A string containing the value you want to give the key you are creating/updating. |
| 38 | + */ |
| 39 | + setItem(key: KeyPath, value: StorageValue): boolean | Error; |
| 40 | + /** |
| 41 | + * When passed a key name, will remove that key from the storage. |
| 42 | + * @param {KeyPath} key A string containing the name of the key you want to remove. |
| 43 | + */ |
| 44 | + removeItem(key: KeyPath): void; |
| 45 | + /** |
| 46 | + * When invoked, will empty all keys out of the storage. |
| 47 | + */ |
| 48 | + clear(): void; |
| 49 | +} |
| 50 | +export interface WebStorage extends Storage { |
| 51 | + /** |
| 52 | + * Add multiple entries of key value pairs to the storage. |
| 53 | + * @param {StorageItem[]} items Items to add individually in the storage. |
| 54 | + */ |
| 55 | + setMultipleItems(items: StorageItem[]): boolean | Error; |
| 56 | + /** |
| 57 | + * Remove multiple entries found in the specified keypaths. |
| 58 | + * Will only work on top level keypaths and will not utilize an `AttributeCompare`. |
| 59 | + * Use `removeItemInItem` to utilize an `AttributeCompare`. |
| 60 | + * @param {KeyPath[]} keys |
| 61 | + */ |
| 62 | + removeMultipleItems(keys: KeyPath[]): void; |
| 63 | + /** |
| 64 | + * Returns multiple entries found in the specified keypaths. |
| 65 | + * Will only work on top level keypaths and will not utilize an `AttributeCompare`. |
| 66 | + * Use `getItemInItem` to utilize an `AttributeCompare`. |
| 67 | + * @param {KeyPath[]} keys |
| 68 | + */ |
| 69 | + getMultipleItems(keys: KeyPath[]): StorageValue[]; |
| 70 | + /** |
| 71 | + * Append item to an existing item on the storage. Works for object and array type data. |
| 72 | + * @param {KeyPath} key keypath of the data you want to append to. |
| 73 | + * @param {StorageValue} value data value you want to append to. |
| 74 | + */ |
| 75 | + appendItemInItem(key: KeyPath, value: StorageValue): boolean | Error; |
| 76 | + /** |
| 77 | + * Updates an item in the specified keypath. |
| 78 | + * @param {KeyPath} key keypath of the data. |
| 79 | + * @param {AttributeCompare} attrCompare data key attribute to be updated. |
| 80 | + */ |
| 81 | + updateItemInItem(key: KeyPath, attrCompare: AttributeCompare | null, newValue: StorageValue): boolean | Error; |
| 82 | + /** |
| 83 | + * Removes an item in the specified keypath. |
| 84 | + * @param {KeyPath} key keypath of the data. |
| 85 | + * @param {AttributeCompare} attrCompare data key attribute to be updated. |
| 86 | + */ |
| 87 | + removeItemInItem(key: KeyPath, attrCompare?: AttributeCompare): boolean | Error; |
| 88 | + /** |
| 89 | + * Returns data found in the specified keypath. |
| 90 | + * @param {KeyPath} key keypath of the data. |
| 91 | + * @param {AttributeCompare} attrCompare data key attribute to be updated. |
| 92 | + */ |
| 93 | + getItemInItem(key: KeyPath, attrCompare?: AttributeCompare): StorageValue; |
| 94 | +} |
| 95 | +export interface EncryptedWebStorage extends Storage { |
| 96 | + /** |
| 97 | + * When passed a key name, will return that key's value. |
| 98 | + * @param {KeyPath} key key name. |
| 99 | + */ |
| 100 | + getEncryptedRawItem(key: KeyPath): StorageValue; |
| 101 | + /** |
| 102 | + * When passed a key name and value, will add that key to the storage, or update that key's value if it already exists. |
| 103 | + * @param {KeyPath} key A string containing the name of the key you want to create/update. |
| 104 | + * @param {StorageValue} value A string containing the value you want to give the key you are creating/updating. |
| 105 | + */ |
| 106 | + setEncryptedRawItem(key: KeyPath, value: StorageValue): boolean | Error; |
| 107 | +} |
| 108 | +/** |
| 109 | + * Cryptor interface |
| 110 | + */ |
| 111 | +export interface CryptorOption { |
| 112 | + salt: string | Buffer; |
| 113 | + keyLength: number; |
| 114 | + algorithm: string; |
| 115 | + password: string | Buffer; |
| 116 | + byteLength: number; |
| 117 | +} |
| 118 | +export type KeyOption = string | null; |
| 119 | +export type ReturnOption = string | null; |
| 120 | +export type VectorIV = string | null; |
| 121 | +export interface CryptorModel { |
| 122 | + /** |
| 123 | + * Returns the cryptor's configs |
| 124 | + */ |
| 125 | + get settings(): CryptorOption; |
| 126 | + /** |
| 127 | + * Returns the cryptor's generated encryption key |
| 128 | + */ |
| 129 | + get key(): KeyOption; |
| 130 | + /** |
| 131 | + * Returns the cryptor's initialization vector. |
| 132 | + * NOTE: This is important for decryption. Make sure you store it somewhere for reuse. |
| 133 | + */ |
| 134 | + get ivHex(): KeyOption; |
| 135 | + /** |
| 136 | + * Encrypt the data and save to storage. |
| 137 | + */ |
| 138 | + encrypt(subject: string): ReturnOption; |
| 139 | + /** |
| 140 | + * Returns the decrypted data from storage. |
| 141 | + */ |
| 142 | + decrypt(encrypted: string): ReturnOption; |
| 143 | +} |
0 commit comments