@cowprotocol/widget-lib 0.22.1 → 1.1.1

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.
@@ -1,5 +0,0 @@
1
- import { SupportedChainId } from '@cowprotocol/cow-sdk';
2
- import { FlexibleConfig, PerNetworkConfig, PerTradeTypeConfig, TradeType } from './types';
3
- export declare function resolveFlexibleConfig<T>(config: FlexibleConfig<T>, chainId: SupportedChainId, tradeType: TradeType): T | undefined;
4
- export declare function isPerTradeTypeConfig<T>(config: FlexibleConfig<T>): config is PerTradeTypeConfig<T>;
5
- export declare function isPerNetworkConfig<T>(config: FlexibleConfig<T>): config is PerNetworkConfig<T>;
package/index.d.ts DELETED
@@ -1,7 +0,0 @@
1
- export { createCowSwapWidget } from './cowSwapWidget';
2
- export { widgetIframeTransport } from './widgetIframeTransport';
3
- export type { CowSwapWidgetHandler } from './cowSwapWidget';
4
- export * from './types';
5
- export * from './themeUtils';
6
- export * from './flexibleConfig';
7
- export { resolveFlexibleConfigValues } from './resolveFlexibleConfigValues';
package/index.iife.js DELETED
@@ -1 +0,0 @@
1
- var cowSwapWidget=function(w){"use strict";class D{key;constructor(t){this.key=t}postMessageToWindow(t,n,s){const p=typeof s=="object"?s:{},l={key:this.key,method:n,...p};t.postMessage(l,"*")}listenToMessageFromWindow(t,n,s){const p=l=>{!x(l.data)||l.data.key!==this.key||l.data.method!==n||s(l.data)};return t.addEventListener("message",p),p}stopListeningToMessageFromWindow(t,n,s){t.removeEventListener("message",s)}stopListeningWindowListener(t,n){t.removeEventListener("message",n)}}function x(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}var m=function(e){return e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e.SEND_PROVIDER_META_INFO="SEND_PROVIDER_META_INFO",e.REQUEST_PROVIDER_META_INFO="REQUEST_PROVIDER_META_INFO",e}({});const R=new D("cowSwapIframeRpcProviderTransport");function Q(e){if(e.isWalletConnect)return e?.session?.peer?.metadata}function j(e,t){return t.find(n=>n.provider===e)?.info}const G=["connect","disconnect","close","chainChanged","accountsChanged"],L=[];window.addEventListener("eip6963:announceProvider",e=>{const t=e;L.push(t.detail)});class q{iframeWidow;ethereumProvider=null;requestWaitingForConnection={};constructor(t){this.iframeWidow=t}disconnect(){this.ethereumProvider=null,R.stopListeningToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),R.stopListeningToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest)}onConnect(t){this.ethereumProvider?this.disconnect():R.listenToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),G.forEach(n=>{t.on(n,s=>this.onProviderEvent(n,s))}),R.listenToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest)}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:s,method:p}=t;if(!this.ethereumProvider||!n)return;(p==="enable"?this.ethereumProvider.request({method:"eth_requestAccounts",id:n}):this.ethereumProvider.request({...t,id:n})).then(_=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,result:_}})).catch(_=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,error:_}}))}processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)};processProviderMetaInfoRequest=()=>{if(!this.ethereumProvider)return;const t=j(this.ethereumProvider,L),n=Q(this.ethereumProvider);R.postMessageToWindow(this.iframeWidow,m.SEND_PROVIDER_META_INFO,{providerEip6963Info:t,providerWcMetadata:n})};onProviderEvent(t,n){R.postMessageToWindow(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){R.postMessageToWindow(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,t)}}var y={exports:{}},b;function $(){return b||(b=1,function(e){var t=Object.prototype.hasOwnProperty,n="~";function s(){}Object.create&&(s.prototype=Object.create(null),new s().__proto__||(n=!1));function p(c,r,i){this.fn=c,this.context=r,this.once=i||!1}function l(c,r,i,o,E){if(typeof i!="function")throw new TypeError("The listener must be a function");var d=new p(i,o||c,E),f=n?n+r:r;return c._events[f]?c._events[f].fn?c._events[f]=[c._events[f],d]:c._events[f].push(d):(c._events[f]=d,c._eventsCount++),c}function _(c,r){--c._eventsCount===0?c._events=new s:delete c._events[r]}function u(){this._events=new s,this._eventsCount=0}u.prototype.eventNames=function(){var r=[],i,o;if(this._eventsCount===0)return r;for(o in i=this._events)t.call(i,o)&&r.push(n?o.slice(1):o);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(i)):r},u.prototype.listeners=function(r){var i=n?n+r:r,o=this._events[i];if(!o)return[];if(o.fn)return[o.fn];for(var E=0,d=o.length,f=new Array(d);E<d;E++)f[E]=o[E].fn;return f},u.prototype.listenerCount=function(r){var i=n?n+r:r,o=this._events[i];return o?o.fn?1:o.length:0},u.prototype.emit=function(r,i,o,E,d,f){var v=n?n+r:r;if(!this._events[v])return!1;var a=this._events[v],P=arguments.length,g,h;if(a.fn){switch(a.once&&this.removeListener(r,a.fn,void 0,!0),P){case 1:return a.fn.call(a.context),!0;case 2:return a.fn.call(a.context,i),!0;case 3:return a.fn.call(a.context,i,o),!0;case 4:return a.fn.call(a.context,i,o,E),!0;case 5:return a.fn.call(a.context,i,o,E,d),!0;case 6:return a.fn.call(a.context,i,o,E,d,f),!0}for(h=1,g=new Array(P-1);h<P;h++)g[h-1]=arguments[h];a.fn.apply(a.context,g)}else{var Ee=a.length,W;for(h=0;h<Ee;h++)switch(a[h].once&&this.removeListener(r,a[h].fn,void 0,!0),P){case 1:a[h].fn.call(a[h].context);break;case 2:a[h].fn.call(a[h].context,i);break;case 3:a[h].fn.call(a[h].context,i,o);break;case 4:a[h].fn.call(a[h].context,i,o,E);break;default:if(!g)for(W=1,g=new Array(P-1);W<P;W++)g[W-1]=arguments[W];a[h].fn.apply(a[h].context,g)}}return!0},u.prototype.on=function(r,i,o){return l(this,r,i,o,!1)},u.prototype.once=function(r,i,o){return l(this,r,i,o,!0)},u.prototype.removeListener=function(r,i,o,E){var d=n?n+r:r;if(!this._events[d])return this;if(!i)return _(this,d),this;var f=this._events[d];if(f.fn)f.fn===i&&(!E||f.once)&&(!o||f.context===o)&&_(this,d);else{for(var v=0,a=[],P=f.length;v<P;v++)(f[v].fn!==i||E&&!f[v].once||o&&f[v].context!==o)&&a.push(f[v]);a.length?this._events[d]=a.length===1?a[0]:a:_(this,d)}return this},u.prototype.removeAllListeners=function(r){var i;return r?(i=n?n+r:r,this._events[i]&&_(this,i)):(this._events=new s,this._eventsCount=0),this},u.prototype.off=u.prototype.removeListener,u.prototype.addListener=u.prototype.on,u.prefixed=n,u.EventEmitter=u,e.exports=u}(y)),y.exports}$();class B{subscriptions={};on(t){const{event:n,handler:s}=t;return this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(s),t}off(t){const{event:n,handler:s}=t;return this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(p=>p!==s)),t}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(s=>s(n))}}var I=function(e){return e.ACTIVATE="ACTIVATE",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}({}),S=function(e){return 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}({}),A=function(e){return e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e}({});const Y=["primary","background","paper","text","danger","warning","alert","info","success"],T=new D("cowSwapWidget");class z{contentWindow;eventEmitter=new B;listeners=[];widgetListener;constructor(t,n=[]){this.contentWindow=t,this.updateListeners(n),this.widgetListener=T.listenToMessageFromWindow(this.contentWindow,I.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){T.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 J{appWindow;iframeWidow;forwardSdkMessage;constructor(t,n){this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=s=>{K(s.data)&&s.origin!==window.location.origin&&(X(s.data)?this.appWindow.parent.postMessage(s.data,"*"):Z(s.data)&&this.iframeWidow.postMessage(s.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function K(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function X(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 Z(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function M(e){return!!(e&&typeof e=="object")}const N="_";function ee(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=V(e);return t+"/#"+n+"?"+F(e)}function V(e){const{chainId:t=1,sell:n,buy:s,tradeType:p=A.SWAP}=e,l=[n?.asset||N,s?.asset||N].map(encodeURIComponent).join("/");return`/${t}/widget/${p}/${l}`}function F(e){const t=new URLSearchParams;return se(ne(te(t,e),e),e)}function te(e,t){const{sell:n,buy:s}=t;return n?.amount&&e.append("sellAmount",n.amount),s?.amount&&e.append("buyAmount",s.amount),e}function ne(e,t){const n=t.theme;return n?(M(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 se(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}const U="640px",re="450px",ie=20;function oe(e,t){const{params:n,provider:s,listeners:p}=t;let l=s,_=n;const u=ae(n);e.innerHTML="",e.appendChild(u);const{contentWindow:c}=u;if(!c)throw console.error("Iframe does not contain a window",u),new Error("Iframe does not contain a window!");const r=[];r.push(ce(c,n.appCode)),r.push(...de(u,n.height,n.maxHeight)),r.push(ue());const i=new z(window,p);let o=k(c,null,l);u.addEventListener("load",()=>H(c,_,l));const E=new J(window,c);return{updateParams:d=>{_=d,H(c,_,l)},updateListeners:d=>i.updateListeners(d),updateProvider:d=>{l=d,o=k(c,o,d)},destroy:()=>{o.disconnect(),i.stopListeningIframe(),r.forEach(d=>window.removeEventListener("message",d)),E.stopListening(),e.removeChild(u)}}}function k(e,t,n){t&&t.disconnect();const s=t||new q(e);return n&&s.onConnect(n),s}function ae(e){const{width:t=re,height:n=U}=e,s=document.createElement("iframe");return s.src=ee(e),s.width=t,s.height=n,s.style.border="0",s.allow="clipboard-read; clipboard-write",s}function H(e,t,n){const s=!!n,p=V(t),l=F(t).toString(),{theme:_,...u}=t;T.postMessageToWindow(e,S.UPDATE_PARAMS,{urlParams:{pathname:p,search:l},appParams:u,hasProvider:s})}function ce(e,t){return T.listenToMessageFromWindow(window,I.ACTIVATE,()=>{T.postMessageToWindow(e,S.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})})}function ue(){return T.listenToMessageFromWindow(window,I.INTERCEPT_WINDOW_OPEN,({href:e,rel:t,target:n})=>{const s=e.toString();if(!s.startsWith("http")&&s.match(/^[a-zA-Z0-9]+:\/\//)){window.open(s,n,t);return}})}function de(e,t=U,n){return[T.listenToMessageFromWindow(window,I.UPDATE_HEIGHT,s=>{const p=s.height?s.height+ie:void 0;e.style.height=p?`${n?Math.min(p,n):p}px`:t}),T.listenToMessageFromWindow(window,I.SET_FULL_HEIGHT,({isUpToSmall:s})=>{e.style.height=s?t:`${n||document.body.offsetHeight}px`})]}const fe=Object.values(A);function pe(e,t,n){if(O(e)){const s=e[n];return C(s)?s[t]:s}if(C(e)){const s=e[t];return O(s)?s[n]:s}return e}function O(e){return typeof e!="object"?!1:Object.keys(e).every(t=>fe.includes(t))}const he=/^\d+$/;function C(e){return typeof e!="object"?!1:Object.keys(e).every(t=>he.test(t))}function le(e){return O(e)?Object.values(e).flatMap(t=>C(t)?Object.values(t):[t]):C(e)?Object.values(e).flatMap(t=>O(t)?Object.values(t):[t]):[e]}return w.TradeType=A,w.WIDGET_PALETTE_COLORS=Y,w.WidgetMethodsEmit=I,w.WidgetMethodsListen=S,w.createCowSwapWidget=oe,w.isCowSwapWidgetPalette=M,w.isPerNetworkConfig=C,w.isPerTradeTypeConfig=O,w.resolveFlexibleConfig=pe,w.resolveFlexibleConfigValues=le,w.widgetIframeTransport=T,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"}),w}({});
package/index.js DELETED
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class M{key;constructor(t){this.key=t}postMessageToWindow(t,n,s){const p=typeof s=="object"?s:{},l={key:this.key,method:n,...p};t.postMessage(l,"*")}listenToMessageFromWindow(t,n,s){const p=l=>{!H(l.data)||l.data.key!==this.key||l.data.method!==n||s(l.data)};return t.addEventListener("message",p),p}stopListeningToMessageFromWindow(t,n,s){t.removeEventListener("message",s)}stopListeningWindowListener(t,n){t.removeEventListener("message",n)}}function H(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}var m=function(e){return e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e.SEND_PROVIDER_META_INFO="SEND_PROVIDER_META_INFO",e.REQUEST_PROVIDER_META_INFO="REQUEST_PROVIDER_META_INFO",e}({});const R=new M("cowSwapIframeRpcProviderTransport");function Q(e){if(e.isWalletConnect)return e?.session?.peer?.metadata}function j(e,t){return t.find(n=>n.provider===e)?.info}const G=["connect","disconnect","close","chainChanged","accountsChanged"],N=[];window.addEventListener("eip6963:announceProvider",e=>{const t=e;N.push(t.detail)});class q{iframeWidow;ethereumProvider=null;requestWaitingForConnection={};constructor(t){this.iframeWidow=t}disconnect(){this.ethereumProvider=null,R.stopListeningToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),R.stopListeningToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest)}onConnect(t){this.ethereumProvider?this.disconnect():R.listenToMessageFromWindow(window,m.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),G.forEach(n=>{t.on(n,s=>this.onProviderEvent(n,s))}),R.listenToMessageFromWindow(window,m.REQUEST_PROVIDER_META_INFO,this.processProviderMetaInfoRequest)}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:s,method:p}=t;if(!this.ethereumProvider||!n)return;(p==="enable"?this.ethereumProvider.request({method:"eth_requestAccounts",id:n}):this.ethereumProvider.request({...t,id:n})).then(w=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,result:w}})).catch(w=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:s,id:n,error:w}}))}processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)};processProviderMetaInfoRequest=()=>{if(!this.ethereumProvider)return;const t=j(this.ethereumProvider,N),n=Q(this.ethereumProvider);R.postMessageToWindow(this.iframeWidow,m.SEND_PROVIDER_META_INFO,{providerEip6963Info:t,providerWcMetadata:n})};onProviderEvent(t,n){R.postMessageToWindow(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){R.postMessageToWindow(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,t)}}var W={exports:{}},A;function $(){return A||(A=1,function(e){var t=Object.prototype.hasOwnProperty,n="~";function s(){}Object.create&&(s.prototype=Object.create(null),new s().__proto__||(n=!1));function p(c,r,i){this.fn=c,this.context=r,this.once=i||!1}function l(c,r,i,o,E){if(typeof i!="function")throw new TypeError("The listener must be a function");var d=new p(i,o||c,E),f=n?n+r:r;return c._events[f]?c._events[f].fn?c._events[f]=[c._events[f],d]:c._events[f].push(d):(c._events[f]=d,c._eventsCount++),c}function w(c,r){--c._eventsCount===0?c._events=new s:delete c._events[r]}function u(){this._events=new s,this._eventsCount=0}u.prototype.eventNames=function(){var r=[],i,o;if(this._eventsCount===0)return r;for(o in i=this._events)t.call(i,o)&&r.push(n?o.slice(1):o);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(i)):r},u.prototype.listeners=function(r){var i=n?n+r:r,o=this._events[i];if(!o)return[];if(o.fn)return[o.fn];for(var E=0,d=o.length,f=new Array(d);E<d;E++)f[E]=o[E].fn;return f},u.prototype.listenerCount=function(r){var i=n?n+r:r,o=this._events[i];return o?o.fn?1:o.length:0},u.prototype.emit=function(r,i,o,E,d,f){var _=n?n+r:r;if(!this._events[_])return!1;var a=this._events[_],v=arguments.length,P,h;if(a.fn){switch(a.once&&this.removeListener(r,a.fn,void 0,!0),v){case 1:return a.fn.call(a.context),!0;case 2:return a.fn.call(a.context,i),!0;case 3:return a.fn.call(a.context,i,o),!0;case 4:return a.fn.call(a.context,i,o,E),!0;case 5:return a.fn.call(a.context,i,o,E,d),!0;case 6:return a.fn.call(a.context,i,o,E,d,f),!0}for(h=1,P=new Array(v-1);h<v;h++)P[h-1]=arguments[h];a.fn.apply(a.context,P)}else{var k=a.length,I;for(h=0;h<k;h++)switch(a[h].once&&this.removeListener(r,a[h].fn,void 0,!0),v){case 1:a[h].fn.call(a[h].context);break;case 2:a[h].fn.call(a[h].context,i);break;case 3:a[h].fn.call(a[h].context,i,o);break;case 4:a[h].fn.call(a[h].context,i,o,E);break;default:if(!P)for(I=1,P=new Array(v-1);I<v;I++)P[I-1]=arguments[I];a[h].fn.apply(a[h].context,P)}}return!0},u.prototype.on=function(r,i,o){return l(this,r,i,o,!1)},u.prototype.once=function(r,i,o){return l(this,r,i,o,!0)},u.prototype.removeListener=function(r,i,o,E){var d=n?n+r:r;if(!this._events[d])return this;if(!i)return w(this,d),this;var f=this._events[d];if(f.fn)f.fn===i&&(!E||f.once)&&(!o||f.context===o)&&w(this,d);else{for(var _=0,a=[],v=f.length;_<v;_++)(f[_].fn!==i||E&&!f[_].once||o&&f[_].context!==o)&&a.push(f[_]);a.length?this._events[d]=a.length===1?a[0]:a:w(this,d)}return this},u.prototype.removeAllListeners=function(r){var i;return r?(i=n?n+r:r,this._events[i]&&w(this,i)):(this._events=new s,this._eventsCount=0),this},u.prototype.off=u.prototype.removeListener,u.prototype.addListener=u.prototype.on,u.prefixed=n,u.EventEmitter=u,e.exports=u}(W)),W.exports}$();class B{subscriptions={};on(t){const{event:n,handler:s}=t;return this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(s),t}off(t){const{event:n,handler:s}=t;return this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(p=>p!==s)),t}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(s=>s(n))}}var g=function(e){return e.ACTIVATE="ACTIVATE",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}({}),y=function(e){return 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}({}),S=function(e){return e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e.YIELD="yield",e}({});const Y=["primary","background","paper","text","danger","warning","alert","info","success"],T=new M("cowSwapWidget");class z{contentWindow;eventEmitter=new B;listeners=[];widgetListener;constructor(t,n=[]){this.contentWindow=t,this.updateListeners(n),this.widgetListener=T.listenToMessageFromWindow(this.contentWindow,g.EMIT_COW_EVENT,s=>this.eventEmitter.emit(s.event,s.payload))}stopListeningIframe(){T.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 J{appWindow;iframeWidow;forwardSdkMessage;constructor(t,n){this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=s=>{K(s.data)&&s.origin!==window.location.origin&&(X(s.data)?this.appWindow.parent.postMessage(s.data,"*"):Z(s.data)&&this.iframeWidow.postMessage(s.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function K(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function X(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 Z(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}function V(e){return!!(e&&typeof e=="object")}const D="_";function ee(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=F(e);return t+"/#"+n+"?"+U(e)}function F(e){const{chainId:t=1,sell:n,buy:s,tradeType:p=S.SWAP}=e,l=[n?.asset||D,s?.asset||D].map(encodeURIComponent).join("/");return`/${t}/widget/${p}/${l}`}function U(e){const t=new URLSearchParams;return se(ne(te(t,e),e),e)}function te(e,t){const{sell:n,buy:s}=t;return n?.amount&&e.append("sellAmount",n.amount),s?.amount&&e.append("buyAmount",s.amount),e}function ne(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 se(e,t){return t.targetChainId&&e.append("targetChainId",t.targetChainId.toString()),e}const x="640px",re="450px",ie=20;function oe(e,t){const{params:n,provider:s,listeners:p}=t;let l=s,w=n;const u=ae(n);e.innerHTML="",e.appendChild(u);const{contentWindow:c}=u;if(!c)throw console.error("Iframe does not contain a window",u),new Error("Iframe does not contain a window!");const r=[];r.push(ce(c,n.appCode)),r.push(...de(u,n.height,n.maxHeight)),r.push(ue());const i=new z(window,p);let o=L(c,null,l);u.addEventListener("load",()=>b(c,w,l));const E=new J(window,c);return{updateParams:d=>{w=d,b(c,w,l)},updateListeners:d=>i.updateListeners(d),updateProvider:d=>{l=d,o=L(c,o,d)},destroy:()=>{o.disconnect(),i.stopListeningIframe(),r.forEach(d=>window.removeEventListener("message",d)),E.stopListening(),e.removeChild(u)}}}function L(e,t,n){t&&t.disconnect();const s=t||new q(e);return n&&s.onConnect(n),s}function ae(e){const{width:t=re,height:n=x}=e,s=document.createElement("iframe");return s.src=ee(e),s.width=t,s.height=n,s.style.border="0",s.allow="clipboard-read; clipboard-write",s}function b(e,t,n){const s=!!n,p=F(t),l=U(t).toString(),{theme:w,...u}=t;T.postMessageToWindow(e,y.UPDATE_PARAMS,{urlParams:{pathname:p,search:l},appParams:u,hasProvider:s})}function ce(e,t){return T.listenToMessageFromWindow(window,g.ACTIVATE,()=>{T.postMessageToWindow(e,y.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})})}function ue(){return T.listenToMessageFromWindow(window,g.INTERCEPT_WINDOW_OPEN,({href:e,rel:t,target:n})=>{const s=e.toString();if(!s.startsWith("http")&&s.match(/^[a-zA-Z0-9]+:\/\//)){window.open(s,n,t);return}})}function de(e,t=x,n){return[T.listenToMessageFromWindow(window,g.UPDATE_HEIGHT,s=>{const p=s.height?s.height+ie:void 0;e.style.height=p?`${n?Math.min(p,n):p}px`:t}),T.listenToMessageFromWindow(window,g.SET_FULL_HEIGHT,({isUpToSmall:s})=>{e.style.height=s?t:`${n||document.body.offsetHeight}px`})]}const fe=Object.values(S);function pe(e,t,n){if(O(e)){const s=e[n];return C(s)?s[t]:s}if(C(e)){const s=e[t];return O(s)?s[n]:s}return e}function O(e){return typeof e!="object"?!1:Object.keys(e).every(t=>fe.includes(t))}const he=/^\d+$/;function C(e){return typeof e!="object"?!1:Object.keys(e).every(t=>he.test(t))}function le(e){return O(e)?Object.values(e).flatMap(t=>C(t)?Object.values(t):[t]):C(e)?Object.values(e).flatMap(t=>O(t)?Object.values(t):[t]):[e]}exports.TradeType=S;exports.WIDGET_PALETTE_COLORS=Y;exports.WidgetMethodsEmit=g;exports.WidgetMethodsListen=y;exports.createCowSwapWidget=oe;exports.isCowSwapWidgetPalette=V;exports.isPerNetworkConfig=C;exports.isPerTradeTypeConfig=O;exports.resolveFlexibleConfig=pe;exports.resolveFlexibleConfigValues=le;exports.widgetIframeTransport=T;
package/index.mjs DELETED
@@ -1,499 +0,0 @@
1
- class L {
2
- key;
3
- constructor(t) {
4
- this.key = t;
5
- }
6
- // TODO: Add proper return type annotation
7
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
8
- postMessageToWindow(t, n, s) {
9
- const p = typeof s == "object" ? s : {}, l = {
10
- key: this.key,
11
- method: n,
12
- ...p
13
- };
14
- t.postMessage(l, "*");
15
- }
16
- listenToMessageFromWindow(t, n, s) {
17
- const p = (l) => {
18
- !k(l.data) || l.data.key !== this.key || l.data.method !== n || s(l.data);
19
- };
20
- return t.addEventListener("message", p), p;
21
- }
22
- // TODO: Add proper return type annotation
23
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
24
- stopListeningToMessageFromWindow(t, n, s) {
25
- t.removeEventListener("message", s);
26
- }
27
- // TODO: Add proper return type annotation
28
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
29
- stopListeningWindowListener(t, n) {
30
- t.removeEventListener("message", n);
31
- }
32
- }
33
- function k(e) {
34
- return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
35
- }
36
- var m = /* @__PURE__ */ function(e) {
37
- return e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", e.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", e.SEND_PROVIDER_META_INFO = "SEND_PROVIDER_META_INFO", e.REQUEST_PROVIDER_META_INFO = "REQUEST_PROVIDER_META_INFO", e;
38
- }({});
39
- const P = new L("cowSwapIframeRpcProviderTransport");
40
- function H(e) {
41
- if (e.isWalletConnect)
42
- return e?.session?.peer?.metadata;
43
- }
44
- function Q(e, t) {
45
- return t.find((n) => n.provider === e)?.info;
46
- }
47
- const j = [
48
- "connect",
49
- "disconnect",
50
- "close",
51
- "chainChanged",
52
- "accountsChanged"
53
- ], b = [];
54
- window.addEventListener("eip6963:announceProvider", (e) => {
55
- const t = e;
56
- b.push(t.detail);
57
- });
58
- class G {
59
- iframeWidow;
60
- /**
61
- * The Ethereum provider instance.
62
- * When is null the JSON-RPC bridge is disconnected from the Ethereum provider.
63
- * */
64
- ethereumProvider = null;
65
- /** Stored JSON-RPC requests, to queue them when disconnected. */
66
- requestWaitingForConnection = {};
67
- /**
68
- * Creates an instance of IframeRpcProviderBridge.
69
- * @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
70
- * Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
71
- */
72
- constructor(t) {
73
- this.iframeWidow = t;
74
- }
75
- /**
76
- * Disconnects the JSON-RPC bridge from the Ethereum provider.
77
- */
78
- // TODO: Add proper return type annotation
79
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
80
- disconnect() {
81
- this.ethereumProvider = null, P.stopListeningToMessageFromWindow(window, m.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), P.stopListeningToMessageFromWindow(window, m.REQUEST_PROVIDER_META_INFO, this.processProviderMetaInfoRequest);
82
- }
83
- /**
84
- * Handles the 'connect' event and sets up event listeners for Ethereum provider events.
85
- * @param newProvider - The Ethereum provider to connect.
86
- */
87
- // TODO: Add proper return type annotation
88
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
89
- onConnect(t) {
90
- this.ethereumProvider ? this.disconnect() : P.listenToMessageFromWindow(window, m.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = t, this.processPendingRequests(), j.forEach((n) => {
91
- t.on(n, (s) => this.onProviderEvent(n, s));
92
- }), P.listenToMessageFromWindow(window, m.REQUEST_PROVIDER_META_INFO, this.processProviderMetaInfoRequest);
93
- }
94
- // TODO: Add proper return type annotation
95
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
96
- processPendingRequests() {
97
- Object.keys(this.requestWaitingForConnection).forEach((t) => {
98
- this.processRpcRequest(this.requestWaitingForConnection[t]);
99
- }), this.requestWaitingForConnection = {};
100
- }
101
- /**
102
- * Processes a JSON-RPC request and sends appropriate response or error via the content window.
103
- * @param request - The JSON-RPC request to be processed.
104
- */
105
- // TODO: Add proper return type annotation
106
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
107
- processRpcRequest(t) {
108
- const { id: n, jsonrpc: s, method: p } = t;
109
- if (!this.ethereumProvider || !n)
110
- return;
111
- // Keep the legacy "enable" method for backward compatibility
112
- (p === "enable" ? this.ethereumProvider.request({
113
- method: "eth_requestAccounts",
114
- id: n
115
- }) : this.ethereumProvider.request({
116
- ...t,
117
- id: n
118
- })).then((w) => this.forwardRpcResponseToIframe({
119
- rpcResponse: {
120
- jsonrpc: s,
121
- id: n,
122
- result: w
123
- }
124
- })).catch((w) => this.forwardRpcResponseToIframe({
125
- rpcResponse: {
126
- jsonrpc: s,
127
- id: n,
128
- error: w
129
- }
130
- }));
131
- }
132
- processRpcCallFromWindow = ({ rpcRequest: t }) => {
133
- if (!this.ethereumProvider) {
134
- t.id && (this.requestWaitingForConnection[t.id] = t);
135
- return;
136
- }
137
- this.processRpcRequest(t);
138
- };
139
- processProviderMetaInfoRequest = () => {
140
- if (!this.ethereumProvider) return;
141
- const t = Q(this.ethereumProvider, b), n = H(this.ethereumProvider);
142
- P.postMessageToWindow(this.iframeWidow, m.SEND_PROVIDER_META_INFO, {
143
- providerEip6963Info: t,
144
- providerWcMetadata: n
145
- });
146
- };
147
- onProviderEvent(t, n) {
148
- P.postMessageToWindow(this.iframeWidow, m.PROVIDER_ON_EVENT, {
149
- event: t,
150
- params: n
151
- });
152
- }
153
- /**
154
- * Forward a JSON-RPC message to the content window.
155
- */
156
- // TODO: Add proper return type annotation
157
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
158
- forwardRpcResponseToIframe(t) {
159
- P.postMessageToWindow(this.iframeWidow, m.PROVIDER_RPC_RESPONSE, t);
160
- }
161
- }
162
- var C = { exports: {} }, y;
163
- function q() {
164
- return y || (y = 1, function(e) {
165
- var t = Object.prototype.hasOwnProperty, n = "~";
166
- function s() {
167
- }
168
- Object.create && (s.prototype = /* @__PURE__ */ Object.create(null), new s().__proto__ || (n = !1));
169
- function p(c, r, o) {
170
- this.fn = c, this.context = r, this.once = o || !1;
171
- }
172
- function l(c, r, o, i, E) {
173
- if (typeof o != "function")
174
- throw new TypeError("The listener must be a function");
175
- var d = new p(o, i || c, E), f = n ? n + r : r;
176
- return c._events[f] ? c._events[f].fn ? c._events[f] = [c._events[f], d] : c._events[f].push(d) : (c._events[f] = d, c._eventsCount++), c;
177
- }
178
- function w(c, r) {
179
- --c._eventsCount === 0 ? c._events = new s() : delete c._events[r];
180
- }
181
- function u() {
182
- this._events = new s(), this._eventsCount = 0;
183
- }
184
- u.prototype.eventNames = function() {
185
- var r = [], o, i;
186
- if (this._eventsCount === 0) return r;
187
- for (i in o = this._events)
188
- t.call(o, i) && r.push(n ? i.slice(1) : i);
189
- return Object.getOwnPropertySymbols ? r.concat(Object.getOwnPropertySymbols(o)) : r;
190
- }, u.prototype.listeners = function(r) {
191
- var o = n ? n + r : r, i = this._events[o];
192
- if (!i) return [];
193
- if (i.fn) return [i.fn];
194
- for (var E = 0, d = i.length, f = new Array(d); E < d; E++)
195
- f[E] = i[E].fn;
196
- return f;
197
- }, u.prototype.listenerCount = function(r) {
198
- var o = n ? n + r : r, i = this._events[o];
199
- return i ? i.fn ? 1 : i.length : 0;
200
- }, u.prototype.emit = function(r, o, i, E, d, f) {
201
- var _ = n ? n + r : r;
202
- if (!this._events[_]) return !1;
203
- var a = this._events[_], T = arguments.length, R, h;
204
- if (a.fn) {
205
- switch (a.once && this.removeListener(r, a.fn, void 0, !0), T) {
206
- case 1:
207
- return a.fn.call(a.context), !0;
208
- case 2:
209
- return a.fn.call(a.context, o), !0;
210
- case 3:
211
- return a.fn.call(a.context, o, i), !0;
212
- case 4:
213
- return a.fn.call(a.context, o, i, E), !0;
214
- case 5:
215
- return a.fn.call(a.context, o, i, E, d), !0;
216
- case 6:
217
- return a.fn.call(a.context, o, i, E, d, f), !0;
218
- }
219
- for (h = 1, R = new Array(T - 1); h < T; h++)
220
- R[h - 1] = arguments[h];
221
- a.fn.apply(a.context, R);
222
- } else {
223
- var x = a.length, g;
224
- for (h = 0; h < x; h++)
225
- switch (a[h].once && this.removeListener(r, a[h].fn, void 0, !0), T) {
226
- case 1:
227
- a[h].fn.call(a[h].context);
228
- break;
229
- case 2:
230
- a[h].fn.call(a[h].context, o);
231
- break;
232
- case 3:
233
- a[h].fn.call(a[h].context, o, i);
234
- break;
235
- case 4:
236
- a[h].fn.call(a[h].context, o, i, E);
237
- break;
238
- default:
239
- if (!R) for (g = 1, R = new Array(T - 1); g < T; g++)
240
- R[g - 1] = arguments[g];
241
- a[h].fn.apply(a[h].context, R);
242
- }
243
- }
244
- return !0;
245
- }, u.prototype.on = function(r, o, i) {
246
- return l(this, r, o, i, !1);
247
- }, u.prototype.once = function(r, o, i) {
248
- return l(this, r, o, i, !0);
249
- }, u.prototype.removeListener = function(r, o, i, E) {
250
- var d = n ? n + r : r;
251
- if (!this._events[d]) return this;
252
- if (!o)
253
- return w(this, d), this;
254
- var f = this._events[d];
255
- if (f.fn)
256
- f.fn === o && (!E || f.once) && (!i || f.context === i) && w(this, d);
257
- else {
258
- for (var _ = 0, a = [], T = f.length; _ < T; _++)
259
- (f[_].fn !== o || E && !f[_].once || i && f[_].context !== i) && a.push(f[_]);
260
- a.length ? this._events[d] = a.length === 1 ? a[0] : a : w(this, d);
261
- }
262
- return this;
263
- }, u.prototype.removeAllListeners = function(r) {
264
- var o;
265
- return r ? (o = n ? n + r : r, this._events[o] && w(this, o)) : (this._events = new s(), this._eventsCount = 0), this;
266
- }, u.prototype.off = u.prototype.removeListener, u.prototype.addListener = u.prototype.on, u.prefixed = n, u.EventEmitter = u, e.exports = u;
267
- }(C)), C.exports;
268
- }
269
- q();
270
- class $ {
271
- subscriptions = {};
272
- on(t) {
273
- const { event: n, handler: s } = t;
274
- return this.subscriptions[n] || (this.subscriptions[n] = []), this.subscriptions[n].push(s), t;
275
- }
276
- off(t) {
277
- const { event: n, handler: s } = t;
278
- return this.subscriptions[n] && (this.subscriptions[n] = this.subscriptions[n].filter((p) => p !== s)), t;
279
- }
280
- emit(t, n) {
281
- this.subscriptions[t] && this.subscriptions[t].forEach((s) => s(n));
282
- }
283
- }
284
- var I = /* @__PURE__ */ function(e) {
285
- return e.ACTIVATE = "ACTIVATE", 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;
286
- }({}), M = /* @__PURE__ */ function(e) {
287
- return 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;
288
- }({}), N = /* @__PURE__ */ function(e) {
289
- return e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e.YIELD = "yield", e;
290
- }({});
291
- const fe = [
292
- "primary",
293
- "background",
294
- "paper",
295
- "text",
296
- "danger",
297
- "warning",
298
- "alert",
299
- "info",
300
- "success"
301
- ], v = new L("cowSwapWidget");
302
- class B {
303
- contentWindow;
304
- eventEmitter = new $();
305
- listeners = [];
306
- widgetListener;
307
- constructor(t, n = []) {
308
- this.contentWindow = t, this.updateListeners(n), this.widgetListener = v.listenToMessageFromWindow(this.contentWindow, I.EMIT_COW_EVENT, (s) => this.eventEmitter.emit(s.event, s.payload));
309
- }
310
- // TODO: Add proper return type annotation
311
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
312
- stopListeningIframe() {
313
- v.stopListeningWindowListener(this.contentWindow, this.widgetListener);
314
- }
315
- updateListeners(t) {
316
- for (const n of this.listeners)
317
- this.eventEmitter.off(n);
318
- this.listeners = t || [];
319
- for (const n of this.listeners)
320
- this.eventEmitter.on(n);
321
- }
322
- }
323
- class Y {
324
- appWindow;
325
- iframeWidow;
326
- forwardSdkMessage;
327
- constructor(t, n) {
328
- this.appWindow = t, this.iframeWidow = n, this.forwardSdkMessage = (s) => {
329
- z(s.data) && s.origin !== window.location.origin && (J(s.data) ? this.appWindow.parent.postMessage(s.data, "*") : K(s.data) && this.iframeWidow.postMessage(s.data, "*"));
330
- }, this.startListening();
331
- }
332
- // TODO: Add proper return type annotation
333
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
334
- startListening() {
335
- this.appWindow.addEventListener("message", this.forwardSdkMessage);
336
- }
337
- // TODO: Add proper return type annotation
338
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
339
- stopListening() {
340
- this.appWindow.removeEventListener("message", this.forwardSdkMessage);
341
- }
342
- }
343
- function z(e) {
344
- return typeof e == "object" && e !== null && "id" in e && typeof e.id == "string";
345
- }
346
- function J(e) {
347
- 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;
348
- }
349
- function K(e) {
350
- return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
351
- }
352
- function X(e) {
353
- return !!(e && typeof e == "object");
354
- }
355
- const A = "_";
356
- function Z(e) {
357
- const t = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", n = V(e);
358
- return t + "/#" + n + "?" + F(e);
359
- }
360
- function V(e) {
361
- const { chainId: t = 1, sell: n, buy: s, tradeType: p = N.SWAP } = e, l = [
362
- n?.asset || A,
363
- s?.asset || A
364
- ].map(encodeURIComponent).join("/");
365
- return `/${t}/widget/${p}/${l}`;
366
- }
367
- function F(e) {
368
- const t = new URLSearchParams();
369
- return ne(te(ee(t, e), e), e);
370
- }
371
- function ee(e, t) {
372
- const { sell: n, buy: s } = t;
373
- return n?.amount && e.append("sellAmount", n.amount), s?.amount && e.append("buyAmount", s.amount), e;
374
- }
375
- function te(e, t) {
376
- const n = t.theme;
377
- return n ? (X(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);
378
- }
379
- function ne(e, t) {
380
- return t.targetChainId && e.append("targetChainId", t.targetChainId.toString()), e;
381
- }
382
- const U = "640px", se = "450px", re = 20;
383
- function pe(e, t) {
384
- const { params: n, provider: s, listeners: p } = t;
385
- let l = s, w = n;
386
- const u = oe(n);
387
- e.innerHTML = "", e.appendChild(u);
388
- const { contentWindow: c } = u;
389
- if (!c)
390
- throw console.error("Iframe does not contain a window", u), new Error("Iframe does not contain a window!");
391
- const r = [];
392
- r.push(ie(c, n.appCode)), r.push(...ce(u, n.height, n.maxHeight)), r.push(ae());
393
- const o = new B(window, p);
394
- let i = S(c, null, l);
395
- u.addEventListener("load", () => D(c, w, l));
396
- const E = new Y(window, c);
397
- return {
398
- updateParams: (d) => {
399
- w = d, D(c, w, l);
400
- },
401
- updateListeners: (d) => o.updateListeners(d),
402
- updateProvider: (d) => {
403
- l = d, i = S(c, i, d);
404
- },
405
- destroy: () => {
406
- i.disconnect(), o.stopListeningIframe(), r.forEach((d) => window.removeEventListener("message", d)), E.stopListening(), e.removeChild(u);
407
- }
408
- };
409
- }
410
- function S(e, t, n) {
411
- t && t.disconnect();
412
- const s = t || new G(e);
413
- return n && s.onConnect(n), s;
414
- }
415
- function oe(e) {
416
- const { width: t = se, height: n = U } = e, s = document.createElement("iframe");
417
- return s.src = Z(e), s.width = t, s.height = n, s.style.border = "0", s.allow = "clipboard-read; clipboard-write", s;
418
- }
419
- function D(e, t, n) {
420
- const s = !!n, p = V(t), l = F(t).toString(), { theme: w, ...u } = t;
421
- v.postMessageToWindow(e, M.UPDATE_PARAMS, {
422
- urlParams: {
423
- pathname: p,
424
- search: l
425
- },
426
- appParams: u,
427
- hasProvider: s
428
- });
429
- }
430
- function ie(e, t) {
431
- return v.listenToMessageFromWindow(window, I.ACTIVATE, () => {
432
- v.postMessageToWindow(e, M.UPDATE_APP_DATA, {
433
- metaData: t ? {
434
- appCode: t
435
- } : void 0
436
- });
437
- });
438
- }
439
- function ae() {
440
- return v.listenToMessageFromWindow(window, I.INTERCEPT_WINDOW_OPEN, ({ href: e, rel: t, target: n }) => {
441
- const s = e.toString();
442
- if (!s.startsWith("http") && s.match(/^[a-zA-Z0-9]+:\/\//)) {
443
- window.open(s, n, t);
444
- return;
445
- }
446
- });
447
- }
448
- function ce(e, t = U, n) {
449
- return [
450
- v.listenToMessageFromWindow(window, I.UPDATE_HEIGHT, (s) => {
451
- const p = s.height ? s.height + re : void 0;
452
- e.style.height = p ? `${n ? Math.min(p, n) : p}px` : t;
453
- }),
454
- v.listenToMessageFromWindow(window, I.SET_FULL_HEIGHT, ({ isUpToSmall: s }) => {
455
- e.style.height = s ? t : `${n || document.body.offsetHeight}px`;
456
- })
457
- ];
458
- }
459
- const ue = Object.values(N);
460
- function he(e, t, n) {
461
- if (O(e)) {
462
- const s = e[n];
463
- return W(s) ? s[t] : s;
464
- }
465
- if (W(e)) {
466
- const s = e[t];
467
- return O(s) ? s[n] : s;
468
- }
469
- return e;
470
- }
471
- function O(e) {
472
- return typeof e != "object" ? !1 : Object.keys(e).every((t) => ue.includes(t));
473
- }
474
- const de = /^\d+$/;
475
- function W(e) {
476
- return typeof e != "object" ? !1 : Object.keys(e).every((t) => de.test(t));
477
- }
478
- function le(e) {
479
- return O(e) ? Object.values(e).flatMap((t) => W(t) ? Object.values(t) : [
480
- t
481
- ]) : W(e) ? Object.values(e).flatMap((t) => O(t) ? Object.values(t) : [
482
- t
483
- ]) : [
484
- e
485
- ];
486
- }
487
- export {
488
- N as TradeType,
489
- fe as WIDGET_PALETTE_COLORS,
490
- I as WidgetMethodsEmit,
491
- M as WidgetMethodsListen,
492
- pe as createCowSwapWidget,
493
- X as isCowSwapWidgetPalette,
494
- W as isPerNetworkConfig,
495
- O as isPerTradeTypeConfig,
496
- he as resolveFlexibleConfig,
497
- le as resolveFlexibleConfigValues,
498
- v as widgetIframeTransport
499
- };
@@ -1,2 +0,0 @@
1
- import { FlexibleConfig } from './types';
2
- export declare function resolveFlexibleConfigValues<T>(config: FlexibleConfig<T>): T[];
package/themeUtils.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { CowSwapTheme, CowSwapWidgetPalette } from './types';
2
- export declare function isCowSwapWidgetPalette(palette: CowSwapTheme | CowSwapWidgetPalette | undefined): palette is CowSwapWidgetPalette;