-
Notifications
You must be signed in to change notification settings - Fork 95
/
index.js
58 lines (52 loc) · 1.87 KB
/
index.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
50
51
52
53
54
55
56
57
58
/**
* Copyright (c) Joseph P. Ferraro
*
* This source code is licensed under the MIT license found in the
* LICENSE file here: https://github.com/joeferraro/react-native-cookies/blob/master/LICENSE.md.
*/
import { NativeModules, Platform } from 'react-native';
const invariant = require('invariant');
const RNCookieManagerIOS = NativeModules.RNCookieManagerIOS;
const RNCookieManagerAndroid = NativeModules.RNCookieManagerAndroid;
let CookieManager;
if (Platform.OS === 'ios') {
invariant(
RNCookieManagerIOS,
'@react-native-community/cookies: Add RNCookieManagerIOS.h and RNCookieManagerIOS.m to your Xcode project',
);
CookieManager = RNCookieManagerIOS;
} else if (Platform.OS === 'android') {
invariant(
RNCookieManagerAndroid,
'@react-native-community/cookies: Import libraries to android "react-native link @react-native-community/cookies"',
);
CookieManager = RNCookieManagerAndroid;
} else {
invariant(
CookieManager,
'@react-native-community/cookies: Invalid platform. This library only supports Android and iOS.',
);
}
const functions = ['setFromResponse', 'getFromResponse'];
module.exports = {
getAll: (useWebKit = false) => CookieManager.getAll(useWebKit),
clearAll: (useWebKit = false) => CookieManager.clearAll(useWebKit),
get: (url, useWebKit = false) => CookieManager.get(url, useWebKit),
set: (url, cookie, useWebKit = false) =>
CookieManager.set(url, cookie, useWebKit),
clearByName: (url, name, useWebKit = false) =>
CookieManager.clearByName(url, name, useWebKit),
flush: async () => {
if (Platform.OS === 'android') {
await CookieManager.flush();
}
},
removeSessionCookies: async () => {
if (Platform.OS === 'android') {
return await CookieManager.removeSessionCookies();
}
},
};
for (var i = 0; i < functions.length; i++) {
module.exports[functions[i]] = CookieManager[functions[i]];
}