@cowprotocol/widget-react 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,7 +10,6 @@ Install dependency
10
10
  yarn add @cowprotocol/widget-react
11
11
  ```
12
12
 
13
-
14
13
  ```bash
15
14
  npm install @cowprotocol/widget-react
16
15
  ```
@@ -25,7 +24,7 @@ Prepare the config for the widget:
25
24
 
26
25
  ```ts
27
26
  const cowSwapWidgetParams: CowSwapWidgetParams = {
28
- appKey: '<YOUR_APP_KEY>',
27
+ appCode: '<YOUR_APP_KEY>',
29
28
  width: '600px',
30
29
  height: '700px',
31
30
  tradeType: 'swap',
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("react/jsx-runtime"),i=require("react"),l="2.0",g="*",m=["connect","disconnect","close","chainChanged","accountsChanged"];class w{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(n=>{this.processRequest(this.requests[n])}),this.requests={},m.forEach(n=>{e.on(n,s=>{this.postMessage({method:n,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})})}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:l,...e},g)}}function E(){return`https://swap-dev-git-${location.hostname.replace("widget-configurator-git-","").replace("-cowswap.vercel.app","")}-cowswap.vercel.app`}const v={local:"http://localhost:3000",prod:"https://swap.cow.fi",dev:"https://dev.swap.cow.fi",pr:E()};var d=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(d||{});function y(t){const e=v[t.env||"prod"],n=u(t),s=h(t);return e+"/#"+n+"?"+s}function u(t){const{chainId:e=1,tradeAssets:n,tradeType:s=d.SWAP}=t,o=n?[n.sell.asset,n.buy.asset].map(encodeURIComponent).join("/"):"";return`/${e}/widget/${s}/${o}`}function h(t){const{tradeAssets:e,theme:n}=t,s=new URLSearchParams;if(e){const{sell:o,buy:r}=e;o.amount&&s.append("sellAmount",o.amount),r.amount&&s.append("buyAmount",r.amount)}return n&&s.append("theme",n),s}const c="cowSwapWidget",p="640px",R="450px";function W(t,e={}){const{provider:n}=e,s=P(e);t.innerHTML="",t.appendChild(s);const{contentWindow:o}=s;if(!o)throw new Error("Iframe does not contain a window!");return A(o,e.appKey),S(s,e.height),n&&new w(o).onConnect(n),s.addEventListener("load",()=>{a(e,o)}),r=>a(r,o)}function P(t){const{width:e=R,height:n=p}=t,s=document.createElement("iframe");return s.src=y(t),s.width=e,s.height=n,s.style.border="0",s}function a(t,e){const n=u(t),s=h(t).toString();e.postMessage({key:c,method:"update",urlParams:{pathname:n,search:s},appParams:{...t,provider:void 0}},"*")}function A(t,e){window.addEventListener("message",n=>{n.data.key!==c||n.data.method!=="activate"||t.postMessage({key:c,method:"metaData",metaData:e?{appKey:e}:void 0},"*")})}function S(t,e=p){window.addEventListener("message",n=>{n.data.key!==c||n.data.method!=="iframeHeight"||(t.style.height=n.data.height?`${n.data.height}px`:e)})}function C({params:t,provider:e}){const n=i.useRef(),s=i.useRef(null),o=i.useRef(null);return i.useEffect(()=>{s.current&&(e&&n.current!==e&&(o.current=null),o.current?o.current(t):o.current=W(s.current,t))},[e,t]),i.useEffect(()=>{n.current=e},[e]),f.jsx("div",{ref:s})}exports.CowSwapWidget=C;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react/jsx-runtime"),r=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 d=(t=>(t.SWAP="swap",t.LIMIT="limit",t.ADVANCED="advanced",t))(d||{});function v(t){const e=R[t.env||"prod"],s=u(t),n=h(t);return e+"/#"+s+"?"+n}function u(t){const{chainId:e=1,sell:s,buy:n,tradeType:o=d.SWAP}=t,i=[];s?.asset&&i.push(s.asset),n?.asset&&i.push(n.asset);const f=i.map(encodeURIComponent).join("/");return`/${e}/widget/${o}/${f}`}function h(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 a="cowSwapWidget",p="640px",W="450px",P=20;function S(t,e={}){const{provider:s}=e,n=C(e);t.innerHTML="",t.appendChild(n);const{contentWindow:o}=n;if(!o)throw new Error("Iframe does not contain a window!");return b(o,e.appCode),A(n,e.height),s&&new E(o).onConnect(s),n.addEventListener("load",()=>{c(e,o)}),i=>c(i,o)}function C(t){const{width:e=W,height:s=p}=t,n=document.createElement("iframe");return n.src=v(t),n.width=e,n.height=s,n.style.border="0",n}function c(t,e){const s=u(t),n=h(t).toString();e.postMessage({key:a,method:"update",urlParams:{pathname:s,search:n},appParams:{...t,provider:void 0}},"*")}function b(t,e){window.addEventListener("message",s=>{s.data.key!==a||s.data.method!=="activate"||t.postMessage({key:a,method:"metaData",metaData:e?{appCode:e}:void 0},"*")})}function A(t,e=p){window.addEventListener("message",s=>{s.data.key!==a||s.data.method!=="iframeHeight"||(t.style.height=s.data.height?`${s.data.height+P}px`:e)})}function M({params:t,provider:e}){const s=r.useRef(),n=r.useRef(null),o=r.useRef(null);return r.useEffect(()=>{n.current&&(e&&s.current!==e&&(o.current=null),o.current?o.current(t):o.current=S(n.current,t))},[e,t]),r.useEffect(()=>{s.current=e},[e]),l.jsx("div",{ref:n})}exports.CowSwapWidget=M;
package/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
- import { jsx as l } from "react/jsx-runtime";
1
+ import { jsx as w } from "react/jsx-runtime";
2
2
  import { useRef as a, useEffect as c } from "react";
