@parafin/core 3.0.0 → 3.1.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.
@@ -0,0 +1 @@
1
+ $ tsc
package/index.ts CHANGED
@@ -169,13 +169,29 @@ export const defaultWidgetStyles = {
169
169
  boxSizing: 'border-box' as const,
170
170
  }
171
171
 
172
- export type WidgetEvent = 'opted_in' | 'opted_out'
172
+ export type WidgetEvent =
173
+ | 'opted_in'
174
+ | 'opted_out'
175
+ | 'dashboard_opened'
176
+ | 'widget_loaded'
177
+ | 'widget_error'
178
+
179
+ export type WidgetEventMetadata = {
180
+ timeToLoadInMs: number | null
181
+ }
182
+
183
+ const emptyMetadata: WidgetEventMetadata = {
184
+ timeToLoadInMs: null,
185
+ }
173
186
 
174
187
  export type WidgetProps = {
175
188
  token: string
176
189
  product: 'capital' | 'spend_card' | 'cash_account'
177
190
  externalBusinessId?: string
178
- onEvent?: (eventType: WidgetEvent) => Promise<void> | void
191
+ onEvent?: (
192
+ eventType: WidgetEvent,
193
+ metadata: WidgetEventMetadata
194
+ ) => Promise<void> | void
179
195
  onExit?: () => void
180
196
  openInNewTab?: boolean
181
197
  onLinkOpened?: (url: string, metadata: LinkOpenedMetadata) => void
@@ -186,6 +202,8 @@ export const initializeParafinWidget = (
186
202
  iframe: HTMLIFrameElement,
187
203
  props: WidgetProps
188
204
  ) => {
205
+ const initStartTime = Date.now()
206
+
189
207
  // @ts-ignore
190
208
  const url = new URL(props.widgetUrlOverride ?? 'https://widget.parafin.com')
191
209
  const query = {
@@ -216,6 +234,9 @@ export const initializeParafinWidget = (
216
234
  }
217
235
  break
218
236
  case 'open-dashboard':
237
+ if (props.onEvent) {
238
+ props.onEvent('dashboard_opened', emptyMetadata)
239
+ }
219
240
  openParafinDashboard({
220
241
  ...props,
221
242
  route: data?.route,
@@ -228,7 +249,7 @@ export const initializeParafinWidget = (
228
249
  case 'person-opt-in':
229
250
  if (props.onEvent) {
230
251
  try {
231
- await props.onEvent('opted_in')
252
+ await props.onEvent('opted_in', emptyMetadata)
232
253
  sendMessage({ message: 'person-opt-in', state: 'success' })
233
254
  } catch {
234
255
  sendMessage({ message: 'person-opt-in', state: 'error' })
@@ -240,7 +261,7 @@ export const initializeParafinWidget = (
240
261
  case 'person-opt-out':
241
262
  if (props.onEvent) {
242
263
  try {
243
- await props.onEvent('opted_out')
264
+ await props.onEvent('opted_out', emptyMetadata)
244
265
  sendMessage({ message: 'person-opt-out', state: 'success' })
245
266
  } catch {
246
267
  sendMessage({ message: 'person-opt-out', state: 'error' })
@@ -254,6 +275,17 @@ export const initializeParafinWidget = (
254
275
  iframe.style.height = data.height
255
276
  }
256
277
  break
278
+ case 'widget-error':
279
+ if (props.onEvent) {
280
+ props.onEvent('widget_error', emptyMetadata)
281
+ }
282
+ break
283
+ case 'widget-load-complete':
284
+ if (props.onEvent) {
285
+ const timeToLoadInMs = Date.now() - initStartTime
286
+ props.onEvent('widget_loaded', { timeToLoadInMs })
287
+ }
288
+ break
257
289
  }
258
290
  }
259
291
  }
package/out/index.d.ts CHANGED
@@ -34,12 +34,15 @@ export declare const defaultWidgetStyles: {
34
34
  transition: string;
35
35
  boxSizing: "border-box";
36
36
  };
37
- export type WidgetEvent = 'opted_in' | 'opted_out';
37
+ export type WidgetEvent = 'opted_in' | 'opted_out' | 'dashboard_opened' | 'widget_loaded' | 'widget_error';
38
+ export type WidgetEventMetadata = {
39
+ timeToLoadInMs: number | null;
40
+ };
38
41
  export type WidgetProps = {
39
42
  token: string;
40
43
  product: 'capital' | 'spend_card' | 'cash_account';
41
44
  externalBusinessId?: string;
42
- onEvent?: (eventType: WidgetEvent) => Promise<void> | void;
45
+ onEvent?: (eventType: WidgetEvent, metadata: WidgetEventMetadata) => Promise<void> | void;
43
46
  onExit?: () => void;
44
47
  openInNewTab?: boolean;
45
48
  onLinkOpened?: (url: string, metadata: LinkOpenedMetadata) => void;
package/out/index.js CHANGED
@@ -127,7 +127,11 @@ export const defaultWidgetStyles = {
127
127
  transition: 'border 0.2s, border-radius 0.2s',
128
128
  boxSizing: 'border-box',
129
129
  };
130
+ const emptyMetadata = {
131
+ timeToLoadInMs: null,
132
+ };
130
133
  export const initializeParafinWidget = (iframe, props) => {
134
+ const initStartTime = Date.now();
131
135
  // @ts-ignore
132
136
  const url = new URL(props.widgetUrlOverride ?? 'https://widget.parafin.com');
133
137
  const query = {
@@ -155,6 +159,9 @@ export const initializeParafinWidget = (iframe, props) => {
155
159
  }
156
160
  break;
157
161
  case 'open-dashboard':
162
+ if (props.onEvent) {
163
+ props.onEvent('dashboard_opened', emptyMetadata);
164
+ }
158
165
  openParafinDashboard({
159
166
  ...props,
160
167
  route: data?.route,
@@ -167,7 +174,7 @@ export const initializeParafinWidget = (iframe, props) => {
167
174
  case 'person-opt-in':
168
175
  if (props.onEvent) {
169
176
  try {
170
- await props.onEvent('opted_in');
177
+ await props.onEvent('opted_in', emptyMetadata);
171
178
  sendMessage({ message: 'person-opt-in', state: 'success' });
172
179
  }
173
180
  catch {
@@ -181,7 +188,7 @@ export const initializeParafinWidget = (iframe, props) => {
181
188
  case 'person-opt-out':
182
189
  if (props.onEvent) {
183
190
  try {
184
- await props.onEvent('opted_out');
191
+ await props.onEvent('opted_out', emptyMetadata);
185
192
  sendMessage({ message: 'person-opt-out', state: 'success' });
186
193
  }
187
194
  catch {
@@ -197,6 +204,17 @@ export const initializeParafinWidget = (iframe, props) => {
197
204
  iframe.style.height = data.height;
198
205
  }
199
206
  break;
207
+ case 'widget-error':
208
+ if (props.onEvent) {
209
+ props.onEvent('widget_error', emptyMetadata);
210
+ }
211
+ break;
212
+ case 'widget-load-complete':
213
+ if (props.onEvent) {
214
+ const timeToLoadInMs = Date.now() - initStartTime;
215
+ props.onEvent('widget_loaded', { timeToLoadInMs });
216
+ }
217
+ break;
200
218
  }
201
219
  }
202
220
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parafin/core",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Parafin embedded core",
5
5
  "author": "Parafin (https://www.parafin.com)",
6
6
  "module": "out/index.js",