|
| 1 | +export interface Storage< |
| 2 | + BaseStorageTypes extends Record<string, any> = Record<string, unknown>, |
| 3 | +> { |
| 4 | + /** |
| 5 | + * Sets the value of a key in the storage. |
| 6 | + * |
| 7 | + * @param key - The key to set the value for. |
| 8 | + * @param value - The value to set for the key. |
| 9 | + * Can be any primitive type supported by `JSON.stringify`. |
| 10 | + * |
| 11 | + * Rejects with a `StorageExceededError` if the extension exceeds its allotted storage limit. |
| 12 | + */ |
| 13 | + set< |
| 14 | + StorageTypes extends BaseStorageTypes = BaseStorageTypes, |
| 15 | + Keys extends keyof StorageTypes = keyof StorageTypes, |
| 16 | + >( |
| 17 | + key: Keys, |
| 18 | + value: StorageTypes[Keys], |
| 19 | + ): Promise<void>; |
| 20 | + |
| 21 | + /** |
| 22 | + * Sets multiple key-value pairs in the storage at once. |
| 23 | + * |
| 24 | + * If the operation fails, no changes are made to storage. |
| 25 | + * |
| 26 | + * @param entries - An object containing key-value pairs to store. |
| 27 | + * Values can be any primitive type supported by `JSON.stringify`. |
| 28 | + * |
| 29 | + * Rejects with a `StorageExceededError` if the extension exceeds its allotted storage limit. |
| 30 | + */ |
| 31 | + setMany<StorageTypes extends BaseStorageTypes = BaseStorageTypes>( |
| 32 | + entries: Partial<StorageTypes>, |
| 33 | + ): Promise<void>; |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets the value of a key in the storage. |
| 37 | + * |
| 38 | + * @param key - The key to get the value for. |
| 39 | + * @returns The value of the key. |
| 40 | + * |
| 41 | + * If no value for the key exists, the resolved value is undefined. |
| 42 | + */ |
| 43 | + get< |
| 44 | + StorageTypes extends BaseStorageTypes = BaseStorageTypes, |
| 45 | + Keys extends keyof StorageTypes = keyof StorageTypes, |
| 46 | + >( |
| 47 | + key: Keys, |
| 48 | + ): Promise<StorageTypes[Keys] | undefined>; |
| 49 | + |
| 50 | + /** |
| 51 | + * Gets the values of multiple keys in the storage at once. |
| 52 | + * |
| 53 | + * @param keys - An array of keys to get the values for. |
| 54 | + * @returns An object containing key-value pairs for the requested keys. |
| 55 | + * |
| 56 | + * The returned array is in the same order as `keys`, with `undefined` values for keys that do not exist. |
| 57 | + */ |
| 58 | + getMany< |
| 59 | + StorageTypes extends BaseStorageTypes = BaseStorageTypes, |
| 60 | + Keys extends keyof StorageTypes = keyof StorageTypes, |
| 61 | + >( |
| 62 | + keys: Keys[], |
| 63 | + ): Promise<(StorageTypes[Keys] | undefined)[]>; |
| 64 | + |
| 65 | + /** |
| 66 | + * Clears the storage. |
| 67 | + */ |
| 68 | + clear(): Promise<void>; |
| 69 | + |
| 70 | + /** |
| 71 | + * Deletes a key from the storage. |
| 72 | + * |
| 73 | + * @param key - The key to delete. |
| 74 | + * @returns A promise that resolves to `true` if the key was deleted, or `false` if the key did not exist. |
| 75 | + */ |
| 76 | + delete< |
| 77 | + StorageTypes extends BaseStorageTypes = BaseStorageTypes, |
| 78 | + Keys extends keyof StorageTypes = keyof StorageTypes, |
| 79 | + >( |
| 80 | + key: Keys, |
| 81 | + ): Promise<boolean>; |
| 82 | + |
| 83 | + /** |
| 84 | + * Deletes multiple keys from the storage at once. |
| 85 | + * |
| 86 | + * @param keys - An array of keys to delete. |
| 87 | + * @returns A promise that resolves to an object with `keys` keys, and boolean values, |
| 88 | + * which are `true` if the key was deleted, or `false` if the key did not exist. |
| 89 | + */ |
| 90 | + deleteMany< |
| 91 | + StorageTypes extends BaseStorageTypes = BaseStorageTypes, |
| 92 | + Keys extends keyof StorageTypes = keyof StorageTypes, |
| 93 | + >( |
| 94 | + keys: Keys[], |
| 95 | + ): Promise<Record<Keys, boolean>>; |
| 96 | + |
| 97 | + /** |
| 98 | + * Gets all the keys and values in the storage. |
| 99 | + * |
| 100 | + * @returns An iterator containing all the keys and values in the storage. |
| 101 | + */ |
| 102 | + entries< |
| 103 | + StorageTypes extends BaseStorageTypes = BaseStorageTypes, |
| 104 | + Keys extends keyof StorageTypes = keyof StorageTypes, |
| 105 | + >(): Promise<IterableIterator<[Keys, StorageTypes[Keys]]>>; |
| 106 | +} |
| 107 | + |
| 108 | +export interface StorageExceededError extends Error { |
| 109 | + name: 'StorageExceededError'; |
| 110 | +} |
0 commit comments