3
- const m = "2.0", g = "*", w = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
4
- class E {
3
+ const m = "2.0", g = "*", E = ["connect", "disconnect", "close", "chainChanged", "accountsChanged"];
4
+ class v {
5
5
  /**
6
6
  * Creates an instance of JsonRpcManager.
7
7
  * @param contentWindow - The window for handling events.
@@ -27,11 +27,11 @@ class E {
27
27
  * @param ethereumProvider - The Ethereum provider to connect.
28
28
  */
29
29
  onConnect(e) {
30
- this.ethereumProvider = e, Object.keys(this.requests).forEach((n) => {
31
- this.processRequest(this.requests[n]);
32
- }), this.requests = {}, w.forEach((n) => {
33
- e.on(n, (s) => {
34
- this.postMessage({ method: n, params: [s] });
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] });
35
35
  });
36
36
  });
37
37
  }
@@ -42,10 +42,10 @@ class E {
42
42
  processRequest(e) {
43
43
  if (!this.ethereumProvider)
44
44
  return;
45
- (e.method === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request(e)).then((s) => {
46
- this.postMessage({ id: e.id, result: s });
47
- }).catch((s) => {
48
- this.postMessage({ id: e.id, error: s });
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 });
49
49
  });
50
50
  }
51
51
  processEvent = (e) => {
@@ -61,56 +61,54 @@ class E {
61
61
  );
62
62
  }
63
63
  }
64
- function v() {
65
- return `https://swap-dev-git-${location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
64
+ function y() {
65
+ return typeof window > "u" || !window ? "" : `https://swap-dev-git-${window.location.hostname.replace("widget-configurator-git-", "").replace("-cowswap.vercel.app", "")}-cowswap.vercel.app`;
66
66
  }
67
67
  const W = {
68
68
  local: "http://localhost:3000",
69
69
  prod: "https://swap.cow.fi",
70
70
  dev: "https://dev.swap.cow.fi",
71
- pr: v()
71
+ pr: y()
72
72
  };
73
73
  var u = /* @__PURE__ */ ((t) => (t.SWAP = "swap", t.LIMIT = "limit", t.ADVANCED = "advanced", t))(u || {});
