( names: T, )
| 11 | }; |
| 12 | |
| 13 | export const getCookies = < |
| 14 | T extends string[] = string[], |
| 15 | R = Record<T[number], string>, |
| 16 | >( |
| 17 | names: T, |
| 18 | ): R | undefined => { |
| 19 | const cookies = |
| 20 | globalThis?.document?.cookie?.split(';')?.map((cookie) => cookie.trim()) || |
| 21 | []; |
| 22 | if (!cookies.length) { |
| 23 | return undefined; |
| 24 | } |
| 25 | |
| 26 | return names.reduce((acc, name) => { |
| 27 | const foundCookie = cookies.find((cookie) => cookie.startsWith(`${name}=`)); |
| 28 | if (!foundCookie) { |
| 29 | return acc; |
| 30 | } |
| 31 | return { |
| 32 | ...acc, |
| 33 | [name]: decodeURIComponent(foundCookie.split('=')[1]), |
| 34 | }; |
| 35 | }, {} as R); |
| 36 | }; |
| 37 | |
| 38 | export const setCookie = ( |
| 39 | name: string, |
no outgoing calls