@mxenabled/web-connect-widget-sdk 0.0.4 → 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.
Files changed (3) hide show
  1. package/API.md +172 -0
  2. package/CONTRIBUTING.md +10 -0
  3. package/package.json +4 -2
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/package.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "name": "@mxenabled/web-connect-widget-sdk",
3
- "version": "0.0.4",
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",