Skip to content

Commit

Permalink
Update delete method (#36)
Browse files Browse the repository at this point in the history
* Return cookie object from get method
* Improve delete method and documentation
* Add basic test for delete method
  • Loading branch information
Mark Kennedy committed Aug 8, 2020
1 parent 2804bcf commit fe99d16
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import 'cookie-store';
await cookieStore.set('forgive', 'me');
// get a cookie
const foo = await cookieStore.get('forgive');
console.log(foo); // 'me'
console.log(foo); // { name: 'forgive', value: 'me' }

// set another cookie
await cookieStore.set('forget', 'it');
Expand Down
15 changes: 12 additions & 3 deletions index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ describe('Cookie Store', () => {
});
describe('get', () => {
it('returns cookie matching supplied name', async () => {
document.cookie = 'foo=bar';
const foo = await window.cookieStore.get('foo');
expect(foo).to.equal('bar');
const foo = 'foo';
const bar = 'bar';
document.cookie = `${foo}=${bar}`;
const result = await window.cookieStore.get(foo);
expect(result).to.deep.equal({ name: foo, value: bar });
});
});
describe('set', () => {
Expand All @@ -27,4 +29,11 @@ describe('Cookie Store', () => {
expect(document.cookie).to.equal('foo=bar');
});
});
describe('delete', () => {
it('sets max age to 0 on cookie that matches supplied name', async () => {
document.cookie = 'foo=bar';
await window.cookieStore.delete('foo');
expect(document.cookie).to.equal('foo=bar; Max-Age=0; Path=/');
});
});
});
44 changes: 35 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
* @param {function} decode
* @private
*/

function tryDecode(
str: string,
decode: ((encodedURIComponent: string) => string) | boolean
Expand All @@ -37,7 +36,25 @@ function tryDecode(
}
}

type ParsedCookies = Record<string, unknown>;
type CookieSameSite = 'no_restriction' | 'lax' | 'strict';

interface Cookie {
domain?: string;
expires?: number;
name: string;
path?: string;
secure?: boolean;
sameSite?: CookieSameSite;
value: string;
}

interface CookieStoreDeleteOptions {
name: string;
domain: null;
path: '/';
}

type ParsedCookies = Record<string, Cookie>;

interface ParseOptions {
decode?: boolean;
Expand Down Expand Up @@ -95,7 +112,10 @@ function parse(str, options: ParseOptions = {}): ParsedCookies {

// only assign once
if (undefined == obj[key]) {
obj[key] = tryDecode(val, dec);
obj[key] = {
name: key,
value: tryDecode(val, dec),
};
}
}

Expand Down Expand Up @@ -210,8 +230,8 @@ const CookieStore = {
* @param {string} name
* @return {Promise}
*/
get(name): Promise<string> {
return Promise.resolve(parse(document.cookie)[name] as string);
get(name): Promise<Cookie> {
return Promise.resolve(parse(document.cookie)[name]);
},

/**
Expand All @@ -221,7 +241,7 @@ const CookieStore = {
* @param {string} value
* @return {Promise}
*/
set(name, value): Promise<void> {
set(name: string, value: string): Promise<void> {
return new Promise((resolve, reject) => {
try {
const cookieString = serialize(name, value);
Expand All @@ -245,12 +265,18 @@ const CookieStore = {
/**
* Remove a cookie.
*
* @param {String} name
* @return {Promise}
*/
delete(name): Promise<void> {
return this.get(name).then((str) => {
document.cookie = document.cookie.replace(str, '');
async delete(name: CookieStoreDeleteOptions['name']): Promise<void> {
const { value } = await this.get(name);
const serializedValue = serialize(name, value, {
maxAge: 0,
domain: null,
path: '/',
});
document.cookie = serializedValue;
return Promise.resolve();
},
};

Expand Down

0 comments on commit fe99d16

Please sign in to comment.