UNPKG

3.94 kBTypeScriptView Raw
1interface LocalForageDbInstanceOptions {
2 name?: string;
3
4 storeName?: string;
5}
6
7interface LocalForageOptions extends LocalForageDbInstanceOptions {
8 driver?: string | string[];
9
10 size?: number;
11
12 version?: number;
13
14 description?: string;
15}
16
17interface LocalForageDbMethodsCore {
18 getItem<T>(key: string, callback?: (err: any, value: T | null) => void): Promise<T | null>;
19
20 setItem<T>(key: string, value: T, callback?: (err: any, value: T) => void): Promise<T>;
21
22 removeItem(key: string, callback?: (err: any) => void): Promise<void>;
23
24 clear(callback?: (err: any) => void): Promise<void>;
25
26 length(callback?: (err: any, numberOfKeys: number) => void): Promise<number>;
27
28 key(keyIndex: number, callback?: (err: any, key: string) => void): Promise<string>;
29
30 keys(callback?: (err: any, keys: string[]) => void): Promise<string[]>;
31
32 iterate<T, U>(iteratee: (value: T, key: string, iterationNumber: number) => U,
33 callback?: (err: any, result: U) => void): Promise<U>;
34}
35
36interface LocalForageDropInstanceFn {
37 (dbInstanceOptions?: LocalForageDbInstanceOptions, callback?: (err: any) => void): Promise<void>;
38}
39
40interface LocalForageDriverMethodsOptional {
41 dropInstance?: LocalForageDropInstanceFn;
42}
43
44// duplicating LocalForageDriverMethodsOptional to preserve TS v2.0 support,
45// since Partial<> isn't supported there
46interface LocalForageDbMethodsOptional {
47 dropInstance: LocalForageDropInstanceFn;
48}
49
50interface LocalForageDriverDbMethods extends LocalForageDbMethodsCore, LocalForageDriverMethodsOptional {}
51
52interface LocalForageDriverSupportFunc {
53 (): Promise<boolean>;
54}
55
56interface LocalForageDriver extends LocalForageDriverDbMethods {
57 _driver: string;
58
59 _initStorage(options: LocalForageOptions): void;
60
61 _support?: boolean | LocalForageDriverSupportFunc;
62}
63
64interface LocalForageSerializer {
65 serialize<T>(value: T | ArrayBuffer | Blob, callback: (value: string, error: any) => void): void;
66
67 deserialize<T>(value: string): T | ArrayBuffer | Blob;
68
69 stringToBuffer(serializedString: string): ArrayBuffer;
70
71 bufferToString(buffer: ArrayBuffer): string;
72}
73
74interface LocalForageDbMethods extends LocalForageDbMethodsCore, LocalForageDbMethodsOptional {}
75
76interface LocalForage extends LocalForageDbMethods {
77 LOCALSTORAGE: string;
78 WEBSQL: string;
79 INDEXEDDB: string;
80
81 /**
82 * Set and persist localForage options. This must be called before any other calls to localForage are made, but can be called after localForage is loaded.
83 * If you set any config values with this method they will persist after driver changes, so you can call config() then setDriver()
84 * @param {LocalForageOptions} options?
85 */
86 config(options: LocalForageOptions): boolean;
87 config(options: string): any;
88 config(): LocalForageOptions;
89
90 /**
91 * Create a new instance of localForage to point to a different store.
92 * All the configuration options used by config are supported.
93 * @param {LocalForageOptions} options
94 */
95 createInstance(options: LocalForageOptions): LocalForage;
96
97 driver(): string;
98
99 /**
100 * Force usage of a particular driver or drivers, if available.
101 * @param {string} driver
102 */
103 setDriver(driver: string | string[], callback?: () => void, errorCallback?: (error: any) => void): Promise<void>;
104
105 defineDriver(driver: LocalForageDriver, callback?: () => void, errorCallback?: (error: any) => void): Promise<void>;
106
107 /**
108 * Return a particular driver
109 * @param {string} driver
110 */
111 getDriver(driver: string): Promise<LocalForageDriver>;
112
113 getSerializer(callback?: (serializer: LocalForageSerializer) => void): Promise<LocalForageSerializer>;
114
115 supports(driverName: string): boolean;
116
117 ready(callback?: (error: any) => void): Promise<void>;
118}
119
120declare module "localforage" {
121 let localforage: LocalForage;
122 export = localforage;
123}
124
\No newline at end of file