@meshconnect/web-link-sdk 2.0.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/Link.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { LinkOptions, Link } from './utils/types';
2
+ export declare const createLink: (options: LinkOptions) => Link;
package/Link.js ADDED
@@ -0,0 +1,144 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { addPopup, iframeId, removePopup } from './utils/popup';
38
+ import { isLinkEventTypeKey } from './utils/event-types';
39
+ var currentOptions;
40
+ var iframeUrlObject;
41
+ var iframeElement = function () {
42
+ return document.getElementById(iframeId);
43
+ };
44
+ function eventsListener(event) {
45
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
46
+ switch (event.data.type) {
47
+ case 'brokerageAccountAccessToken': {
48
+ var payload = {
49
+ accessToken: event.data.payload
50
+ };
51
+ (_a = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _a === void 0 ? void 0 : _a.call(currentOptions, {
52
+ type: 'integrationConnected',
53
+ payload: payload
54
+ });
55
+ (_b = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onIntegrationConnected) === null || _b === void 0 ? void 0 : _b.call(currentOptions, payload);
56
+ break;
57
+ }
58
+ case 'delayedAuthentication': {
59
+ var payload = {
60
+ delayedAuth: event.data.payload
61
+ };
62
+ (_c = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _c === void 0 ? void 0 : _c.call(currentOptions, {
63
+ type: 'integrationConnected',
64
+ payload: payload
65
+ });
66
+ (_d = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onIntegrationConnected) === null || _d === void 0 ? void 0 : _d.call(currentOptions, payload);
67
+ break;
68
+ }
69
+ case 'transferFinished': {
70
+ var payload = event.data.payload;
71
+ (_e = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _e === void 0 ? void 0 : _e.call(currentOptions, {
72
+ type: 'transferCompleted',
73
+ payload: payload
74
+ });
75
+ (_f = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onTransferFinished) === null || _f === void 0 ? void 0 : _f.call(currentOptions, payload);
76
+ break;
77
+ }
78
+ case 'close':
79
+ case 'done': {
80
+ (_g = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onExit) === null || _g === void 0 ? void 0 : _g.call(currentOptions, event.data.message);
81
+ removePopup();
82
+ break;
83
+ }
84
+ case 'oauthLinkOpen': {
85
+ if (event.data.link) {
86
+ var w = 700;
87
+ var h = 800;
88
+ var left = screen.width / 2 - w / 2;
89
+ var top_1 = screen.height / 2 - h / 2;
90
+ (_h = window
91
+ .open(event.data.link, '_blank', "popup,noopener,noreferrer,resizable,scrollbars,width=".concat(w, ",height=").concat(h, ",top=").concat(top_1, ",left=").concat(left))) === null || _h === void 0 ? void 0 : _h.focus();
92
+ }
93
+ break;
94
+ }
95
+ case 'loaded': {
96
+ if (currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.accessTokens) {
97
+ (_j = iframeElement().contentWindow) === null || _j === void 0 ? void 0 : _j.postMessage({ type: 'frontAccessTokens', payload: currentOptions.accessTokens }, (iframeUrlObject === null || iframeUrlObject === void 0 ? void 0 : iframeUrlObject.origin) || 'https://web.meshconnect.com');
98
+ }
99
+ if (currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.transferDestinationTokens) {
100
+ (_k = iframeElement().contentWindow) === null || _k === void 0 ? void 0 : _k.postMessage({
101
+ type: 'frontTransferDestinationTokens',
102
+ payload: currentOptions.transferDestinationTokens
103
+ }, (iframeUrlObject === null || iframeUrlObject === void 0 ? void 0 : iframeUrlObject.origin) || 'https://web.meshconnect.com');
104
+ }
105
+ (_l = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _l === void 0 ? void 0 : _l.call(currentOptions, { type: 'pageLoaded' });
106
+ break;
107
+ }
108
+ default: {
109
+ if (isLinkEventTypeKey(event.data.type)) {
110
+ (_m = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _m === void 0 ? void 0 : _m.call(currentOptions, event.data);
111
+ }
112
+ break;
113
+ }
114
+ }
115
+ }
116
+ export var createLink = function (options) {
117
+ var openLink = function (linkToken) { return __awaiter(void 0, void 0, void 0, function () {
118
+ var linkUrl;
119
+ var _a;
120
+ return __generator(this, function (_b) {
121
+ if (!linkToken) {
122
+ (_a = options === null || options === void 0 ? void 0 : options.onExit) === null || _a === void 0 ? void 0 : _a.call(options, 'Invalid link token!');
123
+ return [2 /*return*/];
124
+ }
125
+ currentOptions = options;
126
+ linkUrl = window.atob(linkToken);
127
+ iframeUrlObject = new URL(linkUrl);
128
+ window.removeEventListener('message', eventsListener);
129
+ addPopup(linkUrl);
130
+ window.addEventListener('message', eventsListener);
131
+ return [2 /*return*/];
132
+ });
133
+ }); };
134
+ var closeLink = function () {
135
+ var _a;
136
+ removePopup();
137
+ window.removeEventListener('message', eventsListener);
138
+ (_a = options.onExit) === null || _a === void 0 ? void 0 : _a.call(options);
139
+ };
140
+ return {
141
+ openLink: openLink,
142
+ closeLink: closeLink
143
+ };
144
+ };
package/README.md ADDED
@@ -0,0 +1,127 @@
1
+ # @meshconnect/web-link-sdk
2
+
3
+ A client-side JS library for integrating with Mesh Connect
4
+
5
+ ### Install
6
+
7
+ With `npm`:
8
+
9
+ ```
10
+ npm install --save @meshconnect/web-link-sdk
11
+ ```
12
+
13
+ With `yarn`
14
+
15
+ ```
16
+ yarn add @meshconnect/web-link-sdk
17
+ ```
18
+
19
+ ### Getting Link token
20
+
21
+ Link token should be obtained from the GET `/api/v1/linktoken` endpoint. Api reference for this request is available [here](https://integration-api.getfront.com/apireference#tag/Managed-Account-Authentication/paths/~1api~1v1~1linktoken/post). Request must be preformed from the server side because it requires the client secret. You will get the response in the following format:
22
+
23
+ ```json
24
+ {
25
+ "content": {
26
+ "linkToken": "{linktoken}"
27
+ },
28
+ },
29
+ "status": "ok",
30
+ "message": ""
31
+ }
32
+ ```
33
+
34
+ You can use `linkToken` value from this response to open the popup window with `openLink` method.
35
+
36
+ ### Generating connection method
37
+
38
+ ```tsx
39
+ import { createLink } from '@meshconnect/web-link-sdk';
40
+
41
+ // ...
42
+
43
+ const linkConnection = createLink({
44
+ clientId: '<Your Mesh Connect Client Id>',
45
+ onIntegrationConnected: (data: LinkPayload) => {
46
+ // use broker account data
47
+ },
48
+ onExit: (error?: string) => {
49
+ if (error) {
50
+ // handle error
51
+ } else {
52
+ // ...
53
+ }
54
+ }
55
+
56
+ ```
57
+
58
+ ### Using connection to open auth link
59
+
60
+ To open authentication link provided by Front Finance Integration API you need to call `openLink` method:
61
+
62
+ ```tsx
63
+ linkConnection.openLink(linkToken)
64
+ ```
65
+
66
+ ℹ️ See full source code example at [react-example/src/ui/Link.tsx](../../examples/react-example/src/ui/Link.tsx)
67
+
68
+ ```tsx
69
+ import {
70
+ createLink,
71
+ Link,
72
+ LinkPayload
73
+ } from '@meshconnect/web-link-sdk'
74
+
75
+ // ...
76
+
77
+ const [linkConnection, setLinkConnection] = useState<Link | null>(
78
+ null
79
+ )
80
+
81
+ useEffect(() => {
82
+ setLinkConnection(createLink(options))
83
+ }, [])
84
+
85
+ useEffect(() => {
86
+ if (authLink) {
87
+ linkConnection?.openLink(linkToken)
88
+ }
89
+ }, [linkConnection, authLink])
90
+
91
+ return <></>
92
+ ```
93
+
94
+ ### Getting tokens
95
+
96
+ After successfull authentication on the Link session, the popup will be closed and the broker tokens will be passed to the `onIntegrationConnected` function.
97
+ `Link` instance will check if URL contains query parameters, load broker tokens and fire the events.
98
+
99
+ ### Available Connection configuration options
100
+
101
+ ℹ️ See [src/types/index.ts](src/utils/types.ts) for exported types.
102
+
103
+ #### `createLink` arguments
104
+
105
+ | key | type | description |
106
+ |--------------------------|---------------------------------------------------------| ------------------------------------------------------------------------------------ |
107
+ | `clientId` | `string` | Keys from https://dashboard.getfront.com/company/keys page |
108
+ | `onIntegrationConnected` | `(payload: LinkPayload) => void` | Callback called when users connects their accounts |
109
+ | `onExit` | `((error?: string \| undefined) => void) \| undefined` | Called if connection not happened |
110
+ | `onTransferFinished` | `(payload: TransferFinishedPayload) => void` | Callback called when a crypto transfer is executed |
111
+ | `onEvent` | `(payload: LinkEventType) => void` | A callback function that is called when various events occur within the Front iframe |
112
+ | `accessTokens` | `IntegrationAccessToken[]` | An array of integration access tokens |
113
+
114
+ #### `createLink` return value
115
+
116
+ | key | type | description |
117
+ | ------------ | -------------------------------------- | ------------------------------ |
118
+ | `openLink` | `(linkToken: string) => Promise<void>` | Opens the Link UI popup |
119
+ | `closeLink` | `() => Promise<void>` | Closes the Link UI popup |
120
+
121
+ ### Using tokens
122
+
123
+ You can use broker tokens to perform requests to get current balance, assets and execute transactions. Full API reference can be found [here](https://integration-api.meshconnect.com/apireference).
124
+
125
+ ## Typescript support
126
+
127
+ TypeScript definitions for `@meshconnect/web-link-sdk` are built into the npm package.
package/cjs/Link.js ADDED
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.createLink = void 0;
40
+ var popup_1 = require("./utils/popup");
41
+ var event_types_1 = require("./utils/event-types");
42
+ var currentOptions;
43
+ var iframeUrlObject;
44
+ var iframeElement = function () {
45
+ return document.getElementById(popup_1.iframeId);
46
+ };
47
+ function eventsListener(event) {
48
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
49
+ switch (event.data.type) {
50
+ case 'brokerageAccountAccessToken': {
51
+ var payload = {
52
+ accessToken: event.data.payload
53
+ };
54
+ (_a = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _a === void 0 ? void 0 : _a.call(currentOptions, {
55
+ type: 'integrationConnected',
56
+ payload: payload
57
+ });
58
+ (_b = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onIntegrationConnected) === null || _b === void 0 ? void 0 : _b.call(currentOptions, payload);
59
+ break;
60
+ }
61
+ case 'delayedAuthentication': {
62
+ var payload = {
63
+ delayedAuth: event.data.payload
64
+ };
65
+ (_c = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _c === void 0 ? void 0 : _c.call(currentOptions, {
66
+ type: 'integrationConnected',
67
+ payload: payload
68
+ });
69
+ (_d = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onIntegrationConnected) === null || _d === void 0 ? void 0 : _d.call(currentOptions, payload);
70
+ break;
71
+ }
72
+ case 'transferFinished': {
73
+ var payload = event.data.payload;
74
+ (_e = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _e === void 0 ? void 0 : _e.call(currentOptions, {
75
+ type: 'transferCompleted',
76
+ payload: payload
77
+ });
78
+ (_f = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onTransferFinished) === null || _f === void 0 ? void 0 : _f.call(currentOptions, payload);
79
+ break;
80
+ }
81
+ case 'close':
82
+ case 'done': {
83
+ (_g = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onExit) === null || _g === void 0 ? void 0 : _g.call(currentOptions, event.data.message);
84
+ (0, popup_1.removePopup)();
85
+ break;
86
+ }
87
+ case 'oauthLinkOpen': {
88
+ if (event.data.link) {
89
+ var w = 700;
90
+ var h = 800;
91
+ var left = screen.width / 2 - w / 2;
92
+ var top_1 = screen.height / 2 - h / 2;
93
+ (_h = window
94
+ .open(event.data.link, '_blank', "popup,noopener,noreferrer,resizable,scrollbars,width=".concat(w, ",height=").concat(h, ",top=").concat(top_1, ",left=").concat(left))) === null || _h === void 0 ? void 0 : _h.focus();
95
+ }
96
+ break;
97
+ }
98
+ case 'loaded': {
99
+ if (currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.accessTokens) {
100
+ (_j = iframeElement().contentWindow) === null || _j === void 0 ? void 0 : _j.postMessage({ type: 'frontAccessTokens', payload: currentOptions.accessTokens }, (iframeUrlObject === null || iframeUrlObject === void 0 ? void 0 : iframeUrlObject.origin) || 'https://web.meshconnect.com');
101
+ }
102
+ if (currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.transferDestinationTokens) {
103
+ (_k = iframeElement().contentWindow) === null || _k === void 0 ? void 0 : _k.postMessage({
104
+ type: 'frontTransferDestinationTokens',
105
+ payload: currentOptions.transferDestinationTokens
106
+ }, (iframeUrlObject === null || iframeUrlObject === void 0 ? void 0 : iframeUrlObject.origin) || 'https://web.meshconnect.com');
107
+ }
108
+ (_l = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _l === void 0 ? void 0 : _l.call(currentOptions, { type: 'pageLoaded' });
109
+ break;
110
+ }
111
+ default: {
112
+ if ((0, event_types_1.isLinkEventTypeKey)(event.data.type)) {
113
+ (_m = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.onEvent) === null || _m === void 0 ? void 0 : _m.call(currentOptions, event.data);
114
+ }
115
+ break;
116
+ }
117
+ }
118
+ }
119
+ var createLink = function (options) {
120
+ var openLink = function (linkToken) { return __awaiter(void 0, void 0, void 0, function () {
121
+ var linkUrl;
122
+ var _a;
123
+ return __generator(this, function (_b) {
124
+ if (!linkToken) {
125
+ (_a = options === null || options === void 0 ? void 0 : options.onExit) === null || _a === void 0 ? void 0 : _a.call(options, 'Invalid link token!');
126
+ return [2 /*return*/];
127
+ }
128
+ currentOptions = options;
129
+ linkUrl = window.atob(linkToken);
130
+ iframeUrlObject = new URL(linkUrl);
131
+ window.removeEventListener('message', eventsListener);
132
+ (0, popup_1.addPopup)(linkUrl);
133
+ window.addEventListener('message', eventsListener);
134
+ return [2 /*return*/];
135
+ });
136
+ }); };
137
+ var closeLink = function () {
138
+ var _a;
139
+ (0, popup_1.removePopup)();
140
+ window.removeEventListener('message', eventsListener);
141
+ (_a = options.onExit) === null || _a === void 0 ? void 0 : _a.call(options);
142
+ };
143
+ return {
144
+ openLink: openLink,
145
+ closeLink: closeLink
146
+ };
147
+ };
148
+ exports.createLink = createLink;
package/cjs/index.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.createLink = void 0;
18
+ __exportStar(require("./utils/types"), exports);
19
+ __exportStar(require("./utils/event-types"), exports);
20
+ var Link_1 = require("./Link");
21
+ Object.defineProperty(exports, "createLink", { enumerable: true, get: function () { return Link_1.createLink; } });
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isLinkEventTypeKey = void 0;
4
+ var LINK_EVENT_TYPE_KEYS = [
5
+ 'integrationConnected',
6
+ 'integrationConnectionError',
7
+ 'transferCompleted',
8
+ 'integrationSelected',
9
+ 'credentialsEntered',
10
+ 'transferStarted',
11
+ 'transferPreviewed',
12
+ 'transferPreviewError',
13
+ 'transferExecutionError',
14
+ 'pageLoaded'
15
+ ];
16
+ function isLinkEventTypeKey(key) {
17
+ return LINK_EVENT_TYPE_KEYS.includes(key);
18
+ }
19
+ exports.isLinkEventTypeKey = isLinkEventTypeKey;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addPopup = exports.removePopup = exports.iframeId = void 0;
4
+ var style_1 = require("./style");
5
+ var popupId = 'mesh-link-popup';
6
+ var backdropId = 'mesh-link-popup__backdrop';
7
+ var popupContentId = 'mesh-link-popup__popup-content';
8
+ var stylesId = 'mesh-link-popup__styles';
9
+ exports.iframeId = 'mesh-link-popup__iframe';
10
+ var getPopupHtml = function (link) { return "\n<div id=\"".concat(popupId, "\">\n <div id=\"").concat(backdropId, "\"></div>\n <div id=\"").concat(popupContentId, "\">\n <iframe id=\"").concat(exports.iframeId, "\" src=\"").concat(link, "\" allow=\"clipboard-read *; clipboard-write *\" />\n </div>\n</div>\n"); };
11
+ var getStyles = function (style) { return "\n<style id=\"".concat(stylesId, "\">\n body {\n position: fixed;\n left: 0;\n top: 0;\n bottom: 0;\n right: 0;\n overflow: hidden;\n }\n\n #").concat(popupId, " {\n all: unset;\n position: fixed;\n left: 0;\n top: 0;\n bottom: 0;\n right: 0;\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n z-index: 10000;\n }\n\n #").concat(backdropId, " {\n position: absolute;\n left: 0;\n top: 0;\n bottom: 0;\n right: 0;\n z-index: 10000;\n background: black;\n opacity: ").concat((0, style_1.getNumber)(0.6, style === null || style === void 0 ? void 0 : style.io), ";\n }\n\n #").concat(popupContentId, " {\n position: absolute;\n height: 80%;\n max-height: 710px;\n min-height: 685px;\n margin: auto;\n z-index: 10001;\n width: 30%;\n max-width: 430px;\n min-width: 380px;\n display: flex;\n flex-direction: column;\n border-radius: ").concat((0, style_1.getNumber)(24, style === null || style === void 0 ? void 0 : style.ir), "px;\n background: white;\n flex-grow: 1;\n }\n\n #").concat(popupContentId, " iframe {\n border: none;\n width: 100%;\n flex-grow: 1;\n border-radius: ").concat((0, style_1.getNumber)(24, style === null || style === void 0 ? void 0 : style.ir), "px;\n }\n\n @media only screen and (max-width: 768px) {\n #").concat(popupContentId, " {\n height: 100vh;\n width: 100vw;\n max-width: 100%;\n min-width: 100%;\n max-height: 100%;\n min-height: 100%;\n border-radius: 0px;\n }\n\n #").concat(popupContentId, " iframe {\n border-radius: 0px;\n }\n }\n\n @media only screen and (max-height: 710px) {\n #").concat(popupContentId, " {\n max-height: 100%;\n min-height: 100%;\n }\n }\n</style>\n"); };
12
+ function removePopup() {
13
+ var _a, _b;
14
+ var existingPopup = window.document.getElementById(popupId);
15
+ (_a = existingPopup === null || existingPopup === void 0 ? void 0 : existingPopup.parentElement) === null || _a === void 0 ? void 0 : _a.removeChild(existingPopup);
16
+ var existingStyles = window.document.getElementById(stylesId);
17
+ (_b = existingStyles === null || existingStyles === void 0 ? void 0 : existingStyles.parentElement) === null || _b === void 0 ? void 0 : _b.removeChild(existingStyles);
18
+ }
19
+ exports.removePopup = removePopup;
20
+ function addPopup(iframeLink) {
21
+ var style = (0, style_1.getLinkStyle)(iframeLink);
22
+ removePopup();
23
+ var popup = getPopupHtml(iframeLink);
24
+ var stylesElement = htmlToElement(getStyles(style));
25
+ if (stylesElement) {
26
+ window.document.head.appendChild(stylesElement);
27
+ }
28
+ var popupElement = htmlToElement(popup);
29
+ if (popupElement) {
30
+ window.document.body.appendChild(popupElement);
31
+ }
32
+ }
33
+ exports.addPopup = addPopup;
34
+ function htmlToElement(html) {
35
+ var template = document.createElement('template');
36
+ html = html.trim();
37
+ template.innerHTML = html;
38
+ return template.content.firstChild;
39
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getNumber = exports.getLinkStyle = void 0;
4
+ function getLinkStyle(url) {
5
+ try {
6
+ var params = new URLSearchParams(new URL(url).search);
7
+ var style = params.get('link_style');
8
+ return style && JSON.parse(window.atob(style));
9
+ }
10
+ catch (e) {
11
+ return undefined;
12
+ }
13
+ }
14
+ exports.getLinkStyle = getLinkStyle;
15
+ function getNumber(def, value) {
16
+ return value !== undefined ? value : def;
17
+ }
18
+ exports.getNumber = getNumber;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './utils/types';
2
+ export * from './utils/event-types';
3
+ export { createLink } from './Link';
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './utils/types';
2
+ export * from './utils/event-types';
3
+ export { createLink } from './Link';
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@meshconnect/web-link-sdk",
3
+ "version": "2.0.0",
4
+ "description": "A client-side JS library for integrating with Mesh Connect",
5
+ "license": "MIT",
6
+ "repository": "https://github.com/FrontFin/mesh-web-sdk.git",
7
+ "homepage": "https://github.com/FrontFin/mesh-web-sdk",
8
+ "devDependencies": {
9
+ "@typescript-eslint/eslint-plugin": "^4.21.0",
10
+ "@typescript-eslint/parser": "^4.21.0",
11
+ "eslint": "^7.23.0",
12
+ "eslint-config-prettier": "^8.1.0",
13
+ "eslint-plugin-import": "^2.22.1",
14
+ "eslint-plugin-prettier": "^3.4.0",
15
+ "prettier": "^2.2.1",
16
+ "typescript": "^4.6.4",
17
+ "ts-jest": "^29.1.1",
18
+ "jest": "^29.6.4",
19
+ "@types/jest": "^29.5.4",
20
+ "jest-environment-jsdom": "^29.6.4"
21
+ },
22
+ "dependencies": {
23
+ "@meshconnect/node-api": "^2.0.0"
24
+ },
25
+ "jest": {
26
+ "preset": "ts-jest",
27
+ "testEnvironment": "jsdom",
28
+ "testPathIgnorePatterns": [
29
+ "/node_modules/",
30
+ "/dist/"
31
+ ],
32
+ "coverageDirectory": "./coverage",
33
+ "transform": {
34
+ "\\.[jt]sx?$": "ts-jest"
35
+ },
36
+ "testEnvironmentOptions": {
37
+ "url": "http://localhost/"
38
+ }
39
+ },
40
+ "main": "cjs/index.js",
41
+ "module": "index.js",
42
+ "types": "index.d.ts"
43
+ }
@@ -0,0 +1,68 @@
1
+ import { LinkPayload, TransferFinishedPayload } from './types';
2
+ export type LinkEventType = IntegrationConnected | IntegrationConnectionError | TransferCompleted | IntegrationSelected | CredentialsEntered | TransferStarted | TransferPreviewed | TransferPreviewError | TransferExecutionError | PageLoaded;
3
+ declare const LINK_EVENT_TYPE_KEYS: readonly ["integrationConnected", "integrationConnectionError", "transferCompleted", "integrationSelected", "credentialsEntered", "transferStarted", "transferPreviewed", "transferPreviewError", "transferExecutionError", "pageLoaded"];
4
+ export type LinkEventTypeKeys = (typeof LINK_EVENT_TYPE_KEYS)[number];
5
+ export declare function isLinkEventTypeKey(key: string): key is LinkEventTypeKeys;
6
+ interface LinkEventBase {
7
+ type: LinkEventTypeKeys;
8
+ }
9
+ export interface PageLoaded {
10
+ type: 'pageLoaded';
11
+ }
12
+ export interface IntegrationConnected extends LinkEventBase {
13
+ type: 'integrationConnected';
14
+ payload: LinkPayload;
15
+ }
16
+ export interface IntegrationConnectionError extends LinkEventBase {
17
+ type: 'integrationConnectionError';
18
+ payload: {
19
+ errorMessage: string;
20
+ };
21
+ }
22
+ export interface TransferCompleted extends LinkEventBase {
23
+ type: 'transferCompleted';
24
+ payload: TransferFinishedPayload;
25
+ }
26
+ export interface IntegrationSelected extends LinkEventBase {
27
+ type: 'integrationSelected';
28
+ payload: {
29
+ integrationType: string;
30
+ integrationName: string;
31
+ };
32
+ }
33
+ export interface CredentialsEntered extends LinkEventBase {
34
+ type: 'credentialsEntered';
35
+ }
36
+ export interface TransferStarted extends LinkEventBase {
37
+ type: 'transferStarted';
38
+ }
39
+ export interface TransferPreviewed extends LinkEventBase {
40
+ type: 'transferPreviewed';
41
+ payload: {
42
+ amount: number;
43
+ symbol: string;
44
+ toAddress: string;
45
+ networkId: string;
46
+ previewId: string;
47
+ networkName?: string;
48
+ amountInFiat?: number;
49
+ estimatedNetworkGasFee?: {
50
+ fee?: number;
51
+ feeCurrency?: string;
52
+ feeInFiat?: number;
53
+ };
54
+ };
55
+ }
56
+ export interface TransferPreviewError extends LinkEventBase {
57
+ type: 'transferPreviewError';
58
+ payload: {
59
+ errorMessage: string;
60
+ };
61
+ }
62
+ export interface TransferExecutionError extends LinkEventBase {
63
+ type: 'transferExecutionError';
64
+ payload: {
65
+ errorMessage: string;
66
+ };
67
+ }
68
+ export {};
@@ -0,0 +1,15 @@
1
+ var LINK_EVENT_TYPE_KEYS = [
2
+ 'integrationConnected',
3
+ 'integrationConnectionError',
4
+ 'transferCompleted',
5
+ 'integrationSelected',
6
+ 'credentialsEntered',
7
+ 'transferStarted',
8
+ 'transferPreviewed',
9
+ 'transferPreviewError',
10
+ 'transferExecutionError',
11
+ 'pageLoaded'
12
+ ];
13
+ export function isLinkEventTypeKey(key) {
14
+ return LINK_EVENT_TYPE_KEYS.includes(key);
15
+ }
@@ -0,0 +1,3 @@
1
+ export declare const iframeId = "mesh-link-popup__iframe";
2
+ export declare function removePopup(): void;
3
+ export declare function addPopup(iframeLink: string): void;
package/utils/popup.js ADDED
@@ -0,0 +1,34 @@
1
+ import { getLinkStyle, getNumber } from './style';
2
+ var popupId = 'mesh-link-popup';
3
+ var backdropId = 'mesh-link-popup__backdrop';
4
+ var popupContentId = 'mesh-link-popup__popup-content';
5
+ var stylesId = 'mesh-link-popup__styles';
6
+ export var iframeId = 'mesh-link-popup__iframe';
7
+ var getPopupHtml = function (link) { return "\n<div id=\"".concat(popupId, "\">\n <div id=\"").concat(backdropId, "\"></div>\n <div id=\"").concat(popupContentId, "\">\n <iframe id=\"").concat(iframeId, "\" src=\"").concat(link, "\" allow=\"clipboard-read *; clipboard-write *\" />\n </div>\n</div>\n"); };
8
+ var getStyles = function (style) { return "\n<style id=\"".concat(stylesId, "\">\n body {\n position: fixed;\n left: 0;\n top: 0;\n bottom: 0;\n right: 0;\n overflow: hidden;\n }\n\n #").concat(popupId, " {\n all: unset;\n position: fixed;\n left: 0;\n top: 0;\n bottom: 0;\n right: 0;\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n z-index: 10000;\n }\n\n #").concat(backdropId, " {\n position: absolute;\n left: 0;\n top: 0;\n bottom: 0;\n right: 0;\n z-index: 10000;\n background: black;\n opacity: ").concat(getNumber(0.6, style === null || style === void 0 ? void 0 : style.io), ";\n }\n\n #").concat(popupContentId, " {\n position: absolute;\n height: 80%;\n max-height: 710px;\n min-height: 685px;\n margin: auto;\n z-index: 10001;\n width: 30%;\n max-width: 430px;\n min-width: 380px;\n display: flex;\n flex-direction: column;\n border-radius: ").concat(getNumber(24, style === null || style === void 0 ? void 0 : style.ir), "px;\n background: white;\n flex-grow: 1;\n }\n\n #").concat(popupContentId, " iframe {\n border: none;\n width: 100%;\n flex-grow: 1;\n border-radius: ").concat(getNumber(24, style === null || style === void 0 ? void 0 : style.ir), "px;\n }\n\n @media only screen and (max-width: 768px) {\n #").concat(popupContentId, " {\n height: 100vh;\n width: 100vw;\n max-width: 100%;\n min-width: 100%;\n max-height: 100%;\n min-height: 100%;\n border-radius: 0px;\n }\n\n #").concat(popupContentId, " iframe {\n border-radius: 0px;\n }\n }\n\n @media only screen and (max-height: 710px) {\n #").concat(popupContentId, " {\n max-height: 100%;\n min-height: 100%;\n }\n }\n</style>\n"); };
9
+ export function removePopup() {
10
+ var _a, _b;
11
+ var existingPopup = window.document.getElementById(popupId);
12
+ (_a = existingPopup === null || existingPopup === void 0 ? void 0 : existingPopup.parentElement) === null || _a === void 0 ? void 0 : _a.removeChild(existingPopup);
13
+ var existingStyles = window.document.getElementById(stylesId);
14
+ (_b = existingStyles === null || existingStyles === void 0 ? void 0 : existingStyles.parentElement) === null || _b === void 0 ? void 0 : _b.removeChild(existingStyles);
15
+ }
16
+ export function addPopup(iframeLink) {
17
+ var style = getLinkStyle(iframeLink);
18
+ removePopup();
19
+ var popup = getPopupHtml(iframeLink);
20
+ var stylesElement = htmlToElement(getStyles(style));
21
+ if (stylesElement) {
22
+ window.document.head.appendChild(stylesElement);
23
+ }
24
+ var popupElement = htmlToElement(popup);
25
+ if (popupElement) {
26
+ window.document.body.appendChild(popupElement);
27
+ }
28
+ }
29
+ function htmlToElement(html) {
30
+ var template = document.createElement('template');
31
+ html = html.trim();
32
+ template.innerHTML = html;
33
+ return template.content.firstChild;
34
+ }
@@ -0,0 +1,3 @@
1
+ import { LinkStyle } from './types';
2
+ export declare function getLinkStyle(url: string): LinkStyle | undefined;
3
+ export declare function getNumber(def: number, value?: number): number;
package/utils/style.js ADDED
@@ -0,0 +1,13 @@
1
+ export function getLinkStyle(url) {
2
+ try {
3
+ var params = new URLSearchParams(new URL(url).search);
4
+ var style = params.get('link_style');
5
+ return style && JSON.parse(window.atob(style));
6
+ }
7
+ catch (e) {
8
+ return undefined;
9
+ }
10
+ }
11
+ export function getNumber(def, value) {
12
+ return value !== undefined ? value : def;
13
+ }
@@ -0,0 +1,108 @@
1
+ import type { BrokerType } from '@meshconnect/node-api';
2
+ import { LinkEventType } from './event-types';
3
+ export type EventType = 'brokerageAccountAccessToken' | 'delayedAuthentication' | 'close' | 'done' | 'loaded' | 'oauthLinkOpen' | 'transferFinished';
4
+ export interface Link {
5
+ /**
6
+ * A function that takes linkToken parameter from `/api/v1/linktoken` endpoint as an input, and opens the Link UI popup
7
+ */
8
+ openLink: (linkToken: string) => Promise<void>;
9
+ /**
10
+ * A function to close Link UI popup
11
+ */
12
+ closeLink: () => void;
13
+ }
14
+ export interface AccountToken {
15
+ account: Account;
16
+ accessToken: string;
17
+ refreshToken?: string;
18
+ }
19
+ export interface Account {
20
+ accountId: string;
21
+ accountName: string;
22
+ fund?: number;
23
+ cash?: number;
24
+ isReconnected?: boolean;
25
+ }
26
+ export interface BrandInfo {
27
+ brokerLogo: string;
28
+ brokerPrimaryColor?: string;
29
+ }
30
+ export interface LinkPayload {
31
+ accessToken?: AccessTokenPayload;
32
+ delayedAuth?: DelayedAuthPayload;
33
+ }
34
+ export interface AccessTokenPayload {
35
+ accountTokens: AccountToken[];
36
+ brokerBrandInfo: BrandInfo;
37
+ expiresInSeconds?: number;
38
+ refreshTokenExpiresInSeconds?: number;
39
+ brokerType: BrokerType;
40
+ brokerName: string;
41
+ }
42
+ export interface DelayedAuthPayload {
43
+ refreshTokenExpiresInSeconds?: number;
44
+ brokerType: BrokerType;
45
+ refreshToken: string;
46
+ brokerName: string;
47
+ brokerBrandInfo: BrandInfo;
48
+ }
49
+ export interface TransferFinishedSuccessPayload {
50
+ status: 'success';
51
+ txId: string;
52
+ fromAddress: string;
53
+ toAddress: string;
54
+ symbol: string;
55
+ amount: number;
56
+ networkId: string;
57
+ }
58
+ export interface TransferFinishedErrorPayload {
59
+ status: 'error';
60
+ errorMessage: string;
61
+ }
62
+ export type TransferFinishedPayload = TransferFinishedSuccessPayload | TransferFinishedErrorPayload;
63
+ export interface IntegrationAccessToken {
64
+ accountId: string;
65
+ accountName: string;
66
+ accessToken: string;
67
+ brokerType: BrokerType;
68
+ brokerName: string;
69
+ }
70
+ export interface LinkOptions {
71
+ /**
72
+ * Client ID that can be obtained at https://dashboard.meshconnect.com/company/keys
73
+ */
74
+ clientId: string;
75
+ /**
76
+ * A callback function that is called when an integration is successfully connected.
77
+ * It receives a payload of type `LinkPayload`.
78
+ */
79
+ onIntegrationConnected: (payload: LinkPayload) => void;
80
+ /**
81
+ * (Optional) A callback function that is called when the Front iframe is closed.
82
+ */
83
+ onExit?: (error?: string) => void;
84
+ /**
85
+ * (Optional) A callback function that is called when a transfer is finished.
86
+ * It receives a payload of type `TransferFinishedPayload`.
87
+ */
88
+ onTransferFinished?: (payload: TransferFinishedPayload) => void;
89
+ /**
90
+ * (Optional) A callback function that is called when various events occur within the Front iframe.
91
+ * It receives an object with type `LinkEventTypeKeys` indicating the event, and an optional 'payload' containing additional data.
92
+ */
93
+ onEvent?: (event: LinkEventType) => void;
94
+ /**
95
+ * (Optional) An array of integration access tokens.
96
+ * These access tokens are used to initialize crypto transfers flow at 'Select asset step'
97
+ */
98
+ accessTokens?: IntegrationAccessToken[];
99
+ /**
100
+ * (Optional) An array of integration access tokens.
101
+ * Can be used to initialize the crypto transfers flow as an alternative to the target addresses.
102
+ */
103
+ transferDestinationTokens?: IntegrationAccessToken[];
104
+ }
105
+ export interface LinkStyle {
106
+ ir: number;
107
+ io: number;
108
+ }
package/utils/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};