Skip to content

Commit 67ae214

Browse files
itsjgfkrizzu
authored andcommitted
chore: Adds typescript support
Closes #27.
1 parent 58d7727 commit 67ae214

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "@react-native-community/async-storage",
33
"version": "1.1.0",
44
"description": "Asynchronous, persistent, key-value storage system for React Native.",
5+
"types": "./types/index.d.ts",
56
"main": "lib/index.js",
67
"author": "Krzysztof Borowy <krizzu.dev@gmail.com>",
78
"contributors": [],

types/index.d.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)