Skip to content

TypeScript support #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@react-native-community/async-storage",
"version": "1.0.2",
"description": "Asynchronous, persistent, key-value storage system for React Native.",
"types": "./types/index.d.ts",
"main": "lib/index.js",
"author": "Krzysztof Borowy <[email protected]>",
"contributors": [],
Expand Down
88 changes: 88 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// CREDITS: This types are based on the original work made by all the people who contributed to @types/react-native

declare module '@react-native-community/async-storage' {
/**
* AsyncStorage is a simple, unencrypted, asynchronous, persistent, key-value storage
* system that is global to the app. It should be used instead of LocalStorage.
*
* It is recommended that you use an abstraction on top of `AsyncStorage`
* instead of `AsyncStorage` directly for anything more than light usage since
* it operates globally.
*
* On iOS, `AsyncStorage` is backed by native code that stores small values in a
* serialized dictionary and larger values in separate files. On Android,
* `AsyncStorage` will use either [RocksDB](http://rocksdb.org/) or SQLite
* based on what is available.
*
* @see https://github.com/react-native-community/react-native-async-storage/blob/master/docs/API.md
*/
export interface AsyncStorageStatic {
/**
* Fetches key and passes the result to callback, along with an Error if there is any.
*/
getItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null>;

/**
* Sets value for key and calls callback on completion, along with an Error if there is any
*/
setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void>;

removeItem(key: string, callback?: (error?: Error) => void): Promise<void>;

/**
* Merges existing value with input value, assuming they are stringified json. Returns a Promise object.
* Not supported by all native implementation
*/
mergeItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void>;

/**
* Erases all AsyncStorage for all clients, libraries, etc. You probably don't want to call this.
* Use removeItem or multiRemove to clear only your own keys instead.
*/
clear(callback?: (error?: Error) => void): Promise<void>;

/**
* Gets all keys known to the app, for all callers, libraries, etc
*/
getAllKeys(callback?: (error?: Error, keys?: string[]) => void): Promise<string[]>;

/**
* multiGet invokes callback with an array of key-value pair arrays that matches the input format of multiSet
*/
multiGet(
keys: string[],
callback?: (errors?: Error[], result?: [string, string][]) => void
): Promise<[string, string][]>;

/**
* multiSet and multiMerge take arrays of key-value array pairs that match the output of multiGet,
*
* multiSet([['k1', 'val1'], ['k2', 'val2']], cb);
*/
multiSet(keyValuePairs: string[][], callback?: (errors?: Error[]) => void): Promise<void>;

/**
* Delete all the keys in the keys array.
*/
multiRemove(keys: string[], callback?: (errors?: Error[]) => void): Promise<void>;

/**
* Merges existing values with input values, assuming they are stringified json.
* Returns a Promise object.
*
* Not supported by all native implementations.
*/
multiMerge(keyValuePairs: string[][], callback?: (errors?: Error[]) => void): Promise<void>;
}

export function useAsyncStorage(key: string): {
getItem(callback?: (error?: Error, result?: string) => void): Promise<string | null>;
setItem(value: string, callback?: (error?: Error) => void): Promise<void>;
mergeItem(value: string, callback?: (error?: Error) => void): Promise<void>;
removeItem(callback?: (error?: Error) => void): Promise<void>;
}

const AsyncStorage: AsyncStorageStatic;

export default AsyncStorage;
}