Skip to content

Commit

Permalink
♻️ added options prop for setNookie
Browse files Browse the repository at this point in the history
  • Loading branch information
harshzalavadiya committed May 27, 2020
1 parent 18b98c1 commit 5da0a5a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-nookies-persist",
"version": "1.3.4",
"version": "1.3.5",
"description": "Next.js persist cookie data with hooks",
"author": "harshzalavadiya",
"license": "MIT",
Expand Down
5 changes: 4 additions & 1 deletion src/cookies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ import { NK } from "./static";
*
* @param {string} key
* @param {*} value
* @param {*} [options={}]
* @param {number} maxAge
* @param {string} path
* @param {*} [ctx={}]
*/
export const setCookie = (
key: string,
value: any,
options: any = NK.OPTIONS,
maxAge: number = NK.MAX_AGE,
path: string = NK.PATH,
ctx: any = {}
) => {
const nKey = NK.PREFIX + key;
set(ctx, nKey, JSON.stringify(value), {
maxAge,
path
path,
...options,
});
sparkles().emit(NK.ADDED, { [key]: value });
};
Expand Down
16 changes: 9 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { INookiesProvider, IStorage, NK } from "./static";
const NookiesContext = createContext({
nookies: {},
setNookie: () => {},
removeNookie: () => {}
removeNookie: () => {},
} as IStorage);

/**
Expand All @@ -17,6 +17,7 @@ const NookiesContext = createContext({
* @param {INookiesProvider} {
* children,
* initialValue,
* options
* maxAge = NK.MAX_AGE,
* path = NK.PATH
* }
Expand All @@ -25,22 +26,23 @@ const NookiesContext = createContext({
const NookiesProvider = ({
children,
initialValue,
options = NK.OPTIONS,
maxAge = NK.MAX_AGE,
path = NK.PATH
path = NK.PATH,
}: INookiesProvider) => {
const [nookies, setNookies] = useState(initialValue);

const setNookie = (key: string, value: any, ctx: any = {}) => {
setCookie(key, value, maxAge, path, ctx);
setCookie(key, value, options, maxAge, path, ctx);
};

sparkles().on(NK.ADDED, function(evt) {
sparkles().on(NK.ADDED, function (evt) {
setNookies({ ...nookies, ...evt });
});

const removeNookie = destroyCookie;

sparkles().on(NK.REMOVED, function(key) {
sparkles().on(NK.REMOVED, function (key) {
let tNookies = { ...nookies };
delete tNookies[key];
setNookies(tNookies);
Expand All @@ -51,7 +53,7 @@ const NookiesProvider = ({
value={{
nookies,
setNookie,
removeNookie
removeNookie,
}}
>
{children}
Expand All @@ -69,5 +71,5 @@ export {
destroyCookie as removeNookie,
setCookie as setNookie,
NookiesProvider,
useNookies as default
useNookies as default,
};
2 changes: 1 addition & 1 deletion src/parser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const parseNookies = (ctx?) => {
...parsedCookies,
[key.substring(NK.PREFIX.length, key.length)]: JSON.parse(
value.toString()
)
),
};
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export const NK = {
REMOVED: "nk_removed",
PREFIX: "nk_",
PATH: "/",
MAX_AGE: 60 * 60 * 24 * 7 // 1 Week
OPTIONS: {},
MAX_AGE: 60 * 60 * 24 * 7, // 1 Week
};

export interface IStorage {
Expand All @@ -14,7 +15,8 @@ export interface IStorage {

export interface INookiesProvider {
children;
initialValue: { [key: string]: any };
initialValue: Record<string, any>;
options?: Record<string, any>;
maxAge?: number;
path?: string;
}

0 comments on commit 5da0a5a

Please sign in to comment.