@truzyx/consent-widget 0.1.0 → 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.
- package/README.md +3 -3
- package/dist/core/types.d.ts +1 -1
- package/dist/core/widget.d.ts +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/react.cjs +1 -1
- package/dist/react.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ No bundler? Use the script-tag build served from your gateway host instead (glob
|
|
|
19
19
|
| Value | Where it comes from |
|
|
20
20
|
|-----------|---------------------|
|
|
21
21
|
| `baseUrl` | Your gateway origin, e.g. `https://consent.truzyx.com`. Optional — defaults to the origin of the `<script src=".../consent-widget.js">` tag, or `window.location.origin`. |
|
|
22
|
-
| `token` | Principal JWT for the end user, issued by your backend. Sent to the gateway as `?
|
|
22
|
+
| `token` | Principal JWT for the end user, issued by your backend. Sent to the gateway as `?auth_token=` and via `postMessage`. |
|
|
23
23
|
| `requestId` | Consent request id created via the gateway API. Required for the consent dialog only. |
|
|
24
24
|
|
|
25
25
|
## Usage
|
|
@@ -128,14 +128,14 @@ The hook subscribes/unsubscribes the callback automatically and closes the dialo
|
|
|
128
128
|
|
|
129
129
|
## How it works
|
|
130
130
|
|
|
131
|
-
- The widget renders a modal overlay with an `<iframe>` pointing at `{baseUrl}/consent_manager/consent/{requestId}` or `{baseUrl}/consent_manager/preferences`, with `?
|
|
131
|
+
- The widget renders a modal overlay with an `<iframe>` pointing at `{baseUrl}/consent_manager/consent/{requestId}` or `{baseUrl}/consent_manager/preferences`, with `?auth_token=<token>&embed=true&origin=<your-origin>`.
|
|
132
132
|
- The token is also delivered via `postMessage` (`PRINCIPAL_TOKEN`) when the iframe signals readiness.
|
|
133
133
|
- Flow completion is reported back as `CONSENT_WIDGET_EVENT` messages, validated against the gateway origin, and fanned out to your callbacks. In embed mode the gateway pages render chromeless (no extra card/background) so only the widget's single card is visible.
|
|
134
134
|
- Because the dialog is an iframe, there is no CSS leakage in either direction.
|
|
135
135
|
|
|
136
136
|
## Trying it locally
|
|
137
137
|
|
|
138
|
-
The gateway repo ships a dev harness at `/consent_manager/check` (query params `requestId`, `token`/`
|
|
138
|
+
The gateway repo ships a dev harness at `/consent_manager/check` (query params `requestId`, `token`/`auth_token`, `baseUrl`) that drives this same core and logs every callback event — useful to validate your `requestId`/`token` before integrating.
|
|
139
139
|
|
|
140
140
|
## Package layout
|
|
141
141
|
|
package/dist/core/types.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ export type WidgetCallback = (event: WidgetEvent) => void;
|
|
|
28
28
|
export interface WidgetInitOptions {
|
|
29
29
|
/** Consent request id. Required for mode "consent", omitted for "preferences". */
|
|
30
30
|
requestId?: string;
|
|
31
|
-
/** Principal JWT used by the gateway APIs (sent as ?
|
|
31
|
+
/** Principal JWT used by the gateway APIs (sent as ?auth_token= + postMessage). */
|
|
32
32
|
token: string;
|
|
33
33
|
/** Which dialog to open. Defaults to "consent" when requestId is present. */
|
|
34
34
|
mode?: WidgetMode;
|
package/dist/core/widget.d.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* plain <script> inclusion (see `vite.widget.config.ts` at the repo root).
|
|
14
14
|
*
|
|
15
15
|
* How it works: opens the gateway consent / preferences page in a modal
|
|
16
|
-
* iframe overlay. Auth token travels as `?
|
|
16
|
+
* iframe overlay. Auth token travels as `?auth_token=` (already supported by
|
|
17
17
|
* `usePrincipalTokenHandshake`) and via postMessage PRINCIPAL_TOKEN.
|
|
18
18
|
* Completion events flow back as CONSENT_WIDGET_EVENT messages and are
|
|
19
19
|
* fanned out to `onCallback`.
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var p=Object.defineProperty;var y=(n,e,t)=>e in n?p(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var s=(n,e,t)=>y(n,typeof e!="symbol"?e+"":e,t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const c={live:"https://ui.gateway.nodbook.in",sandbox:"https://sandbox-ui.gateway.nodbook.in"},b="CONSENT_WIDGET_EVENT",h="GATEWAY_UI_READY",u="PRINCIPAL_TOKEN";function E(n){return typeof n=="object"&&n!==null&&n.type===b&&typeof n.event=="object"}function
|
|
1
|
+
"use strict";var p=Object.defineProperty;var y=(n,e,t)=>e in n?p(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var s=(n,e,t)=>y(n,typeof e!="symbol"?e+"":e,t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const c={live:"https://ui.gateway.nodbook.in",sandbox:"https://sandbox-ui.gateway.nodbook.in"},b="CONSENT_WIDGET_EVENT",h="GATEWAY_UI_READY",u="PRINCIPAL_TOKEN";function E(n){return typeof n=="object"&&n!==null&&n.type===b&&typeof n.event=="object"}function _(n){return typeof n=="object"&&n!==null&&n.type===h}const I="__consent_widget_overlay__",k=600,x=640;function A(n,e){var t;if(n)return n.replace(/\/$/,"");if(e&&c[e])return c[e];try{const r=document.getElementsByTagName("script");for(let i=r.length-1;i>=0;i--){const a=((t=r[i])==null?void 0:t.getAttribute("src"))??"";if(a.includes("consent-widget"))return new URL(a,window.location.href).origin}const o=document.currentScript;if(o!=null&&o.src)return new URL(o.src,window.location.href).origin}catch{}return window.location.origin}function C(n){var t;const e=new URLSearchParams({auth_token:n.token,embed:"true",origin:window.location.origin});return n.mode==="preferences"?`${n.baseUrl}/consent_manager/preferences?${e.toString()}`:n.mode==="direct_consent"?((t=n.purposeIds)!=null&&t.length&&e.set("purpose_id",n.purposeIds.join(",")),n.orgId&&e.set("org_id",n.orgId),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()}`}class v{constructor(){s(this,"overlay",null);s(this,"iframe",null);s(this,"session",null);s(this,"callbacks",new Set);s(this,"listening",!1);s(this,"escHandler",e=>{var t;e.key==="Escape"&&this.close({type:"closed",requestId:(t=this.session)==null?void 0:t.requestId})});s(this,"messageHandler",e=>{var r,o;if(!this.session||!this.iframe)return;let t="";try{t=new URL(this.session.baseUrl).origin}catch{t=""}if(!(t&&e.origin!==t)&&e.source===this.iframe.contentWindow){if(_(e.data)){try{(r=this.iframe.contentWindow)==null||r.postMessage({type:u,token:this.session.token},t||"*")}catch{}return}if(typeof e.data=="object"&&e.data!==null&&e.data.type===h){try{(o=this.iframe.contentWindow)==null||o.postMessage({type:u,token:this.session.token},t||"*")}catch{}return}if(E(e.data)){const i=e.data.event;this.emit(i),this.session.autoClose&&(i.type==="consent_accepted"||i.type==="consent_declined"||i.type==="already_resolved"||i.type==="redirecting")&&this.teardown()}}})}openConsent(e,t,r){this.openDialog({...r,requestId:e,token:(r==null?void 0:r.token)??t})}openPreferences(e,t){this.openDialog({...t,token:(t==null?void 0:t.token)??e})}openDirectConsent(e,t,r){const o=(Array.isArray(e)?e:[e]).map(i=>i.trim()).filter(Boolean);if(o.length===0)throw new Error("ConsentWidget.openDirectConsent: at least one `purposeId` is required.");this.openDialog({...r,purposeIds:o,token:(r==null?void 0:r.token)??t,mode:"direct_consent"})}openDialog(e){var r;if(!e||typeof e.token!="string"||e.token.length===0)throw new Error("ConsentWidget: `token` is required.");const t=e.mode??(e.requestId?"consent":"preferences");if(t==="consent"&&!e.requestId)throw new Error("ConsentWidget: `requestId` is required for the consent dialog.");this.open({requestId:e.requestId,token:e.token,mode:t,purposeIds:e.purposeIds,orgId:e.orgId??null,locale:e.locale,baseUrl:A(e.baseUrl,e.env),autoClose:e.autoClose??!0}),this.render(e.allowClose??!0,e.width??k,e.height??x),this.emit({type:"opened",requestId:e.requestId,data:(r=e.purposeIds)!=null&&r.length?{purposeIds:e.purposeIds}:void 0})}onCallback(e){return this.callbacks.add(e),()=>{this.callbacks.delete(e)}}close(e){var o;const t=e??{type:"closed",requestId:(o=this.session)==null?void 0:o.requestId},r=this.overlay!==null;this.teardown(),r&&this.emit(t)}destroy(){this.callbacks.clear(),this.teardown()}isOpen(){return this.overlay!==null}open(e){this.teardown(),this.session=e,this.ensureListening()}ensureListening(){this.listening||(window.addEventListener("message",this.messageHandler),window.addEventListener("keydown",this.escHandler),this.listening=!0)}render(e,t,r){if(!this.session)return;const o=C(this.session),i=document.createElement("div");i.id=I,i.setAttribute("role","dialog"),i.setAttribute("aria-modal","true"),i.style.cssText=["position:fixed","inset:0","z-index:2147483647","display:flex","align-items:center","justify-content:center","background:rgba(15,23,42,0.55)","padding:16px","box-sizing:border-box"].join(";");const a=document.createElement("div"),f=Math.min(t,window.innerWidth-32),m=Math.min(r,window.innerHeight-32);if(a.style.cssText=[`width:${f}px`,"max-width:100%",`height:${m}px`,"max-height:100%","background:#fff","border-radius:16px","overflow:hidden","position:relative","box-shadow:0 25px 50px -12px rgba(0,0,0,0.35)","display:flex","flex-direction:column"].join(";"),e){const l=document.createElement("button");l.setAttribute("aria-label","Close"),l.textContent="×",l.style.cssText=["position:absolute","top:8px","right:12px","z-index:2","width:28px","height:28px","border-radius:9999px","border:none","background:rgba(15,23,42,0.06)","color:#475569","font-size:18px","line-height:1","cursor:pointer"].join(";"),l.onclick=()=>this.close(),a.appendChild(l),i.addEventListener("mousedown",w=>{w.target===i&&this.close()})}const d=document.createElement("iframe");d.src=o,d.title="Consent manager",d.setAttribute("allow","clipboard-write"),d.style.cssText=["flex:1","width:100%","height:100%","border:0","background:#fff"].join(";"),a.appendChild(d),i.appendChild(a),document.body.appendChild(i),document.documentElement.style.overflow="hidden",this.overlay=i,this.iframe=d}teardown(){var e;(e=this.overlay)!=null&&e.parentNode&&this.overlay.parentNode.removeChild(this.overlay),this.overlay=null,this.iframe=null,this.session=null,this.listening&&(window.removeEventListener("message",this.messageHandler),window.removeEventListener("keydown",this.escHandler),this.listening=!1);try{document.documentElement.style.overflow=""}catch{}}emit(e){this.callbacks.forEach(t=>{try{t(e)}catch{}})}}const g=new v;exports.ConsentWidget=g;exports.GATEWAY_BASE_URLS=c;exports.default=g;
|
package/dist/index.js
CHANGED
package/dist/react.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var E=Object.defineProperty;var k=(n,e,t)=>e in n?E(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var h=(n,e,t)=>k(n,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("react"),w={live:"https://ui.gateway.nodbook.in",sandbox:"https://sandbox-ui.gateway.nodbook.in"},C="CONSENT_WIDGET_EVENT",y="GATEWAY_UI_READY",b="PRINCIPAL_TOKEN";function I(n){return typeof n=="object"&&n!==null&&n.type===C&&typeof n.event=="object"}function _(n){return typeof n=="object"&&n!==null&&n.type===y}const x="__consent_widget_overlay__",v=600,A=640;function T(n,e){var t;if(n)return n.replace(/\/$/,"");if(e&&w[e])return w[e];try{const r=document.getElementsByTagName("script");for(let s=r.length-1;s>=0;s--){const c=((t=r[s])==null?void 0:t.getAttribute("src"))??"";if(c.includes("consent-widget"))return new URL(c,window.location.href).origin}const i=document.currentScript;if(i!=null&&i.src)return new URL(i.src,window.location.href).origin}catch{}return window.location.origin}function W(n){var t;const e=new URLSearchParams({
|
|
1
|
+
"use strict";var E=Object.defineProperty;var k=(n,e,t)=>e in n?E(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var h=(n,e,t)=>k(n,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("react"),w={live:"https://ui.gateway.nodbook.in",sandbox:"https://sandbox-ui.gateway.nodbook.in"},C="CONSENT_WIDGET_EVENT",y="GATEWAY_UI_READY",b="PRINCIPAL_TOKEN";function I(n){return typeof n=="object"&&n!==null&&n.type===C&&typeof n.event=="object"}function _(n){return typeof n=="object"&&n!==null&&n.type===y}const x="__consent_widget_overlay__",v=600,A=640;function T(n,e){var t;if(n)return n.replace(/\/$/,"");if(e&&w[e])return w[e];try{const r=document.getElementsByTagName("script");for(let s=r.length-1;s>=0;s--){const c=((t=r[s])==null?void 0:t.getAttribute("src"))??"";if(c.includes("consent-widget"))return new URL(c,window.location.href).origin}const i=document.currentScript;if(i!=null&&i.src)return new URL(i.src,window.location.href).origin}catch{}return window.location.origin}function W(n){var t;const e=new URLSearchParams({auth_token:n.token,embed:"true",origin:window.location.origin});return n.mode==="preferences"?`${n.baseUrl}/consent_manager/preferences?${e.toString()}`:n.mode==="direct_consent"?((t=n.purposeIds)!=null&&t.length&&e.set("purpose_id",n.purposeIds.join(",")),n.orgId&&e.set("org_id",n.orgId),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()}`}class U{constructor(){h(this,"overlay",null);h(this,"iframe",null);h(this,"session",null);h(this,"callbacks",new Set);h(this,"listening",!1);h(this,"escHandler",e=>{var t;e.key==="Escape"&&this.close({type:"closed",requestId:(t=this.session)==null?void 0:t.requestId})});h(this,"messageHandler",e=>{var r,i;if(!this.session||!this.iframe)return;let t="";try{t=new URL(this.session.baseUrl).origin}catch{t=""}if(!(t&&e.origin!==t)&&e.source===this.iframe.contentWindow){if(_(e.data)){try{(r=this.iframe.contentWindow)==null||r.postMessage({type:b,token:this.session.token},t||"*")}catch{}return}if(typeof e.data=="object"&&e.data!==null&&e.data.type===y){try{(i=this.iframe.contentWindow)==null||i.postMessage({type:b,token:this.session.token},t||"*")}catch{}return}if(I(e.data)){const s=e.data.event;this.emit(s),this.session.autoClose&&(s.type==="consent_accepted"||s.type==="consent_declined"||s.type==="already_resolved"||s.type==="redirecting")&&this.teardown()}}})}openConsent(e,t,r){this.openDialog({...r,requestId:e,token:(r==null?void 0:r.token)??t})}openPreferences(e,t){this.openDialog({...t,token:(t==null?void 0:t.token)??e})}openDirectConsent(e,t,r){const i=(Array.isArray(e)?e:[e]).map(s=>s.trim()).filter(Boolean);if(i.length===0)throw new Error("ConsentWidget.openDirectConsent: at least one `purposeId` is required.");this.openDialog({...r,purposeIds:i,token:(r==null?void 0:r.token)??t,mode:"direct_consent"})}openDialog(e){var r;if(!e||typeof e.token!="string"||e.token.length===0)throw new Error("ConsentWidget: `token` is required.");const t=e.mode??(e.requestId?"consent":"preferences");if(t==="consent"&&!e.requestId)throw new Error("ConsentWidget: `requestId` is required for the consent dialog.");this.open({requestId:e.requestId,token:e.token,mode:t,purposeIds:e.purposeIds,orgId:e.orgId??null,locale:e.locale,baseUrl:T(e.baseUrl,e.env),autoClose:e.autoClose??!0}),this.render(e.allowClose??!0,e.width??v,e.height??A),this.emit({type:"opened",requestId:e.requestId,data:(r=e.purposeIds)!=null&&r.length?{purposeIds:e.purposeIds}:void 0})}onCallback(e){return this.callbacks.add(e),()=>{this.callbacks.delete(e)}}close(e){var i;const t=e??{type:"closed",requestId:(i=this.session)==null?void 0:i.requestId},r=this.overlay!==null;this.teardown(),r&&this.emit(t)}destroy(){this.callbacks.clear(),this.teardown()}isOpen(){return this.overlay!==null}open(e){this.teardown(),this.session=e,this.ensureListening()}ensureListening(){this.listening||(window.addEventListener("message",this.messageHandler),window.addEventListener("keydown",this.escHandler),this.listening=!0)}render(e,t,r){if(!this.session)return;const i=W(this.session),s=document.createElement("div");s.id=x,s.setAttribute("role","dialog"),s.setAttribute("aria-modal","true"),s.style.cssText=["position:fixed","inset:0","z-index:2147483647","display:flex","align-items:center","justify-content:center","background:rgba(15,23,42,0.55)","padding:16px","box-sizing:border-box"].join(";");const c=document.createElement("div"),g=Math.min(t,window.innerWidth-32),p=Math.min(r,window.innerHeight-32);if(c.style.cssText=[`width:${g}px`,"max-width:100%",`height:${p}px`,"max-height:100%","background:#fff","border-radius:16px","overflow:hidden","position:relative","box-shadow:0 25px 50px -12px rgba(0,0,0,0.35)","display:flex","flex-direction:column"].join(";"),e){const o=document.createElement("button");o.setAttribute("aria-label","Close"),o.textContent="×",o.style.cssText=["position:absolute","top:8px","right:12px","z-index:2","width:28px","height:28px","border-radius:9999px","border:none","background:rgba(15,23,42,0.06)","color:#475569","font-size:18px","line-height:1","cursor:pointer"].join(";"),o.onclick=()=>this.close(),c.appendChild(o),s.addEventListener("mousedown",l=>{l.target===s&&this.close()})}const u=document.createElement("iframe");u.src=i,u.title="Consent manager",u.setAttribute("allow","clipboard-write"),u.style.cssText=["flex:1","width:100%","height:100%","border:0","background:#fff"].join(";"),c.appendChild(u),s.appendChild(c),document.body.appendChild(s),document.documentElement.style.overflow="hidden",this.overlay=s,this.iframe=u}teardown(){var e;(e=this.overlay)!=null&&e.parentNode&&this.overlay.parentNode.removeChild(this.overlay),this.overlay=null,this.iframe=null,this.session=null,this.listening&&(window.removeEventListener("message",this.messageHandler),window.removeEventListener("keydown",this.escHandler),this.listening=!1);try{document.documentElement.style.overflow=""}catch{}}emit(e){this.callbacks.forEach(t=>{try{t(e)}catch{}})}}const a=new U;function q(n){const{onEvent:e,closeOnUnmount:t=!0}=n??{};d.useEffect(()=>{if(e)return a.onCallback(e)},[e]),d.useEffect(()=>{if(t)return()=>{a.isOpen()&&a.close()}},[t]);const r=d.useCallback(o=>{const{requestId:l,token:f,...m}=o;l?a.openConsent(l,f,{...m,token:f}):a.openPreferences(f,m)},[]),i=d.useCallback((o,l,f)=>{a.openConsent(o,l,f)},[]),s=d.useCallback((o,l)=>{a.openPreferences(o,l)},[]),c=d.useCallback((o,l,f)=>{a.openDirectConsent(o,l,f)},[]),g=d.useCallback(o=>{a.close(o)},[]),p=d.useCallback(o=>a.onCallback(o),[]),u=d.useCallback(()=>a.isOpen(),[]);return{open:r,openConsent:i,openPreferences:s,openDirectConsent:c,close:g,onEvent:p,isOpen:u}}exports.useConsentWidget=q;
|
package/dist/react.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@truzyx/consent-widget",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.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",
|