diff --git a/src/types/cookies.type.ts b/src/types/cookies.type.ts new file mode 100644 index 00000000..3f2ffb0f --- /dev/null +++ b/src/types/cookies.type.ts @@ -0,0 +1,19 @@ +export interface CookieGetOptions { + doNotParse?: boolean; + doNotUpdate?: boolean; +} +export interface CookieSetOptions { + path?: string; + expires?: Date; + maxAge?: number; + domain?: string; + secure?: boolean; + httpOnly?: boolean; + sameSite?: boolean | 'none' | 'lax' | 'strict'; +} +export interface CookieChangeOptions { + name: string; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + value?: any; + options?: CookieSetOptions; +} diff --git a/src/utils/cookies.ts b/src/utils/cookies.ts new file mode 100644 index 00000000..c2879eed --- /dev/null +++ b/src/utils/cookies.ts @@ -0,0 +1,18 @@ +import { Cookies } from 'react-cookie'; + +import { CookieGetOptions, CookieSetOptions } from '@/types/cookies.type'; + +const cookies = new Cookies(); + +export const setCookie = (name: string, value: string, options?: CookieSetOptions) => { + return cookies.set(name, value, { ...options }); +}; + +export const getCookie = (name: string, options?: CookieGetOptions) => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return cookies.get(name, options); +}; + +export const removeCookie = (name: string, options?: CookieSetOptions) => { + return cookies.remove(name, options); +};