74
- function y(t) {
75
- const e = W[t.env || "prod"], n = h(t), s = p(t);
76
- return e + "/#" + n + "?" + s;
74
+ function P(t) {
75
+ const e = W[t.env || "prod"], s = h(t), n = p(t);
76
+ return e + "/#" + s + "?" + n;
77
77
  }
78
78
  function h(t) {
79
- const { chainId: e = 1, tradeAssets: n, tradeType: s = u.SWAP } = t, o = n ? [n.sell.asset, n.buy.asset].map(encodeURIComponent).join("/") : "";
80
- return `/${e}/widget/${s}/${o}`;
79
+ const { chainId: e = 1, sell: s, buy: n, tradeType: o = u.SWAP } = t, i = [];
80
+ s?.asset && i.push(s.asset), n?.asset && i.push(n.asset);
81
+ const l = i.map(encodeURIComponent).join("/");
82
+ return `/${e}/widget/${o}/${l}`;
81
83
  }
82
84
  function p(t) {
83
- const { tradeAssets: e, theme: n } = t, s = new URLSearchParams();
84
- if (e) {
85
- const { sell: o, buy: r } = e;
86
- o.amount && s.append("sellAmount", o.amount), r.amount && s.append("buyAmount", r.amount);
87
- }
88
- return n && s.append("theme", n), s;
85
+ const { sell: e, buy: s, theme: n } = t, o = new URLSearchParams();
86
+ 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;
89
87
  }
90
- const i = "cowSwapWidget", f = "640px", P = "450px";
91
- function R(t, e = {}) {
92
- const { provider: n } = e, s = A(e);
93
- t.innerHTML = "", t.appendChild(s);
94
- const { contentWindow: o } = s;
88
+ const r = "cowSwapWidget", f = "640px", R = "450px", A = 20;
89
+ function C(t, e = {}) {
90
+ const { provider: s } = e, n = S(e);
91
+ t.innerHTML = "", t.appendChild(n);
92
+ const { contentWindow: o } = n;
95
93
  if (!o)
96
94
  throw new Error("Iframe does not contain a window!");
97
- return C(o, e.appKey), M(s, e.height), n && new E(o).onConnect(n), s.addEventListener("load", () => {
95
+ return I(o, e.appCode), L(n, e.height), s && new v(o).onConnect(s), n.addEventListener("load", () => {
98
96
  d(e, o);
99
- }), (r) => d(r, o);
97
+ }), (i) => d(i, o);
100
98
  }
101
- function A(t) {
102
- const { width: e = P, height: n = f } = t, s = document.createElement("iframe");
103
- return s.src = y(t), s.width = e, s.height = n, s.style.border = "0", s;
99
+ function S(t) {
100
+ const { width: e = R, height: s = f } = t, n = document.createElement("iframe");
101
+ return n.src = P(t), n.width = e, n.height = s, n.style.border = "0", n;
104
102
  }
105
103
  function d(t, e) {
106
- const n = h(t), s = p(t).toString();
104
+ const s = h(t), n = p(t).toString();
107
105
  e.postMessage(
108
106
  {
109
- key: i,
107
+ key: r,
110
108
  method: "update",
111
109
  urlParams: {
112
- pathname: n,
113
- search: s
110
+ pathname: s,
111
+ search: n
114
112
  },
115
113
  appParams: {
116
114
  ...t,
@@ -120,31 +118,31 @@ function d(t, e) {
120
118
  "*"
121
119
  );
122
120
  }
123
- function C(t, e) {
124
- window.addEventListener("message", (n) => {
125
- n.data.key !== i || n.data.method !== "activate" || t.postMessage(
121
+ function I(t, e) {
122
+ window.addEventListener("message", (s) => {
123
+ s.data.key !== r || s.data.method !== "activate" || t.postMessage(
126
124
  {
127
- key: i,
125
+ key: r,
128
126
  method: "metaData",
129
- metaData: e ? { appKey: e } : void 0
127
+ metaData: e ? { appCode: e } : void 0
130
128
  },
131
129
  "*"
132
130
  );
133
131
  });
134
132
  }
135
- function M(t, e = f) {
136
- window.addEventListener("message", (n) => {
137
- n.data.key !== i || n.data.method !== "iframeHeight" || (t.style.height = n.data.height ? `${n.data.height}px` : e);
133
+ function L(t, e = f) {
134
+ window.addEventListener("message", (s) => {
135
+ s.data.key !== r || s.data.method !== "iframeHeight" || (t.style.height = s.data.height ? `${s.data.height + A}px` : e);
138
136
  });
139
137
  }
140
- function L({ params: t, provider: e }) {
141
- const n = a(), s = a(null), o = a(null);
138
+ function b({ params: t, provider: e }) {
139
+ const s = a(), n = a(null), o = a(null);
142
140
  return c(() => {
143
- s.current && (e && n.current !== e && (o.current = null), o.current ? o.current(t) : o.current = R(s.current, t));
141
+ n.current && (e && s.current !== e && (o.current = null), o.current ? o.current(t) : o.current = C(n.current, t));
144
142
  }, [e, t]), c(() => {
145
- n.current = e;
146
- }, [e]), /* @__PURE__ */ l("div", { ref: s });
143
+ s.current = e;
144
+ }, [e]), /* @__PURE__ */ w("div", { ref: n });
147
145
  }
148
146
  export {
149
- L as CowSwapWidget
147
+ b as CowSwapWidget
150
148
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/widget-react",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {