1 |
|
2 | import * as R from "ramda";
|
3 |
|
4 | import {
|
5 | noop,
|
6 | parseJsonIfNeeded,
|
7 | } from "@applicaster/zapp-react-native-utils/functionUtils";
|
8 |
|
9 | export type StorageI = {
|
10 | setItem: (string, string, ?string) => Promise<any>,
|
11 | getItem: (string, ?string) => Promise<any>,
|
12 | getAllItems: () => Promise<any>,
|
13 | };
|
14 |
|
15 | export const STORAGE_TYPES = ["localStorage", "sessionStorage"];
|
16 | export const DEFAULT_NAMESPACE = "applicaster.v2";
|
17 |
|
18 | const NAMESPACE_SEPARATOR = "_::_";
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | export function applyNamespaceToKeyName(
|
27 | key: string,
|
28 | namespace: ?string
|
29 | ): string {
|
30 | return namespace ? `${namespace}${NAMESPACE_SEPARATOR}${key}` : key;
|
31 | }
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | function tryAndResolve(fn: (any) => any): Promise<any> {
|
42 | try {
|
43 | return R.compose(
|
44 | (value) => Promise.resolve(value),
|
45 | parseJsonIfNeeded,
|
46 | fn
|
47 | )();
|
48 | } catch (error) {
|
49 | return Promise.reject(error);
|
50 | }
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | function getStorageObject(type: string): (any) => void | Promise<any> {
|
64 | const storage = R.prop(type, window);
|
65 |
|
66 | if (!R.includes(type, STORAGE_TYPES) || !storage) {
|
67 |
|
68 | console.warn(`Storage type ${type} does not exist in this environment`);
|
69 | return {
|
70 | callMethod: noop,
|
71 | };
|
72 | }
|
73 |
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | function callMethod(method: string, ...args: [any]): Promise<any> {
|
81 | return storage[method](...args);
|
82 | }
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 | function getKeys() {
|
89 |
|
90 |
|
91 | if (process.env.NODE_ENV === "test") {
|
92 | return storage.keys;
|
93 | }
|
94 | return R.keys(storage);
|
95 | }
|
96 |
|
97 | return {
|
98 | callMethod,
|
99 | getKeys,
|
100 | };
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | export function getStorageModule(type: string): StorageI {
|
109 | const Storage = getStorageObject(type);
|
110 |
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | function setItem(
|
120 | key: string,
|
121 | value: string,
|
122 | namespace: ?string = DEFAULT_NAMESPACE
|
123 | ): Promise<boolean | Error> {
|
124 | const keyName = applyNamespaceToKeyName(
|
125 | key,
|
126 | namespace || DEFAULT_NAMESPACE
|
127 | );
|
128 |
|
129 | return tryAndResolve(() => {
|
130 | Storage.callMethod("setItem", keyName, value);
|
131 | return true;
|
132 | });
|
133 | }
|
134 |
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | function getItem(
|
143 | key: string,
|
144 | namespace: ?string = DEFAULT_NAMESPACE
|
145 | ): Promise<any> {
|
146 | const keyName = applyNamespaceToKeyName(
|
147 | key,
|
148 | namespace || DEFAULT_NAMESPACE
|
149 | );
|
150 | return tryAndResolve(() => Storage.callMethod("getItem", keyName));
|
151 | }
|
152 |
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 | function getAllItems(namespace: ?string = null): Promise<string> {
|
159 | return tryAndResolve(() => {
|
160 | const keys = Storage.getKeys();
|
161 |
|
162 | const filteredKeys = R.filter(
|
163 | R.ifElse(
|
164 | () => namespace,
|
165 | (R.contains || R.includes)(`${namespace}${NAMESPACE_SEPARATOR}`),
|
166 | R.T
|
167 | ),
|
168 | keys
|
169 | );
|
170 |
|
171 | const getValuesForKeys = R.map((key) =>
|
172 | Storage.callMethod("getItem", key)
|
173 | );
|
174 |
|
175 | const parseNamespace = (value, namespacedKey) => {
|
176 | const [namespace, key] = R.split(NAMESPACE_SEPARATOR, namespacedKey);
|
177 | return { namespace, key, value };
|
178 | };
|
179 |
|
180 | const reduceStorageProperties = (storageProperties, entry) => {
|
181 | const { key, value } = entry;
|
182 | return R.assoc(key, value, storageProperties);
|
183 | };
|
184 |
|
185 | const groupEntriesByNamespace = R.compose(
|
186 | R.mapObjIndexed(R.reduce(reduceStorageProperties, {})),
|
187 | R.groupBy(R.prop("namespace")),
|
188 | R.values,
|
189 | R.mapObjIndexed(parseNamespace)
|
190 | );
|
191 |
|
192 | return Promise.all(getValuesForKeys(filteredKeys))
|
193 | .then(R.zipObj(filteredKeys))
|
194 | .then(groupEntriesByNamespace)
|
195 | .then(R.assoc("zapp", R.__, {}));
|
196 | });
|
197 | }
|
198 |
|
199 | return {
|
200 | setItem,
|
201 | getItem,
|
202 | getAllItems,
|
203 | };
|
204 | }
|