@truzyx/consent-widget 0.2.1 → 0.2.2

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 CHANGED
@@ -35,13 +35,13 @@ ConsentWidget.openConsent("req_123", "<principal-jwt>", {
35
35
  baseUrl: "https://consent.truzyx.com", // optional
36
36
  });
37
37
 
38
- // Or preferences dialog (my consents + grievances, no requestId)
38
+ // Or preferences dialog (my consents + grievances)
39
39
  ConsentWidget.openPreferences("<principal-jwt>", {
40
40
  baseUrl: "https://consent.truzyx.com",
41
41
  });
42
42
 
43
- // Or direct-consent dialog for purpose(s)
44
- ConsentWidget.openDirectConsent("purpose_123", "<principal-jwt>", {
43
+ // Or direct-consent dialog for a purpose
44
+ ConsentWidget.openDirectConsent("purpose_123", "<principal-jwt>", null, undefined, {
45
45
  env: "sandbox", // or live; ignored when baseUrl is set
46
46
  });
47
47
 
@@ -97,25 +97,24 @@ The hook subscribes/unsubscribes the callback automatically and closes the dialo
97
97
 
98
98
  ### Methods
99
99
 
100
- Vanilla singleton methods and hook return value are the same, except the hook additionally exposes `open(options)` (routes to consent when `requestId` is set, else preferences) and names the subscribe function `onEvent` instead of `onCallback`.
100
+ Vanilla singleton methods and hook return value are the same, except the hook additionally exposes `open(token: string, options?: WidgetOptions & { requestId?: string })` (`options.requestId` set → consent, else preferences) and names the subscribe function `onEvent` instead of `onCallback`.
101
101
 
102
102
  | Method | Signature | Description |
103
103
  |--------|-----------|-------------|
104
- | `openConsent` | `(requestId, token, opts?) => void` | Open the consent dialog for a request. Replaces any open dialog. |
105
- | `openPreferences` | `(token, opts?) => void` | Open the preferences dialog. Replaces any open dialog. |
106
- | `openDirectConsent` | `(purposeIds, token, opts?) => void` | Open the direct-consent dialog for one or more purpose ids (`string \| string[]`, sent as `?purpose_id=`). `opts` adds `orgId` (`?org_id=`) and `locale` (`?locale=`). |
107
- | `onCallback` | `(cb: WidgetCallback) => () => void` | Subscribe to every flow event. Returns an unsubscribe function. |
108
- | `close` | `(event?) => void` | Close the dialog; emits a `closed` event if one was open. |
104
+ | `openConsent` | `(requestId: string, token: string, options?: WidgetOptions) => void` | Open the consent dialog for a request. Replaces any open dialog. |
105
+ | `openPreferences` | `(token: string, options?: WidgetOptions) => void` | Open the preferences dialog. Replaces any open dialog. |
106
+ | `openDirectConsent` | `(purposeId: string, token: string, orgId?: string \| null, locale?: string, options?: WidgetOptions) => void` | Open the direct-consent dialog (`purposeId` sent as `?purpose_id=`, `orgId` as `?org_id=`, `locale` as `?locale=`). |
107
+ | `onCallback` | `(cb: WidgetCallback) => void` | Subscribe to every flow event. Returns an unsubscribe function. |
108
+ | `close` | `(event?: WidgetEvent) => void` | Close the dialog; emits a `closed` event if one was open. |
109
109
  | `destroy` | `() => void` | Clear all callbacks and close without emitting. |
110
110
  | `isOpen` | `() => boolean` | Whether the dialog is open. |
111
111
 
112
- ### `WidgetInitOptions`
112
+ ### `WidgetOptions`
113
+
114
+ Common options shared by every method. Identifiers (`token`, `requestId`, `purposeId`, `orgId`, `locale`) travel directly as method arguments — never inside this object.
113
115
 
114
116
  | Field | Type | Default | Description |
115
117
  |-------|------|---------|-------------|
116
- | `token` | `string` | — (required) | Principal JWT. |
117
- | `requestId` | `string` | — | Consent request id. Required for `mode: "consent"`. |
118
- | `mode` | `"consent" \| "preferences" \| "direct_consent"` | `"consent"` if `requestId` is set, else `"preferences"` | Which dialog to open. Set implicitly by `openConsent` / `openPreferences` / `openDirectConsent`. |
119
118
  | `baseUrl` | `string` | script-tag origin, else current origin | Gateway origin. Optional — when provided it wins and `env` is ignored. |
120
119
  | `env` | `"live" \| "sandbox"` | — (no default) | Picks the gateway host when `baseUrl` is omitted: live → `https://ui.gateway.nodbook.in`, sandbox → `https://sandbox-ui.gateway.nodbook.in`. |
121
120
  | `autoClose` | `boolean` | `true` | Auto-close on terminal events (`consent_accepted`, `consent_declined`, `already_resolved`, `redirecting`). |
@@ -126,9 +125,9 @@ Vanilla singleton methods and hook return value are the same, except the hook ad
126
125
 
127
126
  | `type` | Extra fields | Meaning |
128
127
  |--------|--------------|---------|
129
- | `opened` | `requestId?`, `data?` | Dialog opened (`data.purposeIds` for direct consent). |
130
- | `consent_accepted` | `requestId?`, `data?` | User accepted (all or selected purposes). Direct consent includes `data.purposeIds`. |
131
- | `consent_declined` | `requestId?`, `data?` | User declined/cancelled. Direct consent includes `data.purposeIds`. |
128
+ | `opened` | `requestId?`, `data?` | Dialog opened (`data.purposeId` for direct consent). |
129
+ | `consent_accepted` | `requestId?`, `data?` | User accepted (all or selected purposes). Direct consent includes `data.purposeId`. |
130
+ | `consent_declined` | `requestId?`, `data?` | User declined/cancelled. Direct consent includes `data.purposeId`. |
132
131
  | `already_resolved` | `requestId?` | Request was already approved/declined. |
133
132
  | `redirecting` | `requestId?`, `redirectUrl`, `data?` | Gateway is redirecting (e.g. back to your site). |
134
133
  | `error` | `requestId?`, `message?`, `data?` | Load/submit failure. |
@@ -25,13 +25,13 @@ export interface WidgetEvent {
25
25
  data?: unknown;
26
26
  }
27
27
  export type WidgetCallback = (event: WidgetEvent) => void;
28
- export interface WidgetInitOptions {
29
- /** Consent request id. Required for mode "consent", omitted for "preferences". */
30
- requestId?: string;
31
- /** Principal JWT used by the gateway APIs (sent as ?auth_token= + postMessage). */
32
- token: string;
33
- /** Which dialog to open. Defaults to "consent" when requestId is present. */
34
- mode?: WidgetMode;
28
+ /**
29
+ * Common options shared by every widget method (`openConsent`,
30
+ * `openPreferences`, `openDirectConsent`, and the hook's `open`).
31
+ * Identifiers travel directly as method arguments (`requestId`, `token`,
32
+ * `purposeId`, `orgId`, `locale`) — never inside this object.
33
+ */
34
+ export interface WidgetOptions {
35
35
  /**
36
36
  * Gateway origin, e.g. "https://consent.truzyx.com".
37
37
  * Optional. When provided it wins and `env` is ignored.
@@ -49,19 +49,6 @@ export interface WidgetInitOptions {
49
49
  width?: number;
50
50
  height?: number;
51
51
  }
52
- export interface OpenConsentOptions extends Omit<WidgetInitOptions, "requestId" | "mode" | "token"> {
53
- token?: WidgetInitOptions["token"];
54
- }
55
- export interface OpenPreferencesOptions extends Omit<WidgetInitOptions, "requestId" | "mode" | "token"> {
56
- token?: WidgetInitOptions["token"];
57
- }
58
- export interface OpenDirectConsentOptions extends Omit<WidgetInitOptions, "requestId" | "mode" | "token"> {
59
- token?: WidgetInitOptions["token"];
60
- /** Organisation id, sent as `?org_id=`. */
61
- orgId?: string | null;
62
- /** Locale hint, sent as `?locale=`. */
63
- locale?: string;
64
- }
65
52
  /** postMessage wire format: iframe (gateway) -> parent (third-party site). */
66
53
  export declare const WIDGET_EVENT_MESSAGE = "CONSENT_WIDGET_EVENT";
67
54
  /** postMessage wire format: iframe -> parent "i am ready, send me the token". */
@@ -18,7 +18,7 @@
18
18
  * Completion events flow back as CONSENT_WIDGET_EVENT messages and are
19
19
  * fanned out to `onCallback`.
20
20
  */
21
- import { type OpenConsentOptions, type OpenDirectConsentOptions, type OpenPreferencesOptions, type WidgetCallback, type WidgetEvent, type WidgetInitOptions } from "./types";
21
+ import { type WidgetCallback, type WidgetEvent, type WidgetOptions } from "./types";
22
22
  declare class ConsentWidgetImpl {
23
23
  private overlay;
24
24
  private iframe;
@@ -28,15 +28,13 @@ declare class ConsentWidgetImpl {
28
28
  private escHandler;
29
29
  private messageHandler;
30
30
  /** Open the consent dialog for a request. */
31
- openConsent(requestId: string, token: string, opts?: OpenConsentOptions): void;
31
+ openConsent(requestId: string, token: string, options?: WidgetOptions): void;
32
32
  /** Open the preferences (my consents + grievances) dialog. */
33
- openPreferences(token: string, opts?: OpenPreferencesOptions): void;
34
- /** Open the direct-consent (permission-style) dialog for purpose(s). */
35
- openDirectConsent(purposeIds: string | string[], token: string, opts?: OpenDirectConsentOptions): void;
33
+ openPreferences(token: string, options?: WidgetOptions): void;
34
+ /** Open the direct-consent (permission-style) dialog for a purpose. */
35
+ openDirectConsent(purposeId: string, token: string, orgId?: string | null, locale?: string, options?: WidgetOptions): void;
36
36
  /**
37
37
  * Shared opener behind `openConsent` / `openPreferences` / `openDirectConsent`.
38
- * The view follows `mode`, defaulting from `requestId`: set → consent
39
- * dialog, omitted → preferences dialog.
40
38
  */
41
39
  private openDialog;
42
40
  /**
@@ -57,4 +55,4 @@ declare class ConsentWidgetImpl {
57
55
  private emit;
58
56
  }
59
57
  export declare const ConsentWidget: ConsentWidgetImpl;
60
- export type { WidgetInitOptions, WidgetEvent, WidgetCallback };
58
+ export type { WidgetOptions, WidgetEvent, WidgetCallback };
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 _(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;
1
+ "use strict";var w=Object.defineProperty;var y=(n,e,t)=>e in n?w(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var a=(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__",x=600,k=640;function C(n,e){var t;if(n)return n.replace(/\/$/,"");if(e&&c[e])return c[e];try{const o=document.getElementsByTagName("script");for(let i=o.length-1;i>=0;i--){const s=((t=o[i])==null?void 0:t.getAttribute("src"))??"";if(s.includes("consent-widget"))return new URL(s,window.location.href).origin}const r=document.currentScript;if(r!=null&&r.src)return new URL(r.src,window.location.href).origin}catch{}return window.location.origin}function v(n){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"?(n.purposeId&&e.set("purpose_id",n.purposeId),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 A{constructor(){a(this,"overlay",null);a(this,"iframe",null);a(this,"session",null);a(this,"callbacks",new Set);a(this,"listening",!1);a(this,"escHandler",e=>{var t;e.key==="Escape"&&this.close({type:"closed",requestId:(t=this.session)==null?void 0:t.requestId})});a(this,"messageHandler",e=>{var o,r;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{(o=this.iframe.contentWindow)==null||o.postMessage({type:u,token:this.session.token},t||"*")}catch{}return}if(typeof e.data=="object"&&e.data!==null&&e.data.type===h){try{(r=this.iframe.contentWindow)==null||r.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,o){this.openDialog({...o,requestId:e,token:t,mode:"consent"})}openPreferences(e,t){this.openDialog({...t,token:e,mode:"preferences"})}openDirectConsent(e,t,o,r,i){const s=e==null?void 0:e.trim();if(!s)throw new Error("ConsentWidget.openDirectConsent: `purposeId` is required.");this.openDialog({...i,purposeId:s,token:t,orgId:o??null,locale:r,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=((r=e.purposeId)==null?void 0:r.trim())||void 0,o=e.mode??(t?"direct_consent":e.requestId?"consent":"preferences");if(o==="consent"&&!e.requestId)throw new Error("ConsentWidget: `requestId` is required for the consent dialog.");this.open({requestId:e.requestId,token:e.token,mode:o,purposeId:t,orgId:e.orgId??null,locale:e.locale,baseUrl:C(e.baseUrl,e.env),autoClose:e.autoClose??!0}),this.render(e.allowClose??!0,e.width??x,e.height??k),this.emit({type:"opened",requestId:e.requestId,data:t?{purposeId:t}:void 0})}onCallback(e){return this.callbacks.add(e),()=>{this.callbacks.delete(e)}}close(e){var r;const t=e??{type:"closed",requestId:(r=this.session)==null?void 0:r.requestId},o=this.overlay!==null;this.teardown(),o&&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,o){if(!this.session)return;const r=v(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 s=document.createElement("div"),f=Math.min(t,window.innerWidth-32),p=Math.min(o,window.innerHeight-32);if(s.style.cssText=[`width:${f}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 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(),s.appendChild(l),i.addEventListener("mousedown",m=>{m.target===i&&this.close()})}const d=document.createElement("iframe");d.src=r,d.title="Consent manager",d.setAttribute("allow","clipboard-write"),d.style.cssText=["flex:1","width:100%","height:100%","border:0","background:#fff"].join(";"),s.appendChild(d),i.appendChild(s),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 A;exports.ConsentWidget=g;exports.GATEWAY_BASE_URLS=c;exports.default=g;
package/dist/index.d.ts CHANGED
@@ -16,4 +16,4 @@ export { ConsentWidget } from "./core/widget";
16
16
  import { ConsentWidget as ConsentWidgetDefault } from "./core/widget";
17
17
  export default ConsentWidgetDefault;
18
18
  export { GATEWAY_BASE_URLS } from "./core/types";
19
- export type { WidgetCallback, WidgetEnv, WidgetEvent, WidgetEventType, WidgetInitOptions, WidgetMode, OpenConsentOptions, OpenDirectConsentOptions, OpenPreferencesOptions, } from "./core/types";
19
+ export type { WidgetCallback, WidgetEnv, WidgetEvent, WidgetEventType, WidgetOptions, WidgetMode, } from "./core/types";
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- var w = Object.defineProperty;
2
- var p = (n, e, t) => e in n ? w(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
- var s = (n, e, t) => p(n, typeof e != "symbol" ? e + "" : e, t);
1
+ var m = Object.defineProperty;
2
+ var w = (n, e, t) => e in n ? m(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
+ var a = (n, e, t) => w(n, typeof e != "symbol" ? e + "" : e, t);
4
4
  const c = {
5
5
  live: "https://ui.gateway.nodbook.in",
6
6
  sandbox: "https://sandbox-ui.gateway.nodbook.in"
@@ -11,46 +11,45 @@ function b(n) {
11
11
  function E(n) {
12
12
  return typeof n == "object" && n !== null && n.type === u;
13
13
  }
14
- const I = "__consent_widget_overlay__", _ = 600, k = 640;
15
- function x(n, e) {
14
+ const _ = "__consent_widget_overlay__", I = 600, x = 640;
15
+ function k(n, e) {
16
16
  var t;
17
17
  if (n) return n.replace(/\/$/, "");
18
18
  if (e && c[e]) return c[e];
19
19
  try {
20
- const r = document.getElementsByTagName("script");
21
- for (let i = r.length - 1; i >= 0; i--) {
22
- const a = ((t = r[i]) == null ? void 0 : t.getAttribute("src")) ?? "";
23
- if (a.includes("consent-widget"))
24
- return new URL(a, window.location.href).origin;
20
+ const o = document.getElementsByTagName("script");
21
+ for (let i = o.length - 1; i >= 0; i--) {
22
+ const s = ((t = o[i]) == null ? void 0 : t.getAttribute("src")) ?? "";
23
+ if (s.includes("consent-widget"))
24
+ return new URL(s, window.location.href).origin;
25
25
  }
26
- const o = document.currentScript;
27
- if (o != null && o.src) return new URL(o.src, window.location.href).origin;
26
+ const r = document.currentScript;
27
+ if (r != null && r.src) return new URL(r.src, window.location.href).origin;
28
28
  } catch {
29
29
  }
30
30
  return window.location.origin;
31
31
  }
32
32
  function C(n) {
33
- var t;
34
33
  const e = new URLSearchParams({
35
34
  auth_token: n.token,
36
35
  embed: "true",
37
36
  origin: window.location.origin
38
37
  });
39
- 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()}`;
38
+ return n.mode === "preferences" ? `${n.baseUrl}/consent_manager/preferences?${e.toString()}` : n.mode === "direct_consent" ? (n.purposeId && e.set("purpose_id", n.purposeId), 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()}`;
40
39
  }
41
- class A {
40
+ class v {
42
41
  constructor() {
43
- s(this, "overlay", null);
44
- s(this, "iframe", null);
45
- s(this, "session", null);
46
- s(this, "callbacks", /* @__PURE__ */ new Set());
47
- s(this, "listening", !1);
48
- s(this, "escHandler", (e) => {
42
+ a(this, "overlay", null);
43
+ a(this, "iframe", null);
44
+ a(this, "session", null);
45
+ a(this, "callbacks", /* @__PURE__ */ new Set());
46
+ a(this, "listening", !1);
47
+ a(this, "escHandler", (e) => {
49
48
  var t;
50
49
  e.key === "Escape" && this.close({ type: "closed", requestId: (t = this.session) == null ? void 0 : t.requestId });
51
50
  });
52
- s(this, "messageHandler", (e) => {
53
- var r, o;
51
+ a(this, "messageHandler", (e) => {
52
+ var o, r;
54
53
  if (!this.session || !this.iframe) return;
55
54
  let t = "";
56
55
  try {
@@ -61,7 +60,7 @@ class A {
61
60
  if (!(t && e.origin !== t) && e.source === this.iframe.contentWindow) {
62
61
  if (E(e.data)) {
63
62
  try {
64
- (r = this.iframe.contentWindow) == null || r.postMessage(
63
+ (o = this.iframe.contentWindow) == null || o.postMessage(
65
64
  { type: h, token: this.session.token },
66
65
  t || "*"
67
66
  );
@@ -71,7 +70,7 @@ class A {
71
70
  }
72
71
  if (typeof e.data == "object" && e.data !== null && e.data.type === u) {
73
72
  try {
74
- (o = this.iframe.contentWindow) == null || o.postMessage(
73
+ (r = this.iframe.contentWindow) == null || r.postMessage(
75
74
  { type: h, token: this.session.token },
76
75
  t || "*"
77
76
  );
@@ -87,61 +86,47 @@ class A {
87
86
  });
88
87
  }
89
88
  /** Open the consent dialog for a request. */
90
- openConsent(e, t, r) {
91
- this.openDialog({
92
- ...r,
93
- requestId: e,
94
- token: (r == null ? void 0 : r.token) ?? t
95
- });
89
+ openConsent(e, t, o) {
90
+ this.openDialog({ ...o, requestId: e, token: t, mode: "consent" });
96
91
  }
97
92
  /** Open the preferences (my consents + grievances) dialog. */
98
93
  openPreferences(e, t) {
99
- this.openDialog({
100
- ...t,
101
- token: (t == null ? void 0 : t.token) ?? e
102
- });
94
+ this.openDialog({ ...t, token: e, mode: "preferences" });
103
95
  }
104
- /** Open the direct-consent (permission-style) dialog for purpose(s). */
105
- openDirectConsent(e, t, r) {
106
- const o = (Array.isArray(e) ? e : [e]).map((i) => i.trim()).filter(Boolean);
107
- if (o.length === 0)
108
- throw new Error("ConsentWidget.openDirectConsent: at least one `purposeId` is required.");
109
- this.openDialog({
110
- ...r,
111
- purposeIds: o,
112
- token: (r == null ? void 0 : r.token) ?? t,
113
- mode: "direct_consent"
114
- });
96
+ /** Open the direct-consent (permission-style) dialog for a purpose. */
97
+ openDirectConsent(e, t, o, r, i) {
98
+ const s = e == null ? void 0 : e.trim();
99
+ if (!s)
100
+ throw new Error("ConsentWidget.openDirectConsent: `purposeId` is required.");
101
+ this.openDialog({ ...i, purposeId: s, token: t, orgId: o ?? null, locale: r, mode: "direct_consent" });
115
102
  }
116
103
  /**
117
104
  * Shared opener behind `openConsent` / `openPreferences` / `openDirectConsent`.
118
- * The view follows `mode`, defaulting from `requestId`: set → consent
119
- * dialog, omitted → preferences dialog.
120
105
  */
121
106
  openDialog(e) {
122
107
  var r;
123
108
  if (!e || typeof e.token != "string" || e.token.length === 0)
124
109
  throw new Error("ConsentWidget: `token` is required.");
125
- const t = e.mode ?? (e.requestId ? "consent" : "preferences");
126
- if (t === "consent" && !e.requestId)
110
+ const t = ((r = e.purposeId) == null ? void 0 : r.trim()) || void 0, o = e.mode ?? (t ? "direct_consent" : e.requestId ? "consent" : "preferences");
111
+ if (o === "consent" && !e.requestId)
127
112
  throw new Error("ConsentWidget: `requestId` is required for the consent dialog.");
128
113
  this.open({
129
114
  requestId: e.requestId,
130
115
  token: e.token,
131
- mode: t,
132
- purposeIds: e.purposeIds,
116
+ mode: o,
117
+ purposeId: t,
133
118
  orgId: e.orgId ?? null,
134
119
  locale: e.locale,
135
- baseUrl: x(e.baseUrl, e.env),
120
+ baseUrl: k(e.baseUrl, e.env),
136
121
  autoClose: e.autoClose ?? !0
137
122
  }), this.render(
138
123
  e.allowClose ?? !0,
139
- e.width ?? _,
140
- e.height ?? k
124
+ e.width ?? I,
125
+ e.height ?? x
141
126
  ), this.emit({
142
127
  type: "opened",
143
128
  requestId: e.requestId,
144
- data: (r = e.purposeIds) != null && r.length ? { purposeIds: e.purposeIds } : void 0
129
+ data: t ? { purposeId: t } : void 0
145
130
  });
146
131
  }
147
132
  /**
@@ -156,12 +141,12 @@ class A {
156
141
  }
157
142
  /** Programmatically close the dialog (emits a `closed` event). */
158
143
  close(e) {
159
- var o;
144
+ var r;
160
145
  const t = e ?? {
161
146
  type: "closed",
162
- requestId: (o = this.session) == null ? void 0 : o.requestId
163
- }, r = this.overlay !== null;
164
- this.teardown(), r && this.emit(t);
147
+ requestId: (r = this.session) == null ? void 0 : r.requestId
148
+ }, o = this.overlay !== null;
149
+ this.teardown(), o && this.emit(t);
165
150
  }
166
151
  /** Alias of close() without emitting when already closed. */
167
152
  destroy() {
@@ -177,10 +162,10 @@ class A {
177
162
  ensureListening() {
178
163
  this.listening || (window.addEventListener("message", this.messageHandler), window.addEventListener("keydown", this.escHandler), this.listening = !0);
179
164
  }
180
- render(e, t, r) {
165
+ render(e, t, o) {
181
166
  if (!this.session) return;
182
- const o = C(this.session), i = document.createElement("div");
183
- i.id = I, i.setAttribute("role", "dialog"), i.setAttribute("aria-modal", "true"), i.style.cssText = [
167
+ const r = C(this.session), i = document.createElement("div");
168
+ i.id = _, i.setAttribute("role", "dialog"), i.setAttribute("aria-modal", "true"), i.style.cssText = [
184
169
  "position:fixed",
185
170
  "inset:0",
186
171
  "z-index:2147483647",
@@ -191,8 +176,8 @@ class A {
191
176
  "padding:16px",
192
177
  "box-sizing:border-box"
193
178
  ].join(";");
194
- const a = document.createElement("div"), g = Math.min(t, window.innerWidth - 32), f = Math.min(r, window.innerHeight - 32);
195
- if (a.style.cssText = [
179
+ const s = document.createElement("div"), g = Math.min(t, window.innerWidth - 32), f = Math.min(o, window.innerHeight - 32);
180
+ if (s.style.cssText = [
196
181
  `width:${g}px`,
197
182
  "max-width:100%",
198
183
  `height:${f}px`,
@@ -220,18 +205,18 @@ class A {
220
205
  "font-size:18px",
221
206
  "line-height:1",
222
207
  "cursor:pointer"
223
- ].join(";"), l.onclick = () => this.close(), a.appendChild(l), i.addEventListener("mousedown", (m) => {
224
- m.target === i && this.close();
208
+ ].join(";"), l.onclick = () => this.close(), s.appendChild(l), i.addEventListener("mousedown", (p) => {
209
+ p.target === i && this.close();
225
210
  });
226
211
  }
227
212
  const d = document.createElement("iframe");
228
- d.src = o, d.title = "Consent manager", d.setAttribute("allow", "clipboard-write"), d.style.cssText = [
213
+ d.src = r, d.title = "Consent manager", d.setAttribute("allow", "clipboard-write"), d.style.cssText = [
229
214
  "flex:1",
230
215
  "width:100%",
231
216
  "height:100%",
232
217
  "border:0",
233
218
  "background:#fff"
234
- ].join(";"), a.appendChild(d), i.appendChild(a), document.body.appendChild(i), document.documentElement.style.overflow = "hidden", this.overlay = i, this.iframe = d;
219
+ ].join(";"), s.appendChild(d), i.appendChild(s), document.body.appendChild(i), document.documentElement.style.overflow = "hidden", this.overlay = i, this.iframe = d;
235
220
  }
236
221
  teardown() {
237
222
  var e;
@@ -250,7 +235,7 @@ class A {
250
235
  });
251
236
  }
252
237
  }
253
- const T = new A();
238
+ const T = new v();
254
239
  export {
255
240
  T as ConsentWidget,
256
241
  c as GATEWAY_BASE_URLS,
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({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;
1
+ "use strict";var C=Object.defineProperty;var k=(n,e,t)=>e in n?C(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"},_="CONSENT_WIDGET_EVENT",y="GATEWAY_UI_READY",b="PRINCIPAL_TOKEN";function I(n){return typeof n=="object"&&n!==null&&n.type===_&&typeof n.event=="object"}function x(n){return typeof n=="object"&&n!==null&&n.type===y}const v="__consent_widget_overlay__",A=600,T=640;function W(n,e){var t;if(n)return n.replace(/\/$/,"");if(e&&w[e])return w[e];try{const s=document.getElementsByTagName("script");for(let r=s.length-1;r>=0;r--){const c=((t=s[r])==null?void 0:t.getAttribute("src"))??"";if(c.includes("consent-widget"))return new URL(c,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 U(n){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"?(n.purposeId&&e.set("purpose_id",n.purposeId),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 q{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 s,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(x(e.data)){try{(s=this.iframe.contentWindow)==null||s.postMessage({type:b,token:this.session.token},t||"*")}catch{}return}if(typeof e.data=="object"&&e.data!==null&&e.data.type===y){try{(o=this.iframe.contentWindow)==null||o.postMessage({type:b,token:this.session.token},t||"*")}catch{}return}if(I(e.data)){const r=e.data.event;this.emit(r),this.session.autoClose&&(r.type==="consent_accepted"||r.type==="consent_declined"||r.type==="already_resolved"||r.type==="redirecting")&&this.teardown()}}})}openConsent(e,t,s){this.openDialog({...s,requestId:e,token:t,mode:"consent"})}openPreferences(e,t){this.openDialog({...t,token:e,mode:"preferences"})}openDirectConsent(e,t,s,o,r){const c=e==null?void 0:e.trim();if(!c)throw new Error("ConsentWidget.openDirectConsent: `purposeId` is required.");this.openDialog({...r,purposeId:c,token:t,orgId:s??null,locale:o,mode:"direct_consent"})}openDialog(e){var o;if(!e||typeof e.token!="string"||e.token.length===0)throw new Error("ConsentWidget: `token` is required.");const t=((o=e.purposeId)==null?void 0:o.trim())||void 0,s=e.mode??(t?"direct_consent":e.requestId?"consent":"preferences");if(s==="consent"&&!e.requestId)throw new Error("ConsentWidget: `requestId` is required for the consent dialog.");this.open({requestId:e.requestId,token:e.token,mode:s,purposeId:t,orgId:e.orgId??null,locale:e.locale,baseUrl:W(e.baseUrl,e.env),autoClose:e.autoClose??!0}),this.render(e.allowClose??!0,e.width??A,e.height??T),this.emit({type:"opened",requestId:e.requestId,data:t?{purposeId:t}: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},s=this.overlay!==null;this.teardown(),s&&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,s){if(!this.session)return;const o=U(this.session),r=document.createElement("div");r.id=v,r.setAttribute("role","dialog"),r.setAttribute("aria-modal","true"),r.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"),p=Math.min(t,window.innerWidth-32),m=Math.min(s,window.innerHeight-32);if(c.style.cssText=[`width:${p}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 i=document.createElement("button");i.setAttribute("aria-label","Close"),i.textContent="×",i.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(";"),i.onclick=()=>this.close(),c.appendChild(i),r.addEventListener("mousedown",l=>{l.target===r&&this.close()})}const u=document.createElement("iframe");u.src=o,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),r.appendChild(c),document.body.appendChild(r),document.documentElement.style.overflow="hidden",this.overlay=r,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 q;function L(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 s=d.useCallback((i,l)=>{const{requestId:f,...g}=l??{};f?a.openConsent(f,i,g):a.openPreferences(i,g)},[]),o=d.useCallback((i,l,f)=>{a.openConsent(i,l,f)},[]),r=d.useCallback((i,l)=>{a.openPreferences(i,l)},[]),c=d.useCallback((i,l,f,g,E)=>{a.openDirectConsent(i,l,f,g,E)},[]),p=d.useCallback(i=>{a.close(i)},[]),m=d.useCallback(i=>a.onCallback(i),[]),u=d.useCallback(()=>a.isOpen(),[]);return{open:s,openConsent:o,openPreferences:r,openDirectConsent:c,close:p,onEvent:m,isOpen:u}}exports.useConsentWidget=L;
package/dist/react.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { OpenConsentOptions, OpenDirectConsentOptions, OpenPreferencesOptions, WidgetCallback, WidgetEvent, WidgetInitOptions } from "./core/types";
1
+ import type { WidgetCallback, WidgetEvent, WidgetOptions } from "./core/types";
2
2
  export interface UseConsentWidgetOptions {
3
3
  /** Called on every widget event (opened / accepted / declined / ... / closed). */
4
4
  onEvent?: WidgetCallback;
@@ -6,14 +6,16 @@ export interface UseConsentWidgetOptions {
6
6
  closeOnUnmount?: boolean;
7
7
  }
8
8
  export interface UseConsentWidgetApi {
9
- /** Open consent or preferences dialog (mode defaults from requestId). */
10
- open: (options: WidgetInitOptions) => void;
9
+ /** Open consent (requestId set) or preferences (requestId omitted) dialog. */
10
+ open: (token: string, options?: WidgetOptions & {
11
+ requestId?: string;
12
+ }) => void;
11
13
  /** Open the consent dialog for a request. */
12
- openConsent: (requestId: string, token: string, opts?: OpenConsentOptions) => void;
14
+ openConsent: (requestId: string, token: string, options?: WidgetOptions) => void;
13
15
  /** Open the preferences (my consents + grievances) dialog. */
14
- openPreferences: (token: string, opts?: OpenPreferencesOptions) => void;
15
- /** Open the direct-consent (permission-style) dialog for purpose(s). */
16
- openDirectConsent: (purposeIds: string | string[], token: string, opts?: OpenDirectConsentOptions) => void;
16
+ openPreferences: (token: string, options?: WidgetOptions) => void;
17
+ /** Open the direct-consent (permission-style) dialog for a purpose. */
18
+ openDirectConsent: (purposeId: string, token: string, orgId?: string | null, locale?: string, options?: WidgetOptions) => void;
17
19
  /** Programmatically close the dialog (emits a `closed` event). */
18
20
  close: (event?: WidgetEvent) => void;
19
21
  /** Subscribe to flow events. Returns an unsubscribe function. */
@@ -22,4 +24,4 @@ export interface UseConsentWidgetApi {
22
24
  isOpen: () => boolean;
23
25
  }
24
26
  export declare function useConsentWidget(options?: UseConsentWidgetOptions): UseConsentWidgetApi;
25
- export type { WidgetCallback, WidgetEvent, WidgetInitOptions, OpenConsentOptions, OpenDirectConsentOptions, OpenPreferencesOptions, };
27
+ export type { WidgetCallback, WidgetEvent, WidgetOptions, };
package/dist/react.js CHANGED
@@ -1,28 +1,28 @@
1
- var I = Object.defineProperty;
2
- var k = (n, e, t) => e in n ? I(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
- var u = (n, e, t) => k(n, typeof e != "symbol" ? e + "" : e, t);
4
- import { useEffect as w, useCallback as f } from "react";
1
+ var C = Object.defineProperty;
2
+ var I = (n, e, t) => e in n ? C(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
+ var u = (n, e, t) => I(n, typeof e != "symbol" ? e + "" : e, t);
4
+ import { useEffect as w, useCallback as h } from "react";
5
5
  const y = {
6
6
  live: "https://ui.gateway.nodbook.in",
7
7
  sandbox: "https://sandbox-ui.gateway.nodbook.in"
8
- }, C = "CONSENT_WIDGET_EVENT", E = "GATEWAY_UI_READY", b = "PRINCIPAL_TOKEN";
9
- function _(n) {
10
- return typeof n == "object" && n !== null && n.type === C && typeof n.event == "object";
8
+ }, x = "CONSENT_WIDGET_EVENT", E = "GATEWAY_UI_READY", b = "PRINCIPAL_TOKEN";
9
+ function k(n) {
10
+ return typeof n == "object" && n !== null && n.type === x && typeof n.event == "object";
11
11
  }
12
- function x(n) {
12
+ function v(n) {
13
13
  return typeof n == "object" && n !== null && n.type === E;
14
14
  }
15
- const v = "__consent_widget_overlay__", A = 600, T = 640;
16
- function U(n, e) {
15
+ const A = "__consent_widget_overlay__", T = 600, U = 640;
16
+ function W(n, e) {
17
17
  var t;
18
18
  if (n) return n.replace(/\/$/, "");
19
19
  if (e && y[e]) return y[e];
20
20
  try {
21
- const r = document.getElementsByTagName("script");
22
- for (let i = r.length - 1; i >= 0; i--) {
23
- const l = ((t = r[i]) == null ? void 0 : t.getAttribute("src")) ?? "";
24
- if (l.includes("consent-widget"))
25
- return new URL(l, window.location.href).origin;
21
+ const i = document.getElementsByTagName("script");
22
+ for (let r = i.length - 1; r >= 0; r--) {
23
+ const c = ((t = i[r]) == null ? void 0 : t.getAttribute("src")) ?? "";
24
+ if (c.includes("consent-widget"))
25
+ return new URL(c, window.location.href).origin;
26
26
  }
27
27
  const o = document.currentScript;
28
28
  if (o != null && o.src) return new URL(o.src, window.location.href).origin;
@@ -30,16 +30,15 @@ function U(n, e) {
30
30
  }
31
31
  return window.location.origin;
32
32
  }
33
- function W(n) {
34
- var t;
33
+ function L(n) {
35
34
  const e = new URLSearchParams({
36
35
  auth_token: n.token,
37
36
  embed: "true",
38
37
  origin: window.location.origin
39
38
  });
40
- 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()}`;
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.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()}`;
41
40
  }
42
- class L {
41
+ class q {
43
42
  constructor() {
44
43
  u(this, "overlay", null);
45
44
  u(this, "iframe", null);
@@ -51,7 +50,7 @@ class L {
51
50
  e.key === "Escape" && this.close({ type: "closed", requestId: (t = this.session) == null ? void 0 : t.requestId });
52
51
  });
53
52
  u(this, "messageHandler", (e) => {
54
- var r, o;
53
+ var i, o;
55
54
  if (!this.session || !this.iframe) return;
56
55
  let t = "";
57
56
  try {
@@ -60,9 +59,9 @@ class L {
60
59
  t = "";
61
60
  }
62
61
  if (!(t && e.origin !== t) && e.source === this.iframe.contentWindow) {
63
- if (x(e.data)) {
62
+ if (v(e.data)) {
64
63
  try {
65
- (r = this.iframe.contentWindow) == null || r.postMessage(
64
+ (i = this.iframe.contentWindow) == null || i.postMessage(
66
65
  { type: b, token: this.session.token },
67
66
  t || "*"
68
67
  );
@@ -80,69 +79,55 @@ class L {
80
79
  }
81
80
  return;
82
81
  }
83
- if (_(e.data)) {
84
- const i = e.data.event;
85
- this.emit(i), this.session.autoClose && (i.type === "consent_accepted" || i.type === "consent_declined" || i.type === "already_resolved" || i.type === "redirecting") && this.teardown();
82
+ if (k(e.data)) {
83
+ const r = e.data.event;
84
+ this.emit(r), this.session.autoClose && (r.type === "consent_accepted" || r.type === "consent_declined" || r.type === "already_resolved" || r.type === "redirecting") && this.teardown();
86
85
  }
87
86
  }
88
87
  });
89
88
  }
90
89
  /** Open the consent dialog for a request. */
91
- openConsent(e, t, r) {
92
- this.openDialog({
93
- ...r,
94
- requestId: e,
95
- token: (r == null ? void 0 : r.token) ?? t
96
- });
90
+ openConsent(e, t, i) {
91
+ this.openDialog({ ...i, requestId: e, token: t, mode: "consent" });
97
92
  }
98
93
  /** Open the preferences (my consents + grievances) dialog. */
99
94
  openPreferences(e, t) {
100
- this.openDialog({
101
- ...t,
102
- token: (t == null ? void 0 : t.token) ?? e
103
- });
95
+ this.openDialog({ ...t, token: e, mode: "preferences" });
104
96
  }
105
- /** Open the direct-consent (permission-style) dialog for purpose(s). */
106
- openDirectConsent(e, t, r) {
107
- const o = (Array.isArray(e) ? e : [e]).map((i) => i.trim()).filter(Boolean);
108
- if (o.length === 0)
109
- throw new Error("ConsentWidget.openDirectConsent: at least one `purposeId` is required.");
110
- this.openDialog({
111
- ...r,
112
- purposeIds: o,
113
- token: (r == null ? void 0 : r.token) ?? t,
114
- mode: "direct_consent"
115
- });
97
+ /** Open the direct-consent (permission-style) dialog for a purpose. */
98
+ openDirectConsent(e, t, i, o, r) {
99
+ const c = e == null ? void 0 : e.trim();
100
+ if (!c)
101
+ throw new Error("ConsentWidget.openDirectConsent: `purposeId` is required.");
102
+ this.openDialog({ ...r, purposeId: c, token: t, orgId: i ?? null, locale: o, mode: "direct_consent" });
116
103
  }
117
104
  /**
118
105
  * Shared opener behind `openConsent` / `openPreferences` / `openDirectConsent`.
119
- * The view follows `mode`, defaulting from `requestId`: set → consent
120
- * dialog, omitted → preferences dialog.
121
106
  */
122
107
  openDialog(e) {
123
- var r;
108
+ var o;
124
109
  if (!e || typeof e.token != "string" || e.token.length === 0)
125
110
  throw new Error("ConsentWidget: `token` is required.");
126
- const t = e.mode ?? (e.requestId ? "consent" : "preferences");
127
- if (t === "consent" && !e.requestId)
111
+ const t = ((o = e.purposeId) == null ? void 0 : o.trim()) || void 0, i = e.mode ?? (t ? "direct_consent" : e.requestId ? "consent" : "preferences");
112
+ if (i === "consent" && !e.requestId)
128
113
  throw new Error("ConsentWidget: `requestId` is required for the consent dialog.");
129
114
  this.open({
130
115
  requestId: e.requestId,
131
116
  token: e.token,
132
- mode: t,
133
- purposeIds: e.purposeIds,
117
+ mode: i,
118
+ purposeId: t,
134
119
  orgId: e.orgId ?? null,
135
120
  locale: e.locale,
136
- baseUrl: U(e.baseUrl, e.env),
121
+ baseUrl: W(e.baseUrl, e.env),
137
122
  autoClose: e.autoClose ?? !0
138
123
  }), this.render(
139
124
  e.allowClose ?? !0,
140
- e.width ?? A,
141
- e.height ?? T
125
+ e.width ?? T,
126
+ e.height ?? U
142
127
  ), this.emit({
143
128
  type: "opened",
144
129
  requestId: e.requestId,
145
- data: (r = e.purposeIds) != null && r.length ? { purposeIds: e.purposeIds } : void 0
130
+ data: t ? { purposeId: t } : void 0
146
131
  });
147
132
  }
148
133
  /**
@@ -161,8 +146,8 @@ class L {
161
146
  const t = e ?? {
162
147
  type: "closed",
163
148
  requestId: (o = this.session) == null ? void 0 : o.requestId
164
- }, r = this.overlay !== null;
165
- this.teardown(), r && this.emit(t);
149
+ }, i = this.overlay !== null;
150
+ this.teardown(), i && this.emit(t);
166
151
  }
167
152
  /** Alias of close() without emitting when already closed. */
168
153
  destroy() {
@@ -178,10 +163,10 @@ class L {
178
163
  ensureListening() {
179
164
  this.listening || (window.addEventListener("message", this.messageHandler), window.addEventListener("keydown", this.escHandler), this.listening = !0);
180
165
  }
181
- render(e, t, r) {
166
+ render(e, t, i) {
182
167
  if (!this.session) return;
183
- const o = W(this.session), i = document.createElement("div");
184
- i.id = v, i.setAttribute("role", "dialog"), i.setAttribute("aria-modal", "true"), i.style.cssText = [
168
+ const o = L(this.session), r = document.createElement("div");
169
+ r.id = A, r.setAttribute("role", "dialog"), r.setAttribute("aria-modal", "true"), r.style.cssText = [
185
170
  "position:fixed",
186
171
  "inset:0",
187
172
  "z-index:2147483647",
@@ -192,11 +177,11 @@ class L {
192
177
  "padding:16px",
193
178
  "box-sizing:border-box"
194
179
  ].join(";");
195
- const l = document.createElement("div"), g = Math.min(t, window.innerWidth - 32), p = Math.min(r, window.innerHeight - 32);
196
- if (l.style.cssText = [
197
- `width:${g}px`,
180
+ const c = document.createElement("div"), p = Math.min(t, window.innerWidth - 32), m = Math.min(i, window.innerHeight - 32);
181
+ if (c.style.cssText = [
182
+ `width:${p}px`,
198
183
  "max-width:100%",
199
- `height:${p}px`,
184
+ `height:${m}px`,
200
185
  "max-height:100%",
201
186
  "background:#fff",
202
187
  "border-radius:16px",
@@ -221,8 +206,8 @@ class L {
221
206
  "font-size:18px",
222
207
  "line-height:1",
223
208
  "cursor:pointer"
224
- ].join(";"), s.onclick = () => this.close(), l.appendChild(s), i.addEventListener("mousedown", (c) => {
225
- c.target === i && this.close();
209
+ ].join(";"), s.onclick = () => this.close(), c.appendChild(s), r.addEventListener("mousedown", (l) => {
210
+ l.target === r && this.close();
226
211
  });
227
212
  }
228
213
  const d = document.createElement("iframe");
@@ -232,7 +217,7 @@ class L {
232
217
  "height:100%",
233
218
  "border:0",
234
219
  "background:#fff"
235
- ].join(";"), l.appendChild(d), i.appendChild(l), document.body.appendChild(i), document.documentElement.style.overflow = "hidden", this.overlay = i, this.iframe = d;
220
+ ].join(";"), c.appendChild(d), r.appendChild(c), document.body.appendChild(r), document.documentElement.style.overflow = "hidden", this.overlay = r, this.iframe = d;
236
221
  }
237
222
  teardown() {
238
223
  var e;
@@ -251,8 +236,8 @@ class L {
251
236
  });
252
237
  }
253
238
  }
254
- const a = new L();
255
- function S(n) {
239
+ const a = new q();
240
+ function R(n) {
256
241
  const { onEvent: e, closeOnUnmount: t = !0 } = n ?? {};
257
242
  w(() => {
258
243
  if (e)
@@ -263,29 +248,20 @@ function S(n) {
263
248
  a.isOpen() && a.close();
264
249
  };
265
250
  }, [t]);
266
- const r = f((s) => {
267
- const { requestId: c, token: h, ...m } = s;
268
- c ? a.openConsent(c, h, { ...m, token: h }) : a.openPreferences(h, m);
269
- }, []), o = f(
270
- (s, c, h) => {
271
- a.openConsent(s, c, h);
272
- },
273
- []
274
- ), i = f(
275
- (s, c) => {
276
- a.openPreferences(s, c);
277
- },
278
- []
279
- ), l = f(
280
- (s, c, h) => {
281
- a.openDirectConsent(s, c, h);
282
- },
283
- []
284
- ), g = f((s) => {
251
+ const i = h((s, l) => {
252
+ const { requestId: f, ...g } = l ?? {};
253
+ f ? a.openConsent(f, s, g) : a.openPreferences(s, g);
254
+ }, []), o = h((s, l, f) => {
255
+ a.openConsent(s, l, f);
256
+ }, []), r = h((s, l) => {
257
+ a.openPreferences(s, l);
258
+ }, []), c = h((s, l, f, g, _) => {
259
+ a.openDirectConsent(s, l, f, g, _);
260
+ }, []), p = h((s) => {
285
261
  a.close(s);
286
- }, []), p = f((s) => a.onCallback(s), []), d = f(() => a.isOpen(), []);
287
- return { open: r, openConsent: o, openPreferences: i, openDirectConsent: l, close: g, onEvent: p, isOpen: d };
262
+ }, []), m = h((s) => a.onCallback(s), []), d = h(() => a.isOpen(), []);
263
+ return { open: i, openConsent: o, openPreferences: r, openDirectConsent: c, close: p, onEvent: m, isOpen: d };
288
264
  }
289
265
  export {
290
- S as useConsentWidget
266
+ R as useConsentWidget
291
267
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@truzyx/consent-widget",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
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",