|
| 1 | +import { z } from 'zod'; |
| 2 | + |
| 3 | +export const CONSENT_COOKIE_NAME = 'c15t-consent'; |
| 4 | + |
| 5 | +const ConsentNamesSchema = z.enum([ |
| 6 | + 'necessary', |
| 7 | + 'experience', |
| 8 | + 'functionality', |
| 9 | + 'marketing', |
| 10 | + 'measurement', |
| 11 | +]); |
| 12 | +const ConsentStateSchema = z.record(ConsentNamesSchema, z.boolean()); |
| 13 | +const ConsentCookieSchema = z.object({ |
| 14 | + preferences: ConsentStateSchema, |
| 15 | + timestamp: z.date(), |
| 16 | +}); |
| 17 | + |
| 18 | +function getConsentCookie() { |
| 19 | + if (typeof document === 'undefined') return null; |
| 20 | + |
| 21 | + const cookies = document.cookie.split('; '); |
| 22 | + const cookie = cookies.find((c) => c.startsWith(`${CONSENT_COOKIE_NAME}=`)); |
| 23 | + |
| 24 | + if (!cookie) return null; |
| 25 | + |
| 26 | + const value = cookie.split('=')[1] ?? null; |
| 27 | + |
| 28 | + if (!value) return null; |
| 29 | + |
| 30 | + const decoded = decodeURIComponent(value); |
| 31 | + const raw: unknown = JSON.parse(decoded); |
| 32 | + const result = ConsentCookieSchema.safeParse(raw); |
| 33 | + |
| 34 | + if (!result.success) return null; |
| 35 | + |
| 36 | + return result.data; |
| 37 | +} |
| 38 | + |
| 39 | +function setConsentCookie(consent: Record<string, boolean>) { |
| 40 | + if (typeof document === 'undefined') return; |
| 41 | + |
| 42 | + const encoded = encodeURIComponent(JSON.stringify(consent)); |
| 43 | + |
| 44 | + document.cookie = `${CONSENT_COOKIE_NAME}=${encoded}; path=/`; |
| 45 | +} |
| 46 | + |
| 47 | +export function showConsentBanner() { |
| 48 | + let show = true; |
| 49 | + |
| 50 | + try { |
| 51 | + const consent = getConsentCookie(); |
| 52 | + |
| 53 | + show = !consent; |
| 54 | + } catch { |
| 55 | + show = false; |
| 56 | + } |
| 57 | + |
| 58 | + return { |
| 59 | + data: { |
| 60 | + showConsentBanner: show, |
| 61 | + branding: 'none', |
| 62 | + }, |
| 63 | + error: null, |
| 64 | + ok: true, |
| 65 | + response: null, |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +export function setConsent(options?: { |
| 70 | + body?: { preferences?: Record<string, boolean>; type?: string; domain?: string }; |
| 71 | +}) { |
| 72 | + const { preferences } = options?.body ?? {}; |
| 73 | + |
| 74 | + setConsentCookie(preferences ?? {}); |
| 75 | + |
| 76 | + return { |
| 77 | + data: null, |
| 78 | + error: null, |
| 79 | + ok: true, |
| 80 | + response: null, |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +export function verifyConsent() { |
| 85 | + const consent = getConsentCookie(); |
| 86 | + |
| 87 | + if (!consent) { |
| 88 | + return { |
| 89 | + data: { |
| 90 | + isValid: false, |
| 91 | + }, |
| 92 | + error: null, |
| 93 | + ok: true, |
| 94 | + response: null, |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + return { |
| 99 | + data: { |
| 100 | + isValid: true, |
| 101 | + }, |
| 102 | + error: null, |
| 103 | + ok: true, |
| 104 | + response: null, |
| 105 | + }; |
| 106 | +} |
0 commit comments