Skip to content

Commit

Permalink
feat: add ability to set any cookie option
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerwolff committed Apr 3, 2022
1 parent 698c998 commit 8fad04a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 14 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
});
```

Expand Down Expand Up @@ -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,
});
};
```
Expand All @@ -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;
}
```

Expand Down
6 changes: 5 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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];
Expand Down
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,8 +37,7 @@ export const setCookie = (name, value, options) => {
encodeURIComponent(value) +
'; expires=' +
expires +
'; path=' +
optionsWithDefaults.path;
stringifyOptions(optionsWithDefaults);
};

export const getCookie = (name, initialValue = '') => {
Expand All @@ -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);
});
Expand Down
32 changes: 32 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8fad04a

Please sign in to comment.