@mxenabled/web-connect-widget-sdk 0.0.3 → 0.0.5

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/API.md ADDED
@@ -0,0 +1,172 @@
1
+ # API Documentation
2
+
3
+ ## ConnectWidget factory function
4
+
5
+ The `ConnectWidget` factory function returns an object that allows you to mount and unmount an MX connect widget into a web application. It provides easy access to MX connect widget events.
6
+
7
+ ### Props
8
+
9
+ #### `containerSelector` (required)
10
+
11
+ **Type:** `string`
12
+
13
+ A CSS selector used to find the DOM element where the widget iframe will be mounted when `mount` is called.
14
+
15
+ **Example:**
16
+
17
+ ```typescript
18
+ ConnectWidget({
19
+ containerSelector: '#widget-container',
20
+ onEvent,
21
+ url: 'https://.....',
22
+ });
23
+ ```
24
+
25
+ #### `url` (required)
26
+
27
+ **Type:** `string`
28
+
29
+ The generated connect widget url. This URL will be loaded in the underlying iframe.
30
+
31
+ #### `onEvent` (required)
32
+
33
+ **Type:** `(event: Event) => void`
34
+
35
+ A callback function that handles post message events emitted by the iframed connect widget. This function is called whenever an event occurs within the widget.
36
+
37
+ | Parameter | Type | Description |
38
+ | --------- | ------- | -------------------------------------- |
39
+ | `event` | `Event` | The event object emitted by the widget |
40
+
41
+ **Event Object Structure:**
42
+
43
+ ```typescript
44
+ type Event = {
45
+ type: string; // Event type identifier (e.g., "mx/connect/oauthRequested")
46
+ metadata: Record<string, unknown>; // Event-specific data and context
47
+ };
48
+ ```
49
+
50
+ **Event Types:**
51
+
52
+ Event types can be found within the MX docs [here](https://docs.mx.com/connect/guides/handling-events/).
53
+
54
+ **Example:**
55
+
56
+ ```typescript
57
+ const handleEvent = (event: Event) => {
58
+ switch (event.type) {
59
+ case 'mx/connect/memberConnected':
60
+ console.log('OAuth requested with URL:', event.metadata);
61
+ break;
62
+
63
+ default:
64
+ console.log('Received event:', event.type, event.metadata);
65
+ }
66
+ };
67
+
68
+ const widget = ConnectWidget({
69
+ containerSelector: '#widget-container',
70
+ onEvent,
71
+ url: 'https://.....',
72
+ });
73
+ ```
74
+
75
+ #### `iframeProps` (optional)
76
+
77
+ Additional props to pass to the underlying iframe. This allows customization of iframe behavior and styling. Any prop that can be passed to an iframe element can be passed here.
78
+
79
+ **Default:** iframe will be styled with `{ height: "100%", width: "100%" }` if no `iframeProps.style` is provided.
80
+
81
+ **Example:**
82
+
83
+ ```typescript
84
+ ConnectWidget({
85
+ containerSelector: '#widget-container',
86
+ iframeProps: {
87
+ id: 'exampleId',
88
+ style: {
89
+ border: 'solid 1px black',
90
+ },
91
+ // other iframe props...
92
+ },
93
+ onEvent,
94
+ url: 'https://.....',
95
+ });
96
+ ```
97
+
98
+ ### Methods
99
+
100
+ #### `mount`
101
+
102
+ **Type:** `() => void`
103
+
104
+ Mounts the connect widget iframe into the element matched by `containerSelector` and starts listening for widget events via `window.postMessage`.
105
+
106
+ Throws an error if the container element cannot be found.
107
+
108
+ **Example:**
109
+
110
+ ```typescript
111
+ const widget = ConnectWidget({
112
+ containerSelector: '#widget-container',
113
+ onEvent,
114
+ url: 'https://.....',
115
+ });
116
+
117
+ widget.mount();
118
+ ```
119
+
120
+ #### `unmount`
121
+
122
+ **Type:** `() => void`
123
+
124
+ Removes the widget iframe from the DOM and unregisters the message event listener added by `mount`.
125
+
126
+ **Example:**
127
+
128
+ ```typescript
129
+ const widget = ConnectWidget({
130
+ containerSelector: '#widget-container',
131
+ onEvent,
132
+ url: 'https://.....',
133
+ });
134
+
135
+ widget.mount();
136
+
137
+ // Later, when done with the widget:
138
+ widget.unmount();
139
+ ```
140
+
141
+ ### Usage Example
142
+
143
+ ```typescript
144
+ import { ConnectWidget, type Event } from '@mxenabled/web-connect-widget-sdk';
145
+
146
+ const handleEvent = (event: Event) => {
147
+ switch (event.type) {
148
+ case 'mx/connect/memberConnected':
149
+ console.log('OAuth requested with URL:', event.metadata);
150
+ break;
151
+
152
+ default:
153
+ console.log('Received event:', event.type, event.metadata);
154
+ }
155
+ };
156
+
157
+ const widget = ConnectWidget({
158
+ containerSelector: '#widget-container',
159
+ iframeProps: {
160
+ style: {
161
+ border: 'solid 1px black',
162
+ },
163
+ },
164
+ onEvent,
165
+ url: 'https://.....',
166
+ });
167
+
168
+ widget.mount();
169
+
170
+ // Later, when done with the widget:
171
+ widget.unmount();
172
+ ```
@@ -0,0 +1,10 @@
1
+ # Contributing
2
+
3
+ ## What is in this repo
4
+
5
+ 1. An sdk that exports a `ConnectWidget` that mounts/unmounts a connect widget iframe to the dom.
6
+ 1. An example app built with React that uses the `ConnectWidget`
7
+
8
+ ## Setting up a local development environment
9
+
10
+ Read the [Local Development](./LOCAL_DEVELOPMENT.md) guide in order to get the example app running.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # MX Web Connect Widget SDK
2
+
3
+ ## Purpose
4
+
5
+ The purpose of this project is to be able to easily integrate the MX connect widget into a web app.
6
+
7
+ ## Implementation guide
8
+
9
+ The implementation guide lives in the MX docs [here](Add something here when it exists).
10
+
11
+ ## How it works
12
+
13
+ This package mounts an connect widget iframe to the DOM, listens for post messages, and provides callbacks for handling MX events.
14
+
15
+ ## API Documentation
16
+
17
+ The api documentation is [here](./API.md)
18
+
19
+ ## Contributing
20
+
21
+ Read the [contributing guide](./CONTRIBUTING.md) to learn about the development process.
22
+
23
+ ## License
24
+
25
+ This project is lincensed under the terms of the [MIT license](LICENSE).
@@ -2,7 +2,7 @@ import { Event } from './consts';
2
2
  interface ConnectWidgetProps {
3
3
  containerSelector: string;
4
4
  iframeProps?: Record<string, unknown>;
5
- onEvent?: (event: Event) => void;
5
+ onEvent: (event: Event) => void;
6
6
  url: string;
7
7
  }
8
8
  export declare const ConnectWidget: ({ containerSelector, iframeProps, onEvent, url, }: ConnectWidgetProps) => {
package/dist/index.js CHANGED
@@ -4,11 +4,7 @@ const g = ({
4
4
  onEvent: a,
5
5
  url: o
6
6
  }) => {
7
- const t = document.createElement("iframe"), { style: c, ...d } = i || {}, m = {
8
- height: "100%",
9
- width: "100%",
10
- ...c ?? {}
11
- };
7
+ const t = document.createElement("iframe"), { style: c, ...d } = i || {}, m = c || { height: "100%", width: "100%" };
12
8
  Object.assign(t.style, m);
13
9
  for (const [e, n] of Object.entries(d))
14
10
  e.startsWith("data-") ? t.setAttribute(e, String(n)) : Object.assign(t, { [e]: n });
@@ -17,7 +13,7 @@ const g = ({
17
13
  if (e.origin !== new URL(o).origin)
18
14
  return;
19
15
  const { data: n } = e, { metadata: f, type: u } = n;
20
- a?.({
16
+ a({
21
17
  metadata: f,
22
18
  type: u
23
19
  });
@@ -1 +1 @@
1
- (function(t,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(t=typeof globalThis<"u"?globalThis:t||self,o(t.index={}))})(this,(function(t){"use strict";const o=({containerSelector:s,iframeProps:a,onEvent:c,url:r})=>{const n=document.createElement("iframe"),{style:f,...u}=a||{},m={height:"100%",width:"100%",...f??{}};Object.assign(n.style,m);for(const[e,i]of Object.entries(u))e.startsWith("data-")?n.setAttribute(e,String(i)):Object.assign(n,{[e]:i});n.src=r;const d=e=>{if(e.origin!==new URL(r).origin)return;const{data:i}=e,{metadata:l,type:g}=i;c?.({metadata:l,type:g})};return{mount:()=>{const e=document.querySelector(s);if(!e)throw new Error(`Container with selector "${s}" not found.`);e.appendChild(n),window.addEventListener("message",d)},unmount:()=>{n.remove(),window.removeEventListener("message",d)}}};t.ConnectWidget=o,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(t,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(t=typeof globalThis<"u"?globalThis:t||self,o(t.index={}))})(this,(function(t){"use strict";const o=({containerSelector:s,iframeProps:a,onEvent:c,url:r})=>{const n=document.createElement("iframe"),{style:f,...u}=a||{},m=f||{height:"100%",width:"100%"};Object.assign(n.style,m);for(const[e,i]of Object.entries(u))e.startsWith("data-")?n.setAttribute(e,String(i)):Object.assign(n,{[e]:i});n.src=r;const d=e=>{if(e.origin!==new URL(r).origin)return;const{data:i}=e,{metadata:l,type:g}=i;c({metadata:l,type:g})};return{mount:()=>{const e=document.querySelector(s);if(!e)throw new Error(`Container with selector "${s}" not found.`);e.appendChild(n),window.addEventListener("message",d)},unmount:()=>{n.remove(),window.removeEventListener("message",d)}}};t.ConnectWidget=o,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "name": "@mxenabled/web-connect-widget-sdk",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "MX Connect Web Widget SDK",
5
5
  "files": [
6
- "dist"
6
+ "dist",
7
+ "API.md",
8
+ "CONTRIBUTING.md"
7
9
  ],
8
10
  "main": "dist/index.js",
9
11
  "type": "module",