-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstorage.ts
40 lines (31 loc) · 895 Bytes
/
storage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import AsyncStorage from '@react-native-async-storage/async-storage';
export const CACHE_PREFIX = '@papercups';
export const AUTH_CACHE_KEY = `${CACHE_PREFIX}:auth`;
export const get = async (key: string) => {
const result = await AsyncStorage.getItem(`${CACHE_PREFIX}${key}`);
if (!result) {
return null;
}
try {
return JSON.parse(result);
} catch (e) {
return result;
}
};
export const set = async (key: string, value: any) => {
return AsyncStorage.setItem(`${CACHE_PREFIX}${key}`, JSON.stringify(value));
};
export const remove = async (key: string) => {
return AsyncStorage.removeItem(`${CACHE_PREFIX}${key}`);
};
export const Storage = {
get,
set,
remove,
auth: {
get: async () => get(AUTH_CACHE_KEY),
set: async (value: any) => set(AUTH_CACHE_KEY, value),
remove: async () => remove(AUTH_CACHE_KEY),
},
};
export default Storage;