Skip to content

Commit d3cb7db

Browse files
committed
main 🧊 add default options for use cookie
1 parent 352c79a commit d3cb7db

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

‎packages/core/src/bundle/hooks/useCookie/useCookie.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const useCookie = (key, params) => {
8585
const cookieValue = getCookieItem(key);
8686
if (cookieValue === undefined && initialValue !== undefined) {
8787
const value = initialValue instanceof Function ? initialValue() : initialValue;
88-
setCookieItem(key, serializer(value));
88+
setCookieItem(key, serializer(value), options);
8989
return value;
9090
}
9191
return cookieValue ? deserializer(cookieValue) : undefined;
@@ -98,7 +98,7 @@ export const useCookie = (key, params) => {
9898
window.addEventListener(COOKIE_EVENT, onChange);
9999
return () => window.removeEventListener(COOKIE_EVENT, onChange);
100100
}, [key]);
101-
const set = (value, options) => setCookieItem(key, serializer(value), options);
102-
const remove = (options) => removeCookieItem(key, options);
101+
const set = (value, params) => setCookieItem(key, serializer(value), { ...options, ...params });
102+
const remove = (params) => removeCookieItem(key, { ...options, ...params });
103103
return { value, set, remove };
104104
};

‎packages/core/src/hooks/useCookie/useCookie.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,24 @@ export type UseCookieInitialValue<Value> = (() => Value) | Value;
5555

5656
/* The use storage options type */
5757
export interface UseCookieOptions<Value> {
58+
/* The domain of the cookie */
59+
domain?: string;
60+
/* The expiration date of the cookie */
61+
expires?: Date;
62+
/* Whether the cookie is httpOnly */
63+
httpOnly?: boolean;
5864
/* The initial value of the storage */
5965
initialValue?: UseCookieInitialValue<Value>;
66+
/* The maximum age of the cookie */
67+
maxAge?: number;
68+
/* The path of the cookie */
69+
path?: string;
70+
/* The sameSite of the cookie */
71+
sameSite?: 'Lax' | 'None' | 'Strict';
72+
/* Whether the cookie is secure */
73+
secure?: boolean;
74+
/* Whether to update the cookie on change */
75+
updateOnChange?: boolean;
6076
/* The deserializer function to be invoked */
6177
deserializer?: (value: string) => Value;
6278
/* The serializer function to be invoked */
@@ -144,7 +160,7 @@ export const useCookie = <Value>(
144160
const cookieValue = getCookieItem(key);
145161
if (cookieValue === undefined && initialValue !== undefined) {
146162
const value = initialValue instanceof Function ? initialValue() : initialValue;
147-
setCookieItem(key, serializer(value));
163+
setCookieItem(key, serializer(value), options);
148164
return value;
149165
}
150166
return cookieValue ? deserializer(cookieValue) : undefined;
@@ -159,9 +175,9 @@ export const useCookie = <Value>(
159175
return () => window.removeEventListener(COOKIE_EVENT, onChange);
160176
}, [key]);
161177

162-
const set = (value: Value, options?: SetCookieParams) =>
163-
setCookieItem(key, serializer(value), options);
164-
const remove = (options?: RemoveCookieParams) => removeCookieItem(key, options);
178+
const set = (value: Value, params?: SetCookieParams) =>
179+
setCookieItem(key, serializer(value), { ...options, ...params });
180+
const remove = (params?: RemoveCookieParams) => removeCookieItem(key, { ...options, ...params });
165181

166182
return { value, set, remove };
167183
};

0 commit comments

Comments
 (0)