From 8fad04a9a1f4cf7a70895540abb907bbe20a2898 Mon Sep 17 00:00:00 2001 From: Tyler Wolff Date: Sun, 3 Apr 2022 12:22:57 -0700 Subject: [PATCH] feat: add ability to set any cookie option --- README.md | 19 ++++++++++--------- index.d.ts | 6 +++++- index.js | 21 ++++++++++++++++++--- index.test.js | 32 ++++++++++++++++++++++++++++++++ package.json | 2 +- 5 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 index.test.js diff --git a/README.md b/README.md index ef5a1ef..1d71bc2 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ yarn add react-use-cookie ```jsx import useCookie from 'react-use-cookie'; -export default props => { +export default (props) => { const [userToken, setUserToken] = useCookie('token', '0'); render( @@ -40,8 +40,8 @@ In this example you can also set custom cookie options by passing an options obj ```js setUserToken('abcd', { days: 365, - samesite: 'strict', - secure: true, + SameSite: 'Strict', + Secure: true, }); ``` @@ -71,12 +71,12 @@ exported `setCookie` function: ```js import { setCookie } from 'react-use-cookie'; -const saveLocale = locale => { +const saveLocale = (locale) => { setCookie('locale', locale, { days: 1, domain: 'github.com', - samesite: 'lax', - secure: true, + SameSite: 'Lax', + Secure: true, }); }; ``` @@ -91,10 +91,11 @@ You can also specify cookie options as a third argument: // The path of the cookie (defaults to '/') path?: string; - // No defaults + // Browser defaults unless set domain?: string; - samesite?: 'none' | 'lax' | 'strict'; - secure?: boolean; + SameSite?: 'None' | 'Lax' | 'Strict'; + Secure?: boolean; + HttpOnly?: boolean; } ``` diff --git a/index.d.ts b/index.d.ts index f6acc73..5f3125d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,6 +7,10 @@ declare module 'react-use-cookie' { interface cookieOptions { days?: number; path?: string; + domain?: string; + SameSite?: 'None' | 'Lax' | 'Strict'; + Secure?: boolean; + HttpOnly?: boolean; } export interface updateItem { @@ -21,7 +25,7 @@ declare module 'react-use-cookie' { export function getCookie(name: string, initialValue?: string): string; - export default function( + export default function ( key: string, initialValue?: string ): [string, updateItem]; diff --git a/index.js b/index.js index c7a2fcc..a0f90cc 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,22 @@ import { useState } from 'react'; const isBrowser = typeof window !== 'undefined'; +export function stringifyOptions(options) { + return Object.keys(options).reduce((acc, key) => { + if (key === 'days') { + return acc; + } else { + if (options[key] === false) { + return acc; + } else if (options[key] === true) { + return `${acc}; ${key}`; + } else { + return `${acc}; ${key}=${options[key]}`; + } + } + }, ''); +} + export const setCookie = (name, value, options) => { if (!isBrowser) return; @@ -21,8 +37,7 @@ export const setCookie = (name, value, options) => { encodeURIComponent(value) + '; expires=' + expires + - '; path=' + - optionsWithDefaults.path; + stringifyOptions(optionsWithDefaults); }; export const getCookie = (name, initialValue = '') => { @@ -36,7 +51,7 @@ export const getCookie = (name, initialValue = '') => { ); }; -export default function(key, initialValue) { +export default function (key, initialValue) { const [item, setItem] = useState(() => { return getCookie(key, initialValue); }); diff --git a/index.test.js b/index.test.js new file mode 100644 index 0000000..23eb999 --- /dev/null +++ b/index.test.js @@ -0,0 +1,32 @@ +import { getCookie, setCookie, stringifyOptions } from './index'; + +describe('#getCookie', () => { + // TODO: add tests +}); + +describe('#setCookie', () => { + // TODO: add tests +}); + +describe('#stringifyOptions', () => { + it('should not include the days param', () => { + expect( + stringifyOptions({ + days: 10, + path: '/', + }) + ).toEqual('; path=/'); + }); + + it('should stringify any extra cookie options', () => { + expect( + stringifyOptions({ + days: 7, + path: '/test', + SameSite: 'Lax', + Secure: true, + HttpOnly: false, + }) + ).toEqual('; path=/test; SameSite=Lax; Secure'); + }); +}); diff --git a/package.json b/package.json index 616f935..0f2c3be 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "scripts": { "build": "babel index.js -d dist && cp index.d.ts dist/index.d.ts", "prepublishOnly": "npm run build", - "test": "jest" + "test": "jest --watch" }, "peerDependencies": { "react": ">=16.8"