@luwiostack/cookie-consent 0.0.0-stage → 0.1.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.
- package/README.md +292 -2
- package/dist/errors-BqpghFQf.cjs +55 -0
- package/dist/errors-BqpghFQf.cjs.map +1 -0
- package/dist/errors-D2ZIcEYK.mjs +32 -0
- package/dist/errors-D2ZIcEYK.mjs.map +1 -0
- package/dist/index.cjs +571 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +163 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +163 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +558 -0
- package/dist/index.mjs.map +1 -0
- package/dist/react.cjs +124 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +109 -0
- package/dist/react.d.cts.map +1 -0
- package/dist/react.d.mts +109 -0
- package/dist/react.d.mts.map +1 -0
- package/dist/react.mjs +120 -0
- package/dist/react.mjs.map +1 -0
- package/dist/types-BrxRJL4I.d.cts +198 -0
- package/dist/types-BrxRJL4I.d.cts.map +1 -0
- package/dist/types-BrxRJL4I.d.mts +198 -0
- package/dist/types-BrxRJL4I.d.mts.map +1 -0
- package/dist/used-vendors-C8uSNIkd.cjs +14 -0
- package/dist/used-vendors-C8uSNIkd.cjs.map +1 -0
- package/dist/used-vendors-Dxk4PZtY.mjs +9 -0
- package/dist/used-vendors-Dxk4PZtY.mjs.map +1 -0
- package/dist/vendors.cjs +449 -0
- package/dist/vendors.cjs.map +1 -0
- package/dist/vendors.d.cts +163 -0
- package/dist/vendors.d.cts.map +1 -0
- package/dist/vendors.d.mts +163 -0
- package/dist/vendors.d.mts.map +1 -0
- package/dist/vendors.mjs +436 -0
- package/dist/vendors.mjs.map +1 -0
- package/package.json +90 -3
|
@@ -0,0 +1,198 @@
|
|
|
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. Each id is also the translation key of
|
|
6
|
+
* the category's name (`category.<id>`) and description (`category.<id>.description`) in the
|
|
7
|
+
* 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.${id}\`)`).
|
|
57
|
+
*/
|
|
58
|
+
interface ConsentCategory {
|
|
59
|
+
readonly id: ConsentCategoryId;
|
|
60
|
+
/** `true` for `necessary` only: always granted, can't be switched off. */
|
|
61
|
+
readonly required: boolean;
|
|
62
|
+
/** The vendors with cookies in this category, their cookies narrowed to it. May be empty. */
|
|
63
|
+
readonly vendors: readonly ConsentVendor[];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A partner that sets cookies — your own site, Google, Meta, YouTube. Carries no display text:
|
|
67
|
+
* render its name with `t(vendor.id)`. One vendor may set cookies in several categories.
|
|
68
|
+
*/
|
|
69
|
+
interface ConsentVendor {
|
|
70
|
+
/** Stable key — also the translation key for the vendor's name. */
|
|
71
|
+
readonly id: string;
|
|
72
|
+
/** Where the vendor explains what it does with the data. */
|
|
73
|
+
readonly privacy_policy_url?: string;
|
|
74
|
+
/** Every cookie (or storage item) this vendor sets, each with its category. */
|
|
75
|
+
readonly cookies: readonly ConsentCookie[];
|
|
76
|
+
}
|
|
77
|
+
/** Where a cookie lives. `http` is a real cookie; the others are browser storage or a tracking pixel. */
|
|
78
|
+
type ConsentCookieType = 'http' | 'local_storage' | 'session_storage' | 'indexed_db' | 'pixel';
|
|
79
|
+
/**
|
|
80
|
+
* How long a cookie lives: milliseconds (like `maxAge`), `'session'` (gone when the browser closes)
|
|
81
|
+
* or `'persistent'` (no expiry — e.g. a `localStorage` item).
|
|
82
|
+
*/
|
|
83
|
+
type ConsentCookieMaxAge = number | 'session' | 'persistent';
|
|
84
|
+
/**
|
|
85
|
+
* One cookie a vendor sets, as declared to the visitor. `name` is the real cookie name (`_ga`,
|
|
86
|
+
* `_fbp`) — a technical identifier, not display text. The explanation of its purpose is yours to
|
|
87
|
+
* translate, keyed however you like (e.g. `t(\`${vendor.id}.${cookie.name}\`)`).
|
|
88
|
+
*/
|
|
89
|
+
interface ConsentCookie {
|
|
90
|
+
/** The cookie's actual name, e.g. `_ga`. Unique within its vendor. */
|
|
91
|
+
readonly name: string;
|
|
92
|
+
/** What it's for — the category the visitor must grant before it may be set. */
|
|
93
|
+
readonly category: ConsentCategoryId;
|
|
94
|
+
/** Default `'http'`. */
|
|
95
|
+
readonly type?: ConsentCookieType;
|
|
96
|
+
/** Its lifetime. Format it with `ConsentCookieFormatter.formatLocale`. */
|
|
97
|
+
readonly max_age: ConsentCookieMaxAge;
|
|
98
|
+
}
|
|
99
|
+
/** The serialized form of a decision, as kept in the store. Fields are snake_case like every dataset. */
|
|
100
|
+
interface StoredConsent {
|
|
101
|
+
/** Random id for this visitor's consent — reused across later changes; quote it in a consent log. */
|
|
102
|
+
consent_id: string;
|
|
103
|
+
/** The `policyVersion` the visitor agreed to. A different version invalidates the decision. */
|
|
104
|
+
policy_version: string;
|
|
105
|
+
/** The ids of every granted category (required ones included). */
|
|
106
|
+
granted: string[];
|
|
107
|
+
/** When the decision was made, in epoch milliseconds. */
|
|
108
|
+
decided_at: number;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* - `pending` — no valid decision: none stored, or it's for another policy version, or it expired.
|
|
112
|
+
* Only required categories count as granted; a banner should ask.
|
|
113
|
+
* - `decided` — a valid decision is stored.
|
|
114
|
+
* - `unknown` — not readable yet: the server render (and hydration pass) in React, where storage is
|
|
115
|
+
* out of reach. Treated like `pending` for gating, but a banner must not show.
|
|
116
|
+
*/
|
|
117
|
+
type ConsentStatus = 'unknown' | 'pending' | 'decided';
|
|
118
|
+
/** The current consent state — referentially stable between changes (safe for `useSyncExternalStore`). */
|
|
119
|
+
interface ConsentSnapshot {
|
|
120
|
+
readonly status: ConsentStatus;
|
|
121
|
+
/** The valid decision, or `null` while `pending` / `unknown`. */
|
|
122
|
+
readonly decision: ConsentDecision | null;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Per category, the vendors the app uses there. A vendor listed under a category brings only its
|
|
126
|
+
* cookies of that category — plus its necessary cookies, which are always allowed. So
|
|
127
|
+
* `preferences: [youtube()]` is the YouTube embed without its marketing cookies; list it under
|
|
128
|
+
* `marketing` too to declare those.
|
|
129
|
+
*/
|
|
130
|
+
type ConsentCategoriesConfig = Partial<Record<ConsentCategoryId, readonly ConsentVendor[]>>;
|
|
131
|
+
interface ConsentConfig {
|
|
132
|
+
/**
|
|
133
|
+
* Per category, the vendors used in it — usually presets from `@luwiostack/cookie-consent/vendors`.
|
|
134
|
+
* Default: none.
|
|
135
|
+
*/
|
|
136
|
+
categories?: ConsentCategoriesConfig;
|
|
137
|
+
/**
|
|
138
|
+
* The version of your cookie policy. Bump it when the policy changes materially (new vendors, new
|
|
139
|
+
* purposes) and every stored decision for an older version becomes `pending` again. Default `1`.
|
|
140
|
+
*/
|
|
141
|
+
policyVersion?: string | number;
|
|
142
|
+
/**
|
|
143
|
+
* How long a decision stays valid, in milliseconds; after that the visitor is asked again.
|
|
144
|
+
* Default: 365 days. Pass `Number.POSITIVE_INFINITY` to never expire.
|
|
145
|
+
*/
|
|
146
|
+
maxAge?: number;
|
|
147
|
+
/**
|
|
148
|
+
* Where the decision is kept. Default: `localStore('luwio.consent', { onError: 'ignore' })` from
|
|
149
|
+
* `@luwiostack/storage` (persists across tabs and restarts, synced between tabs; where storage is
|
|
150
|
+
* blocked or full the decision just isn't kept). A store you pass in fails per its own `onError`
|
|
151
|
+
* — by default a failed write throws out of `acceptAll()` & co. Pass `memoryStore()` in tests.
|
|
152
|
+
*/
|
|
153
|
+
store?: ObservableStore<StoredConsent>;
|
|
154
|
+
/** The current time in epoch milliseconds. Default `Date.now` — inject for tests. */
|
|
155
|
+
now?: () => number;
|
|
156
|
+
/**
|
|
157
|
+
* Called after every decision made **through this instance** (accept, reject, save, grant, revoke,
|
|
158
|
+
* reset) — not for changes arriving from another tab. The place to log consent server-side.
|
|
159
|
+
*/
|
|
160
|
+
onChange?: (next: ConsentDecision | null, previous: ConsentDecision | null) => void;
|
|
161
|
+
}
|
|
162
|
+
/** A consent instance — created once with `createConsent(config)` and handed to `<Consent>`. */
|
|
163
|
+
interface ConsentManager {
|
|
164
|
+
/** The four categories in order, each with the vendors (and cookies) configured in it. */
|
|
165
|
+
readonly categories: readonly ConsentCategory[];
|
|
166
|
+
/** Every vendor in use, once, with only the cookies its categories bring in. */
|
|
167
|
+
readonly vendors: readonly ConsentVendor[];
|
|
168
|
+
/** The configured policy version, as a string. */
|
|
169
|
+
readonly policyVersion: string;
|
|
170
|
+
/** The current state. Never `unknown` — that status is only produced by the React server snapshot. */
|
|
171
|
+
getSnapshot(): ConsentSnapshot;
|
|
172
|
+
/** Subscribe to changes (this tab and, for `localStorage`, others); returns an unsubscribe. */
|
|
173
|
+
subscribe(onChange: () => void): () => void;
|
|
174
|
+
/**
|
|
175
|
+
* Whether a category is granted now — `necessary` always is, others only once decided. With a
|
|
176
|
+
* vendor, also whether that vendor is used in this category: `isGranted('preferences', 'youtube')`
|
|
177
|
+
* is the YouTube embed, `isGranted('marketing', 'youtube')` its tracking. `false` for a vendor
|
|
178
|
+
* that isn't configured in the category.
|
|
179
|
+
*/
|
|
180
|
+
isGranted(category: ConsentCategoryId, vendorId?: string): boolean;
|
|
181
|
+
/** Whether a category is required (always granted) — `necessary`. */
|
|
182
|
+
isRequired(category: ConsentCategoryId): boolean;
|
|
183
|
+
/** Grant every category. */
|
|
184
|
+
acceptAll(): ConsentDecision;
|
|
185
|
+
/** Grant only `necessary`. */
|
|
186
|
+
rejectAll(): ConsentDecision;
|
|
187
|
+
/** Grant exactly these categories (`necessary` is always added; unknown ids are ignored). */
|
|
188
|
+
save(granted: Iterable<ConsentCategoryId>): ConsentDecision;
|
|
189
|
+
/** Grant these categories on top of the current decision (or on top of `necessary` while pending). */
|
|
190
|
+
grant(...ids: ConsentCategoryId[]): ConsentDecision;
|
|
191
|
+
/** Withdraw these categories from the current decision. `necessary` can't be withdrawn. */
|
|
192
|
+
revoke(...ids: ConsentCategoryId[]): ConsentDecision;
|
|
193
|
+
/** Forget the decision — the status goes back to `pending`. */
|
|
194
|
+
reset(): void;
|
|
195
|
+
}
|
|
196
|
+
//#endregion
|
|
197
|
+
export { isConsentCategoryId as _, ConsentCookieMaxAge as a, ConsentSnapshot as c, StoredConsent as d, ConsentDecision as f, REQUIRED_CATEGORY as g, ConsentCategoryId as h, ConsentCookie as i, ConsentStatus as l, CONSENT_CATEGORIES as m, ConsentCategory as n, ConsentCookieType as o, ConsentDecisionInput as p, ConsentConfig as r, ConsentManager as s, ConsentCategoriesConfig as t, ConsentVendor as u };
|
|
198
|
+
//# sourceMappingURL=types-BrxRJL4I.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-BrxRJL4I.d.cts","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;;;;;;;;;UCvEK;WACN,IAAI;;WAEJ;;WAEA,kBAAkB;;;;;;UAOZ;;WAEN;;WAEA;;WAEA,kBAAkB;;;KAIjB;;;;;KAWA;;;;;;UAOK;;WAEN;;WAEA,UAAU;;WAEV,OAAO;;WAEP,SAAS;;;UAIH;;EAEf;;EAEA;;EAEA;;EAEA;;;;;;;;;KAUU;;UAGK;WACN,QAAQ;;WAER,UAAU;;;;;;;;KAST,0BAA0B,QAAQ,OAAO,4BAA4B;UAEhE;;;;;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,198 @@
|
|
|
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. Each id is also the translation key of
|
|
6
|
+
* the category's name (`category.<id>`) and description (`category.<id>.description`) in the
|
|
7
|
+
* 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.${id}\`)`).
|
|
57
|
+
*/
|
|
58
|
+
interface ConsentCategory {
|
|
59
|
+
readonly id: ConsentCategoryId;
|
|
60
|
+
/** `true` for `necessary` only: always granted, can't be switched off. */
|
|
61
|
+
readonly required: boolean;
|
|
62
|
+
/** The vendors with cookies in this category, their cookies narrowed to it. May be empty. */
|
|
63
|
+
readonly vendors: readonly ConsentVendor[];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A partner that sets cookies — your own site, Google, Meta, YouTube. Carries no display text:
|
|
67
|
+
* render its name with `t(vendor.id)`. One vendor may set cookies in several categories.
|
|
68
|
+
*/
|
|
69
|
+
interface ConsentVendor {
|
|
70
|
+
/** Stable key — also the translation key for the vendor's name. */
|
|
71
|
+
readonly id: string;
|
|
72
|
+
/** Where the vendor explains what it does with the data. */
|
|
73
|
+
readonly privacy_policy_url?: string;
|
|
74
|
+
/** Every cookie (or storage item) this vendor sets, each with its category. */
|
|
75
|
+
readonly cookies: readonly ConsentCookie[];
|
|
76
|
+
}
|
|
77
|
+
/** Where a cookie lives. `http` is a real cookie; the others are browser storage or a tracking pixel. */
|
|
78
|
+
type ConsentCookieType = 'http' | 'local_storage' | 'session_storage' | 'indexed_db' | 'pixel';
|
|
79
|
+
/**
|
|
80
|
+
* How long a cookie lives: milliseconds (like `maxAge`), `'session'` (gone when the browser closes)
|
|
81
|
+
* or `'persistent'` (no expiry — e.g. a `localStorage` item).
|
|
82
|
+
*/
|
|
83
|
+
type ConsentCookieMaxAge = number | 'session' | 'persistent';
|
|
84
|
+
/**
|
|
85
|
+
* One cookie a vendor sets, as declared to the visitor. `name` is the real cookie name (`_ga`,
|
|
86
|
+
* `_fbp`) — a technical identifier, not display text. The explanation of its purpose is yours to
|
|
87
|
+
* translate, keyed however you like (e.g. `t(\`${vendor.id}.${cookie.name}\`)`).
|
|
88
|
+
*/
|
|
89
|
+
interface ConsentCookie {
|
|
90
|
+
/** The cookie's actual name, e.g. `_ga`. Unique within its vendor. */
|
|
91
|
+
readonly name: string;
|
|
92
|
+
/** What it's for — the category the visitor must grant before it may be set. */
|
|
93
|
+
readonly category: ConsentCategoryId;
|
|
94
|
+
/** Default `'http'`. */
|
|
95
|
+
readonly type?: ConsentCookieType;
|
|
96
|
+
/** Its lifetime. Format it with `ConsentCookieFormatter.formatLocale`. */
|
|
97
|
+
readonly max_age: ConsentCookieMaxAge;
|
|
98
|
+
}
|
|
99
|
+
/** The serialized form of a decision, as kept in the store. Fields are snake_case like every dataset. */
|
|
100
|
+
interface StoredConsent {
|
|
101
|
+
/** Random id for this visitor's consent — reused across later changes; quote it in a consent log. */
|
|
102
|
+
consent_id: string;
|
|
103
|
+
/** The `policyVersion` the visitor agreed to. A different version invalidates the decision. */
|
|
104
|
+
policy_version: string;
|
|
105
|
+
/** The ids of every granted category (required ones included). */
|
|
106
|
+
granted: string[];
|
|
107
|
+
/** When the decision was made, in epoch milliseconds. */
|
|
108
|
+
decided_at: number;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* - `pending` — no valid decision: none stored, or it's for another policy version, or it expired.
|
|
112
|
+
* Only required categories count as granted; a banner should ask.
|
|
113
|
+
* - `decided` — a valid decision is stored.
|
|
114
|
+
* - `unknown` — not readable yet: the server render (and hydration pass) in React, where storage is
|
|
115
|
+
* out of reach. Treated like `pending` for gating, but a banner must not show.
|
|
116
|
+
*/
|
|
117
|
+
type ConsentStatus = 'unknown' | 'pending' | 'decided';
|
|
118
|
+
/** The current consent state — referentially stable between changes (safe for `useSyncExternalStore`). */
|
|
119
|
+
interface ConsentSnapshot {
|
|
120
|
+
readonly status: ConsentStatus;
|
|
121
|
+
/** The valid decision, or `null` while `pending` / `unknown`. */
|
|
122
|
+
readonly decision: ConsentDecision | null;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Per category, the vendors the app uses there. A vendor listed under a category brings only its
|
|
126
|
+
* cookies of that category — plus its necessary cookies, which are always allowed. So
|
|
127
|
+
* `preferences: [youtube()]` is the YouTube embed without its marketing cookies; list it under
|
|
128
|
+
* `marketing` too to declare those.
|
|
129
|
+
*/
|
|
130
|
+
type ConsentCategoriesConfig = Partial<Record<ConsentCategoryId, readonly ConsentVendor[]>>;
|
|
131
|
+
interface ConsentConfig {
|
|
132
|
+
/**
|
|
133
|
+
* Per category, the vendors used in it — usually presets from `@luwiostack/cookie-consent/vendors`.
|
|
134
|
+
* Default: none.
|
|
135
|
+
*/
|
|
136
|
+
categories?: ConsentCategoriesConfig;
|
|
137
|
+
/**
|
|
138
|
+
* The version of your cookie policy. Bump it when the policy changes materially (new vendors, new
|
|
139
|
+
* purposes) and every stored decision for an older version becomes `pending` again. Default `1`.
|
|
140
|
+
*/
|
|
141
|
+
policyVersion?: string | number;
|
|
142
|
+
/**
|
|
143
|
+
* How long a decision stays valid, in milliseconds; after that the visitor is asked again.
|
|
144
|
+
* Default: 365 days. Pass `Number.POSITIVE_INFINITY` to never expire.
|
|
145
|
+
*/
|
|
146
|
+
maxAge?: number;
|
|
147
|
+
/**
|
|
148
|
+
* Where the decision is kept. Default: `localStore('luwio.consent', { onError: 'ignore' })` from
|
|
149
|
+
* `@luwiostack/storage` (persists across tabs and restarts, synced between tabs; where storage is
|
|
150
|
+
* blocked or full the decision just isn't kept). A store you pass in fails per its own `onError`
|
|
151
|
+
* — by default a failed write throws out of `acceptAll()` & co. Pass `memoryStore()` in tests.
|
|
152
|
+
*/
|
|
153
|
+
store?: ObservableStore<StoredConsent>;
|
|
154
|
+
/** The current time in epoch milliseconds. Default `Date.now` — inject for tests. */
|
|
155
|
+
now?: () => number;
|
|
156
|
+
/**
|
|
157
|
+
* Called after every decision made **through this instance** (accept, reject, save, grant, revoke,
|
|
158
|
+
* reset) — not for changes arriving from another tab. The place to log consent server-side.
|
|
159
|
+
*/
|
|
160
|
+
onChange?: (next: ConsentDecision | null, previous: ConsentDecision | null) => void;
|
|
161
|
+
}
|
|
162
|
+
/** A consent instance — created once with `createConsent(config)` and handed to `<Consent>`. */
|
|
163
|
+
interface ConsentManager {
|
|
164
|
+
/** The four categories in order, each with the vendors (and cookies) configured in it. */
|
|
165
|
+
readonly categories: readonly ConsentCategory[];
|
|
166
|
+
/** Every vendor in use, once, with only the cookies its categories bring in. */
|
|
167
|
+
readonly vendors: readonly ConsentVendor[];
|
|
168
|
+
/** The configured policy version, as a string. */
|
|
169
|
+
readonly policyVersion: string;
|
|
170
|
+
/** The current state. Never `unknown` — that status is only produced by the React server snapshot. */
|
|
171
|
+
getSnapshot(): ConsentSnapshot;
|
|
172
|
+
/** Subscribe to changes (this tab and, for `localStorage`, others); returns an unsubscribe. */
|
|
173
|
+
subscribe(onChange: () => void): () => void;
|
|
174
|
+
/**
|
|
175
|
+
* Whether a category is granted now — `necessary` always is, others only once decided. With a
|
|
176
|
+
* vendor, also whether that vendor is used in this category: `isGranted('preferences', 'youtube')`
|
|
177
|
+
* is the YouTube embed, `isGranted('marketing', 'youtube')` its tracking. `false` for a vendor
|
|
178
|
+
* that isn't configured in the category.
|
|
179
|
+
*/
|
|
180
|
+
isGranted(category: ConsentCategoryId, vendorId?: string): boolean;
|
|
181
|
+
/** Whether a category is required (always granted) — `necessary`. */
|
|
182
|
+
isRequired(category: ConsentCategoryId): boolean;
|
|
183
|
+
/** Grant every category. */
|
|
184
|
+
acceptAll(): ConsentDecision;
|
|
185
|
+
/** Grant only `necessary`. */
|
|
186
|
+
rejectAll(): ConsentDecision;
|
|
187
|
+
/** Grant exactly these categories (`necessary` is always added; unknown ids are ignored). */
|
|
188
|
+
save(granted: Iterable<ConsentCategoryId>): ConsentDecision;
|
|
189
|
+
/** Grant these categories on top of the current decision (or on top of `necessary` while pending). */
|
|
190
|
+
grant(...ids: ConsentCategoryId[]): ConsentDecision;
|
|
191
|
+
/** Withdraw these categories from the current decision. `necessary` can't be withdrawn. */
|
|
192
|
+
revoke(...ids: ConsentCategoryId[]): ConsentDecision;
|
|
193
|
+
/** Forget the decision — the status goes back to `pending`. */
|
|
194
|
+
reset(): void;
|
|
195
|
+
}
|
|
196
|
+
//#endregion
|
|
197
|
+
export { isConsentCategoryId as _, ConsentCookieMaxAge as a, ConsentSnapshot as c, StoredConsent as d, ConsentDecision as f, REQUIRED_CATEGORY as g, ConsentCategoryId as h, ConsentCookie as i, ConsentStatus as l, CONSENT_CATEGORIES as m, ConsentCategory as n, ConsentCookieType as o, ConsentDecisionInput as p, ConsentConfig as r, ConsentManager as s, ConsentCategoriesConfig as t, ConsentVendor as u };
|
|
198
|
+
//# sourceMappingURL=types-BrxRJL4I.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-BrxRJL4I.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;;;;;;;;;UCvEK;WACN,IAAI;;WAEJ;;WAEA,kBAAkB;;;;;;UAOZ;;WAEN;;WAEA;;WAEA,kBAAkB;;;KAIjB;;;;;KAWA;;;;;;UAOK;;WAEN;;WAEA,UAAU;;WAEV,OAAO;;WAEP,SAAS;;;UAIH;;EAEf;;EAEA;;EAEA;;EAEA;;;;;;;;;KAUU;;UAGK;WACN,QAAQ;;WAER,UAAU;;;;;;;;KAST,0BAA0B,QAAQ,OAAO,4BAA4B;UAEhE;;;;;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"}
|