react-cookies
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -1,7 +0,7 @@ | ||
import cookie from '../src/cookie'; | ||
import cookie from '../src/cookie' | ||
describe('react-cookie', () => { | ||
beforeEach(() => { | ||
cookie.setRawCookie(''); | ||
}); | ||
cookie.setRawCookie('') | ||
}) | ||
@@ -11,146 +11,145 @@ describe('load', () => { | ||
expect(() => { | ||
cookie.setRawCookie(undefined); | ||
}).not.toThrowError(); | ||
}); | ||
cookie.setRawCookie(undefined) | ||
}).not.toThrowError() | ||
}) | ||
it('should read the cookie', () => { | ||
cookie.setRawCookie('test=test'); | ||
expect(cookie.load('test')).toBe('test'); | ||
}); | ||
it('should load all cookiee', () => { | ||
cookie.setRawCookie('test=test') | ||
cookie.setRawCookie('test2=test2') | ||
expect(typeof cookie.loadAll()).toBe('object') | ||
}) | ||
it('should parse if an object', () => { | ||
cookie.setRawCookie('test={"test": true}'); | ||
expect(cookie.load('test').test).toBe(true); | ||
}); | ||
cookie.setRawCookie('test={"test": true}') | ||
expect(cookie.load('test').test).toBe(true) | ||
}) | ||
it('should not parse if not an object', function() { | ||
cookie.setRawCookie('test=1230.00'); | ||
expect(cookie.load('test')).toBe('1230.00'); | ||
}); | ||
it('should not parse if not an object', () => { | ||
cookie.setRawCookie('test=1230.00') | ||
expect(cookie.load('test')).toBe('1230.00') | ||
}) | ||
it('should not parse if we ask not to', function() { | ||
cookie.setRawCookie('test={"test": true}'); | ||
expect(typeof cookie.load('test', true)).toBe('string'); | ||
}); | ||
}); | ||
it('should not parse if we ask not to', () => { | ||
cookie.setRawCookie('test={"test": true}') | ||
expect(typeof cookie.load('test', true)).toBe('string') | ||
}) | ||
}) | ||
describe('select', function() { | ||
it('should not crash if cookies undefined', function() { | ||
expect(function() { | ||
cookie.setRawCookie(undefined); | ||
}).not.toThrow(); | ||
describe('select', () => { | ||
it('should not crash if cookies undefined', () => { | ||
expect(() => { | ||
cookie.setRawCookie(undefined) | ||
}).not.toThrow() | ||
expect(cookie.select(/^test/g)).toEqual({}); | ||
}); | ||
expect(cookie.select(/^test/g)).toEqual({}) | ||
}) | ||
it('should select and read all the matching cookies into an object', function() { | ||
cookie.setRawCookie('test=foo;something=bar;foo=bar'); | ||
expect(cookie.select(/(test|foo)/)).toEqual({ test: 'foo', foo: 'bar' }); | ||
}); | ||
it('should select and read all the matching cookies into an object', () => { | ||
cookie.setRawCookie('test=foo;something=bar;foo=bar') | ||
expect(cookie.select(/(test|foo)/)).toEqual({test: 'foo', foo: 'bar'}) | ||
}) | ||
it('should read all cookies into an object if no parameter is passed', function() { | ||
cookie.setRawCookie('test=foo;something=bar;foo=bar'); | ||
expect(cookie.select()).toEqual({ test: 'foo', something: 'bar', foo: 'bar' }); | ||
}); | ||
}); | ||
it('should read all cookies into an object if no parameter is passed', () => { | ||
cookie.setRawCookie('test=foo;something=bar;foo=bar') | ||
expect(cookie.select()).toEqual({test: 'foo', something: 'bar', foo: 'bar'}) | ||
}) | ||
}) | ||
describe('save', function() { | ||
it('should not crash if not in the browser', function() { | ||
expect(function() { | ||
cookie.save('test', 'test'); | ||
}).not.toThrow(); | ||
}); | ||
describe('save', () => { | ||
it('should not crash if not in the browser', () => { | ||
expect(() => { | ||
cookie.save('test', 'test') | ||
}).not.toThrow() | ||
}) | ||
it('should update the value', function() { | ||
cookie.setRawCookie('test=test'); | ||
expect(cookie.load('test')).toBe('test'); | ||
it('should update the value', () => { | ||
cookie.setRawCookie('test=test') | ||
expect(cookie.load('test')).toBe('test') | ||
cookie.save('test', 'other'); | ||
expect(cookie.load('test')).not.toBe('test'); | ||
}); | ||
cookie.save('test', 'other') | ||
expect(cookie.load('test')).not.toBe('test') | ||
}) | ||
it('should stringify an object', function() { | ||
cookie.setRawCookie('test=test'); | ||
expect(cookie.load('test')).toBe('test'); | ||
it('should stringify an object', () => { | ||
cookie.setRawCookie('test=test') | ||
expect(cookie.load('test')).toBe('test') | ||
cookie.save('test', { test: true }); | ||
expect(typeof cookie.load('test')).toBe('object'); | ||
}); | ||
}); | ||
cookie.save('test', {test: true}) | ||
expect(typeof cookie.load('test')).toBe('object') | ||
}) | ||
}) | ||
describe('remove', function() { | ||
it('should do nothing if not in the browser', function() { | ||
expect(function() { | ||
cookie.remove('test'); | ||
}).not.toThrow(); | ||
}); | ||
}); | ||
describe('remove', () => { | ||
it('should do nothing if not in the browser', () => { | ||
expect(() => { | ||
cookie.remove('test') | ||
}).not.toThrow() | ||
}) | ||
}) | ||
describe('cookie', () => { | ||
describe('plugToRequest', () => { | ||
it('should load the request cookie', () => { | ||
cookie.plugToRequest({cookie: {test: 123}}) | ||
expect(cookie.load('test')).toBe(123) | ||
}) | ||
describe('cookie', function() { | ||
describe('plugToRequest', function() { | ||
it('should load the request cookie', function() { | ||
cookie.plugToRequest({ cookie: { test: 123 } }); | ||
expect(cookie.load('test')).toBe(123); | ||
}); | ||
it('should load the request cookies', () => { | ||
cookie.plugToRequest({cookies: {test: 123}}) | ||
expect(cookie.load('test')).toBe(123) | ||
}) | ||
it('should load the request cookies', function() { | ||
cookie.plugToRequest({ cookies: { test: 123 } }); | ||
expect(cookie.load('test')).toBe(123); | ||
}); | ||
it('should load the raw cookie header', () => { | ||
cookie.plugToRequest({headers: {cookie: 'test=123'}}) | ||
expect(cookie.load('test')).toBe('123') | ||
}) | ||
it('should load the raw cookie header', function() { | ||
cookie.plugToRequest({ headers: { cookie: 'test=123' } }); | ||
expect(cookie.load('test')).toBe('123'); | ||
}); | ||
it('should clear the cookies if their is none', () => { | ||
cookie.setRawCookie('test=123') | ||
expect(cookie.load('test')).toBe('123') | ||
it('should clear the cookies if their is none', function() { | ||
cookie.setRawCookie('test=123'); | ||
expect(cookie.load('test')).toBe('123'); | ||
cookie.plugToRequest({}) | ||
expect(cookie.load('test')).toBeUndefined() | ||
}) | ||
}) | ||
cookie.plugToRequest({}); | ||
expect(cookie.load('test')).toBeUndefined(); | ||
}); | ||
}); | ||
describe('unplug', () => { | ||
it('should return an unplug function', () => { | ||
const unplug = cookie.plugToRequest({headers: {cookie: 'test=123'}}) | ||
expect(typeof unplug).toBe('function') | ||
}) | ||
it('should save cookie on the request before unplugged', () => { | ||
const cookieFunc = jest.fn() | ||
const req = {headers: {cookie: 'test=123'}} | ||
const res = {headersSent: false, cookie: cookieFunc} | ||
describe('unplug', function () { | ||
it('should return an unplug function', function() { | ||
const unplug = cookie.plugToRequest({ headers: { cookie: 'test=123' } }); | ||
expect(typeof unplug).toBe('function'); | ||
}); | ||
cookie.plugToRequest(req, res) | ||
cookie.save('test2', 'test2') | ||
expect(cookieFunc).toHaveBeenCalled() | ||
}) | ||
it('should save cookie on the request before unplugged', function() { | ||
const cookieFunc = jest.fn(); | ||
const req = { headers: { cookie: 'test=123' } }; | ||
const res = { headersSent: false, cookie: cookieFunc }; | ||
it('should not change after head is sent', () => { | ||
const cookieFunc = jest.fn() | ||
const req = {headers: {cookie: 'test=123'}} | ||
const res = {headersSent: true, cookie: cookieFunc} | ||
cookie.plugToRequest(req, res); | ||
cookie.save('test2', 'test2'); | ||
expect(cookieFunc).toHaveBeenCalled(); | ||
}); | ||
cookie.plugToRequest(req, res) | ||
cookie.save('test2', 'test2') | ||
expect(cookieFunc).not.toHaveBeenCalled() | ||
}) | ||
it('should not change after head is sent', function() { | ||
const cookieFunc = jest.fn(); | ||
const req = { headers: { cookie: 'test=123' } }; | ||
const res = { headersSent: true, cookie: cookieFunc }; | ||
cookie.plugToRequest(req, res); | ||
cookie.save('test2', 'test2'); | ||
expect(cookieFunc).not.toHaveBeenCalled(); | ||
}); | ||
it('should not change after unplugged', () => { | ||
const cookieFunc = jest.fn(); | ||
const req = { headers: { cookie: 'test=123' } }; | ||
const res = { headersSent: false, cookie: cookieFunc }; | ||
const unplug = cookie.plugToRequest(req, res); | ||
const cookieFunc = jest.fn() | ||
const req = {headers: {cookie: 'test=123'}} | ||
const res = {headersSent: false, cookie: cookieFunc} | ||
const unplug = cookie.plugToRequest(req, res) | ||
unplug(); | ||
cookie.save('test2', 'test2'); | ||
unplug() | ||
cookie.save('test2', 'test2') | ||
expect(cookieFunc).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
expect(cookieFunc).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -10,2 +10,3 @@ 'use strict'; | ||
exports.load = load; | ||
exports.loadAll = loadAll; | ||
exports.select = select; | ||
@@ -29,3 +30,3 @@ exports.save = save; | ||
var _rawCookie = {}; | ||
var _res = undefined; | ||
var _res = void 0; | ||
@@ -47,3 +48,3 @@ function _isResWritable() { | ||
cookieVal = JSON.parse(cookieVal); | ||
} catch (e) { | ||
} catch (err) { | ||
// Not serialized object | ||
@@ -56,2 +57,21 @@ } | ||
function loadAll(doNotParse) { | ||
var cookies = IS_NODE ? _rawCookie : _cookie2.default.parse(document.cookie); | ||
var cookieVal = cookies; | ||
if (typeof doNotParse === 'undefined') { | ||
doNotParse = !cookieVal || cookieVal[0] !== '{' && cookieVal[0] !== '['; | ||
} | ||
if (!doNotParse) { | ||
try { | ||
cookieVal = JSON.parse(cookieVal); | ||
} catch (err) { | ||
// Not serialized object | ||
} | ||
} | ||
return cookieVal; | ||
} | ||
function select(regex) { | ||
@@ -82,3 +102,3 @@ var cookies = IS_NODE ? _rawCookie : _cookie2.default.parse(document.cookie); | ||
// allow you to work with cookies as objects. | ||
// Allow you to work with cookies as objects. | ||
if ((typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object') { | ||
@@ -152,2 +172,3 @@ _rawCookie[name] = JSON.stringify(val); | ||
load: load, | ||
loadAll: loadAll, | ||
select: select, | ||
@@ -154,0 +175,0 @@ save: save, |
{ | ||
"name": "react-cookies", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Load and save cookies with React", | ||
@@ -8,14 +8,18 @@ "main": "build/cookie.js", | ||
"author": "Bu Kinoshita <bukinoshita@gmail.com>", | ||
"license": "MIT", | ||
"scripts": { | ||
"lint": "node_modules/.bin/eslint --ext .js .", | ||
"lint": "xo --quiet", | ||
"test": "npm run lint && node_modules/.bin/jest", | ||
"watch": "node_modules/.bin/jest --watch", | ||
"build": "node_modules/.bin/babel src -d build --ignore __tests__" | ||
"build": "node_modules/.bin/babel src -d build --ignore __tests__", | ||
"precommit": "lint-staged" | ||
}, | ||
"config": { | ||
"ghooks": { | ||
"pre-commit": "npm test" | ||
} | ||
}, | ||
"keywords": [ | ||
"react-cookies", | ||
"react-cookie", | ||
"cookie", | ||
"cookies", | ||
"react", | ||
"reactjs", | ||
"jsx" | ||
], | ||
"dependencies": { | ||
@@ -27,3 +31,2 @@ "cookie": "^0.3.1", | ||
"babel-cli": "^6.24.1", | ||
"babel-eslint": "^7.2.3", | ||
"babel-jest": "^19.0.0", | ||
@@ -34,15 +37,33 @@ "babel-plugin-add-module-exports": "^0.2.1", | ||
"babel-register": "^6.24.1", | ||
"eslint": "^3.19.0", | ||
"eslint-config-cyprex": "^1.1.2", | ||
"eslint-plugin-flowtype": "^2.32.1", | ||
"ghooks": "^2.0.0", | ||
"jest": "^19.0.2" | ||
"eslint-config-prettier": "^2.2.0", | ||
"husky": "^0.14.1", | ||
"jest": "^19.0.2", | ||
"lint-staged": "^4.0.0", | ||
"prettier": "^1.4.4", | ||
"xo": "^0.18.2" | ||
}, | ||
"keywords": [ | ||
"cookie", | ||
"cookies", | ||
"react", | ||
"reactjs", | ||
"jsx" | ||
] | ||
"xo": { | ||
"space": true, | ||
"esnext": true, | ||
"semicolon": false, | ||
"ignores": [ | ||
"build/*.js", | ||
"__tests__/*.js" | ||
], | ||
"globals": [ | ||
"document" | ||
], | ||
"rules": { | ||
"func-names": 0, | ||
"object-curly-spacing": 0 | ||
} | ||
}, | ||
"lint-staged": { | ||
"src/*.js": [ | ||
"npm run test", | ||
"prettier --semi false --single-quote --write", | ||
"git add" | ||
] | ||
}, | ||
"license": "MIT" | ||
} |
297
README.md
@@ -1,5 +0,4 @@ | ||
# react-cookies | ||
# react-cookies [![Build Status](https://travis-ci.org/bukinoshita/react-cookies.svg?branch=master)](https://travis-ci.org/bukinoshita/react-cookies) | ||
> Load and save cookies with React | ||
React cookies is the v1.0.4 of react-cookie.s | ||
@@ -11,95 +10,277 @@ ## Install | ||
## Usage | ||
```js | ||
import { Component } from 'react' | ||
import cookie from 'react-cookies' | ||
import LoginPanel from './LoginPanel' | ||
import Dashboard from './Dashboard' | ||
class MyApp extends Component { | ||
constructor () { | ||
super() | ||
this.onLogin = this.onLogin.bind(this) | ||
this.onLogout = this.onLogout.bind(this) | ||
} | ||
componentWillMount() { | ||
this.state = { userId: cookie.load('userId') } | ||
} | ||
onLogin(userId) { | ||
this.setState({ userId }) | ||
cookie.save('userId', userId, { path: '/' }) | ||
} | ||
onLogout() { | ||
cookie.remove('userId', { path: '/' }) | ||
} | ||
render() { | ||
const { userId } = this.state | ||
if (!userId) { | ||
return <LoginPanel onSuccess={this.onLogin} /> | ||
} | ||
return <Dashboard userId={userId} /> | ||
} | ||
} | ||
``` | ||
_React cookies is the v1.0.4 of react-cookie with a couple of changes._ | ||
## Isomorphic cookies! | ||
To be able to access user cookies while doing server-rendering, you can use [`plugToRequest`](#user-content-plugtorequestreq-res-unplug) or [`setRawCookie`](#user-content-setrawcookiecookies). | ||
## API | ||
### load(name, [doNotParse]) | ||
### .load(name, [doNotParse]) | ||
Load the cookie value.<br /> | ||
<br /> | ||
Returns `undefined` if the cookie does not exist.<br /> | ||
Deserialize any cookie starting with { or [ unless `dotNotParse` is `true`. | ||
Deserialize any cookie starting with `{` or `[` unless `dotNotParse` is `true`. | ||
#### name | ||
### select([regex]) | ||
Type: `string`<br/> | ||
Required | ||
#### doNotParse | ||
Type: `boolean`<br/> | ||
Default: false | ||
#### Example | ||
```js | ||
import cookie from 'react-cookies' | ||
componentWillMount() { | ||
this.state = { token: cookie.load('token') } | ||
// => 123456789 | ||
} | ||
``` | ||
### .loadAll() | ||
Load all available cookies.<br /> | ||
Returns an `object` containing all cookies. | ||
#### Example | ||
```js | ||
import cookie from 'react-cookies' | ||
componentWillMount() { | ||
this.state = { cookies: cookie.loadAll() } | ||
// => { cookies: { token: 123456789, _ga: GA198712 } } | ||
} | ||
``` | ||
### .select([regex]) | ||
Find all the cookies with a name that match the regex.<br /> | ||
<br /> | ||
Returns an `object` with the cookie name as the key. | ||
### save(name, val, [[options]](#user-content-options)) | ||
Set a cookie | ||
#### Example | ||
### remove(name, [[options]](#user-content-options)) | ||
Remove a cookie | ||
```js | ||
import cookie from 'react-cookies' | ||
### plugToRequest(req, res): unplug() | ||
Load the user cookies so you can do server-rendering and match the same result.<br /> | ||
Also send back to the user the new cookies.<br /> | ||
Work with connect or express.js by using the cookieParser middleware first.<br /> | ||
Use `const unplug = plugToRequest(req, res)` just before your `renderToString`.<br /> | ||
<br /> | ||
Returns `unplug()` function so it stops setting cookies on the response. | ||
componentWillMount() { | ||
this.state = { tests: cookie.select(/\btest(er|ing|ed|s)?\b/g) } | ||
// => { tests: { test: 'test', 'testing': 'testing' } } | ||
} | ||
``` | ||
### .save(name, value, [options]) | ||
### setRawCookie(cookies) | ||
Load the user cookies so you can do server-rendering and match the same result.<br /> | ||
Use `setRawCookie(headers.cookie)` just before your `renderToString`.<br /> | ||
Make sure it is the raw string from the request headers.<br /> | ||
Set a cookie. | ||
### Options | ||
#### name | ||
Type: `string`<br/> | ||
Required | ||
#### value | ||
Type: `string`||`number`||`object`<br/> | ||
Required | ||
#### options | ||
Support all the cookie options from the [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.2.1). | ||
### path | ||
> cookie path<br /> | ||
> Use `/` as the path if you want your cookie to be accessible on all pages. | ||
Type: `object` | ||
### expires | ||
> absolute expiration date for the cookie **(Date object)** | ||
##### path | ||
### maxAge | ||
> relative max age of the cookie from when the client receives it **(seconds)** | ||
Cookie path.<br/> | ||
Use `/` as the path if you want your cookie to be accessible on all pages. | ||
### domain | ||
> domain for the cookie<br /> | ||
> Use `https://*.yourdomain.com` if you want to access the cookie in all your subdomains. | ||
Type: `string` | ||
### secure | ||
> Is only accessible through HTTPS? **true or false** | ||
##### expires | ||
### httpOnly | ||
> Is only the server can access the cookie? **true or false** | ||
Absolute expiration date for the cookie. | ||
# Example | ||
Type: `object (date)` | ||
##### maxAge | ||
Relative max age of the cookie from when the client receives it in `seconds`. | ||
Type: `number` | ||
##### domain | ||
Domain for the cookie.<br/> | ||
Use `https://*.yourdomain.com` if you want to access the cookie in all your subdomains. | ||
Type: `string` | ||
##### secure | ||
If set `true` it will only be accessible through https. | ||
Type: `boolean` | ||
##### httpOnly | ||
If set `true` it will only be accessible on the server. | ||
Type: `boolean` | ||
#### Example | ||
```js | ||
import { Component } from 'react' | ||
import cookie from 'react-cookies' | ||
import LoginPanel from './LoginPanel' | ||
import Dashboard from './Dashboard' | ||
handleButtonClick() { | ||
const expires = new Date() | ||
expires.setDate(now.getDate() + 14) | ||
export default class MyApp extends Component { | ||
componentWillMount() { | ||
this.state = { userId: cookie.load('userId') } | ||
} | ||
cookie.save( | ||
'userId', | ||
'1234', | ||
{ | ||
path: '/', | ||
expires, | ||
maxAge: 1000, | ||
domain: 'https://play.bukinoshita.io', | ||
secure: true | ||
httpOnly: true | ||
} | ||
) | ||
} | ||
``` | ||
onLogin(userId) { | ||
this.setState({ userId }) | ||
cookie.save('userId', userId, { path: '/' }) | ||
} | ||
onLogout() { | ||
cookie.remove('userId', { path: '/' }) | ||
} | ||
### .remove(name, [options]) | ||
render() { | ||
if (!this.state.userId) { | ||
return <LoginPanel onSuccess={this.onLogin.bind(this)} /> | ||
} | ||
Remove a cookie. | ||
return <Dashboard userId={this.state.userId} /> | ||
} | ||
#### name | ||
Type: `string`<br/> | ||
Required | ||
#### options | ||
Support all the cookie options from the [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.2.1). | ||
Type: `object` | ||
##### path | ||
Cookie path.<br/> | ||
Use `/` as the path if you want your cookie to be accessible on all pages. | ||
Type: `string` | ||
##### expires | ||
Absolute expiration date for the cookie. | ||
Type: `object (date)` | ||
##### maxAge | ||
Relative max age of the cookie from when the client receives it in `seconds`. | ||
Type: `number` | ||
##### domain | ||
Domain for the cookie.<br/> | ||
Use `https://*.yourdomain.com` if you want to access the cookie in all your subdomains. | ||
Type: `string` | ||
##### secure | ||
If set `true` it will only be accessible through https. | ||
Type: `boolean` | ||
##### httpOnly | ||
If set `true` it will only be accessible on the server. | ||
Type: `boolean` | ||
#### Example | ||
```js | ||
import cookie from 'react-cookies' | ||
handleButtonClick() { | ||
cookie.remove('userId', { path: '/' }) | ||
} | ||
``` | ||
### .plugToRequest(req, res): unplug() | ||
Load the user cookies so you can do server-rendering and match the same result.<br /> | ||
Also send back to the user the new cookies.<br /> | ||
Work with [connect](https://github.com/senchalabs/connect) or [express.js](https://github.com/expressjs/express) by using the cookieParser middleware first.<br /> | ||
Use `const unplug = plugToRequest(req, res)` just before your `renderToString`.<br /> | ||
<br /> | ||
Returns `unplug()` function so it stops setting cookies on the response. | ||
### .setRawCookie(cookies) | ||
Load the user cookies so you can do server-rendering and match the same result.<br /> | ||
Use `setRawCookie(headers.cookie)` just before your `renderToString`.<br /> | ||
Make sure it is the raw string from the request headers.<br /> | ||
## License | ||
[MIT](https://github.com/bukinoshita/react-cookies/blob/master/LICENSE) © Bu Kinoshita |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
166118
260
286
7
1