Instantly share code, notes, and snippets.
Created
May 4, 2017 17:32
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save iegik/21633e999aa653b0f046cc31138cdc5b to your computer and use it in GitHub Desktop.
merged react-native-cookies and react-native-cookiemanager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { Platform } from 'react-native'; | |
import { HOST } from '../../api/constants'; | |
import { serialize, parse } from 'cookie'; | |
import { reduce, map, forEach, filter } from 'lodash'; | |
export function callback(err, res) { | |
return res; | |
} | |
/** | |
* @typedef cookie | |
* @type {object} | |
* @property {string} domain | |
* @property {string} path | |
* @property {string} name | |
* @property {string} value | |
* @property {string} origin 'same origin' | |
* @property {number} version 1 | |
* @property {date} expiration ISODateString | |
*/ | |
function getModule() { | |
if (Platform.OS === 'ios') { | |
let CM = require('react-native-cookies'); | |
/** | |
* class CookieManager { | |
* set({object}, {function({error} err, {object} res)}) | |
* setFromResponse({string} HOST, {string}, {function({object} res)}) + | |
* | |
* get({string} HOST, {function({error} err, {string} res)}) + | |
* getAll({object}, {function({error} err, {object} res)}) iOS only | |
* | |
* clearAll({function({error} err, {object} res)}) + | |
* clearByName({string}, {function({error} err, {object} res)}) iOS only | |
* } | |
*/ | |
return { | |
/** | |
* Clears all cookies | |
* @param cb Callback | |
* @returns {*} | |
*/ | |
clearCookies(cb = callback){ | |
return CM.clearAll(cb); | |
}, | |
/** | |
* Get {cookie} and passes to Callback | |
* @param cb Callback | |
* @returns {*} | |
*/ | |
getCookie(cb = callback){ | |
return CM.get(HOST, (err, res) => { | |
return cb(err, res) | |
}) | |
}, | |
/** | |
* Set {cookie} | |
* @param {cookie} options | |
* @param cb Callback | |
* @returns {*} | |
*/ | |
setCookie(options, cb = callback){ | |
options = { | |
"Set-Cookie": map(options, (value, key) => serialize(key, value)).join(';') | |
}; | |
return CM.setFromResponse(HOST, options, (err, res) => { | |
return cb(err, res) | |
}) | |
}, | |
}; | |
} else { | |
let CM = require('react-native-cookiemanager').default; | |
/** | |
* class CookieManager { | |
* setCookie({object} options) + | |
* | |
* getCookie({string} HOST, {function({object} res)}) + | |
* | |
* removeAllCookies() + | |
* } | |
*/ | |
return { | |
/** | |
* Clears all cookies | |
* @param cb Callback | |
* @returns {*} | |
*/ | |
clearCookies(cb = callback){ | |
CM.removeAllCookies(); | |
return cb(); | |
}, | |
/** | |
* Get {cookie} and passes to Callback | |
* @param cb Callback | |
* @returns {*} | |
*/ | |
getCookie(cb = callback){ | |
return CM.getCookie(HOST, (res) => { | |
let parsed = parse(res || ''); | |
return cb(null, parsed); | |
}); | |
}, | |
/** | |
* Set {cookie} | |
* @param {cookie} options | |
* @param cb Callback | |
* @returns {*} | |
*/ | |
setCookie(options, cb = callback) { | |
let {expiration = '', domain = HOST, path = '/', origin = 'same origin'} = options; | |
forEach(options, (value, name) => { | |
if(['expiration', 'domain', 'path'].indexOf(name) > -1){ | |
return; | |
} | |
return CM.setCookie({ | |
name, value: encodeURIComponent(value), | |
expiration, domain, path, origin | |
}) | |
}); | |
return cb(); | |
}, | |
} | |
} | |
} | |
const CookieManager = getModule(); | |
export default CookieManager; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment