@resk/core
Version:
An innovative TypeScript framework that empowers developers to build applications with a fully decorator-based architecture for efficient resource management. By combining the power of decorators with a resource-oriented design, DecorRes enhances code cla
146 lines (145 loc) • 4.82 kB
TypeScript
import { IClassConstructor } from '../types/index';
/**
* Session manager class.
*
* This class provides a way to manage sessions using a storage object.
*/
declare class SessionManager {
static readonly sessionStorageMetaData: unique symbol;
/**
* The storage object used by the session manager.
*
* This property is initialized lazily when the `storage` getter is called.
*/
private static _storage;
/**
* The prefix to use for all keys in the session storage.
*
* This property is optional and can be set using the `allKeyPrefix` setter.
*/
private static _allKeyPrefix?;
/**
* Gets the storage object used by the session manager.
*
* If the storage object has not been initialized, it will be initialized using the `window.localStorage` object if available.
*
* If the storage object is not available, it will be initialized using an in-memory storage.
*
* @returns {ISessionStorage} The storage object used by the session manager.
*/
static get storage(): ISessionStorage;
/**
* Sets the storage object used by the session manager.
*
* The provided storage object must be valid and have the required methods.
*
* @param {ISessionStorage} storage - The storage object to use.
*/
static set storage(storage: ISessionStorage);
/**
* Gets the prefix to use for all keys in the session storage.
*
* If the prefix is not set, an empty string will be returned.
*
* @returns {string} The prefix to use for all keys in the session storage.
*/
static get allKeyPrefix(): string;
static set allKeyPrefix(prefix: string);
/**
* Sanitizes a key by trimming and removing whitespace, and adding the prefix if set.
*
* @param {string} [key] - The key to sanitize.
* @returns {string} The sanitized key.
*/
static sanitizeKey(key?: string): string;
}
/**
* Sanitizes a string for session storage.
*
* This function trims and removes whitespace from the key, and adds the prefix if set.
* \nExample
* ```typescript
SessionManager.allKeyPrefix = "my-prefix-";
const prefixedKey = sanitizeKey("my-key");
console.log(prefixedKey); // "my-prefix-my-key"
* ````
* @param {string} key - The key to sanitize.
* @returns {string} The sanitized key.
*/
declare function sanitizeKey(key: string): string;
/**
* Interface for a session storage object.
*
* This interface defines the methods for setting, getting, and removing values from a session storage object.
*/
export interface ISessionStorage {
/**
* Sets a value in the session storage object.
*
* @param {string} key - The key to set the value for.
* @param {any} value - The value to set.
* @param {boolean} [decycle] - Optional parameter to decycle the value.
* @returns {any} The set value.
*/
set: (key: string, value: any, decycle?: boolean) => any;
/**
* Gets a value from the session storage object.
*
* @param {string} key - The key to get the value for.
* @returns {any} The value associated with the key.
*/
get: (key: string) => any;
/**
* Removes a value from the session storage object.
*
* @param {string} key - The key to remove the value for.
* @returns {any} The removed value.
*/
remove: (key: string) => any;
/**
* Removes all values from the session storage object.
*
*/
removeAll: () => any;
}
declare const _default: {
get: (key: string) => any;
set: any;
remove: (key: string) => any;
handleGetValue: any;
sanitizeKey: typeof sanitizeKey;
handleSetValue: (value: any, decycle?: boolean) => any;
isValidStorage: (storage?: ISessionStorage) => boolean;
SessionManager: typeof SessionManager;
removeAll: () => any;
};
export default _default;
/**
* Decorator to set the session storage object.
*
* This decorator is used to set the session storage object for the SessionManager.
* It creates a new instance of the target class and assigns it to the SessionManager's storage property.
*
* @example
* ```typescript
* @SessionStorage()
* class MySessionStorage implements ISessionStorage {
set (key: string, value: any, decycle?: boolean) {
console.log("set", key, value);
}
get (key: string){
return "get";
}
remove(key: string) {
console.log("remove", key);
}
removeAll(){
console.log("removeAll");
}
}
* ```
*
* @param target The target class that implements the ISessionStorage interface.
* @throws {Error} If an error occurs while creating a new instance of the target class.
*/
export declare function SessionStorage(): (target: IClassConstructor<ISessionStorage>) => void;