|
| 1 | +// CREDITS: This types are based on the original work made by all the people who contributed to @types/react-native |
| 2 | + |
| 3 | +declare module '@react-native-community/async-storage' { |
| 4 | + /** |
| 5 | + * AsyncStorage is a simple, unencrypted, asynchronous, persistent, key-value storage |
| 6 | + * system that is global to the app. It should be used instead of LocalStorage. |
| 7 | + * |
| 8 | + * It is recommended that you use an abstraction on top of `AsyncStorage` |
| 9 | + * instead of `AsyncStorage` directly for anything more than light usage since |
| 10 | + * it operates globally. |
| 11 | + * |
| 12 | + * On iOS, `AsyncStorage` is backed by native code that stores small values in a |
| 13 | + * serialized dictionary and larger values in separate files. On Android, |
| 14 | + * `AsyncStorage` will use either [RocksDB](http://rocksdb.org/) or SQLite |
| 15 | + * based on what is available. |
| 16 | + * |
| 17 | + * @see https://github.com/react-native-community/react-native-async-storage/blob/master/docs/API.md |
| 18 | + */ |
| 19 | + export interface AsyncStorageStatic { |
| 20 | + /** |
| 21 | + * Fetches key and passes the result to callback, along with an Error if there is any. |
| 22 | + */ |
| 23 | + getItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null>; |
| 24 | + |
| 25 | + /** |
| 26 | + * Sets value for key and calls callback on completion, along with an Error if there is any |
| 27 | + */ |
| 28 | + setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void>; |
| 29 | + |
| 30 | + removeItem(key: string, callback?: (error?: Error) => void): Promise<void>; |
| 31 | + |
| 32 | + /** |
| 33 | + * Merges existing value with input value, assuming they are stringified json. Returns a Promise object. |
| 34 | + * Not supported by all native implementation |
| 35 | + */ |
| 36 | + mergeItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void>; |
| 37 | + |
| 38 | + /** |
| 39 | + * Erases all AsyncStorage for all clients, libraries, etc. You probably don't want to call this. |
| 40 | + * Use removeItem or multiRemove to clear only your own keys instead. |
| 41 | + */ |
| 42 | + clear(callback?: (error?: Error) => void): Promise<void>; |
| 43 | + |
| 44 | + /** |
| 45 | + * Gets all keys known to the app, for all callers, libraries, etc |
| 46 | + */ |
| 47 | + getAllKeys(callback?: (error?: Error, keys?: string[]) => void): Promise<string[]>; |
| 48 | + |
| 49 | + /** |
| 50 | + * multiGet invokes callback with an array of key-value pair arrays that matches the input format of multiSet |
| 51 | + */ |
| 52 | + multiGet( |
| 53 | + keys: string[], |
| 54 | + callback?: (errors?: Error[], result?: [string, string][]) => void |
| 55 | + ): Promise<[string, string][]>; |
| 56 | + |
| 57 | + /** |
| 58 | + * multiSet and multiMerge take arrays of key-value array pairs that match the output of multiGet, |
| 59 | + * |
| 60 | + * multiSet([['k1', 'val1'], ['k2', 'val2']], cb); |
| 61 | + */ |
| 62 | + multiSet(keyValuePairs: string[][], callback?: (errors?: Error[]) => void): Promise<void>; |
| 63 | + |
| 64 | + /** |
| 65 | + * Delete all the keys in the keys array. |
| 66 | + */ |
| 67 | + multiRemove(keys: string[], callback?: (errors?: Error[]) => void): Promise<void>; |
| 68 | + |
| 69 | + /** |
| 70 | + * Merges existing values with input values, assuming they are stringified json. |
| 71 | + * Returns a Promise object. |
| 72 | + * |
| 73 | + * Not supported by all native implementations. |
| 74 | + */ |
| 75 | + multiMerge(keyValuePairs: string[][], callback?: (errors?: Error[]) => void): Promise<void>; |
| 76 | + } |
| 77 | + |
| 78 | + export function useAsyncStorage(key: string): { |
| 79 | + getItem(callback?: (error?: Error, result?: string) => void): Promise<string | null>; |
| 80 | + setItem(value: string, callback?: (error?: Error) => void): Promise<void>; |
| 81 | + mergeItem(value: string, callback?: (error?: Error) => void): Promise<void>; |
| 82 | + removeItem(callback?: (error?: Error) => void): Promise<void>; |
| 83 | + } |
| 84 | + |
| 85 | + const AsyncStorage: AsyncStorageStatic; |
| 86 | + |
| 87 | + export default AsyncStorage; |
| 88 | +} |
0 commit comments