@truzyx/consent-widget 0.2.3 → 0.4.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 +55 -10
- package/dist/core/embed.d.ts +95 -0
- package/dist/core/types.d.ts +1 -1
- package/dist/core/widget.d.ts +43 -6
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +368 -57
- package/dist/react.cjs +1 -1
- package/dist/react.d.ts +8 -3
- package/dist/react.js +404 -94
- package/package.json +1 -1
package/dist/react.js
CHANGED
|
@@ -1,28 +1,256 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { useEffect as
|
|
5
|
-
const
|
|
1
|
+
var P = Object.defineProperty;
|
|
2
|
+
var H = (n, e, t) => e in n ? P(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
|
|
3
|
+
var y = (n, e, t) => H(n, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
import { useEffect as S, useCallback as C } from "react";
|
|
5
|
+
const O = {
|
|
6
6
|
live: "https://ui.gateway.nodbook.in",
|
|
7
7
|
sandbox: "https://sandbox-ui.gateway.nodbook.in"
|
|
8
|
-
},
|
|
9
|
-
function
|
|
10
|
-
return typeof n == "object" && n !== null && n.type ===
|
|
8
|
+
}, F = "CONSENT_WIDGET_EVENT", j = "GATEWAY_UI_READY", N = "PRINCIPAL_TOKEN";
|
|
9
|
+
function G(n) {
|
|
10
|
+
return typeof n == "object" && n !== null && n.type === F && typeof n.event == "object";
|
|
11
11
|
}
|
|
12
|
-
function
|
|
13
|
-
return typeof n == "object" && n !== null && n.type ===
|
|
12
|
+
function M(n) {
|
|
13
|
+
return typeof n == "object" && n !== null && n.type === j;
|
|
14
14
|
}
|
|
15
|
-
const
|
|
16
|
-
function
|
|
15
|
+
const Y = ".nodbook-widget";
|
|
16
|
+
function U() {
|
|
17
|
+
const n = globalThis.document;
|
|
18
|
+
if (!n)
|
|
19
|
+
throw new Error("nodbook-widget: `document` is not available in this environment.");
|
|
20
|
+
return n;
|
|
21
|
+
}
|
|
22
|
+
function K(n) {
|
|
23
|
+
var t, o;
|
|
24
|
+
const e = n == null ? void 0 : n.trim();
|
|
25
|
+
if (e) return e;
|
|
26
|
+
try {
|
|
27
|
+
const r = globalThis.navigator, i = (o = (t = r == null ? void 0 : r.language) == null ? void 0 : t.split("-")[0]) == null ? void 0 : o.trim();
|
|
28
|
+
if (i) return i;
|
|
29
|
+
} catch {
|
|
30
|
+
}
|
|
31
|
+
return "en";
|
|
32
|
+
}
|
|
33
|
+
function V(n) {
|
|
34
|
+
var t;
|
|
35
|
+
return (((t = n.apiBaseUrl) == null ? void 0 : t.trim()) || R(n.baseUrl, n.env)).replace(/\/$/, "");
|
|
36
|
+
}
|
|
37
|
+
function $(n, e) {
|
|
38
|
+
var t;
|
|
39
|
+
if (typeof n == "object" && n !== null) {
|
|
40
|
+
const o = (t = n.error_data) == null ? void 0 : t.description;
|
|
41
|
+
if (typeof o == "string" && o.trim()) return o;
|
|
42
|
+
}
|
|
43
|
+
return e;
|
|
44
|
+
}
|
|
45
|
+
async function J(n, e) {
|
|
46
|
+
const t = await fetch(`${n}/v1/public/direct_consents/purpose`, {
|
|
47
|
+
method: "POST",
|
|
48
|
+
headers: { "Content-Type": "application/json" },
|
|
49
|
+
body: JSON.stringify(e)
|
|
50
|
+
}), o = await t.json().catch(() => null);
|
|
51
|
+
if (!t.ok)
|
|
52
|
+
throw new Error($(o, `Failed to load purpose (${t.status}).`));
|
|
53
|
+
const r = o == null ? void 0 : o.data;
|
|
54
|
+
if (!r || typeof r != "object" || !("id" in r))
|
|
55
|
+
throw new Error("Failed to load purpose: unexpected response shape.");
|
|
56
|
+
return r;
|
|
57
|
+
}
|
|
58
|
+
async function Q(n, e) {
|
|
59
|
+
let t = "";
|
|
60
|
+
try {
|
|
61
|
+
const s = globalThis.navigator;
|
|
62
|
+
typeof (s == null ? void 0 : s.userAgent) == "string" && (t = s.userAgent);
|
|
63
|
+
} catch {
|
|
64
|
+
}
|
|
65
|
+
const o = await fetch(`${n}/v1/public/direct_consents`, {
|
|
66
|
+
method: "POST",
|
|
67
|
+
headers: { "Content-Type": "application/json" },
|
|
68
|
+
body: JSON.stringify({
|
|
69
|
+
...e,
|
|
70
|
+
device_details: {
|
|
71
|
+
user_agent: t,
|
|
72
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
}), r = await o.json().catch(() => null);
|
|
76
|
+
if (!o.ok)
|
|
77
|
+
throw new Error($(r, `Failed to record consent (${o.status}).`));
|
|
78
|
+
const i = r == null ? void 0 : r.data;
|
|
79
|
+
if (!i || typeof i != "object" || !Array.isArray(i.consent_ids))
|
|
80
|
+
throw new Error("Failed to record consent: unexpected response shape.");
|
|
81
|
+
return i;
|
|
82
|
+
}
|
|
83
|
+
function X(n, e) {
|
|
84
|
+
var o, r;
|
|
85
|
+
const t = ((o = n.locales) == null ? void 0 : o[e]) ?? ((r = n.locales) == null ? void 0 : r.en) ?? Object.values(n.locales ?? {})[0];
|
|
86
|
+
return {
|
|
87
|
+
name: (t == null ? void 0 : t.name) ?? n.name ?? n.identifier ?? n.id,
|
|
88
|
+
description: (t == null ? void 0 : t.description) ?? n.description ?? ""
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function m(n, e, t) {
|
|
92
|
+
const r = U().createElement(n);
|
|
93
|
+
return r.className = t, r.setAttribute("style", e), r;
|
|
94
|
+
}
|
|
95
|
+
function B(n, e, t) {
|
|
96
|
+
var x, A, W;
|
|
97
|
+
const o = (x = e.organizationId) == null ? void 0 : x.trim(), r = (A = e.purposeId) == null ? void 0 : A.trim(), i = (W = e.principalId) == null ? void 0 : W.trim();
|
|
98
|
+
if (!o || !r || !i)
|
|
99
|
+
throw new Error("embedDirectConsent: `organizationId`, `purposeId` and `principalId` are required.");
|
|
100
|
+
const s = {
|
|
101
|
+
organizationId: o,
|
|
102
|
+
purposeId: r,
|
|
103
|
+
principalId: i,
|
|
104
|
+
locale: K(e.locale),
|
|
105
|
+
apiBaseUrl: V(e)
|
|
106
|
+
}, p = { purposeId: r, principalId: i, organizationId: o };
|
|
107
|
+
let a = !1, c = !1;
|
|
108
|
+
const l = m("div", "display:flex;flex-direction:column;gap:8px;font:inherit;color:inherit;", "nbw-root"), u = (b) => {
|
|
109
|
+
const g = m("span", "font-size:13px;opacity:0.65;", "nbw-loading");
|
|
110
|
+
return g.textContent = b, l.replaceChildren(g), g;
|
|
111
|
+
}, d = (b, g) => {
|
|
112
|
+
if (a) return;
|
|
113
|
+
const v = m("div", "display:flex;flex-direction:column;gap:6px;", "nbw-error-wrap"), I = m("span", "font-size:13px;color:#b91c1c;", "nbw-error");
|
|
114
|
+
if (I.setAttribute("role", "alert"), I.textContent = b, v.appendChild(I), g) {
|
|
115
|
+
const z = m(
|
|
116
|
+
"button",
|
|
117
|
+
"align-self:flex-start;font:inherit;font-size:13px;color:#0b57d0;background:none;border:none;padding:0;cursor:pointer;text-decoration:underline;",
|
|
118
|
+
"nbw-retry"
|
|
119
|
+
);
|
|
120
|
+
z.textContent = "Retry", z.addEventListener("click", g), v.appendChild(z);
|
|
121
|
+
}
|
|
122
|
+
l.replaceChildren(v);
|
|
123
|
+
};
|
|
124
|
+
u("Loading consent…"), n.replaceChildren(l);
|
|
125
|
+
const h = () => {
|
|
126
|
+
u("Loading consent…"), J(s.apiBaseUrl, {
|
|
127
|
+
organization_id: s.organizationId,
|
|
128
|
+
purpose_id: s.purposeId
|
|
129
|
+
}).then(
|
|
130
|
+
(b) => {
|
|
131
|
+
a || E(b);
|
|
132
|
+
},
|
|
133
|
+
(b) => {
|
|
134
|
+
if (a) return;
|
|
135
|
+
const g = b instanceof Error ? b.message : "Failed to load purpose.";
|
|
136
|
+
t({ type: "error", message: g, data: p }), d(g, h);
|
|
137
|
+
}
|
|
138
|
+
);
|
|
139
|
+
}, E = (b) => {
|
|
140
|
+
if (a) return;
|
|
141
|
+
const { name: g, description: v } = X(b, s.locale), I = m(
|
|
142
|
+
"label",
|
|
143
|
+
"display:flex;gap:8px;align-items:flex-start;cursor:pointer;font:inherit;",
|
|
144
|
+
"nbw-label"
|
|
145
|
+
), w = U().createElement("input");
|
|
146
|
+
w.type = "checkbox", w.className = "nbw-checkbox", w.setAttribute(
|
|
147
|
+
"style",
|
|
148
|
+
"margin-top:2px;width:16px;height:16px;accent-color:#0b57d0;cursor:pointer;flex-shrink:0;"
|
|
149
|
+
);
|
|
150
|
+
const D = m("span", "display:flex;flex-direction:column;gap:2px;min-width:0;", "nbw-text"), T = m("span", "font-size:14px;font-weight:600;line-height:1.35;", "nbw-name");
|
|
151
|
+
if (T.textContent = g, D.appendChild(T), v) {
|
|
152
|
+
const k = m("span", "font-size:12px;line-height:1.4;opacity:0.75;", "nbw-description");
|
|
153
|
+
k.textContent = v, D.appendChild(k);
|
|
154
|
+
}
|
|
155
|
+
I.appendChild(w), I.appendChild(D);
|
|
156
|
+
const _ = m("span", "font-size:12px;opacity:0.65;", "nbw-status");
|
|
157
|
+
_.setAttribute("aria-live", "polite"), w.addEventListener("change", () => {
|
|
158
|
+
if (c || a) return;
|
|
159
|
+
c = !0;
|
|
160
|
+
const k = w.checked;
|
|
161
|
+
w.disabled = !0, _.textContent = "Saving…", Q(s.apiBaseUrl, {
|
|
162
|
+
organization_id: s.organizationId,
|
|
163
|
+
principal_id: s.principalId,
|
|
164
|
+
purpose_id: s.purposeId,
|
|
165
|
+
active: k,
|
|
166
|
+
locale: s.locale
|
|
167
|
+
}).then(
|
|
168
|
+
() => {
|
|
169
|
+
a || (c = !1, w.disabled = !1, _.textContent = "", t({ type: k ? "consent_accepted" : "consent_declined", data: p }));
|
|
170
|
+
},
|
|
171
|
+
(q) => {
|
|
172
|
+
if (a) return;
|
|
173
|
+
c = !1, w.checked = !k, w.disabled = !1;
|
|
174
|
+
const L = q instanceof Error ? q.message : "Failed to record consent.";
|
|
175
|
+
_.textContent = L, t({ type: "error", message: L, data: p });
|
|
176
|
+
}
|
|
177
|
+
);
|
|
178
|
+
}), l.replaceChildren(I, _);
|
|
179
|
+
};
|
|
180
|
+
return h(), () => {
|
|
181
|
+
a = !0, l.remove();
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function Z(n) {
|
|
185
|
+
const e = (o) => {
|
|
186
|
+
var i;
|
|
187
|
+
return ((i = n.getAttribute(`data-${o}`)) == null ? void 0 : i.trim()) || void 0;
|
|
188
|
+
}, t = e("env");
|
|
189
|
+
return {
|
|
190
|
+
organizationId: e("organization-id"),
|
|
191
|
+
purposeId: e("purpose-id"),
|
|
192
|
+
principalId: e("principal-id"),
|
|
193
|
+
locale: e("locale"),
|
|
194
|
+
baseUrl: e("base-url"),
|
|
195
|
+
env: t === "live" || t === "sandbox" ? t : void 0,
|
|
196
|
+
apiBaseUrl: e("api-base-url")
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function ee(n = {}, e) {
|
|
200
|
+
const t = n.root ?? U(), o = Array.from(t.querySelectorAll(Y)), r = [];
|
|
201
|
+
for (const i of o) {
|
|
202
|
+
if (i.getAttribute("data-nbw-mounted") === "true") continue;
|
|
203
|
+
const s = Z(i), p = s.organizationId ?? "", a = s.purposeId ?? "", c = s.principalId ?? "";
|
|
204
|
+
if (!p || !a || !c) {
|
|
205
|
+
const u = `nodbook-widget: missing ${[
|
|
206
|
+
!p && "`data-organization-id`",
|
|
207
|
+
!a && "`data-purpose-id`",
|
|
208
|
+
!c && "`data-principal-id`"
|
|
209
|
+
].filter(Boolean).join(", ")}.`, d = m("span", "font-size:13px;color:#b91c1c;", "nbw-error");
|
|
210
|
+
d.setAttribute("role", "alert"), d.textContent = u, i.replaceChildren(d), e({ type: "error", message: u, data: { purposeId: a, principalId: c, organizationId: p } });
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
const l = B(
|
|
215
|
+
i,
|
|
216
|
+
{
|
|
217
|
+
organizationId: p,
|
|
218
|
+
purposeId: a,
|
|
219
|
+
principalId: c,
|
|
220
|
+
locale: s.locale ?? n.locale,
|
|
221
|
+
baseUrl: s.baseUrl ?? n.baseUrl,
|
|
222
|
+
env: s.env ?? n.env,
|
|
223
|
+
apiBaseUrl: s.apiBaseUrl ?? n.apiBaseUrl
|
|
224
|
+
},
|
|
225
|
+
e
|
|
226
|
+
);
|
|
227
|
+
i.setAttribute("data-nbw-mounted", "true"), r.push(() => {
|
|
228
|
+
i.removeAttribute("data-nbw-mounted"), l();
|
|
229
|
+
});
|
|
230
|
+
} catch (l) {
|
|
231
|
+
const u = l instanceof Error ? l.message : "Failed to embed consent widget.";
|
|
232
|
+
e({ type: "error", message: u, data: { purposeId: a, principalId: c, organizationId: p } });
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return () => {
|
|
236
|
+
for (const i of r)
|
|
237
|
+
try {
|
|
238
|
+
i();
|
|
239
|
+
} catch {
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
const te = "__consent_widget_overlay__", ne = 600, re = 640;
|
|
244
|
+
function R(n, e) {
|
|
17
245
|
var t;
|
|
18
246
|
if (n) return n.replace(/\/$/, "");
|
|
19
|
-
if (e &&
|
|
247
|
+
if (e && O[e]) return O[e];
|
|
20
248
|
try {
|
|
21
|
-
const
|
|
22
|
-
for (let
|
|
23
|
-
const
|
|
24
|
-
if (
|
|
25
|
-
return new URL(
|
|
249
|
+
const o = document.getElementsByTagName("script");
|
|
250
|
+
for (let i = o.length - 1; i >= 0; i--) {
|
|
251
|
+
const s = ((t = o[i]) == null ? void 0 : t.getAttribute("src")) ?? "";
|
|
252
|
+
if (s.includes("consent-widget"))
|
|
253
|
+
return new URL(s, window.location.href).origin;
|
|
26
254
|
}
|
|
27
255
|
const r = document.currentScript;
|
|
28
256
|
if (r != null && r.src) return new URL(r.src, window.location.href).origin;
|
|
@@ -30,27 +258,26 @@ function W(n, e) {
|
|
|
30
258
|
}
|
|
31
259
|
return window.location.origin;
|
|
32
260
|
}
|
|
33
|
-
function
|
|
261
|
+
function oe(n) {
|
|
34
262
|
const e = new URLSearchParams({
|
|
35
|
-
auth_token: n.token,
|
|
36
263
|
embed: "true",
|
|
37
264
|
origin: window.location.origin
|
|
38
265
|
});
|
|
39
|
-
return n.mode === "preferences" ? `${n.baseUrl}/consent_manager/preferences?${e.toString()}` : n.mode === "direct_consent" ? (n.purposeId && e.set("purpose_id", n.purposeId), n.
|
|
266
|
+
return n.token && e.set("auth_token", n.token), n.mode === "preferences" ? `${n.baseUrl}/consent_manager/preferences?${e.toString()}` : n.mode === "direct_consent" ? (n.purposeId && e.set("purpose_id", n.purposeId), n.organizationId && e.set("organization_id", n.organizationId), n.principalId && e.set("principal_id", n.principalId), n.locale && e.set("locale", n.locale), `${n.baseUrl}/consent_manager/direct_consent?${e.toString()}`) : `${n.baseUrl}/consent_manager/consent/${encodeURIComponent(n.requestId ?? "")}?${e.toString()}`;
|
|
40
267
|
}
|
|
41
|
-
class
|
|
268
|
+
class ie {
|
|
42
269
|
constructor() {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
270
|
+
y(this, "overlay", null);
|
|
271
|
+
y(this, "iframe", null);
|
|
272
|
+
y(this, "session", null);
|
|
273
|
+
y(this, "callbacks", /* @__PURE__ */ new Set());
|
|
274
|
+
y(this, "listening", !1);
|
|
275
|
+
y(this, "escHandler", (e) => {
|
|
49
276
|
var t;
|
|
50
277
|
e.key === "Escape" && this.close({ type: "closed", requestId: (t = this.session) == null ? void 0 : t.requestId });
|
|
51
278
|
});
|
|
52
|
-
|
|
53
|
-
var
|
|
279
|
+
y(this, "messageHandler", (e) => {
|
|
280
|
+
var o, r;
|
|
54
281
|
if (!this.session || !this.iframe) return;
|
|
55
282
|
let t = "";
|
|
56
283
|
try {
|
|
@@ -59,75 +286,102 @@ class q {
|
|
|
59
286
|
t = "";
|
|
60
287
|
}
|
|
61
288
|
if (!(t && e.origin !== t) && e.source === this.iframe.contentWindow) {
|
|
62
|
-
if (
|
|
289
|
+
if (M(e.data)) {
|
|
290
|
+
if (!this.session.token) return;
|
|
63
291
|
try {
|
|
64
|
-
(
|
|
65
|
-
{ type:
|
|
292
|
+
(o = this.iframe.contentWindow) == null || o.postMessage(
|
|
293
|
+
{ type: N, token: this.session.token },
|
|
66
294
|
t || "*"
|
|
67
295
|
);
|
|
68
296
|
} catch {
|
|
69
297
|
}
|
|
70
298
|
return;
|
|
71
299
|
}
|
|
72
|
-
if (typeof e.data == "object" && e.data !== null && e.data.type ===
|
|
300
|
+
if (typeof e.data == "object" && e.data !== null && e.data.type === j) {
|
|
301
|
+
if (!this.session.token) return;
|
|
73
302
|
try {
|
|
74
303
|
(r = this.iframe.contentWindow) == null || r.postMessage(
|
|
75
|
-
{ type:
|
|
304
|
+
{ type: N, token: this.session.token },
|
|
76
305
|
t || "*"
|
|
77
306
|
);
|
|
78
307
|
} catch {
|
|
79
308
|
}
|
|
80
309
|
return;
|
|
81
310
|
}
|
|
82
|
-
if (
|
|
83
|
-
const
|
|
84
|
-
this.emit(
|
|
311
|
+
if (G(e.data)) {
|
|
312
|
+
const i = e.data.event;
|
|
313
|
+
this.emit(i), this.session.autoClose && (i.type === "consent_accepted" || i.type === "consent_declined" || i.type === "already_resolved" || i.type === "redirecting") && this.teardown();
|
|
85
314
|
}
|
|
86
315
|
}
|
|
87
316
|
});
|
|
317
|
+
// ---- inline embed (no iframe) ----
|
|
318
|
+
y(this, "embeds", /* @__PURE__ */ new Set());
|
|
88
319
|
}
|
|
89
320
|
/** Open the consent dialog for a request. */
|
|
90
|
-
openConsent(e, t,
|
|
91
|
-
this.openDialog({ ...
|
|
321
|
+
openConsent(e, t, o) {
|
|
322
|
+
this.openDialog({ ...o, requestId: e, token: t, mode: "consent" });
|
|
92
323
|
}
|
|
93
324
|
/** Open the preferences (my consents + grievances) dialog. */
|
|
94
325
|
openPreferences(e, t) {
|
|
95
326
|
this.openDialog({ ...t, token: e, mode: "preferences" });
|
|
96
327
|
}
|
|
97
|
-
/** Open the direct-consent (permission-style) dialog for a purpose. */
|
|
98
|
-
openDirectConsent(e, t,
|
|
99
|
-
const
|
|
100
|
-
if (!
|
|
328
|
+
/** Open the direct-consent (permission-style) dialog for a purpose (public — no JWT). */
|
|
329
|
+
openDirectConsent(e, t, o, r, i) {
|
|
330
|
+
const s = e == null ? void 0 : e.trim();
|
|
331
|
+
if (!s)
|
|
101
332
|
throw new Error("ConsentWidget.openDirectConsent: `purposeId` is required.");
|
|
102
|
-
|
|
333
|
+
const p = t == null ? void 0 : t.trim();
|
|
334
|
+
if (!p)
|
|
335
|
+
throw new Error("ConsentWidget.openDirectConsent: `principalId` is required.");
|
|
336
|
+
const a = o ?? null;
|
|
337
|
+
if (!(a != null && a.trim()))
|
|
338
|
+
throw new Error("ConsentWidget.openDirectConsent: `organizationId` is required.");
|
|
339
|
+
this.openDialog({ ...i, purposeId: s, principalId: p, organizationId: a.trim(), locale: r, mode: "direct_consent" });
|
|
103
340
|
}
|
|
104
341
|
/**
|
|
105
342
|
* Shared opener behind `openConsent` / `openPreferences` / `openDirectConsent`.
|
|
343
|
+
* `token` (JWT) is required for `consent` / `preferences` but NOT for
|
|
344
|
+
* public `direct_consent`, which identifies via principalId + organizationId.
|
|
106
345
|
*/
|
|
107
346
|
openDialog(e) {
|
|
108
|
-
var
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
347
|
+
var i, s, p, a, c;
|
|
348
|
+
const t = ((i = e.purposeId) == null ? void 0 : i.trim()) || void 0, o = e.mode ?? (t ? "direct_consent" : e.requestId ? "consent" : "preferences");
|
|
349
|
+
if (o === "consent") {
|
|
350
|
+
if (!e.requestId)
|
|
351
|
+
throw new Error("ConsentWidget: `requestId` is required for the consent dialog.");
|
|
352
|
+
if (!e.token)
|
|
353
|
+
throw new Error("ConsentWidget: `token` is required for the consent dialog.");
|
|
354
|
+
} else if (o === "preferences") {
|
|
355
|
+
if (!e.token)
|
|
356
|
+
throw new Error("ConsentWidget: `token` is required for the preferences dialog.");
|
|
357
|
+
} else {
|
|
358
|
+
const l = ((s = e.principalId) == null ? void 0 : s.trim()) || void 0, u = ((p = e.organizationId) == null ? void 0 : p.trim()) || void 0;
|
|
359
|
+
if (!t)
|
|
360
|
+
throw new Error("ConsentWidget.openDirectConsent: `purposeId` is required.");
|
|
361
|
+
if (!l)
|
|
362
|
+
throw new Error("ConsentWidget.openDirectConsent: `principalId` is required.");
|
|
363
|
+
if (!u)
|
|
364
|
+
throw new Error("ConsentWidget.openDirectConsent: `organizationId` is required.");
|
|
365
|
+
}
|
|
366
|
+
const r = ((a = e.organizationId) == null ? void 0 : a.trim()) || void 0;
|
|
114
367
|
this.open({
|
|
115
368
|
requestId: e.requestId,
|
|
116
369
|
token: e.token,
|
|
117
|
-
mode:
|
|
370
|
+
mode: o,
|
|
118
371
|
purposeId: t,
|
|
119
|
-
|
|
372
|
+
principalId: ((c = e.principalId) == null ? void 0 : c.trim()) || void 0,
|
|
373
|
+
organizationId: r ?? null,
|
|
120
374
|
locale: e.locale,
|
|
121
|
-
baseUrl:
|
|
375
|
+
baseUrl: R(e.baseUrl, e.env),
|
|
122
376
|
autoClose: e.autoClose ?? !0
|
|
123
377
|
}), this.render(
|
|
124
378
|
e.allowClose ?? !0,
|
|
125
|
-
e.width ??
|
|
126
|
-
e.height ??
|
|
379
|
+
e.width ?? ne,
|
|
380
|
+
e.height ?? re
|
|
127
381
|
), this.emit({
|
|
128
382
|
type: "opened",
|
|
129
383
|
requestId: e.requestId,
|
|
130
|
-
data: t ? { purposeId: t } : void 0
|
|
384
|
+
data: t ? { purposeId: t, principalId: e.principalId, organizationId: r } : void 0
|
|
131
385
|
});
|
|
132
386
|
}
|
|
133
387
|
/**
|
|
@@ -146,16 +400,72 @@ class q {
|
|
|
146
400
|
const t = e ?? {
|
|
147
401
|
type: "closed",
|
|
148
402
|
requestId: (r = this.session) == null ? void 0 : r.requestId
|
|
149
|
-
},
|
|
150
|
-
this.teardown(),
|
|
403
|
+
}, o = this.overlay !== null;
|
|
404
|
+
this.teardown(), o && this.emit(t);
|
|
151
405
|
}
|
|
152
406
|
/** Alias of close() without emitting when already closed. */
|
|
153
407
|
destroy() {
|
|
154
|
-
this.callbacks.clear(), this.teardown();
|
|
408
|
+
this.callbacks.clear(), this.unmountEmbeds(), this.teardown();
|
|
155
409
|
}
|
|
156
410
|
isOpen() {
|
|
157
411
|
return this.overlay !== null;
|
|
158
412
|
}
|
|
413
|
+
trackEmbed(e) {
|
|
414
|
+
return this.embeds.add(e), () => {
|
|
415
|
+
this.embeds.delete(e), e();
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
unmountEmbeds() {
|
|
419
|
+
for (const e of this.embeds)
|
|
420
|
+
try {
|
|
421
|
+
e();
|
|
422
|
+
} catch {
|
|
423
|
+
}
|
|
424
|
+
this.embeds.clear();
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Render an inline consent checkbox for a purpose into `target`
|
|
428
|
+
* (element or CSS selector), replacing its contents. Toggling the checkbox
|
|
429
|
+
* captures consent via the public direct-consent APIs (no JWT).
|
|
430
|
+
* Returns an unmount function.
|
|
431
|
+
*
|
|
432
|
+
* ConsentWidget.embedDirectConsent("#newsletter-consent", {
|
|
433
|
+
* organizationId: "org_...",
|
|
434
|
+
* purposeId: "purpose_123",
|
|
435
|
+
* principalId: "principal_123",
|
|
436
|
+
* });
|
|
437
|
+
*/
|
|
438
|
+
embedDirectConsent(e, t) {
|
|
439
|
+
const o = typeof e == "string" ? (() => {
|
|
440
|
+
try {
|
|
441
|
+
const r = globalThis.document;
|
|
442
|
+
return (r == null ? void 0 : r.querySelector(e)) ?? null;
|
|
443
|
+
} catch {
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
})() : e;
|
|
447
|
+
if (!o)
|
|
448
|
+
throw new Error("ConsentWidget.embedDirectConsent: target element not found.");
|
|
449
|
+
return this.trackEmbed(B(o, t, (r) => this.emit(r)));
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Embed every `.nodbook-widget` element on the page as an inline consent
|
|
453
|
+
* checkbox. Identifiers come from per-element data attributes
|
|
454
|
+
* (`data-organization-id`, `data-purpose-id`, `data-principal-id`,
|
|
455
|
+
* optional `data-locale`, `data-base-url`, `data-env`, `data-api-base-url`),
|
|
456
|
+
* falling back to `options` for gateway/locale settings.
|
|
457
|
+
* Returns a cleanup that unmounts everything mounted by this call.
|
|
458
|
+
*
|
|
459
|
+
* <div class="nodbook-widget"
|
|
460
|
+
* data-organization-id="org_..."
|
|
461
|
+
* data-purpose-id="purpose_123"
|
|
462
|
+
* data-principal-id="principal_123"></div>
|
|
463
|
+
* <script>ConsentWidget.embedNodbookWidgets();<\/script>
|
|
464
|
+
*/
|
|
465
|
+
embedNodbookWidgets(e) {
|
|
466
|
+
const t = ee(e, (o) => this.emit(o));
|
|
467
|
+
return this.trackEmbed(t);
|
|
468
|
+
}
|
|
159
469
|
// ---- internals ----
|
|
160
470
|
open(e) {
|
|
161
471
|
this.teardown(), this.session = e, this.ensureListening();
|
|
@@ -163,10 +473,10 @@ class q {
|
|
|
163
473
|
ensureListening() {
|
|
164
474
|
this.listening || (window.addEventListener("message", this.messageHandler), window.addEventListener("keydown", this.escHandler), this.listening = !0);
|
|
165
475
|
}
|
|
166
|
-
render(e, t,
|
|
476
|
+
render(e, t, o) {
|
|
167
477
|
if (!this.session) return;
|
|
168
|
-
const r =
|
|
169
|
-
|
|
478
|
+
const r = oe(this.session), i = document.createElement("div");
|
|
479
|
+
i.id = te, i.setAttribute("role", "dialog"), i.setAttribute("aria-modal", "true"), i.style.cssText = [
|
|
170
480
|
"position:fixed",
|
|
171
481
|
"inset:0",
|
|
172
482
|
"z-index:2147483647",
|
|
@@ -178,11 +488,11 @@ class q {
|
|
|
178
488
|
"box-sizing:border-box",
|
|
179
489
|
"pointer-events:auto"
|
|
180
490
|
].join(";");
|
|
181
|
-
const
|
|
182
|
-
if (
|
|
183
|
-
`width:${
|
|
491
|
+
const s = document.createElement("div"), p = Math.min(t, window.innerWidth - 32), a = Math.min(o, window.innerHeight - 32);
|
|
492
|
+
if (s.style.cssText = [
|
|
493
|
+
`width:${p}px`,
|
|
184
494
|
"max-width:100%",
|
|
185
|
-
`height:${
|
|
495
|
+
`height:${a}px`,
|
|
186
496
|
"max-height:100%",
|
|
187
497
|
"background:#fff",
|
|
188
498
|
"border-radius:16px",
|
|
@@ -193,8 +503,8 @@ class q {
|
|
|
193
503
|
"flex-direction:column",
|
|
194
504
|
"pointer-events:auto"
|
|
195
505
|
].join(";"), e) {
|
|
196
|
-
const
|
|
197
|
-
|
|
506
|
+
const l = document.createElement("button");
|
|
507
|
+
l.setAttribute("aria-label", "Close"), l.textContent = "×", l.style.cssText = [
|
|
198
508
|
"position:absolute",
|
|
199
509
|
"top:16px",
|
|
200
510
|
"right:16px",
|
|
@@ -210,12 +520,12 @@ class q {
|
|
|
210
520
|
"cursor:pointer",
|
|
211
521
|
"box-shadow:0 4px 12px rgba(0,0,0,0.25)",
|
|
212
522
|
"pointer-events:auto"
|
|
213
|
-
].join(";"),
|
|
214
|
-
|
|
523
|
+
].join(";"), l.onclick = () => this.close(), i.appendChild(l), i.addEventListener("mousedown", (u) => {
|
|
524
|
+
u.target === i && this.close();
|
|
215
525
|
});
|
|
216
526
|
}
|
|
217
|
-
const
|
|
218
|
-
|
|
527
|
+
const c = document.createElement("iframe");
|
|
528
|
+
c.src = r, c.title = "Consent manager", c.setAttribute("allow", "clipboard-write"), c.style.cssText = [
|
|
219
529
|
"flex:1",
|
|
220
530
|
"width:100%",
|
|
221
531
|
"height:100%",
|
|
@@ -224,7 +534,7 @@ class q {
|
|
|
224
534
|
"position:relative",
|
|
225
535
|
"z-index:1",
|
|
226
536
|
"pointer-events:auto"
|
|
227
|
-
].join(";"),
|
|
537
|
+
].join(";"), s.appendChild(c), i.appendChild(s), document.body.appendChild(i), document.documentElement.style.overflow = "hidden", this.overlay = i, this.iframe = c;
|
|
228
538
|
}
|
|
229
539
|
teardown() {
|
|
230
540
|
var e;
|
|
@@ -243,32 +553,32 @@ class q {
|
|
|
243
553
|
});
|
|
244
554
|
}
|
|
245
555
|
}
|
|
246
|
-
const
|
|
247
|
-
function
|
|
556
|
+
const f = new ie();
|
|
557
|
+
function ce(n) {
|
|
248
558
|
const { onEvent: e, closeOnUnmount: t = !0 } = n ?? {};
|
|
249
|
-
|
|
559
|
+
S(() => {
|
|
250
560
|
if (e)
|
|
251
|
-
return
|
|
252
|
-
}, [e]),
|
|
561
|
+
return f.onCallback(e);
|
|
562
|
+
}, [e]), S(() => {
|
|
253
563
|
if (t)
|
|
254
564
|
return () => {
|
|
255
|
-
|
|
565
|
+
f.isOpen() && f.close();
|
|
256
566
|
};
|
|
257
567
|
}, [t]);
|
|
258
|
-
const
|
|
259
|
-
const { requestId:
|
|
260
|
-
|
|
261
|
-
}, []), r =
|
|
262
|
-
|
|
263
|
-
}, []),
|
|
264
|
-
|
|
265
|
-
}, []),
|
|
266
|
-
|
|
267
|
-
}, []),
|
|
268
|
-
|
|
269
|
-
}, []),
|
|
270
|
-
return { open:
|
|
568
|
+
const o = C((d, h) => {
|
|
569
|
+
const { requestId: E, ...x } = h ?? {};
|
|
570
|
+
E ? f.openConsent(E, d, x) : f.openPreferences(d, x);
|
|
571
|
+
}, []), r = C((d, h, E) => {
|
|
572
|
+
f.openConsent(d, h, E);
|
|
573
|
+
}, []), i = C((d, h) => {
|
|
574
|
+
f.openPreferences(d, h);
|
|
575
|
+
}, []), s = C((d, h, E, x, A) => {
|
|
576
|
+
f.openDirectConsent(d, h, E, x, A);
|
|
577
|
+
}, []), p = C((d) => {
|
|
578
|
+
f.close(d);
|
|
579
|
+
}, []), a = C((d, h) => f.embedDirectConsent(d, h), []), c = C((d) => f.embedNodbookWidgets(d), []), l = C((d) => f.onCallback(d), []), u = C(() => f.isOpen(), []);
|
|
580
|
+
return { open: o, openConsent: r, openPreferences: i, openDirectConsent: s, embedDirectConsent: a, embedNodbookWidgets: c, close: p, onEvent: l, isOpen: u };
|
|
271
581
|
}
|
|
272
582
|
export {
|
|
273
|
-
|
|
583
|
+
ce as useConsentWidget
|
|
274
584
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@truzyx/consent-widget",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Framework-agnostic consent + preferences widget (iframe overlay) with React hook. Reuses the gateway embed core, zero runtime dependencies for the vanilla entry.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|