import { BaseSDK } from './sdk'; import { StandardResponse, Storage } from '@jolibox/types'; /** * @private * Provides functionalities for persistent key-value storage within the Jolibox SDK. * Allows setting, getting, removing items, and clearing the entire storage. * @implements {Storage} */ export class StorageSDK extends BaseSDK implements Storage { /** * @public * Retrieves an item from storage based on its key. * @param key - The key of the item to retrieve. * @returns A promise that resolves with the value of the item, or null if the key is not found. The specific structure of the resolved value (e.g., if wrapped in StandardResponse) depends on the command execution result. */ async getItem(key: string): Promise> { const result = await this.commands.executeCommand('StorageSDK.getItem', key); return result as StandardResponse; } /** * @public * Sets an item in storage with a given key and value. * There are limitations on the length of the key and the combined length of key and value. * @param key - The key for the item. Should be less than 128 characters. * @param value - The value to store. Can be a number, string, or boolean. It will be converted to a string for storage. * @returns A promise that resolves with a StandardResponse. It might contain an error object if validation fails (e.g., key/value length exceeded). */ async setItem(key: string, value: number | string | boolean): Promise> { if (key.length > 128) { return { code: 'PARAMETER_ERROR', message: '[SDK] cloud storage setItem error: length of key should be less than 128' } as StandardResponse; } const valueToStore = typeof value == 'string' ? value : String(value); if (key.length + valueToStore.length > 1024) { return { code: 'PARAMETER_ERROR', message: '[SDK] cloud storage setItem error: length of key and value should be less than 1024' } as StandardResponse; } return await this.commands.executeCommand('StorageSDK.setItem', key, valueToStore); } /** * @public * Removes an item from storage based on its key. * @param key - The key of the item to remove. * @returns A promise that resolves with a StandardResponse (or the direct result of command execution). */ async removeItem(key: string): Promise> { return this.commands.executeCommand('StorageSDK.removeItem', key); } /** * @public * Clears all items from the storage. * @returns A promise that resolves with a StandardResponse (or the direct result of command execution). */ async clear(): Promise> { return this.commands.executeCommand('StorageSDK.clear'); } }