forked from shimohq/react-native-cookie
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cookie.android.js
49 lines (41 loc) · 1.48 KB
/
cookie.android.js
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
41
42
43
44
45
46
47
48
49
import { NativeModules } from 'react-native';
import cookie from 'cookie';
const CLEAR_RETRY_COUNT = 10;
export default {
get(url:String, name: String): Promise<Object|String> {
return NativeModules.RNCookieManager.getCookie(url).then((value: String): Object => {
if (name && value) {
return cookie.parse(value)[name] || null;
} else {
return value ? cookie.parse(value) : null;
}
});
},
set(url:String, name: String, value: any, options?: Object): Promise {
return NativeModules.RNCookieManager.setCookie(url, cookie.serialize(name, value, options));
},
async clear(url?: String): Promise {
await NativeModules.RNCookieManager.clearCookie(url || null);
if (!url) {
return null;
}
// A work around to ensure cookies has been cleared on Android
const data = await this.get(url);
return data ? new Promise((resolve, reject) => {
let retryCount = 0;
const self = this;
async function retry() {
retryCount++;
await NativeModules.RNCookieManager.clearCookie(url);
if (!await self.get(url)) {
resolve(null);
} else if (retryCount <= CLEAR_RETRY_COUNT) {
retry();
} else {
reject(null);
}
}
retry();
}) : null;
}
};