@luwiostack/cookie-consent 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,272 @@
1
+ import { ObservableStore } from "@luwiostack/storage";
2
+ //#region src/categories.d.ts
3
+ /**
4
+ * The fixed consent categories, in display order — the same four every consent tool (and Google
5
+ * Consent Mode) works with. `necessary` is always granted. A category's name and description are
6
+ * translated by machine name (`consent_category_<id>`, `consent_category_<id>_description`) from
7
+ * the package catalogs.
8
+ */
9
+ declare const CONSENT_CATEGORIES: readonly ['necessary', 'preferences', 'statistics', 'marketing'];
10
+ /** `'necessary' | 'preferences' | 'statistics' | 'marketing'` */
11
+ type ConsentCategoryId = (typeof CONSENT_CATEGORIES)[number];
12
+ /** The category that is always granted and can't be refused. */
13
+ declare const REQUIRED_CATEGORY = "necessary";
14
+ /** Whether a value is one of the fixed category ids. */
15
+ declare function isConsentCategoryId(value: unknown): value is ConsentCategoryId;
16
+ //#endregion
17
+ //#region src/domain/consent-decision.d.ts
18
+ interface ConsentDecisionInput {
19
+ consent_id: string;
20
+ policy_version: string;
21
+ granted: Iterable<ConsentCategoryId>;
22
+ decided_at: number;
23
+ }
24
+ /**
25
+ * One visitor's answer to the consent question: which categories they granted, under which policy
26
+ * version, and when. Immutable and always valid — the factory throws on a malformed input.
27
+ *
28
+ * Validity over time (policy version, expiry) is not the decision's concern: the consent instance
29
+ * decides whether a stored decision still counts.
30
+ */
31
+ declare class ConsentDecision {
32
+ #private;
33
+ readonly consent_id: string;
34
+ readonly policy_version: string;
35
+ /** When the decision was made, in epoch milliseconds. */
36
+ readonly decided_at: number;
37
+ private constructor();
38
+ /** @throws {ConsentError} on an empty `consent_id` / `policy_version` or a non-finite `decided_at`. */
39
+ static new(input: ConsentDecisionInput): ConsentDecision;
40
+ /** Like {@link ConsentDecision.new}, but returns `null` instead of throwing. */
41
+ static tryNew(input: ConsentDecisionInput): ConsentDecision | null;
42
+ /** Whether this decision grants the category. */
43
+ isGranted(id: ConsentCategoryId): boolean;
44
+ /** Every granted category id. */
45
+ granted(): ConsentCategoryId[];
46
+ /** Same id, version, time and granted set. */
47
+ equals(other: ConsentDecision): boolean;
48
+ /** The serialized form kept in the store. */
49
+ toJSON(): StoredConsent;
50
+ }
51
+ //#endregion
52
+ //#region src/types.d.ts
53
+ /**
54
+ * A category as the consent instance presents it: one of the fixed four, whether it's required, and
55
+ * the vendors that set cookies in it — each with only its cookies of this category. Derived from the
56
+ * config; carries no display text: `t(category.machine_name)` and
57
+ * `t(category.description_machine_name)`.
58
+ */
59
+ interface ConsentCategory {
60
+ readonly id: ConsentCategoryId;
61
+ /** The translation key of its name, e.g. `consent_category_statistics`. */
62
+ readonly machine_name: string;
63
+ /** The translation key of its description, e.g. `consent_category_statistics_description`. */
64
+ readonly description_machine_name: string;
65
+ /** `true` for `necessary` only: always granted, can't be switched off. */
66
+ readonly required: boolean;
67
+ /** The vendors with cookies in this category, their cookies narrowed to it. May be empty. */
68
+ readonly vendors: readonly ConsentVendor[];
69
+ }
70
+ /**
71
+ * A partner that sets cookies — your own site, Google, Meta, YouTube — as you declare it: its id
72
+ * and every cookie it sets. One vendor may set cookies in several categories. A preset from
73
+ * `@luwiostack/cookie-consent/vendors` is one; so is your own site.
74
+ */
75
+ interface ConsentVendorDeclaration {
76
+ /**
77
+ * Stable id — what `isGranted(category, vendor)` and analytics' `vendor` refer to. snake_case by
78
+ * convention; its machine name is derived from it.
79
+ */
80
+ readonly id: string;
81
+ /** Where the vendor explains what it does with the data. */
82
+ readonly privacy_policy_url?: string;
83
+ /**
84
+ * The countries outside the EEA the vendor sends visitors' personal data to, each with its
85
+ * safeguards. `[]`: it stays in the EEA; absent: not declared.
86
+ */
87
+ readonly transfers?: readonly ConsentTransferDeclaration[];
88
+ /** Every cookie (or storage item) this vendor sets, each with its category. */
89
+ readonly cookies: readonly ConsentCookieDeclaration[];
90
+ }
91
+ /**
92
+ * A vendor as the consent instance presents it: the declaration plus its machine names. Carries no
93
+ * display text: render its name with `t(vendor.machine_name)`.
94
+ */
95
+ interface ConsentVendor extends ConsentVendorDeclaration {
96
+ /** The translation key of its name: its id as a machine name, e.g. `google_analytics`. */
97
+ readonly machine_name: string;
98
+ readonly transfers?: readonly ConsentTransfer[];
99
+ readonly cookies: readonly ConsentCookie[];
100
+ }
101
+ /**
102
+ * What makes a transfer outside the EEA lawful (GDPR art. 45-47): an adequacy decision for the
103
+ * country, the vendor's EU-US Data Privacy Framework certification, standard contractual clauses or
104
+ * binding corporate rules.
105
+ */
106
+ type ConsentTransferSafeguard = 'adequacy' | 'dpf' | 'scc' | 'bcr';
107
+ /** One country outside the EEA a vendor sends personal data to, as you declare it. */
108
+ interface ConsentTransferDeclaration {
109
+ /** ISO 3166-1 alpha-2, e.g. `US`; render it with `Intl.DisplayNames`. */
110
+ readonly country: string;
111
+ /** At least one. */
112
+ readonly safeguards: readonly ConsentTransferSafeguard[];
113
+ }
114
+ /** A transfer as the consent instance presents it: `t(key)` for each of `safeguard_machine_names`. */
115
+ interface ConsentTransfer extends ConsentTransferDeclaration {
116
+ /** The safeguards' translation keys, in order, e.g. `consent_transfer_safeguard_dpf`. */
117
+ readonly safeguard_machine_names: readonly string[];
118
+ }
119
+ /** Whether a cookie is set on the site's own domain or on the vendor's. */
120
+ type ConsentCookieParty = 'first' | 'third';
121
+ /** Where a cookie lives. `http` is a real cookie; the others are browser storage or a tracking pixel. */
122
+ type ConsentCookieType = 'http' | 'local_storage' | 'session_storage' | 'indexed_db' | 'pixel';
123
+ /**
124
+ * How long a cookie lives: milliseconds (like `maxAge`), `'session'` (gone when the browser closes)
125
+ * or `'persistent'` (no expiry — e.g. a `localStorage` item).
126
+ */
127
+ type ConsentCookieMaxAge = number | ConsentCookieWordedMaxAge;
128
+ /** The lifetimes that are a word, not a duration — translated, not formatted. */
129
+ type ConsentCookieWordedMaxAge = 'session' | 'persistent';
130
+ /**
131
+ * One cookie a vendor sets, as you declare it. `name` is the real cookie name (`_ga`, `_fbp`) — a
132
+ * technical identifier, not display text.
133
+ */
134
+ interface ConsentCookieDeclaration {
135
+ /** The cookie's actual name, e.g. `_ga`. Unique within its vendor. */
136
+ readonly name: string;
137
+ /** What it's for — the category the visitor must grant before it may be set. */
138
+ readonly category: ConsentCategoryId;
139
+ /** Default `'http'`. */
140
+ readonly type?: ConsentCookieType;
141
+ /** Its lifetime. Format it with `ConsentCookieFormatter.formatLocale`. */
142
+ readonly max_age: ConsentCookieMaxAge;
143
+ /**
144
+ * The vendor's domain it's set on (or, for storage, the origin's host), e.g. `.youtube.com`.
145
+ * Absent: the site's own domain, so a first-party cookie.
146
+ */
147
+ readonly domain?: string;
148
+ }
149
+ /**
150
+ * A cookie as the consent instance presents it: the declaration plus the translation keys of
151
+ * everything about it that's text — `t(cookie.machine_name)` (its purpose),
152
+ * `t(cookie.type_machine_name)`, `t(cookie.party_machine_name)`, and its lifetime as
153
+ * `ConsentCookieFormatter.formatLocale(cookie, locale) ?? t(cookie.max_age_machine_name)`.
154
+ */
155
+ interface ConsentCookie extends ConsentCookieDeclaration {
156
+ /**
157
+ * The translation key of its purpose: the vendor's machine name plus the cookie name, a `*` read
158
+ * as `wildcard` — `google_analytics_ga`, `google_analytics_ga_wildcard` for `_ga_*`.
159
+ */
160
+ readonly machine_name: string;
161
+ /** The translation key of its type, e.g. `consent_cookie_type_http` (the default type). */
162
+ readonly type_machine_name: string;
163
+ /** `third` when it has a `domain`, else `first`. */
164
+ readonly party: ConsentCookieParty;
165
+ /** The translation key of its party: `consent_cookie_party_first` / `consent_cookie_party_third`. */
166
+ readonly party_machine_name: string;
167
+ /**
168
+ * The translation key of a worded lifetime (`consent_max_age_session`,
169
+ * `consent_max_age_persistent`); absent for a duration, which `formatLocale` formats itself.
170
+ */
171
+ readonly max_age_machine_name?: string;
172
+ }
173
+ /** The serialized form of a decision, as kept in the store. Fields are snake_case like every dataset. */
174
+ interface StoredConsent {
175
+ /** Random id for this visitor's consent — reused across later changes; quote it in a consent log. */
176
+ consent_id: string;
177
+ /** The `policyVersion` the visitor agreed to. A different version invalidates the decision. */
178
+ policy_version: string;
179
+ /** The ids of every granted category (required ones included). */
180
+ granted: string[];
181
+ /** When the decision was made, in epoch milliseconds. */
182
+ decided_at: number;
183
+ }
184
+ /**
185
+ * - `pending` — no valid decision: none stored, or it's for another policy version, or it expired.
186
+ * Only required categories count as granted; a banner should ask.
187
+ * - `decided` — a valid decision is stored.
188
+ * - `unknown` — not readable yet: the server render (and hydration pass) in React, where storage is
189
+ * out of reach. Treated like `pending` for gating, but a banner must not show.
190
+ */
191
+ type ConsentStatus = 'unknown' | 'pending' | 'decided';
192
+ /** The current consent state — referentially stable between changes (safe for `useSyncExternalStore`). */
193
+ interface ConsentSnapshot {
194
+ readonly status: ConsentStatus;
195
+ /** The valid decision, or `null` while `pending` / `unknown`. */
196
+ readonly decision: ConsentDecision | null;
197
+ }
198
+ /**
199
+ * Per category, the vendors the app uses there. A vendor listed under a category brings only its
200
+ * cookies of that category — plus its necessary cookies, which are always allowed. So
201
+ * `preferences: [youtube()]` is the YouTube embed without its marketing cookies; list it under
202
+ * `marketing` too to declare those.
203
+ */
204
+ type ConsentCategoriesConfig = Partial<Record<ConsentCategoryId, readonly ConsentVendorDeclaration[]>>;
205
+ interface ConsentConfig {
206
+ /**
207
+ * Per category, the vendors used in it — usually presets from `@luwiostack/cookie-consent/vendors`.
208
+ * Default: none.
209
+ */
210
+ categories?: ConsentCategoriesConfig;
211
+ /**
212
+ * The version of your cookie policy. Bump it when the policy changes materially (new vendors, new
213
+ * purposes) and every stored decision for an older version becomes `pending` again. Default `1`.
214
+ */
215
+ policyVersion?: string | number;
216
+ /**
217
+ * How long a decision stays valid, in milliseconds; after that the visitor is asked again.
218
+ * Default: 365 days. Pass `Number.POSITIVE_INFINITY` to never expire.
219
+ */
220
+ maxAge?: number;
221
+ /**
222
+ * Where the decision is kept. Default: `localStore('luwio.consent', { onError: 'ignore' })` from
223
+ * `@luwiostack/storage` (persists across tabs and restarts, synced between tabs; where storage is
224
+ * blocked or full the decision just isn't kept). A store you pass in fails per its own `onError`
225
+ * — by default a failed write throws out of `acceptAll()` & co. Pass `memoryStore()` in tests.
226
+ */
227
+ store?: ObservableStore<StoredConsent>;
228
+ /** The current time in epoch milliseconds. Default `Date.now` — inject for tests. */
229
+ now?: () => number;
230
+ /**
231
+ * Called after every decision made **through this instance** (accept, reject, save, grant, revoke,
232
+ * reset) — not for changes arriving from another tab. The place to log consent server-side.
233
+ */
234
+ onChange?: (next: ConsentDecision | null, previous: ConsentDecision | null) => void;
235
+ }
236
+ /** A consent instance — created once with `createConsent(config)` and handed to `<Consent>`. */
237
+ interface ConsentManager {
238
+ /** The four categories in order, each with the vendors (and cookies) configured in it. */
239
+ readonly categories: readonly ConsentCategory[];
240
+ /** Every vendor in use, once, with only the cookies its categories bring in. */
241
+ readonly vendors: readonly ConsentVendor[];
242
+ /** The configured policy version, as a string. */
243
+ readonly policyVersion: string;
244
+ /** The current state. Never `unknown` — that status is only produced by the React server snapshot. */
245
+ getSnapshot(): ConsentSnapshot;
246
+ /** Subscribe to changes (this tab and, for `localStorage`, others); returns an unsubscribe. */
247
+ subscribe(onChange: () => void): () => void;
248
+ /**
249
+ * Whether a category is granted now — `necessary` always is, others only once decided. With a
250
+ * vendor, also whether that vendor is used in this category: `isGranted('preferences', 'youtube')`
251
+ * is the YouTube embed, `isGranted('marketing', 'youtube')` its tracking. `false` for a vendor
252
+ * that isn't configured in the category.
253
+ */
254
+ isGranted(category: ConsentCategoryId, vendorId?: string): boolean;
255
+ /** Whether a category is required (always granted) — `necessary`. */
256
+ isRequired(category: ConsentCategoryId): boolean;
257
+ /** Grant every category. */
258
+ acceptAll(): ConsentDecision;
259
+ /** Grant only `necessary`. */
260
+ rejectAll(): ConsentDecision;
261
+ /** Grant exactly these categories (`necessary` is always added; unknown ids are ignored). */
262
+ save(granted: Iterable<ConsentCategoryId>): ConsentDecision;
263
+ /** Grant these categories on top of the current decision (or on top of `necessary` while pending). */
264
+ grant(...ids: ConsentCategoryId[]): ConsentDecision;
265
+ /** Withdraw these categories from the current decision. `necessary` can't be withdrawn. */
266
+ revoke(...ids: ConsentCategoryId[]): ConsentDecision;
267
+ /** Forget the decision — the status goes back to `pending`. */
268
+ reset(): void;
269
+ }
270
+ //#endregion
271
+ export { REQUIRED_CATEGORY as C, ConsentCategoryId as S, ConsentVendorDeclaration as _, ConsentCookieDeclaration as a, ConsentDecisionInput as b, ConsentCookieType as c, ConsentSnapshot as d, ConsentStatus as f, ConsentVendor as g, ConsentTransferSafeguard as h, ConsentCookie as i, ConsentCookieWordedMaxAge as l, ConsentTransferDeclaration as m, ConsentCategory as n, ConsentCookieMaxAge as o, ConsentTransfer as p, ConsentConfig as r, ConsentCookieParty as s, ConsentCategoriesConfig as t, ConsentManager as u, StoredConsent as v, isConsentCategoryId as w, CONSENT_CATEGORIES as x, ConsentDecision as y };
272
+ //# sourceMappingURL=types-ByyibBUg.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types-ByyibBUg.d.mts","names":[],"sources":["../src/categories.ts","../src/domain/consent-decision.ts","../src/types.ts"],"mappings":";;;;;;;;cAMa;;KAGD,4BAA4B;;cAG3B;;iBAGG,oBAAoB,iBAAiB,SAAS;;;UCX7C;EACf;EACA;EACA,SAAS,SAAS;EAClB;;;;;;;;;cAUW;;WACF;WACA;;WAEA;UAGF;;SAQA,IAAI,OAAO,uBAAuB;;SAclC,OAAO,OAAO,uBAAuB;;EAS5C,UAAU,IAAI;;EAKd,WAAW;;EAKX,OAAO,OAAO;;EAcd,UAAU;;;;;;;;;;UCtEK;WACN,IAAI;;WAEJ;;WAEA;;WAEA;;WAEA,kBAAkB;;;;;;;UAQZ;;;;;WAKN;;WAEA;;;;;WAKA,qBAAqB;;WAErB,kBAAkB;;;;;;UAOZ,sBAAsB;;WAE5B;WACA,qBAAqB;WACrB,kBAAkB;;;;;;;KAQjB;;UAGK;;WAEN;;WAEA,qBAAqB;;;UAIf,wBAAwB;;WAE9B;;;KAIC;;KAGA;;;;;KAWA,+BAA+B;;KAG/B;;;;;UAMK;;WAEN;;WAEA,UAAU;;WAEV,OAAO;;WAEP,SAAS;;;;;WAKT;;;;;;;;UASM,sBAAsB;;;;;WAK5B;;WAEA;;WAEA,OAAO;;WAEP;;;;;WAKA;;;UAIM;;EAEf;;EAEA;;EAEA;;EAEA;;;;;;;;;KAUU;;UAGK;WACN,QAAQ;;WAER,UAAU;;;;;;;;KAST,0BAA0B,QACpC,OAAO,4BAA4B;UAGpB;;;;;EAKf,aAAa;;;;;EAKb;;;;;EAKA;;;;;;;EAOA,QAAQ,gBAAgB;;EAExB;;;;;EAKA,YAAY,MAAM,wBAAwB,UAAU;;;UAIrC;;WAEN,qBAAqB;;WAErB,kBAAkB;;WAElB;;EAET,eAAe;;EAEf,UAAU;;;;;;;EAOV,UAAU,UAAU,mBAAmB;;EAEvC,WAAW,UAAU;;EAErB,aAAa;;EAEb,aAAa;;EAEb,KAAK,SAAS,SAAS,qBAAqB;;EAE5C,SAAS,KAAK,sBAAsB;;EAEpC,UAAU,KAAK,sBAAsB;;EAErC"}
@@ -0,0 +1,14 @@
1
+ //#region src/used-vendors.ts
2
+ /** @internal Per category, the ids of the vendors used in it — for `isGranted(category, vendor)`. */
3
+ function usedVendors(categories) {
4
+ return Object.fromEntries(categories.map((c) => [c.id, new Set(c.vendors.map((v) => v.id))]));
5
+ }
6
+ //#endregion
7
+ Object.defineProperty(exports, "usedVendors", {
8
+ enumerable: true,
9
+ get: function() {
10
+ return usedVendors;
11
+ }
12
+ });
13
+
14
+ //# sourceMappingURL=used-vendors-C8uSNIkd.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"used-vendors-C8uSNIkd.cjs","names":[],"sources":["../src/used-vendors.ts"],"sourcesContent":["import type { ConsentCategoryId } from './categories'\nimport type { ConsentCategory } from './types'\n\n/** @internal Per category, the ids of the vendors used in it — for `isGranted(category, vendor)`. */\nexport function usedVendors(\n categories: readonly ConsentCategory[],\n): Record<ConsentCategoryId, ReadonlySet<string>> {\n return Object.fromEntries(\n categories.map((c) => [c.id, new Set(c.vendors.map((v) => v.id))]),\n ) as unknown as Record<ConsentCategoryId, ReadonlySet<string>>\n}\n"],"mappings":";;AAIA,SAAgB,YACd,YACgD;CAChD,OAAO,OAAO,YACZ,WAAW,KAAK,MAAM,CAAC,EAAE,IAAI,IAAI,IAAI,EAAE,QAAQ,KAAK,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,CACnE;AACF"}
@@ -0,0 +1,9 @@
1
+ //#region src/used-vendors.ts
2
+ /** @internal Per category, the ids of the vendors used in it — for `isGranted(category, vendor)`. */
3
+ function usedVendors(categories) {
4
+ return Object.fromEntries(categories.map((c) => [c.id, new Set(c.vendors.map((v) => v.id))]));
5
+ }
6
+ //#endregion
7
+ export { usedVendors as t };
8
+
9
+ //# sourceMappingURL=used-vendors-Dxk4PZtY.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"used-vendors-Dxk4PZtY.mjs","names":[],"sources":["../src/used-vendors.ts"],"sourcesContent":["import type { ConsentCategoryId } from './categories'\nimport type { ConsentCategory } from './types'\n\n/** @internal Per category, the ids of the vendors used in it — for `isGranted(category, vendor)`. */\nexport function usedVendors(\n categories: readonly ConsentCategory[],\n): Record<ConsentCategoryId, ReadonlySet<string>> {\n return Object.fromEntries(\n categories.map((c) => [c.id, new Set(c.vendors.map((v) => v.id))]),\n ) as unknown as Record<ConsentCategoryId, ReadonlySet<string>>\n}\n"],"mappings":";;AAIA,SAAgB,YACd,YACgD;CAChD,OAAO,OAAO,YACZ,WAAW,KAAK,MAAM,CAAC,EAAE,IAAI,IAAI,IAAI,EAAE,QAAQ,KAAK,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,CACnE;AACF"}