Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add isomorphic cookie reading #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
var cookie = require('cookie');
var getter = require('./lib/getter');
var setter = require('./lib/setter');

module.exports = function setCookie(name, value, options) {
function getCookie(name, options) {
var cookieSource = getter(name, options);
var parsedCookieMap;

if (cookieSource) {
parsedCookieMap = cookie.parse(cookieSource);
return parsedCookieMap[name];
}
}

function setCookie(name, value, options) {
var cookieStr = cookie.serialize(name, value, options);
setter(cookieStr, options);
};

module.exports = {
getCookie: getCookie,
setCookie: setCookie
};
6 changes: 6 additions & 0 deletions lib/getter/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Cookie getter for browser environment.
*/
module.exports = function getter(cookieName, _) {
return document.cookie;
};
10 changes: 10 additions & 0 deletions lib/getter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Cookie getter for Node.js environment.
*/
module.exports = function getter(cookieName, options) {
var req = options && options.req,
parsedCookieHeader;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can drop this - cruft from a refactor

if (!req) throw new Error('Must specify `req` when setting cookie.');

return req.headers.cookie;
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "set-cookie",
"version": "0.0.4",
"description": "Set a cookie using the same API on both the client and the server.",
"version": "0.1.0",
"description": "Get and set cookies using the same API on both the client and the server.",
"main": "index.js",
"scripts": {
"test": "mocha",
Expand All @@ -24,6 +24,7 @@
"cookie": "^0.1.2"
},
"browser": {
"./lib/getter/index.js": "./lib/getter/client.js",
"./lib/setter/index.js": "./lib/setter/client.js"
},
"devDependencies": {
Expand Down
222 changes: 146 additions & 76 deletions test/browser/bundle.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions test/browser/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
var setCookie = require('../../');
var isoCookie = require('../../');
var readValue;

setCookie('thisIsA', 'cookie in a browser!', {
isoCookie.setCookie('thisIsA', 'cookie in a browser!', {
expires: new Date(2020, 1, 1)
});

console.log(document.cookie);

readValue = isoCookie.getCookie('thisIsA');

console.log(readValue);
45 changes: 44 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var setCookie = require('../');
var getCookie = require('../').getCookie;
var setCookie = require('../').setCookie;
var chai = require('chai');
var sinon = require('sinon');
var superagent = require('superagent');
Expand Down Expand Up @@ -69,3 +70,45 @@ describe('set-cookie', function() {
});
});
});

describe('get-cookie', function() {
describe('Node getter', function() {
it('throws when not passed `req`', function() {
expect(function() {
getCookie('myName', {});
}).to.throw(/req/);
});

it('returns undefined when passed `req` without a Cookie header', function() {
var req = {
headers: {}
};

var cookie = getCookie('myName', {req: req});
expect(cookie).to.eql(undefined);
});

describe('when passed a Cookie header', function() {
var req = {
headers: {
cookie: 'foo=bar%20baz; cat=meow; dog=ruff'
}
};

it('returns undefined if the key is not present', function() {
var cookie = getCookie('myName', {req: req});
expect(cookie).to.eql(undefined);
});

it('returns the value if the key is present', function() {
var cookie = getCookie('cat', {req: req});
expect(cookie).to.eql('meow');
});

it('decodes the parsed value', function() {
var cookie = getCookie('foo', {req: req});
expect(cookie).to.eql('bar baz');
});
});
});
});