English | Changelog | Feedback Errors/Missing Points | Gitee
- Typescript writing
- Multi-terminal (web, mini program, nodejs) support, and keep the API consistent
- Support custom plug-ins to customize the access process
- Support final mode, stored data cannot be modified again
- Support protect mode, protect data will not be deleted by remove and clear in normal mode
- Support times mode to limit the number of data access operations
- Support expires mode to set the data expiration time
- Support time monitoring for access deletion operations
- Support settings to use temp mode, only stored in memory, not written to disk
npm i storage-enhance
import storage from 'storage-enhance';
storage.set('key', 'value');
<script src="https://cdn.jsdelivr.net/npm/storage-enhance"></script>
<script>
StorageEnhance.set('key', 'value');
</script>
For details, please refer to index.d.ts
export interface IStorage extends IBaseStorageFuncs {
env: TStorageEnv;
TYPE: IJson<TStorageType>;
count(options?: IStorageTypeArg): number;
keys(options?: IStorageTypeArg): string[];
clear(options?: IStorageClearArg): boolean;
exist(key: string, options: IStorageKeyArg): boolean;
remove(key: string, options: IStorageRemoveArg): boolean;
set(key: string, value: any, options?: IStorageSetOption): boolean;
set(options: IStorageSetOption): boolean;
set(array: (IStorageSetOption)[]): boolean;
get(key: string): any;
get(options: IStorageGetOption): any;
get(array: IStorageGetOption[]): any[];
all(options?: IStorageTypeArg & IStorageDetailArg): IKeyPathValuePair[];
use(...plugins: IStoragePlugin[]): void;
plugins(): IStoragePlugin[];
registScope(arg1: string | IJson<IEvent | any>, arg2?: IEvent | any): void;
scope(): void;
type: TStorageType;
EMPTY: Symbol;
}
storage.set('key', 'value', options);
storage.set(options);
storage.set([options, options]);
Options is used to pass in some optional configurations
The set method has the following options
interface IStorageSetOption {
key?: string;
value?: any;
onGet?: string | IEvent;
onSet?: string | IEvent;
onRemove?: string | IEvent;
expires?: number; Expiration datetime
once?: boolean; Whether it is a one-time or not
times?: number; Number of readable times
path?: string;
final?: boolean; Whether it is immutable
protect?: boolean; Whether it can be deleted
type?: 'local' | 'session' | 'temp' | 'cookie'; What type the current operation uses
Cookie?: { // is only valid when type=cookies are enabled in the web environment
secure?: boolean; default: false
sameSite?: ICookieSameSite; default: Lax
priority?: ICookiePriority; default: Medium
sameParty?: boolean; default: false
expires?: Date | number; default is session
path?: string; default
};
}
storage.get('key', options);
storage.get(options);
storage.get([options, options]);
Options is used to pass in some optional configurations
The get method has the following options
interface IStorageGetOption{
key?: string;
type?: 'local' | 'session' | 'temp' | 'cookie'; What type the current operation uses
detail?: boolean; Whether data details need to be presented
}
storage.remove('key', options);
storage.remove(options);
Options is used to pass in some optional configurations
The remove method has the following options
interface IStorageRemoveOption{
key?: string;
type?: 'local' | 'session' | 'temp' | 'cookie'; What type the current operation uses
protect?: boolean; Whether to delete the protect type
cookie?: {
path?: string;
domain?: string;
}
}
storage.clear();
storage.clear(options);
Options is used to pass in some optional configurations
The clear method has the following options
interface IStorageClearOption{
protect?: boolean; Whether to delete the protect type
type?: 'local' | 'session' | 'temp' | 'cookie'; What type the current operation uses
cookie?: {
path?: string;
domain?: string;
}
}
storage.keys();
storage.keys(options);
Options is used to pass in some optional configurations
The keys method has the following options
interface IStorageKeysOption{
type?: 'local' | 'session' | 'temp' | 'cookie'; What type the current operation uses
}
storage.count();
storage.count(options);
Options is used to pass in some optional configurations
The count method has the following options
interface IStorageCountOption{
type?: 'local' | 'session' | 'temp' | 'cookie'; What type the current operation uses
}
storage.exist(key, options);
storage.exist(options);
Options is used to pass in some optional configurations
The exist method has the following options
interface IStorageExistOption {
key?: string;
type?: 'local' | 'session' | 'temp' | 'cookie'; What type the current operation uses
}
storage.all();
storage.all(options);
Options is used to pass in some optional configurations
The all method has the following options
interface IStorageExistOption {
type?: 'local' | 'session' | 'temp' | 'cookie'; What type the current operation uses
}
storage-enhance can be plug-in to customize third-party users to modify the data access process
The following is the interface of a plug-in
For details, please refer to plugin.d.ts
interface IStoragePlugin {
name: TPluginName;
beforeGet? (options: IPluginBeforeGetOptions): IStorageGetOption;
get? (options: IPluginGetOptions): IStorageData | symbol;
set? (options: IPluginSetOptions): IStorageData | boolean;
remove? (options: IPluginRemoveOptions): boolean;
}
The following is an example of a plugin that adds a 'test' prefix to each key
export const TestPlugin: IStoragePlugin = {
name: 'add-test',
beforeGet ({options}) {
options.key = `test_${options.key}`;
return options;
},
set ({options, data, prevData}) {
options.key = `test_${options.key}`;
return data;
},
};
For other examples, see plugins
import storage from 'storage-enhance';
storage.use(TestPlugin);
storage.plugins(); Gets the installed plug-ins
storage.type = 'cookie'; Use cookies instead of localStorage
storage.type = 'session'; Use session instead of localStorage
Optional value: 'local' | 'session' | 'temp' | 'cookie';
storage.TYPE.LOCAL; //
storage.EMPTY; Represents a null value, of type symbol
scope is used to register globally unique methods or properties, and is mainly used in plug-in mechanisms to avoid duplicate registration methods
storage.registScope('testAttr', () => 1);
storage.registScope('testAttr', () => 2);
storage.scope().testAttr(); // 2