@cowprotocol/widget-lib 2.2.5 → 2.3.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 CHANGED
@@ -6,11 +6,11 @@ look and much more!
6
6
 
7
7
  ## Live example
8
8
 
9
- See the widget in action in the [widget configurator](https://widget.cow.fi)
9
+ See the widget in action in the [widget configurator](https://widget.cow.finance)
10
10
 
11
11
  ## Docs
12
12
 
13
- You can find a detailed description of all widget parameters in the [documentation](https://docs.cow.fi/cow-protocol/tutorials/widget)
13
+ You can find a detailed description of all widget parameters in the [documentation](https://docs.cow.finance/cow-protocol/tutorials/widget)
14
14
 
15
15
  ## Quick start
16
16
 
package/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("@cowprotocol/iframe-transport"),b=require("@cowprotocol/events");var c=(e=>(e.ACTIVATE="ACTIVATE",e.READY="READY",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.SET_FULL_HEIGHT="SET_FULL_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e.INTERCEPT_WINDOW_OPEN="INTERCEPT_WINDOW_OPEN",e.PROCESS_HOOK="PROCESS_HOOK",e))(c||{}),T=(e=>(e.UPDATE_PARAMS="UPDATE_PARAMS",e.UPDATE_APP_DATA="UPDATE_APP_DATA",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e.HOOK_RESULT="HOOK_RESULT",e))(T||{}),E=(e=>(e.ON_BEFORE_APPROVAL="ON_BEFORE_APPROVAL",e.ON_BEFORE_TRADE="ON_BEFORE_TRADE",e.ON_BEFORE_WRAP_UNWRAP="ON_BEFORE_WRAP_UNWRAP",e.ON_BEFORE_ORDER_CANCEL="ON_BEFORE_ORDER_CANCEL",e.ON_BEFORE_ORDERS_CANCEL="ON_BEFORE_ORDERS_CANCEL",e))(E||{}),P=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e))(P||{});const U=["primary","background","paper","text","danger","warning","alert","info","success"],s=new S.IframeTransport("cowSwapWidget");class y{constructor(t,n,o=[]){this.contentWindow=t,this.updateListeners(o),this.widgetListener=s.listenToMessageFromWindow(this.contentWindow,c.EMIT_COW_EVENT,r=>this.eventEmitter.emit(r.event,r.payload),n)}eventEmitter=new b.SimpleCowEventEmitter;listeners=[];widgetListener;stopListeningIframe(){s.stopListeningWindowListener(this.contentWindow,this.widgetListener)}updateListeners(t){for(const n of this.listeners)this.eventEmitter.off(n);this.listeners=t||[];for(const n of this.listeners)this.eventEmitter.on(n)}}class H{constructor(t,n){this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=o=>{F(o.data)&&(typeof window<"u"&&o.origin===window.location.origin||(B(o.data)?this.appWindow.parent.postMessage(o.data,"*"):M(o.data)&&this.iframeWidow.postMessage(o.data,"*")))},this.startListening()}forwardSdkMessage;startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function F(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function B(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function M(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function V(e="Default",t="#1c5dbf",...n){console.debug(`%c [COW][${e}]`,`font-weight: bold; color: ${t}`,...n)}function k(...e){V("Widget","#47aba8",...e)}function v(e){return!!(e&&typeof e=="object")}const C="_";function j(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=I(e);return t+"/#"+n+"?"+W(e)}function I(e){const{chainId:t=1,sell:n,buy:o,tradeType:r=P.SWAP}=e,a=[n?.asset||C,o?.asset||C].map(encodeURIComponent).join("/");return`/${t}/widget/${r}/${a}`}function W(e){const t=new URLSearchParams;return K($(Q(x(G(t,e),e),e),e),e)}function G(e,t){const{sell:n,buy:o}=t;return n?.amount&&e.append("sellAmount",n.amount),o?.amount&&e.append("buyAmount",o.amount),e}function x(e,t){const n=t.theme;return n?(v(n)?(e.append("palette",encodeURIComponent(JSON.stringify(n))),e.append("theme",n.baseTheme)):(e.append("palette","null"),e.append("theme",n)),e):(e.append("palette","null"),e)}function Q(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}function $(e,t){return t.locale&&e.append("lng",t.locale),e}function K(e,t){return Y(t.hooks)?e.append("hooksEnabled","true"):e.delete("hooksEnabled"),e}function Y(e){return!!e&&Object.values(e).some(Boolean)}const N="640px",J="450px",X=20,z={updateParams:()=>{},updateListeners:()=>{},updateProvider:()=>{},destroy:()=>{}};function Z(e,t){const{params:n,provider:o,listeners:r,onReady:a}=t;let f=o,w=n;if(typeof window>"u")return z;const d=q(n),i=ee(d);k("Resolved trusted iframe origin",{iframeOrigin:i});const p=[];a&&p.push(ne(d.contentWindow||window,i,a)),e.innerHTML="",e.appendChild(d);const{contentWindow:u}=d;if(!u)throw console.error("Iframe does not contain a window",d),new Error("Iframe does not contain a window!");p.push(te(u,i,n.appCode)),p.push(...se(d,i,n.height,n.maxHeight)),p.push(oe(i));let R=null;function A(){u&&(R&&window.removeEventListener("message",R),R=ue(u,i,w.hooks),p.push(R))}A();const g=new y(window,i,r);let h=m(u,i,null,f);d.addEventListener("load",()=>{L(u,i,w,f)});const D=new H(window,u);return{updateParams:l=>{w=l,L(u,i,w,f),A()},updateListeners:l=>g.updateListeners(l),updateProvider:l=>{f=l,h=m(u,i,h,l)},destroy:()=>{h.disconnect(),g.stopListeningIframe(),p.forEach(l=>window.removeEventListener("message",l)),D.stopListening(),e.removeChild(d)}}}function m(e,t,n,o){n&&n.disconnect();const r=n||new S.IframeRpcProviderBridge(e,t);return o&&r.onConnect(o),r}function q(e){const{width:t=J,height:n=N}=e,o=document.createElement("iframe");return o.src=j(e),o.width=t,o.height=n,o.style.border="0",o.allow="clipboard-read; clipboard-write",o}function ee(e){return new URL(e.src).origin}function L(e,t,n,o){const r=!!o,a=I(n),f=W(n).toString(),{theme:w,hooks:d,...i}=n;s.postMessageToWindow(e,T.UPDATE_PARAMS,{urlParams:{pathname:a,search:f},appParams:i,hasProvider:r},t)}function te(e,t,n){return s.listenToMessageFromWindow(window,c.ACTIVATE,()=>{s.postMessageToWindow(e,T.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0},t)},t)}function ne(e,t,n){let o=!1;return s.listenToMessageFromWindow(window,c.READY,()=>{o||(o=!0,n())},t)}function oe(e){return s.listenToMessageFromWindow(window,c.INTERCEPT_WINDOW_OPEN,({href:t,rel:n,target:o})=>{const r=re(t.toString(),e);r&&ie(r)&&window.open(r,o,n)},e)}function re(e,t){const n=e.trim();if(!n)return null;try{return new URL(n,t).toString()}catch{return null}}function ie(e){try{const t=new URL(e).protocol;return t==="http:"||t==="https:"}catch{return!1}}function se(e,t,n=N,o){return[s.listenToMessageFromWindow(window,c.UPDATE_HEIGHT,r=>{const a=r.height?r.height+X:void 0;e.style.height=a?`${o?Math.min(a,o):a}px`:n},t),s.listenToMessageFromWindow(window,c.SET_FULL_HEIGHT,({isUpToSmall:r})=>{e.style.height=r?n:`${o||document.body.offsetHeight}px`},t)]}const ae={[E.ON_BEFORE_APPROVAL]:(e,t)=>t?.onBeforeApproval?t.onBeforeApproval(e):!0,[E.ON_BEFORE_TRADE]:(e,t)=>t?.onBeforeTrade?t.onBeforeTrade(e):!0,[E.ON_BEFORE_WRAP_UNWRAP]:(e,t)=>t?.onBeforeWrapOrUnwrap?t.onBeforeWrapOrUnwrap(e):!0,[E.ON_BEFORE_ORDER_CANCEL]:(e,t)=>t?.onBeforeOrderCancel?t.onBeforeOrderCancel(e):!0,[E.ON_BEFORE_ORDERS_CANCEL]:(e,t)=>t?.onBeforeOrdersCancel?t.onBeforeOrdersCancel(e):!0};function de(e,t){return ae[e.event](e.payload,t)}function ue(e,t,n){return s.listenToMessageFromWindow(window,c.PROCESS_HOOK,async o=>{let r=!1;try{r=await de(o,n)}catch{}s.postMessageToWindow(e,T.HOOK_RESULT,{id:o.id,result:r},t)},t)}const ce=Object.values(P);function le(e,t,n){if(_(e)){const o=e[n];return O(o)?o[t]:o}if(O(e)){const o=e[t];return _(o)?o[n]:o}return e}function _(e){return typeof e!="object"?!1:Object.keys(e).every(t=>ce.includes(t))}const Ee=/^\d+$/;function O(e){return typeof e!="object"?!1:Object.keys(e).every(t=>Ee.test(t))}function fe(e){return _(e)?Object.values(e).flatMap(t=>O(t)?Object.values(t):[t]):O(e)?Object.values(e).flatMap(t=>_(t)?Object.values(t):[t]):[e]}exports.TradeType=P;exports.WIDGET_PALETTE_COLORS=U;exports.WidgetHookEvents=E;exports.WidgetMethodsEmit=c;exports.WidgetMethodsListen=T;exports.createCowSwapWidget=Z;exports.isCowSwapWidgetPalette=v;exports.isPerNetworkConfig=O;exports.isPerTradeTypeConfig=_;exports.resolveFlexibleConfig=le;exports.resolveFlexibleConfigValues=fe;exports.widgetIframeTransport=s;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("@cowprotocol/iframe-transport"),b=require("@cowprotocol/events");var c=(e=>(e.ACTIVATE="ACTIVATE",e.READY="READY",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.SET_FULL_HEIGHT="SET_FULL_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e.INTERCEPT_WINDOW_OPEN="INTERCEPT_WINDOW_OPEN",e.PROCESS_HOOK="PROCESS_HOOK",e))(c||{}),T=(e=>(e.UPDATE_PARAMS="UPDATE_PARAMS",e.UPDATE_APP_DATA="UPDATE_APP_DATA",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e.HOOK_RESULT="HOOK_RESULT",e))(T||{}),E=(e=>(e.ON_BEFORE_APPROVAL="ON_BEFORE_APPROVAL",e.ON_BEFORE_TRADE="ON_BEFORE_TRADE",e.ON_BEFORE_WRAP_UNWRAP="ON_BEFORE_WRAP_UNWRAP",e.ON_BEFORE_ORDER_CANCEL="ON_BEFORE_ORDER_CANCEL",e.ON_BEFORE_ORDERS_CANCEL="ON_BEFORE_ORDERS_CANCEL",e))(E||{}),P=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e))(P||{});const U=["primary","background","paper","text","danger","warning","alert","info","success"],s=new S.IframeTransport("cowSwapWidget");class y{constructor(t,n,o=[]){this.contentWindow=t,this.updateListeners(o),this.widgetListener=s.listenToMessageFromWindow(this.contentWindow,c.EMIT_COW_EVENT,r=>this.eventEmitter.emit(r.event,r.payload),n)}eventEmitter=new b.SimpleCowEventEmitter;listeners=[];widgetListener;stopListeningIframe(){s.stopListeningWindowListener(this.contentWindow,this.widgetListener)}updateListeners(t){for(const n of this.listeners)this.eventEmitter.off(n);this.listeners=t||[];for(const n of this.listeners)this.eventEmitter.on(n)}}class H{constructor(t,n){this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=o=>{F(o.data)&&(typeof window<"u"&&o.origin===window.location.origin||(B(o.data)?this.appWindow.parent.postMessage(o.data,"*"):M(o.data)&&this.iframeWidow.postMessage(o.data,"*")))},this.startListening()}forwardSdkMessage;startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function F(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function B(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function M(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function V(e="Default",t="#1c5dbf",...n){console.debug(`%c [COW][${e}]`,`font-weight: bold; color: ${t}`,...n)}function k(...e){V("Widget","#47aba8",...e)}function v(e){return!!(e&&typeof e=="object")}const C="_";function j(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.finance",n=I(e);return t+"/#"+n+"?"+W(e)}function I(e){const{chainId:t=1,sell:n,buy:o,tradeType:r=P.SWAP}=e,a=[n?.asset||C,o?.asset||C].map(encodeURIComponent).join("/");return`/${t}/widget/${r}/${a}`}function W(e){const t=new URLSearchParams;return K($(Q(x(G(t,e),e),e),e),e)}function G(e,t){const{sell:n,buy:o}=t;return n?.amount&&e.append("sellAmount",n.amount),o?.amount&&e.append("buyAmount",o.amount),e}function x(e,t){const n=t.theme;return n?(v(n)?(e.append("palette",encodeURIComponent(JSON.stringify(n))),e.append("theme",n.baseTheme)):(e.append("palette","null"),e.append("theme",n)),e):(e.append("palette","null"),e)}function Q(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}function $(e,t){return t.locale&&e.append("lng",t.locale),e}function K(e,t){return Y(t.hooks)?e.append("hooksEnabled","true"):e.delete("hooksEnabled"),e}function Y(e){return!!e&&Object.values(e).some(Boolean)}const N="640px",J="450px",X=20,z={updateParams:()=>{},updateListeners:()=>{},updateProvider:()=>{},destroy:()=>{}};function Z(e,t){const{params:n,provider:o,listeners:r,onReady:a}=t;let f=o,w=n;if(typeof window>"u")return z;const d=q(n),i=ee(d);k("Resolved trusted iframe origin",{iframeOrigin:i});const p=[];a&&p.push(ne(d.contentWindow||window,i,a)),e.innerHTML="",e.appendChild(d);const{contentWindow:u}=d;if(!u)throw console.error("Iframe does not contain a window",d),new Error("Iframe does not contain a window!");p.push(te(u,i,n.appCode)),p.push(...se(d,i,n.height,n.maxHeight)),p.push(oe(i));let R=null;function A(){u&&(R&&window.removeEventListener("message",R),R=ue(u,i,w.hooks),p.push(R))}A();const g=new y(window,i,r);let h=m(u,i,null,f);d.addEventListener("load",()=>{L(u,i,w,f)});const D=new H(window,u);return{updateParams:l=>{w=l,L(u,i,w,f),A()},updateListeners:l=>g.updateListeners(l),updateProvider:l=>{f=l,h=m(u,i,h,l)},destroy:()=>{h.disconnect(),g.stopListeningIframe(),p.forEach(l=>window.removeEventListener("message",l)),D.stopListening(),e.removeChild(d)}}}function m(e,t,n,o){n&&n.disconnect();const r=n||new S.IframeRpcProviderBridge(e,t);return o&&r.onConnect(o),r}function q(e){const{width:t=J,height:n=N}=e,o=document.createElement("iframe");return o.src=j(e),o.width=t,o.height=n,o.style.border="0",o.allow="clipboard-read; clipboard-write",o}function ee(e){return new URL(e.src).origin}function L(e,t,n,o){const r=!!o,a=I(n),f=W(n).toString(),{theme:w,hooks:d,...i}=n;s.postMessageToWindow(e,T.UPDATE_PARAMS,{urlParams:{pathname:a,search:f},appParams:i,hasProvider:r},t)}function te(e,t,n){return s.listenToMessageFromWindow(window,c.ACTIVATE,()=>{s.postMessageToWindow(e,T.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0},t)},t)}function ne(e,t,n){let o=!1;return s.listenToMessageFromWindow(window,c.READY,()=>{o||(o=!0,n())},t)}function oe(e){return s.listenToMessageFromWindow(window,c.INTERCEPT_WINDOW_OPEN,({href:t,rel:n,target:o})=>{const r=re(t.toString(),e);r&&ie(r)&&window.open(r,o,n)},e)}function re(e,t){const n=e.trim();if(!n)return null;try{return new URL(n,t).toString()}catch{return null}}function ie(e){try{const t=new URL(e).protocol;return t==="http:"||t==="https:"}catch{return!1}}function se(e,t,n=N,o){return[s.listenToMessageFromWindow(window,c.UPDATE_HEIGHT,r=>{const a=r.height?r.height+X:void 0;e.style.height=a?`${o?Math.min(a,o):a}px`:n},t),s.listenToMessageFromWindow(window,c.SET_FULL_HEIGHT,({isUpToSmall:r})=>{e.style.height=r?n:`${o||document.body.offsetHeight}px`},t)]}const ae={[E.ON_BEFORE_APPROVAL]:(e,t)=>t?.onBeforeApproval?t.onBeforeApproval(e):!0,[E.ON_BEFORE_TRADE]:(e,t)=>t?.onBeforeTrade?t.onBeforeTrade(e):!0,[E.ON_BEFORE_WRAP_UNWRAP]:(e,t)=>t?.onBeforeWrapOrUnwrap?t.onBeforeWrapOrUnwrap(e):!0,[E.ON_BEFORE_ORDER_CANCEL]:(e,t)=>t?.onBeforeOrderCancel?t.onBeforeOrderCancel(e):!0,[E.ON_BEFORE_ORDERS_CANCEL]:(e,t)=>t?.onBeforeOrdersCancel?t.onBeforeOrdersCancel(e):!0};function de(e,t){return ae[e.event](e.payload,t)}function ue(e,t,n){return s.listenToMessageFromWindow(window,c.PROCESS_HOOK,async o=>{let r=!1;try{r=await de(o,n)}catch{}s.postMessageToWindow(e,T.HOOK_RESULT,{id:o.id,result:r},t)},t)}const ce=Object.values(P);function le(e,t,n){if(_(e)){const o=e[n];return O(o)?o[t]:o}if(O(e)){const o=e[t];return _(o)?o[n]:o}return e}function _(e){return typeof e!="object"?!1:Object.keys(e).every(t=>ce.includes(t))}const Ee=/^\d+$/;function O(e){return typeof e!="object"?!1:Object.keys(e).every(t=>Ee.test(t))}function fe(e){return _(e)?Object.values(e).flatMap(t=>O(t)?Object.values(t):[t]):O(e)?Object.values(e).flatMap(t=>_(t)?Object.values(t):[t]):[e]}exports.TradeType=P;exports.WIDGET_PALETTE_COLORS=U;exports.WidgetHookEvents=E;exports.WidgetMethodsEmit=c;exports.WidgetMethodsListen=T;exports.createCowSwapWidget=Z;exports.isCowSwapWidgetPalette=v;exports.isPerNetworkConfig=O;exports.isPerTradeTypeConfig=_;exports.resolveFlexibleConfig=le;exports.resolveFlexibleConfigValues=fe;exports.widgetIframeTransport=s;
package/index.js CHANGED
@@ -3,8 +3,8 @@ import { SimpleCowEventEmitter as U } from "@cowprotocol/events";
3
3
  var l = /* @__PURE__ */ ((e) => (e.ACTIVATE = "ACTIVATE", e.READY = "READY", e.UPDATE_HEIGHT = "UPDATE_HEIGHT", e.SET_FULL_HEIGHT = "SET_FULL_HEIGHT", e.EMIT_COW_EVENT = "EMIT_COW_EVENT", e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e.INTERCEPT_WINDOW_OPEN = "INTERCEPT_WINDOW_OPEN", e.PROCESS_HOOK = "PROCESS_HOOK", e))(l || {}), T = /* @__PURE__ */ ((e) => (e.UPDATE_PARAMS = "UPDATE_PARAMS", e.UPDATE_APP_DATA = "UPDATE_APP_DATA", e.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", e.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", e.HOOK_RESULT = "HOOK_RESULT", e))(T || {}), p = /* @__PURE__ */ ((e) => (e.ON_BEFORE_APPROVAL = "ON_BEFORE_APPROVAL", e.ON_BEFORE_TRADE = "ON_BEFORE_TRADE", e.ON_BEFORE_WRAP_UNWRAP = "ON_BEFORE_WRAP_UNWRAP", e.ON_BEFORE_ORDER_CANCEL = "ON_BEFORE_ORDER_CANCEL", e.ON_BEFORE_ORDERS_CANCEL = "ON_BEFORE_ORDERS_CANCEL", e))(p || {}), A = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e.YIELD = "yield", e))(A || {});
4
4
  const fe = ["primary", "background", "paper", "text", "danger", "warning", "alert", "info", "success"], s = new D("cowSwapWidget");
5
5
  class b {
6
- constructor(t, n, o = []) {
7
- this.contentWindow = t, this.updateListeners(o), this.widgetListener = s.listenToMessageFromWindow(this.contentWindow, l.EMIT_COW_EVENT, (r) => this.eventEmitter.emit(r.event, r.payload), n);
6
+ constructor(n, t, o = []) {
7
+ this.contentWindow = n, this.updateListeners(o), this.widgetListener = s.listenToMessageFromWindow(this.contentWindow, l.EMIT_COW_EVENT, (r) => this.eventEmitter.emit(r.event, r.payload), t);
8
8
  }
9
9
  eventEmitter = new U();
10
10
  listeners = [];
@@ -12,17 +12,17 @@ class b {
12
12
  stopListeningIframe() {
13
13
  s.stopListeningWindowListener(this.contentWindow, this.widgetListener);
14
14
  }
15
- updateListeners(t) {
16
- for (const n of this.listeners)
17
- this.eventEmitter.off(n);
18
- this.listeners = t || [];
19
- for (const n of this.listeners)
20
- this.eventEmitter.on(n);
15
+ updateListeners(n) {
16
+ for (const t of this.listeners)
17
+ this.eventEmitter.off(t);
18
+ this.listeners = n || [];
19
+ for (const t of this.listeners)
20
+ this.eventEmitter.on(t);
21
21
  }
22
22
  }
23
23
  class H {
24
- constructor(t, n) {
25
- this.appWindow = t, this.iframeWidow = n, this.forwardSdkMessage = (o) => {
24
+ constructor(n, t) {
25
+ this.appWindow = n, this.iframeWidow = t, this.forwardSdkMessage = (o) => {
26
26
  y(o.data) && (typeof window < "u" && o.origin === window.location.origin || (B(o.data) ? this.appWindow.parent.postMessage(o.data, "*") : F(o.data) && this.iframeWidow.postMessage(o.data, "*")));
27
27
  }, this.startListening();
28
28
  }
@@ -43,8 +43,8 @@ function B(e) {
43
43
  function F(e) {
44
44
  return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
45
45
  }
46
- function M(e = "Default", t = "#1c5dbf", ...n) {
47
- console.debug(`%c [COW][${e}]`, `font-weight: bold; color: ${t}`, ...n);
46
+ function M(e = "Default", n = "#1c5dbf", ...t) {
47
+ console.debug(`%c [COW][${e}]`, `font-weight: bold; color: ${n}`, ...t);
48
48
  }
49
49
  function V(...e) {
50
50
  M("Widget", "#47aba8", ...e);
@@ -54,41 +54,41 @@ function k(e) {
54
54
  }
55
55
  const C = "_";
56
56
  function j(e) {
57
- const t = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", n = S(e);
58
- return t + "/#" + n + "?" + I(e);
57
+ const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.finance", t = S(e);
58
+ return n + "/#" + t + "?" + I(e);
59
59
  }
60
60
  function S(e) {
61
61
  const {
62
- chainId: t = 1,
63
- sell: n,
62
+ chainId: n = 1,
63
+ sell: t,
64
64
  buy: o,
65
65
  tradeType: r = A.SWAP
66
- } = e, a = [n?.asset || C, o?.asset || C].map(encodeURIComponent).join("/");
67
- return `/${t}/widget/${r}/${a}`;
66
+ } = e, a = [t?.asset || C, o?.asset || C].map(encodeURIComponent).join("/");
67
+ return `/${n}/widget/${r}/${a}`;
68
68
  }
69
69
  function I(e) {
70
- const t = new URLSearchParams();
71
- return K($(Q(x(G(t, e), e), e), e), e);
70
+ const n = new URLSearchParams();
71
+ return K($(Q(x(G(n, e), e), e), e), e);
72
72
  }
73
- function G(e, t) {
73
+ function G(e, n) {
74
74
  const {
75
- sell: n,
75
+ sell: t,
76
76
  buy: o
77
- } = t;
78
- return n?.amount && e.append("sellAmount", n.amount), o?.amount && e.append("buyAmount", o.amount), e;
77
+ } = n;
78
+ return t?.amount && e.append("sellAmount", t.amount), o?.amount && e.append("buyAmount", o.amount), e;
79
79
  }
80
- function x(e, t) {
81
- const n = t.theme;
82
- return n ? (k(n) ? (e.append("palette", encodeURIComponent(JSON.stringify(n))), e.append("theme", n.baseTheme)) : (e.append("palette", "null"), e.append("theme", n)), e) : (e.append("palette", "null"), e);
80
+ function x(e, n) {
81
+ const t = n.theme;
82
+ return t ? (k(t) ? (e.append("palette", encodeURIComponent(JSON.stringify(t))), e.append("theme", t.baseTheme)) : (e.append("palette", "null"), e.append("theme", t)), e) : (e.append("palette", "null"), e);
83
83
  }
84
- function Q(e, t) {
85
- return t.targetChainId && e.append("targetChainId", t.targetChainId.toString()), e;
84
+ function Q(e, n) {
85
+ return n.targetChainId && e.append("targetChainId", n.targetChainId.toString()), e;
86
86
  }
87
- function $(e, t) {
88
- return t.locale && e.append("lng", t.locale), e;
87
+ function $(e, n) {
88
+ return n.locale && e.append("lng", n.locale), e;
89
89
  }
90
- function K(e, t) {
91
- return Y(t.hooks) ? e.append("hooksEnabled", "true") : e.delete("hooksEnabled"), e;
90
+ function K(e, n) {
91
+ return Y(n.hooks) ? e.append("hooksEnabled", "true") : e.delete("hooksEnabled"), e;
92
92
  }
93
93
  function Y(e) {
94
94
  return !!e && Object.values(e).some(Boolean);
@@ -103,27 +103,27 @@ const N = "640px", J = "450px", X = 20, z = {
103
103
  destroy: () => {
104
104
  }
105
105
  };
106
- function pe(e, t) {
106
+ function pe(e, n) {
107
107
  const {
108
- params: n,
108
+ params: t,
109
109
  provider: o,
110
110
  listeners: r,
111
111
  onReady: a
112
- } = t;
113
- let E = o, w = n;
112
+ } = n;
113
+ let E = o, w = t;
114
114
  if (typeof window > "u") return z;
115
- const d = Z(n), i = q(d);
115
+ const d = Z(t), i = q(d);
116
116
  V("Resolved trusted iframe origin", {
117
117
  iframeOrigin: i
118
118
  });
119
119
  const f = [];
120
- a && f.push(te(d.contentWindow || window, i, a)), e.innerHTML = "", e.appendChild(d);
120
+ a && f.push(ne(d.contentWindow || window, i, a)), e.innerHTML = "", e.appendChild(d);
121
121
  const {
122
122
  contentWindow: u
123
123
  } = d;
124
124
  if (!u)
125
125
  throw console.error("Iframe does not contain a window", d), new Error("Iframe does not contain a window!");
126
- f.push(ee(u, i, n.appCode)), f.push(...ie(d, i, n.height, n.maxHeight)), f.push(ne(i));
126
+ f.push(ee(u, i, t.appCode)), f.push(...ie(d, i, t.height, t.maxHeight)), f.push(te(i));
127
127
  let _ = null;
128
128
  function h() {
129
129
  u && (_ && window.removeEventListener("message", _), _ = de(u, i, w.hooks), f.push(_));
@@ -148,27 +148,27 @@ function pe(e, t) {
148
148
  }
149
149
  };
150
150
  }
151
- function g(e, t, n, o) {
152
- n && n.disconnect();
153
- const r = n || new W(e, t);
151
+ function g(e, n, t, o) {
152
+ t && t.disconnect();
153
+ const r = t || new W(e, n);
154
154
  return o && r.onConnect(o), r;
155
155
  }
156
156
  function Z(e) {
157
157
  const {
158
- width: t = J,
159
- height: n = N
158
+ width: n = J,
159
+ height: t = N
160
160
  } = e, o = document.createElement("iframe");
161
- return o.src = j(e), o.width = t, o.height = n, o.style.border = "0", o.allow = "clipboard-read; clipboard-write", o;
161
+ return o.src = j(e), o.width = n, o.height = t, o.style.border = "0", o.allow = "clipboard-read; clipboard-write", o;
162
162
  }
163
163
  function q(e) {
164
164
  return new URL(e.src).origin;
165
165
  }
166
- function L(e, t, n, o) {
167
- const r = !!o, a = S(n), E = I(n).toString(), {
166
+ function L(e, n, t, o) {
167
+ const r = !!o, a = S(t), E = I(t).toString(), {
168
168
  theme: w,
169
169
  hooks: d,
170
170
  ...i
171
- } = n;
171
+ } = t;
172
172
  s.postMessageToWindow(e, T.UPDATE_PARAMS, {
173
173
  urlParams: {
174
174
  pathname: a,
@@ -176,105 +176,105 @@ function L(e, t, n, o) {
176
176
  },
177
177
  appParams: i,
178
178
  hasProvider: r
179
- }, t);
179
+ }, n);
180
180
  }
181
- function ee(e, t, n) {
181
+ function ee(e, n, t) {
182
182
  return s.listenToMessageFromWindow(window, l.ACTIVATE, () => {
183
183
  s.postMessageToWindow(e, T.UPDATE_APP_DATA, {
184
- metaData: n ? {
185
- appCode: n
184
+ metaData: t ? {
185
+ appCode: t
186
186
  } : void 0
187
- }, t);
188
- }, t);
187
+ }, n);
188
+ }, n);
189
189
  }
190
- function te(e, t, n) {
190
+ function ne(e, n, t) {
191
191
  let o = !1;
192
192
  return s.listenToMessageFromWindow(window, l.READY, () => {
193
- o || (o = !0, n());
194
- }, t);
193
+ o || (o = !0, t());
194
+ }, n);
195
195
  }
196
- function ne(e) {
196
+ function te(e) {
197
197
  return s.listenToMessageFromWindow(window, l.INTERCEPT_WINDOW_OPEN, ({
198
- href: t,
199
- rel: n,
198
+ href: n,
199
+ rel: t,
200
200
  target: o
201
201
  }) => {
202
- const r = oe(t.toString(), e);
203
- r && re(r) && window.open(r, o, n);
202
+ const r = oe(n.toString(), e);
203
+ r && re(r) && window.open(r, o, t);
204
204
  }, e);
205
205
  }
206
- function oe(e, t) {
207
- const n = e.trim();
208
- if (!n)
206
+ function oe(e, n) {
207
+ const t = e.trim();
208
+ if (!t)
209
209
  return null;
210
210
  try {
211
- return new URL(n, t).toString();
211
+ return new URL(t, n).toString();
212
212
  } catch {
213
213
  return null;
214
214
  }
215
215
  }
216
216
  function re(e) {
217
217
  try {
218
- const t = new URL(e).protocol;
219
- return t === "http:" || t === "https:";
218
+ const n = new URL(e).protocol;
219
+ return n === "http:" || n === "https:";
220
220
  } catch {
221
221
  return !1;
222
222
  }
223
223
  }
224
- function ie(e, t, n = N, o) {
224
+ function ie(e, n, t = N, o) {
225
225
  return [s.listenToMessageFromWindow(window, l.UPDATE_HEIGHT, (r) => {
226
226
  const a = r.height ? r.height + X : void 0;
227
- e.style.height = a ? `${o ? Math.min(a, o) : a}px` : n;
228
- }, t), s.listenToMessageFromWindow(window, l.SET_FULL_HEIGHT, ({
227
+ e.style.height = a ? `${o ? Math.min(a, o) : a}px` : t;
228
+ }, n), s.listenToMessageFromWindow(window, l.SET_FULL_HEIGHT, ({
229
229
  isUpToSmall: r
230
230
  }) => {
231
- e.style.height = r ? n : `${o || document.body.offsetHeight}px`;
232
- }, t)];
231
+ e.style.height = r ? t : `${o || document.body.offsetHeight}px`;
232
+ }, n)];
233
233
  }
234
234
  const se = {
235
- [p.ON_BEFORE_APPROVAL]: (e, t) => t?.onBeforeApproval ? t.onBeforeApproval(e) : !0,
236
- [p.ON_BEFORE_TRADE]: (e, t) => t?.onBeforeTrade ? t.onBeforeTrade(e) : !0,
237
- [p.ON_BEFORE_WRAP_UNWRAP]: (e, t) => t?.onBeforeWrapOrUnwrap ? t.onBeforeWrapOrUnwrap(e) : !0,
238
- [p.ON_BEFORE_ORDER_CANCEL]: (e, t) => t?.onBeforeOrderCancel ? t.onBeforeOrderCancel(e) : !0,
239
- [p.ON_BEFORE_ORDERS_CANCEL]: (e, t) => t?.onBeforeOrdersCancel ? t.onBeforeOrdersCancel(e) : !0
235
+ [p.ON_BEFORE_APPROVAL]: (e, n) => n?.onBeforeApproval ? n.onBeforeApproval(e) : !0,
236
+ [p.ON_BEFORE_TRADE]: (e, n) => n?.onBeforeTrade ? n.onBeforeTrade(e) : !0,
237
+ [p.ON_BEFORE_WRAP_UNWRAP]: (e, n) => n?.onBeforeWrapOrUnwrap ? n.onBeforeWrapOrUnwrap(e) : !0,
238
+ [p.ON_BEFORE_ORDER_CANCEL]: (e, n) => n?.onBeforeOrderCancel ? n.onBeforeOrderCancel(e) : !0,
239
+ [p.ON_BEFORE_ORDERS_CANCEL]: (e, n) => n?.onBeforeOrdersCancel ? n.onBeforeOrdersCancel(e) : !0
240
240
  };
241
- function ae(e, t) {
242
- return se[e.event](e.payload, t);
241
+ function ae(e, n) {
242
+ return se[e.event](e.payload, n);
243
243
  }
244
- function de(e, t, n) {
244
+ function de(e, n, t) {
245
245
  return s.listenToMessageFromWindow(window, l.PROCESS_HOOK, async (o) => {
246
246
  let r = !1;
247
247
  try {
248
- r = await ae(o, n);
248
+ r = await ae(o, t);
249
249
  } catch {
250
250
  }
251
251
  s.postMessageToWindow(e, T.HOOK_RESULT, {
252
252
  id: o.id,
253
253
  result: r
254
- }, t);
255
- }, t);
254
+ }, n);
255
+ }, n);
256
256
  }
257
257
  const ue = Object.values(A);
258
- function we(e, t, n) {
258
+ function we(e, n, t) {
259
259
  if (O(e)) {
260
- const o = e[n];
261
- return R(o) ? o[t] : o;
260
+ const o = e[t];
261
+ return R(o) ? o[n] : o;
262
262
  }
263
263
  if (R(e)) {
264
- const o = e[t];
265
- return O(o) ? o[n] : o;
264
+ const o = e[n];
265
+ return O(o) ? o[t] : o;
266
266
  }
267
267
  return e;
268
268
  }
269
269
  function O(e) {
270
- return typeof e != "object" ? !1 : Object.keys(e).every((t) => ue.includes(t));
270
+ return typeof e != "object" ? !1 : Object.keys(e).every((n) => ue.includes(n));
271
271
  }
272
272
  const ce = /^\d+$/;
273
273
  function R(e) {
274
- return typeof e != "object" ? !1 : Object.keys(e).every((t) => ce.test(t));
274
+ return typeof e != "object" ? !1 : Object.keys(e).every((n) => ce.test(n));
275
275
  }
276
276
  function _e(e) {
277
- return O(e) ? Object.values(e).flatMap((t) => R(t) ? Object.values(t) : [t]) : R(e) ? Object.values(e).flatMap((t) => O(t) ? Object.values(t) : [t]) : [e];
277
+ return O(e) ? Object.values(e).flatMap((n) => R(n) ? Object.values(n) : [n]) : R(e) ? Object.values(e).flatMap((n) => O(n) ? Object.values(n) : [n]) : [e];
278
278
  }
279
279
  export {
280
280
  A as TradeType,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-lib",
3
- "version": "2.2.5",
3
+ "version": "2.3.0",
4
4
  "description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your website.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,9 +15,9 @@
15
15
  "access": "public"
16
16
  },
17
17
  "dependencies": {
18
- "@cowprotocol/cow-sdk": "8.0.5",
19
- "@cowprotocol/events": "^4.1.2",
20
- "@cowprotocol/iframe-transport": "^2.0.13"
18
+ "@cowprotocol/cow-sdk": "8.1.0",
19
+ "@cowprotocol/events": "^4.2.0",
20
+ "@cowprotocol/iframe-transport": "^2.1.0"
21
21
  },
22
22
  "exports": {
23
23
  ".": {
package/types.d.ts CHANGED
@@ -227,7 +227,7 @@ export interface CowSwapWidgetParams {
227
227
  /**
228
228
  * The base url of the widget implementation
229
229
  * The parameter can have the URL directly, or an object with the environment property,
230
- * The base URL will default to the production environment if not specified, so it will use https://swap.cow.fi by default.
230
+ * The base URL will default to the production environment if not specified, so it will use https://swap.cow.finance by default.
231
231
  */
232
232
  baseUrl?: string;
233
233
  /**