@simmalugnt-se/payload-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/CHANGELOG.md +39 -0
- package/LICENSE +21 -0
- package/README.md +238 -2
- package/dist/admin/ScanPanel.d.ts +3 -0
- package/dist/admin/ScanPanel.js +232 -0
- package/dist/block.d.ts +6 -0
- package/dist/block.js +27 -0
- package/dist/defaults.d.ts +56 -0
- package/dist/defaults.js +137 -0
- package/dist/evaluate.d.ts +106 -0
- package/dist/evaluate.js +101 -0
- package/dist/evidence.d.ts +12 -0
- package/dist/evidence.js +113 -0
- package/dist/exports/admin.d.ts +1 -0
- package/dist/exports/admin.js +1 -0
- package/dist/exports/client.d.ts +37 -0
- package/dist/exports/client.js +177 -0
- package/dist/exports/rsc.d.ts +1 -0
- package/dist/exports/rsc.js +1 -0
- package/dist/global.d.ts +14 -0
- package/dist/global.js +272 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +75 -0
- package/dist/integrations.d.ts +40 -0
- package/dist/integrations.js +32 -0
- package/dist/notify.d.ts +15 -0
- package/dist/notify.js +86 -0
- package/dist/records.d.ts +15 -0
- package/dist/records.js +197 -0
- package/dist/scans.d.ts +37 -0
- package/dist/scans.js +423 -0
- package/dist/schedule.d.ts +17 -0
- package/dist/schedule.js +18 -0
- package/dist/services.d.ts +8 -0
- package/dist/services.js +257 -0
- package/dist/site-config.d.ts +128 -0
- package/dist/site-config.js +204 -0
- package/dist/styles/admin.css +144 -0
- package/dist/styles/consent.css +296 -0
- package/dist/types.d.ts +65 -0
- package/dist/types.js +17 -0
- package/dist/widget/ConsentScanWidget.d.ts +8 -0
- package/dist/widget/ConsentScanWidget.js +99 -0
- package/package.json +76 -3
package/dist/services.js
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { CATEGORIES, OPTIONAL_CATEGORIES } from "./defaults.js";
|
|
2
|
+
import { categoryLabels } from "./global.js";
|
|
3
|
+
import { INTEGRATIONS } from "./integrations.js";
|
|
4
|
+
import { CONSENT_GLOBAL, CONSENT_SERVICES } from "./types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Keeps the global's categories in step with the services, for the Banner tab's conditions. A bulk
|
|
7
|
+
* change runs the hooks for its documents at once, in one transaction: the writes take turns.
|
|
8
|
+
*/
|
|
9
|
+
export function syncCategories(req) {
|
|
10
|
+
const previous = req.context.consentCategorySync ?? Promise.resolve();
|
|
11
|
+
const next = previous.then(() => writeCategories(req));
|
|
12
|
+
req.context.consentCategorySync = next;
|
|
13
|
+
return next;
|
|
14
|
+
}
|
|
15
|
+
async function writeCategories(req) {
|
|
16
|
+
const { docs } = await req.payload.find({
|
|
17
|
+
collection: CONSENT_SERVICES,
|
|
18
|
+
depth: 0,
|
|
19
|
+
limit: 0,
|
|
20
|
+
pagination: false,
|
|
21
|
+
select: { category: true },
|
|
22
|
+
req,
|
|
23
|
+
});
|
|
24
|
+
const used = new Set(docs.map((doc) => doc.category));
|
|
25
|
+
const categories = OPTIONAL_CATEGORIES.filter((category) => used.has(category));
|
|
26
|
+
const global = (await req.payload.findGlobal({
|
|
27
|
+
slug: CONSENT_GLOBAL,
|
|
28
|
+
depth: 0,
|
|
29
|
+
req,
|
|
30
|
+
}));
|
|
31
|
+
if (JSON.stringify(global.categories ?? []) === JSON.stringify(categories))
|
|
32
|
+
return;
|
|
33
|
+
await req.payload.updateGlobal({
|
|
34
|
+
slug: CONSENT_GLOBAL,
|
|
35
|
+
data: { categories },
|
|
36
|
+
depth: 0,
|
|
37
|
+
req,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const afterChange = async ({ req, doc, previousDoc }) => {
|
|
41
|
+
if (doc.category !== previousDoc?.category)
|
|
42
|
+
await syncCategories(req);
|
|
43
|
+
return doc;
|
|
44
|
+
};
|
|
45
|
+
export function servicesCollection(options) {
|
|
46
|
+
const collection = {
|
|
47
|
+
slug: CONSENT_SERVICES,
|
|
48
|
+
labels: {
|
|
49
|
+
singular: { en: "Cookie service", sv: "Cookietjänst" },
|
|
50
|
+
plural: { en: "Cookie services", sv: "Cookietjänster" },
|
|
51
|
+
},
|
|
52
|
+
admin: {
|
|
53
|
+
useAsTitle: "name",
|
|
54
|
+
group: options.group ?? { en: "Settings", sv: "Inställningar" },
|
|
55
|
+
defaultColumns: ["name", "provider", "category", "loading"],
|
|
56
|
+
description: {
|
|
57
|
+
en: "Every service that stores something in the visitor's browser or that the site contacts. The banner's categories and the cookie table follow this list.",
|
|
58
|
+
sv: "Varje tjänst som sparar något i besökarens webbläsare eller som webbplatsen kontaktar. Bannerns kategorier och cookietabellen följer listan.",
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
access: { read: () => true, ...options.access },
|
|
62
|
+
hooks: {
|
|
63
|
+
afterChange: [afterChange],
|
|
64
|
+
afterDelete: [async ({ req }) => syncCategories(req)],
|
|
65
|
+
},
|
|
66
|
+
fields: [
|
|
67
|
+
{
|
|
68
|
+
type: "row",
|
|
69
|
+
fields: [
|
|
70
|
+
{
|
|
71
|
+
name: "name",
|
|
72
|
+
type: "text",
|
|
73
|
+
required: true,
|
|
74
|
+
label: { en: "Service", sv: "Tjänst" },
|
|
75
|
+
admin: { width: "50%", placeholder: "Google Analytics" },
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
name: "provider",
|
|
79
|
+
type: "text",
|
|
80
|
+
label: { en: "Provider", sv: "Leverantör" },
|
|
81
|
+
admin: { width: "50%", placeholder: "Google" },
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
name: "category",
|
|
87
|
+
type: "select",
|
|
88
|
+
required: true,
|
|
89
|
+
defaultValue: "necessary",
|
|
90
|
+
label: { en: "Category", sv: "Kategori" },
|
|
91
|
+
options: CATEGORIES.map((value) => ({ value, label: categoryLabels[value] })),
|
|
92
|
+
admin: {
|
|
93
|
+
description: {
|
|
94
|
+
en: "Necessary only when the site cannot work without it, or it stores the visitor's own choice.",
|
|
95
|
+
sv: "Nödvändig bara när webbplatsen inte fungerar utan tjänsten, eller när den sparar besökarens eget val.",
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: "purpose",
|
|
101
|
+
type: "textarea",
|
|
102
|
+
localized: true,
|
|
103
|
+
// Not required: a service saved in one language must stay editable in the others.
|
|
104
|
+
label: { en: "Purpose", sv: "Syfte" },
|
|
105
|
+
admin: {
|
|
106
|
+
description: {
|
|
107
|
+
en: "Why the site uses it, as visitors read it in the cookie table.",
|
|
108
|
+
sv: "Varför webbplatsen använder tjänsten, som besökaren läser det i cookietabellen.",
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
name: "policyUrl",
|
|
114
|
+
type: "text",
|
|
115
|
+
label: { en: "Provider's privacy policy", sv: "Leverantörens integritetspolicy" },
|
|
116
|
+
admin: { placeholder: "https://" },
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
name: "loading",
|
|
120
|
+
type: "select",
|
|
121
|
+
required: true,
|
|
122
|
+
defaultValue: "code",
|
|
123
|
+
label: { en: "Loaded by", sv: "Laddas av" },
|
|
124
|
+
options: [
|
|
125
|
+
{
|
|
126
|
+
value: "integration",
|
|
127
|
+
label: { en: "This plugin, after consent", sv: "Det här pluginet, efter samtycke" },
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
value: "code",
|
|
131
|
+
label: { en: "The site's code", sv: "Webbplatsens kod" },
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
admin: {
|
|
135
|
+
description: {
|
|
136
|
+
en: "Pick the plugin for the services in the list below: they then wait for consent. Anything else is loaded by the site's code, which must wait for consent itself.",
|
|
137
|
+
sv: "Välj pluginet för tjänsterna i listan nedan: de väntar då på samtycke. Allt annat laddas av webbplatsens kod, som själv måste vänta på samtycke.",
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
type: "row",
|
|
143
|
+
admin: { condition: (_, sibling) => sibling?.loading === "integration" },
|
|
144
|
+
fields: [
|
|
145
|
+
{
|
|
146
|
+
name: "integration",
|
|
147
|
+
type: "select",
|
|
148
|
+
label: { en: "Integration", sv: "Integration" },
|
|
149
|
+
options: INTEGRATIONS.map(({ key, label, idLabel }) => ({
|
|
150
|
+
value: key,
|
|
151
|
+
label: `${label} · ${idLabel}`,
|
|
152
|
+
})),
|
|
153
|
+
admin: { width: "50%" },
|
|
154
|
+
validate: (value, { siblingData }) => siblingData?.loading !== "integration" || Boolean(value) || "Pick an integration",
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
name: "integrationId",
|
|
158
|
+
type: "text",
|
|
159
|
+
label: { en: "ID", sv: "ID" },
|
|
160
|
+
admin: {
|
|
161
|
+
width: "50%",
|
|
162
|
+
description: {
|
|
163
|
+
en: "From the provider's settings, such as G-… for Google Analytics or GTM-… for Tag Manager.",
|
|
164
|
+
sv: "Från leverantörens inställningar, till exempel G-… för Google Analytics eller GTM-… för Tag Manager.",
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
validate: (value, { siblingData }) => siblingData?.loading !== "integration" ||
|
|
168
|
+
(typeof value === "string" && value.trim() !== "") ||
|
|
169
|
+
"Enter the integration's ID",
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
name: "storage",
|
|
175
|
+
type: "array",
|
|
176
|
+
label: { en: "Cookies and storage", sv: "Cookies och lagring" },
|
|
177
|
+
labels: {
|
|
178
|
+
singular: { en: "Cookie or key", sv: "Cookie eller nyckel" },
|
|
179
|
+
plural: { en: "Cookies and keys", sv: "Cookies och nycklar" },
|
|
180
|
+
},
|
|
181
|
+
admin: {
|
|
182
|
+
initCollapsed: true,
|
|
183
|
+
description: {
|
|
184
|
+
en: "What the service stores in the browser. A service that stores nothing, such as an embedded font, has none.",
|
|
185
|
+
sv: "Det tjänsten sparar i webbläsaren. En tjänst som inte sparar något, till exempel ett inbäddat typsnitt, har inga.",
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
fields: [
|
|
189
|
+
{
|
|
190
|
+
type: "row",
|
|
191
|
+
fields: [
|
|
192
|
+
{
|
|
193
|
+
name: "name",
|
|
194
|
+
type: "text",
|
|
195
|
+
required: true,
|
|
196
|
+
label: { en: "Name", sv: "Namn" },
|
|
197
|
+
admin: { width: "34%", placeholder: "_ga" },
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
name: "type",
|
|
201
|
+
type: "select",
|
|
202
|
+
required: true,
|
|
203
|
+
defaultValue: "cookie",
|
|
204
|
+
label: { en: "Type", sv: "Typ" },
|
|
205
|
+
options: [
|
|
206
|
+
{ value: "cookie", label: "Cookie" },
|
|
207
|
+
{ value: "localStorage", label: "localStorage" },
|
|
208
|
+
{ value: "sessionStorage", label: "sessionStorage" },
|
|
209
|
+
],
|
|
210
|
+
admin: { width: "22%" },
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
name: "duration",
|
|
214
|
+
type: "text",
|
|
215
|
+
localized: true,
|
|
216
|
+
label: { en: "Kept for", sv: "Sparas i" },
|
|
217
|
+
admin: { width: "22%", placeholder: "2 år" },
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
name: "thirdParty",
|
|
221
|
+
type: "checkbox",
|
|
222
|
+
label: { en: "Third party", sv: "Tredje part" },
|
|
223
|
+
admin: { width: "22%", style: { alignSelf: "center" } },
|
|
224
|
+
},
|
|
225
|
+
],
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
name: "description",
|
|
229
|
+
type: "text",
|
|
230
|
+
localized: true,
|
|
231
|
+
label: { en: "What it holds", sv: "Vad den innehåller" },
|
|
232
|
+
},
|
|
233
|
+
],
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
name: "hosts",
|
|
237
|
+
type: "array",
|
|
238
|
+
label: { en: "Hosts it contacts", sv: "Värdar den kontaktar" },
|
|
239
|
+
labels: {
|
|
240
|
+
singular: { en: "Host", sv: "Värd" },
|
|
241
|
+
plural: { en: "Hosts", sv: "Värdar" },
|
|
242
|
+
},
|
|
243
|
+
admin: { initCollapsed: true },
|
|
244
|
+
fields: [
|
|
245
|
+
{
|
|
246
|
+
name: "host",
|
|
247
|
+
type: "text",
|
|
248
|
+
required: true,
|
|
249
|
+
label: { en: "Host", sv: "Värd" },
|
|
250
|
+
admin: { placeholder: "www.googletagmanager.com" },
|
|
251
|
+
},
|
|
252
|
+
],
|
|
253
|
+
},
|
|
254
|
+
],
|
|
255
|
+
};
|
|
256
|
+
return options.overrideServices ? options.overrideServices(collection) : collection;
|
|
257
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { ConsentManagerOptions, Translations } from "@c15t/react";
|
|
2
|
+
import type { Payload } from "payload";
|
|
3
|
+
import { type Category, defaultTexts } from "./defaults.ts";
|
|
4
|
+
import { type IntegrationKey } from "./integrations.ts";
|
|
5
|
+
/** The Cookies global as read in one locale. Every text may be missing in an old document. */
|
|
6
|
+
export type ConsentDocument = {
|
|
7
|
+
enabled?: boolean | null;
|
|
8
|
+
categories?: string[] | null;
|
|
9
|
+
bannerTitle?: string | null;
|
|
10
|
+
bannerDescription?: string | null;
|
|
11
|
+
bannerDescriptionNecessaryOnly?: string | null;
|
|
12
|
+
buttons?: Partial<Record<string, string | null>> | null;
|
|
13
|
+
dialog?: Partial<Record<string, string | null>> | null;
|
|
14
|
+
categoryTexts?: Partial<Record<string, {
|
|
15
|
+
title?: string | null;
|
|
16
|
+
description?: string | null;
|
|
17
|
+
} | null>> | null;
|
|
18
|
+
embeds?: {
|
|
19
|
+
title?: string | null;
|
|
20
|
+
button?: string | null;
|
|
21
|
+
} | null;
|
|
22
|
+
googleConsentMode?: string | null;
|
|
23
|
+
privacyPolicy?: unknown;
|
|
24
|
+
cookiePolicy?: unknown;
|
|
25
|
+
};
|
|
26
|
+
/** A cookie service as read in one locale. */
|
|
27
|
+
export type ServiceDocument = {
|
|
28
|
+
name?: string | null;
|
|
29
|
+
provider?: string | null;
|
|
30
|
+
category?: string | null;
|
|
31
|
+
purpose?: string | null;
|
|
32
|
+
policyUrl?: string | null;
|
|
33
|
+
loading?: string | null;
|
|
34
|
+
integration?: string | null;
|
|
35
|
+
integrationId?: string | null;
|
|
36
|
+
storage?: {
|
|
37
|
+
name?: string | null;
|
|
38
|
+
type?: string | null;
|
|
39
|
+
duration?: string | null;
|
|
40
|
+
thirdParty?: boolean | null;
|
|
41
|
+
description?: string | null;
|
|
42
|
+
}[] | null;
|
|
43
|
+
hosts?: {
|
|
44
|
+
host?: string | null;
|
|
45
|
+
}[] | null;
|
|
46
|
+
};
|
|
47
|
+
/** What the banner needs, serializable: pass it from a server component to `ConsentProvider`. */
|
|
48
|
+
export type ConsentConfig = {
|
|
49
|
+
enabled: boolean;
|
|
50
|
+
locale: string;
|
|
51
|
+
/** Always starts with `necessary`. */
|
|
52
|
+
categories: Category[];
|
|
53
|
+
legalLinks?: NonNullable<ConsentManagerOptions["legalLinks"]>;
|
|
54
|
+
/** c15t's texts for `locale`. */
|
|
55
|
+
messages: Partial<Translations>;
|
|
56
|
+
/** Basic waits for consent; advanced lets tags that support it load at once in a denied state. */
|
|
57
|
+
googleConsentMode: "basic" | "advanced";
|
|
58
|
+
/** Services the plugin loads through c15t. */
|
|
59
|
+
integrations: {
|
|
60
|
+
key: IntegrationKey;
|
|
61
|
+
id: string;
|
|
62
|
+
category: Category;
|
|
63
|
+
}[];
|
|
64
|
+
/** The placeholder in place of embedded content; `{category}` becomes the category's title. */
|
|
65
|
+
embeds: {
|
|
66
|
+
title: string;
|
|
67
|
+
button: string;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Changes when the categories or the services' cookies change: visitors who chose under an older
|
|
71
|
+
* version are asked again.
|
|
72
|
+
*/
|
|
73
|
+
policyVersion: string;
|
|
74
|
+
/** Where the site posts each saved choice; absent when records are off. */
|
|
75
|
+
recordUrl?: string;
|
|
76
|
+
};
|
|
77
|
+
type Options = {
|
|
78
|
+
locale: string;
|
|
79
|
+
legalUrl?: (doc: Record<string, unknown>, locale: string) => string;
|
|
80
|
+
};
|
|
81
|
+
/** A short hash of what a visitor consents to: the categories and each service's storage. */
|
|
82
|
+
export declare function policyVersion(categories: Category[], services: ServiceDocument[]): string;
|
|
83
|
+
/**
|
|
84
|
+
* Maps the global and the services to the banner's config, filling any missing text with the
|
|
85
|
+
* suggestion. The banner's categories are the services' categories.
|
|
86
|
+
*/
|
|
87
|
+
export declare function toConsentConfig(doc: ConsentDocument, services: ServiceDocument[], options: Options): ConsentConfig;
|
|
88
|
+
type ReadArgs = {
|
|
89
|
+
payload: Payload;
|
|
90
|
+
locale?: string;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Reads the global and the services in one locale. A text never saved in that locale stays empty,
|
|
94
|
+
* so the locale's own suggestion fills it instead of another language's text.
|
|
95
|
+
*/
|
|
96
|
+
export declare function readConsent(args: ReadArgs, depth?: number): Promise<{
|
|
97
|
+
locale: string;
|
|
98
|
+
doc: ConsentDocument;
|
|
99
|
+
services: ServiceDocument[];
|
|
100
|
+
}>;
|
|
101
|
+
/** Reads the Cookies global and services for a locale and returns the config for `ConsentProvider`. */
|
|
102
|
+
export declare function getConsentConfig(args: ReadArgs): Promise<ConsentConfig>;
|
|
103
|
+
export type CookieTableData = {
|
|
104
|
+
labels: ReturnType<typeof defaultTexts>["table"];
|
|
105
|
+
categories: {
|
|
106
|
+
key: Category;
|
|
107
|
+
title: string;
|
|
108
|
+
description: string;
|
|
109
|
+
services: {
|
|
110
|
+
name: string;
|
|
111
|
+
provider?: string;
|
|
112
|
+
purpose?: string;
|
|
113
|
+
policyUrl?: string;
|
|
114
|
+
storage: {
|
|
115
|
+
name: string;
|
|
116
|
+
type: string;
|
|
117
|
+
duration?: string;
|
|
118
|
+
thirdParty: boolean;
|
|
119
|
+
description?: string;
|
|
120
|
+
}[];
|
|
121
|
+
}[];
|
|
122
|
+
}[];
|
|
123
|
+
};
|
|
124
|
+
/** Turns the services into the cookie table, with the consent storage itself as necessary. */
|
|
125
|
+
export declare function toCookieTable(doc: ConsentDocument, services: ServiceDocument[], locale: string): CookieTableData;
|
|
126
|
+
/** Reads what the cookie table block shows, in a locale. */
|
|
127
|
+
export declare function getCookieTable(args: ReadArgs): Promise<CookieTableData>;
|
|
128
|
+
export {};
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { CATEGORIES, defaultTexts, OPTIONAL_CATEGORIES } from "./defaults.js";
|
|
3
|
+
import { isIntegration } from "./integrations.js";
|
|
4
|
+
import { CONSENT_GLOBAL, CONSENT_RECORDS, CONSENT_SERVICES, POLICY_COOKIE, } from "./types.js";
|
|
5
|
+
const isDoc = (value) => Boolean(value) && typeof value === "object";
|
|
6
|
+
/** A short hash of what a visitor consents to: the categories and each service's storage. */
|
|
7
|
+
export function policyVersion(categories, services) {
|
|
8
|
+
const material = {
|
|
9
|
+
categories,
|
|
10
|
+
services: services
|
|
11
|
+
.map((service) => ({
|
|
12
|
+
name: service.name ?? "",
|
|
13
|
+
category: service.category ?? "",
|
|
14
|
+
storage: (service.storage ?? []).map((entry) => `${entry.type}:${entry.name}`).sort(),
|
|
15
|
+
}))
|
|
16
|
+
.sort((a, b) => `${a.category}:${a.name}`.localeCompare(`${b.category}:${b.name}`)),
|
|
17
|
+
};
|
|
18
|
+
return createHash("sha256").update(JSON.stringify(material)).digest("hex").slice(0, 16);
|
|
19
|
+
}
|
|
20
|
+
const isCategory = (value) => CATEGORIES.includes(value);
|
|
21
|
+
/**
|
|
22
|
+
* Maps the global and the services to the banner's config, filling any missing text with the
|
|
23
|
+
* suggestion. The banner's categories are the services' categories.
|
|
24
|
+
*/
|
|
25
|
+
export function toConsentConfig(doc, services, options) {
|
|
26
|
+
const suggested = defaultTexts(options.locale);
|
|
27
|
+
const used = new Set(services.map((service) => service.category));
|
|
28
|
+
const categories = [
|
|
29
|
+
"necessary",
|
|
30
|
+
...OPTIONAL_CATEGORIES.filter((category) => used.has(category)),
|
|
31
|
+
];
|
|
32
|
+
const integrations = services.flatMap((service) => service.loading === "integration" &&
|
|
33
|
+
isIntegration(service.integration) &&
|
|
34
|
+
service.integrationId?.trim() &&
|
|
35
|
+
isCategory(service.category)
|
|
36
|
+
? [{ key: service.integration, id: service.integrationId.trim(), category: service.category }]
|
|
37
|
+
: []);
|
|
38
|
+
const optional = categories.length > 1;
|
|
39
|
+
const pick = (value, fallback) => value?.trim() || fallback;
|
|
40
|
+
const buttons = doc.buttons ?? {};
|
|
41
|
+
const dialog = doc.dialog ?? {};
|
|
42
|
+
const legalLinks = {};
|
|
43
|
+
for (const name of ["privacyPolicy", "cookiePolicy"]) {
|
|
44
|
+
const page = doc[name];
|
|
45
|
+
if (options.legalUrl && isDoc(page))
|
|
46
|
+
legalLinks[name] = { href: options.legalUrl(page, options.locale), target: "_self" };
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
enabled: doc.enabled !== false,
|
|
50
|
+
locale: options.locale,
|
|
51
|
+
categories,
|
|
52
|
+
policyVersion: policyVersion(categories, services),
|
|
53
|
+
googleConsentMode: doc.googleConsentMode === "advanced" ? "advanced" : "basic",
|
|
54
|
+
integrations,
|
|
55
|
+
embeds: {
|
|
56
|
+
title: pick(doc.embeds?.title, suggested.embeds.title),
|
|
57
|
+
button: pick(doc.embeds?.button, suggested.embeds.button),
|
|
58
|
+
},
|
|
59
|
+
...(Object.keys(legalLinks).length ? { legalLinks } : {}),
|
|
60
|
+
messages: {
|
|
61
|
+
common: {
|
|
62
|
+
acceptAll: optional
|
|
63
|
+
? pick(buttons.acceptAll, suggested.buttons.acceptAll)
|
|
64
|
+
: pick(buttons.acceptNecessaryOnly, suggested.buttons.acceptNecessaryOnly),
|
|
65
|
+
rejectAll: pick(buttons.rejectAll, suggested.buttons.rejectAll),
|
|
66
|
+
customize: pick(buttons.customize, suggested.buttons.customize),
|
|
67
|
+
save: pick(buttons.save, suggested.buttons.save),
|
|
68
|
+
close: pick(buttons.close, suggested.buttons.close),
|
|
69
|
+
},
|
|
70
|
+
cookieBanner: {
|
|
71
|
+
title: pick(doc.bannerTitle, suggested.bannerTitle),
|
|
72
|
+
description: optional
|
|
73
|
+
? pick(doc.bannerDescription, suggested.bannerDescription)
|
|
74
|
+
: pick(doc.bannerDescriptionNecessaryOnly, suggested.bannerDescriptionNecessaryOnly),
|
|
75
|
+
},
|
|
76
|
+
consentManagerDialog: {
|
|
77
|
+
title: pick(dialog.title, suggested.dialog.title),
|
|
78
|
+
description: optional
|
|
79
|
+
? pick(dialog.description, suggested.dialog.description)
|
|
80
|
+
: pick(dialog.descriptionNecessaryOnly, suggested.dialog.descriptionNecessaryOnly),
|
|
81
|
+
},
|
|
82
|
+
consentTypes: Object.fromEntries(CATEGORIES.map((category) => {
|
|
83
|
+
const own = doc.categoryTexts?.[category];
|
|
84
|
+
const fallback = suggested.categoryTexts[category];
|
|
85
|
+
return [
|
|
86
|
+
category,
|
|
87
|
+
{
|
|
88
|
+
title: pick(own?.title, fallback.title),
|
|
89
|
+
description: pick(own?.description, fallback.description),
|
|
90
|
+
},
|
|
91
|
+
];
|
|
92
|
+
})),
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function resolveLocale({ payload, locale }) {
|
|
97
|
+
const localization = payload.config.localization;
|
|
98
|
+
return locale ?? (localization ? localization.defaultLocale : "en");
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Reads the global and the services in one locale. A text never saved in that locale stays empty,
|
|
102
|
+
* so the locale's own suggestion fills it instead of another language's text.
|
|
103
|
+
*/
|
|
104
|
+
export async function readConsent(args, depth = 0) {
|
|
105
|
+
const locale = resolveLocale(args);
|
|
106
|
+
const [doc, services] = await Promise.all([
|
|
107
|
+
args.payload.findGlobal({
|
|
108
|
+
slug: CONSENT_GLOBAL,
|
|
109
|
+
locale: locale,
|
|
110
|
+
fallbackLocale: false,
|
|
111
|
+
depth,
|
|
112
|
+
}),
|
|
113
|
+
args.payload.find({
|
|
114
|
+
collection: CONSENT_SERVICES,
|
|
115
|
+
locale: locale,
|
|
116
|
+
fallbackLocale: false,
|
|
117
|
+
depth: 0,
|
|
118
|
+
limit: 0,
|
|
119
|
+
pagination: false,
|
|
120
|
+
sort: "name",
|
|
121
|
+
}),
|
|
122
|
+
]);
|
|
123
|
+
return {
|
|
124
|
+
locale,
|
|
125
|
+
doc: doc,
|
|
126
|
+
services: services.docs,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/** Reads the Cookies global and services for a locale and returns the config for `ConsentProvider`. */
|
|
130
|
+
export async function getConsentConfig(args) {
|
|
131
|
+
const options = args.payload.config.custom?.consent;
|
|
132
|
+
const legalPages = options?.legalPages;
|
|
133
|
+
const { locale, doc, services } = await readConsent(args, legalPages ? 1 : 0);
|
|
134
|
+
const config = toConsentConfig(doc, services, {
|
|
135
|
+
locale,
|
|
136
|
+
legalUrl: legalPages ? (page, pageLocale) => legalPages.url(page, pageLocale) : undefined,
|
|
137
|
+
});
|
|
138
|
+
if (options?.records === false)
|
|
139
|
+
return config;
|
|
140
|
+
const { serverURL, routes } = args.payload.config;
|
|
141
|
+
return { ...config, recordUrl: `${serverURL ?? ""}${routes.api}/${CONSENT_RECORDS}/record` };
|
|
142
|
+
}
|
|
143
|
+
/** Turns the services into the cookie table, with the consent storage itself as necessary. */
|
|
144
|
+
export function toCookieTable(doc, services, locale) {
|
|
145
|
+
const config = toConsentConfig(doc, services, { locale });
|
|
146
|
+
const suggested = defaultTexts(locale);
|
|
147
|
+
const labels = suggested.table;
|
|
148
|
+
const text = (value) => value?.trim() || undefined;
|
|
149
|
+
return {
|
|
150
|
+
labels,
|
|
151
|
+
categories: config.categories.map((key) => {
|
|
152
|
+
const type = config.messages.consentTypes?.[key];
|
|
153
|
+
const rows = services
|
|
154
|
+
.filter((service) => service.category === key)
|
|
155
|
+
.map((service) => ({
|
|
156
|
+
name: text(service.name) ?? "",
|
|
157
|
+
provider: text(service.provider),
|
|
158
|
+
purpose: text(service.purpose),
|
|
159
|
+
policyUrl: text(service.policyUrl),
|
|
160
|
+
storage: (service.storage ?? []).map((entry) => ({
|
|
161
|
+
name: entry.name ?? "",
|
|
162
|
+
type: entry.type ?? "cookie",
|
|
163
|
+
duration: text(entry.duration),
|
|
164
|
+
thirdParty: Boolean(entry.thirdParty),
|
|
165
|
+
description: text(entry.description),
|
|
166
|
+
})),
|
|
167
|
+
}));
|
|
168
|
+
if (key === "necessary")
|
|
169
|
+
rows.unshift({
|
|
170
|
+
name: labels.consentService,
|
|
171
|
+
provider: undefined,
|
|
172
|
+
purpose: labels.consentPurpose,
|
|
173
|
+
policyUrl: undefined,
|
|
174
|
+
storage: [
|
|
175
|
+
...["cookie", "localStorage"].map((storageType) => ({
|
|
176
|
+
name: "c15t",
|
|
177
|
+
type: storageType,
|
|
178
|
+
duration: labels.consentDuration,
|
|
179
|
+
thirdParty: false,
|
|
180
|
+
description: undefined,
|
|
181
|
+
})),
|
|
182
|
+
{
|
|
183
|
+
name: POLICY_COOKIE,
|
|
184
|
+
type: "cookie",
|
|
185
|
+
duration: labels.consentDuration,
|
|
186
|
+
thirdParty: false,
|
|
187
|
+
description: undefined,
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
});
|
|
191
|
+
return {
|
|
192
|
+
key,
|
|
193
|
+
title: type?.title ?? key,
|
|
194
|
+
description: type?.description ?? "",
|
|
195
|
+
services: rows,
|
|
196
|
+
};
|
|
197
|
+
}),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/** Reads what the cookie table block shows, in a locale. */
|
|
201
|
+
export async function getCookieTable(args) {
|
|
202
|
+
const { locale, doc, services } = await readConsent(args);
|
|
203
|
+
return toCookieTable(doc, services, locale);
|
|
204
|
+
}
|