@cowprotocol/widget-react 0.2.2 → 0.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/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export * from './lib/CowSwapWidget';
2
- export type * from '@cowprotocol/widget-lib';
2
+ export * from '@cowprotocol/widget-lib';
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react/jsx-runtime"),i=require("react"),w="2.0",g="*",m=["connect","disconnect","close","chainChanged","accountsChanged"];class E{constructor(e){this.contentWindow=e,window.addEventListener("message",this.processEvent)}ethereumProvider=null;requests={};disconnect(){this.ethereumProvider=null,window.removeEventListener("message",this.processEvent)}onConnect(e){this.ethereumProvider=e,Object.keys(this.requests).forEach(s=>{this.processRequest(this.requests[s])}),this.requests={},m.forEach(s=>{e.on(s,n=>{this.postMessage({method:s,params:[n]})})})}processRequest(e){if(!this.ethereumProvider)return;(e.method==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request(e)).then(n=>{this.postMessage({id:e.id,result:n})}).catch(n=>{this.postMessage({id:e.id,error:n})})}processEvent=e=>{e.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(e.data):this.requests[e.data.id]=e.data)};postMessage(e){this.contentWindow.postMessage({jsonrpc:w,...e},g)}}function y(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const R={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:y()};var u=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(u||{});const a="_";function v(t){const e=R[t.env||"prod"],s=h(t),n=p(t);return e+"/#"+s+"?"+n}function h(t){const{chainId:e=1,sell:s,buy:n,tradeType:o=u.SWAP}=t,r=[s?.asset||a,n?.asset||a].map(encodeURIComponent).join("/");return`/${e}/widget/${o}/${r}`}function p(t){const{sell:e,buy:s,theme:n}=t,o=new URLSearchParams;return e?.amount&&o.append("sellAmount",e.amount),s?.amount&&o.append("buyAmount",s.amount),n&&o.append("theme",typeof n=="string"?n:n.baseTheme),o}const c="cowSwapWidget",f="640px",P="450px",W=20;function S(t,e={}){const{provider:s}=e,n=T(e);t.innerHTML="",t.appendChild(n);const{contentWindow:o}=n;if(!o)throw new Error("Iframe does not contain a window!");return C(o,e.appCode),M(n,e.height),s&&new E(o).onConnect(s),n.addEventListener("load",()=>{d(e,o)}),r=>d(r,o)}function T(t){const{width:e=P,height:s=f}=t,n=document.createElement("iframe");return n.src=v(t),n.width=e,n.height=s,n.style.border="0",n}function d(t,e){const s=h(t),n=p(t).toString();e.postMessage({key:c,method:"update",urlParams:{pathname:s,search:n},appParams:{...t,provider:void 0}},"*")}function C(t,e){window.addEventListener("message",s=>{s.data.key!==c||s.data.method!=="activate"||t.postMessage({key:c,method:"metaData",metaData:e?{appCode:e}:void 0},"*")})}function M(t,e=f){window.addEventListener("message",s=>{s.data.key!==c||s.data.method!=="iframeHeight"||(t.style.height=s.data.height?`${s.data.height+W}px`:e)})}function b({params:t,provider:e}){const s=i.useRef(),n=i.useRef(null),o=i.useRef(null);return i.useEffect(()=>{n.current&&(e&&s.current!==e&&(o.current=null),o.current?o.current(t):o.current=S(n.current,t))},[e,t]),i.useEffect(()=>{s.current=e},[e]),l.jsx("div",{ref:n})}exports.CowSwapWidget=b;
1
+ "use strict";var l=Object.defineProperty;var E=(n,e,t)=>e in n?l(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var c=(n,e,t)=>(E(n,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const W=require("react/jsx-runtime"),i=require("react"),R="2.0",P="*",v=["connect","disconnect","close","chainChanged","accountsChanged"];class S{constructor(e){c(this,"ethereumProvider",null);c(this,"requests",{});c(this,"processEvent",e=>{e.data.jsonrpc==="2.0"&&(this.ethereumProvider?this.processRequest(e.data):this.requests[e.data.id]=e.data)});this.contentWindow=e,window.addEventListener("message",this.processEvent)}disconnect(){this.ethereumProvider=null,window.removeEventListener("message",this.processEvent)}onConnect(e){this.ethereumProvider=e,Object.keys(this.requests).forEach(t=>{this.processRequest(this.requests[t])}),this.requests={},v.forEach(t=>{e.on(t,s=>{this.postMessage({method:t,params:[s]})})})}processRequest(e){if(!this.ethereumProvider)return;(e.method==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request(e)).then(s=>{this.postMessage({id:e.id,result:s})}).catch(s=>{this.postMessage({id:e.id,error:s})})}postMessage(e){this.contentWindow.postMessage({jsonrpc:R,...e},P)}}function y(){return typeof window>"u"||!window?"":`https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const p={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:y()};var d=(n=>(n.SWAP="swap",n.LIMIT="limit",n.ADVANCED="advanced",n))(d||{});const u="_";function T(n){const e=p[n.env||"prod"],t=f(n),s=w(n);return e+"/#"+t+"?"+s}function f(n){const{chainId:e=1,sell:t,buy:s,tradeType:o=d.SWAP}=n,r=[(t==null?void 0:t.asset)||u,(s==null?void 0:s.asset)||u].map(encodeURIComponent).join("/");return`/${e}/widget/${o}/${r}`}function w(n){const{sell:e,buy:t,theme:s}=n,o=new URLSearchParams;return e!=null&&e.amount&&o.append("sellAmount",e.amount),t!=null&&t.amount&&o.append("buyAmount",t.amount),s&&o.append("theme",typeof s=="string"?s:s.baseTheme),o}const a="cowSwapWidget",g="640px",C="450px",A=20;function m(n,e={}){const{provider:t}=e,s=M(e);n.innerHTML="",n.appendChild(s);const{contentWindow:o}=s;if(!o)throw new Error("Iframe does not contain a window!");return _(o,e.appCode),L(s,e.height),t&&new S(o).onConnect(t),s.addEventListener("load",()=>{h(e,o)}),r=>h(r,o)}function M(n){const{width:e=C,height:t=g}=n,s=document.createElement("iframe");return s.src=T(n),s.width=e,s.height=t,s.style.border="0",s}function h(n,e){const t=f(n),s=w(n).toString();e.postMessage({key:a,method:"update",urlParams:{pathname:t,search:s},appParams:{...n,provider:void 0}},"*")}function _(n,e){window.addEventListener("message",t=>{t.data.key!==a||t.data.method!=="activate"||n.postMessage({key:a,method:"metaData",metaData:e?{appCode:e}:void 0},"*")})}function L(n,e=g){window.addEventListener("message",t=>{t.data.key!==a||t.data.method!=="iframeHeight"||(n.style.height=t.data.height?`${t.data.height+A}px`:e)})}function q({params:n,provider:e}){const t=i.useRef(),s=i.useRef(null),o=i.useRef(null);return i.useEffect(()=>{s.current&&(e&&t.current!==e&&(o.current=null),o.current?o.current(n):o.current=m(s.current,n))},[e,n]),i.useEffect(()=>{t.current=e},[e]),W.jsx("div",{ref:s})}exports.COWSWAP_URLS=p;exports.CowSwapWidget=q;exports.TradeType=d;exports.cowSwapWidget=m;
package/index.mjs CHANGED
@@ -1,21 +1,27 @@
1
- import { jsx as w } from "react/jsx-runtime";
2
- import { useRef as c, useEffect as a } from "react";
3
- const m = "2.0", g = "*", E = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
4
- class v {
1
+ var g = Object.defineProperty;
2
+ var l = (n, e, t) => e in n ? g(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
+ var r = (n, e, t) => (l(n, typeof e != "symbol" ? e + "" : e, t), t);
4
+ import { jsx as E } from "react/jsx-runtime";
5
+ import { useRef as a, useEffect as d } from "react";
6
+ const v = "2.0", P = "*", W = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
7
+ class R {
5
8
  /**
6
9
  * Creates an instance of JsonRpcManager.
7
10
  * @param contentWindow - The window for handling events.
8
11
  */
9
12
  constructor(e) {
13
+ /**
14
+ * The Ethereum provider instance.
15
+ * When is null the JSON-RPC manager is disconnected from the Ethereum provider.
16
+ * */
17
+ r(this, "ethereumProvider", null);
18
+ /** Stored JSON-RPC requests. */
19
+ r(this, "requests", {});
20
+ r(this, "processEvent", (e) => {
21
+ e.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(e.data) : this.requests[e.data.id] = e.data);
22
+ });
10
23
  this.contentWindow = e, window.addEventListener("message", this.processEvent);
11
24
  }
12
- /**
13
- * The Ethereum provider instance.
14
- * When is null the JSON-RPC manager is disconnected from the Ethereum provider.
15
- * */
16
- ethereumProvider = null;
17
- /** Stored JSON-RPC requests. */
18
- requests = {};
19
25
  /**
20
26
  * Disconnects the JSON-RPC manager from the Ethereum provider.
21
27
  */
@@ -27,11 +33,11 @@ class v {
27
33
  * @param ethereumProvider - The Ethereum provider to connect.
28
34
  */
29
35
  onConnect(e) {
30
- this.ethereumProvider = e, Object.keys(this.requests).forEach((s) => {
31
- this.processRequest(this.requests[s]);
32
- }), this.requests = {}, E.forEach((s) => {
33
- e.on(s, (n) => {
34
- this.postMessage({ method: s, params: [n] });
36
+ this.ethereumProvider = e, Object.keys(this.requests).forEach((t) => {
37
+ this.processRequest(this.requests[t]);
38
+ }), this.requests = {}, W.forEach((t) => {
39
+ e.on(t, (o) => {
40
+ this.postMessage({ method: t, params: [o] });
35
41
  });
36
42
  });
37
43
  }
@@ -42,86 +48,83 @@ class v {
42
48
  processRequest(e) {
43
49
  if (!this.ethereumProvider)
44
50
  return;
45
- (e.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(e)).then((n) => {
46
- this.postMessage({ id: e.id, result: n });
47
- }).catch((n) => {
48
- this.postMessage({ id: e.id, error: n });
51
+ (e.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(e)).then((o) => {
52
+ this.postMessage({ id: e.id, result: o });
53
+ }).catch((o) => {
54
+ this.postMessage({ id: e.id, error: o });
49
55
  });
50
56
  }
51
- processEvent = (e) => {
52
- e.data.jsonrpc === "2.0" && (this.ethereumProvider ? this.processRequest(e.data) : this.requests[e.data.id] = e.data);
53
- };
54
57
  postMessage(e) {
55
58
  this.contentWindow.postMessage(
56
59
  {
57
- jsonrpc: m,
60
+ jsonrpc: v,
58
61
  ...e
59
62
  },
60
- g
63
+ P
61
64
  );
62
65
  }
63
66
  }
64
67
  function y() {
65
68
  return typeof window > "u" || !window ? "" : `https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
66
69
  }
67
- const P = {
70
+ const T = {
68
71
  local: "http://localhost:3000",
69
72
  prod: "https://swap.cow.fi",
70
73
  dev: "https://dev.swap.cow.fi",
71
74
  pr: y()
72
75
  };
73
- var h = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(h || {});
74
- const d = "_";
75
- function W(t) {
76
- const e = P[t.env || "prod"], s = p(t), n = f(t);
77
- return e + "/#" + s + "?" + n;
76
+ var p = /* @__PURE__ */ ((n) => (n.SWAP = "swap", n.LIMIT = "limit", n.ADVANCED = "advanced", n))(p || {});
77
+ const h = "_";
78
+ function A(n) {
79
+ const e = T[n.env || "prod"], t = f(n), o = w(n);
80
+ return e + "/#" + t + "?" + o;
78
81
  }
79
- function p(t) {
80
- const { chainId: e = 1, sell: s, buy: n, tradeType: o = h.SWAP } = t, i = [s?.asset || d, n?.asset || d].map(encodeURIComponent).join("/");
81
- return `/${e}/widget/${o}/${i}`;
82
+ function f(n) {
83
+ const { chainId: e = 1, sell: t, buy: o, tradeType: s = p.SWAP } = n, i = [(t == null ? void 0 : t.asset) || h, (o == null ? void 0 : o.asset) || h].map(encodeURIComponent).join("/");
84
+ return `/${e}/widget/${s}/${i}`;
82
85
  }
83
- function f(t) {
84
- const { sell: e, buy: s, theme: n } = t, o = new URLSearchParams();
85
- return e?.amount && o.append("sellAmount", e.amount), s?.amount && o.append("buyAmount", s.amount), n && o.append("theme", typeof n == "string" ? n : n.baseTheme), o;
86
+ function w(n) {
87
+ const { sell: e, buy: t, theme: o } = n, s = new URLSearchParams();
88
+ return e != null && e.amount && s.append("sellAmount", e.amount), t != null && t.amount && s.append("buyAmount", t.amount), o && s.append("theme", typeof o == "string" ? o : o.baseTheme), s;
86
89
  }
87
- const r = "cowSwapWidget", l = "640px", R = "450px", T = 20;
88
- function A(t, e = {}) {
89
- const { provider: s } = e, n = C(e);
90
- t.innerHTML = "", t.appendChild(n);
91
- const { contentWindow: o } = n;
92
- if (!o)
90
+ const c = "cowSwapWidget", m = "640px", C = "450px", M = 20;
91
+ function S(n, e = {}) {
92
+ const { provider: t } = e, o = _(e);
93
+ n.innerHTML = "", n.appendChild(o);
94
+ const { contentWindow: s } = o;
95
+ if (!s)
93
96
  throw new Error("Iframe does not contain a window!");
94
- return M(o, e.appCode), S(n, e.height), s && new v(o).onConnect(s), n.addEventListener("load", () => {
95
- u(e, o);
96
- }), (i) => u(i, o);
97
+ return I(s, e.appCode), L(o, e.height), t && new R(s).onConnect(t), o.addEventListener("load", () => {
98
+ u(e, s);
99
+ }), (i) => u(i, s);
97
100
  }
98
- function C(t) {
99
- const { width: e = R, height: s = l } = t, n = document.createElement("iframe");
100
- return n.src = W(t), n.width = e, n.height = s, n.style.border = "0", n;
101
+ function _(n) {
102
+ const { width: e = C, height: t = m } = n, o = document.createElement("iframe");
103
+ return o.src = A(n), o.width = e, o.height = t, o.style.border = "0", o;
101
104
  }
102
- function u(t, e) {
103
- const s = p(t), n = f(t).toString();
105
+ function u(n, e) {
106
+ const t = f(n), o = w(n).toString();
104
107
  e.postMessage(
105
108
  {
106
- key: r,
109
+ key: c,
107
110
  method: "update",
108
111
  urlParams: {
109
- pathname: s,
110
- search: n
112
+ pathname: t,
113
+ search: o
111
114
  },
112
115
  appParams: {
113
- ...t,
116
+ ...n,
114
117
  provider: void 0
115
118
  }
116
119
  },
117
120
  "*"
118
121
  );
119
122
  }
120
- function M(t, e) {
121
- window.addEventListener("message", (s) => {
122
- s.data.key !== r || s.data.method !== "activate" || t.postMessage(
123
+ function I(n, e) {
124
+ window.addEventListener("message", (t) => {
125
+ t.data.key !== c || t.data.method !== "activate" || n.postMessage(
123
126
  {
124
- key: r,
127
+ key: c,
125
128
  method: "metaData",
126
129
  metaData: e ? { appCode: e } : void 0
127
130
  },
@@ -129,19 +132,22 @@ function M(t, e) {
129
132
  );
130
133
  });
131
134
  }
132
- function S(t, e = l) {
133
- window.addEventListener("message", (s) => {
134
- s.data.key !== r || s.data.method !== "iframeHeight" || (t.style.height = s.data.height ? `${s.data.height + T}px` : e);
135
+ function L(n, e = m) {
136
+ window.addEventListener("message", (t) => {
137
+ t.data.key !== c || t.data.method !== "iframeHeight" || (n.style.height = t.data.height ? `${t.data.height + M}px` : e);
135
138
  });
136
139
  }
137
- function L({ params: t, provider: e }) {
138
- const s = c(), n = c(null), o = c(null);
139
- return a(() => {
140
- n.current && (e && s.current !== e && (o.current = null), o.current ? o.current(t) : o.current = A(n.current, t));
141
- }, [e, t]), a(() => {
142
- s.current = e;
143
- }, [e]), /* @__PURE__ */ w("div", { ref: n });
140
+ function b({ params: n, provider: e }) {
141
+ const t = a(), o = a(null), s = a(null);
142
+ return d(() => {
143
+ o.current && (e && t.current !== e && (s.current = null), s.current ? s.current(n) : s.current = S(o.current, n));
144
+ }, [e, n]), d(() => {
145
+ t.current = e;
146
+ }, [e]), /* @__PURE__ */ E("div", { ref: o });
144
147
  }
145
148
  export {
146
- L as CowSwapWidget
149
+ T as COWSWAP_URLS,
150
+ b as CowSwapWidget,
151
+ p as TradeType,
152
+ S as cowSwapWidget
147
153
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-react",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "type": "commonjs",
5
5
  "description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your React application.",
6
6
  "main": "index.js",
@@ -22,6 +22,6 @@
22
22
  "react"
23
23
  ],
24
24
  "dependencies": {
25
- "@cowprotocol/widget-lib": "^0.2.1"
25
+ "@cowprotocol/widget-lib": "^0.3.0"
26
26
  }
27
27
  }