|
| 1 | +// https://github.com/localForage/localForage/blob/master/typings/localforage.d.ts |
| 2 | + |
| 3 | +interface LocalForageDbInstanceOptions { |
| 4 | + name?: string; |
| 5 | + |
| 6 | + storeName?: string; |
| 7 | +} |
| 8 | + |
| 9 | +interface LocalForageOptions extends LocalForageDbInstanceOptions { |
| 10 | + driver?: string | string[]; |
| 11 | + |
| 12 | + size?: number; |
| 13 | + |
| 14 | + version?: number; |
| 15 | + |
| 16 | + description?: string; |
| 17 | +} |
| 18 | + |
| 19 | +interface LocalForageDbMethodsCore { |
| 20 | + getItem<T>( |
| 21 | + key: string, |
| 22 | + callback?: (err: any, value: T | null) => void |
| 23 | + ): Promise<T | null>; |
| 24 | + |
| 25 | + setItem<T>( |
| 26 | + key: string, |
| 27 | + value: T, |
| 28 | + callback?: (err: any, value: T) => void |
| 29 | + ): Promise<T>; |
| 30 | + |
| 31 | + removeItem(key: string, callback?: (err: any) => void): Promise<void>; |
| 32 | + |
| 33 | + clear(callback?: (err: any) => void): Promise<void>; |
| 34 | + |
| 35 | + length(callback?: (err: any, numberOfKeys: number) => void): Promise<number>; |
| 36 | + |
| 37 | + key( |
| 38 | + keyIndex: number, |
| 39 | + callback?: (err: any, key: string) => void |
| 40 | + ): Promise<string>; |
| 41 | + |
| 42 | + keys(callback?: (err: any, keys: string[]) => void): Promise<string[]>; |
| 43 | + |
| 44 | + iterate<T, U>( |
| 45 | + iteratee: (value: T, key: string, iterationNumber: number) => U, |
| 46 | + callback?: (err: any, result: U) => void |
| 47 | + ): Promise<U>; |
| 48 | +} |
| 49 | + |
| 50 | +interface LocalForageDropInstanceFn { |
| 51 | + ( |
| 52 | + dbInstanceOptions?: LocalForageDbInstanceOptions, |
| 53 | + callback?: (err: any) => void |
| 54 | + ): Promise<void>; |
| 55 | +} |
| 56 | + |
| 57 | +interface LocalForageDriverMethodsOptional { |
| 58 | + dropInstance?: LocalForageDropInstanceFn; |
| 59 | +} |
| 60 | + |
| 61 | +// duplicating LocalForageDriverMethodsOptional to preserve TS v2.0 support, |
| 62 | +// since Partial<> isn't supported there |
| 63 | +interface LocalForageDbMethodsOptional { |
| 64 | + dropInstance: LocalForageDropInstanceFn; |
| 65 | +} |
| 66 | + |
| 67 | +interface LocalForageDriverDbMethods |
| 68 | + extends LocalForageDbMethodsCore, |
| 69 | + LocalForageDriverMethodsOptional {} |
| 70 | + |
| 71 | +interface LocalForageDriverSupportFunc { |
| 72 | + (): Promise<boolean>; |
| 73 | +} |
| 74 | + |
| 75 | +interface LocalForageDriver extends LocalForageDriverDbMethods { |
| 76 | + _driver: string; |
| 77 | + |
| 78 | + _initStorage(options: LocalForageOptions): void; |
| 79 | + |
| 80 | + _support?: boolean | LocalForageDriverSupportFunc; |
| 81 | +} |
| 82 | + |
| 83 | +interface LocalForageSerializer { |
| 84 | + serialize<T>( |
| 85 | + value: T | ArrayBuffer | Blob, |
| 86 | + callback: (value: string, error: any) => void |
| 87 | + ): void; |
| 88 | + |
| 89 | + deserialize<T>(value: string): T | ArrayBuffer | Blob; |
| 90 | + |
| 91 | + stringToBuffer(serializedString: string): ArrayBuffer; |
| 92 | + |
| 93 | + bufferToString(buffer: ArrayBuffer): string; |
| 94 | +} |
| 95 | + |
| 96 | +interface LocalForageDbMethods |
| 97 | + extends LocalForageDbMethodsCore, |
| 98 | + LocalForageDbMethodsOptional {} |
| 99 | + |
| 100 | +export interface LocalForage extends LocalForageDbMethods { |
| 101 | + LOCALSTORAGE: string; |
| 102 | + WEBSQL: string; |
| 103 | + INDEXEDDB: string; |
| 104 | + |
| 105 | + /** |
| 106 | + * 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. |
| 107 | + * If you set any config values with this method they will persist after driver changes, so you can call config() then setDriver() |
| 108 | + * @param {LocalForageOptions} options? |
| 109 | + */ |
| 110 | + config(options: LocalForageOptions): boolean; |
| 111 | + config(options: string): any; |
| 112 | + config(): LocalForageOptions; |
| 113 | + |
| 114 | + /** |
| 115 | + * Create a new instance of localForage to point to a different store. |
| 116 | + * All the configuration options used by config are supported. |
| 117 | + * @param {LocalForageOptions} options |
| 118 | + */ |
| 119 | + createInstance(options: LocalForageOptions): LocalForage; |
| 120 | + |
| 121 | + driver(): string; |
| 122 | + |
| 123 | + /** |
| 124 | + * Force usage of a particular driver or drivers, if available. |
| 125 | + * @param {string} driver |
| 126 | + */ |
| 127 | + setDriver( |
| 128 | + driver: string | string[], |
| 129 | + callback?: () => void, |
| 130 | + errorCallback?: (error: any) => void |
| 131 | + ): Promise<void>; |
| 132 | + |
| 133 | + defineDriver( |
| 134 | + driver: LocalForageDriver, |
| 135 | + callback?: () => void, |
| 136 | + errorCallback?: (error: any) => void |
| 137 | + ): Promise<void>; |
| 138 | + |
| 139 | + /** |
| 140 | + * Return a particular driver |
| 141 | + * @param {string} driver |
| 142 | + */ |
| 143 | + getDriver(driver: string): Promise<LocalForageDriver>; |
| 144 | + |
| 145 | + getSerializer( |
| 146 | + callback?: (serializer: LocalForageSerializer) => void |
| 147 | + ): Promise<LocalForageSerializer>; |
| 148 | + |
| 149 | + supports(driverName: string): boolean; |
| 150 | + |
| 151 | + ready(callback?: (error: any) => void): Promise<void>; |
| 152 | +} |
| 153 | + |
| 154 | +// Customize |
| 155 | + |
| 156 | +export interface ProxyStorage { |
| 157 | + setItem<T>(k: string, v: T, m: number): Promise<T>; |
| 158 | + getItem<T>(k: string): Promise<T>; |
| 159 | + removeItem(k: string): Promise<void>; |
| 160 | + clear(): Promise<void>; |
| 161 | +} |
| 162 | + |
| 163 | +export interface ExpiresData<T> { |
| 164 | + data: T; |
| 165 | + expires: number; |
| 166 | +} |
0 commit comments