@parafin/core 2.2.0 → 2.3.1

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/index.ts CHANGED
@@ -1,13 +1,17 @@
1
+ export type LinkOpenedMetadata = {
2
+ type: 'auth' | 'document' | 'webpage' | 'phone' | 'email'
3
+ }
4
+
1
5
  type BaseProps = ({ token: string } | { partner: string }) & {
2
6
  dashboardTargetElementId?: string
3
- onLinkOpened?: (url: string) => void
7
+ onLinkOpened?: (url: string, metadata: LinkOpenedMetadata) => void
8
+ inWebView?: boolean
4
9
  }
5
10
 
6
11
  type CapitalOrSpendProps = {
7
12
  product: 'capital' | 'spend_card' | 'cash_account'
8
13
  route?: string
9
14
  externalBusinessId?: string
10
- additionalQueryParams?: Record<string, string>
11
15
  openInNewTab?: boolean
12
16
  onExit?: () => Promise<void> | void
13
17
  }
@@ -29,31 +33,45 @@ export const openParafinDashboard = (
29
33
  props.dashboardUrlOverride ?? 'https://app.parafin.com'
30
34
  )
31
35
  const route = 'route' in props ? props.route : '/'
36
+
32
37
  const query = {
33
38
  product: props.product,
34
39
  referrer: 'partner',
35
- ...('token' in props && { token: props.token }),
36
- ...('partner' in props && { partner: props.partner }),
37
- ...('externalBusinessId' in props && {
38
- externalBusinessId: props.externalBusinessId,
39
- }),
40
- ...('orderId' in props && { orderId: props.orderId }),
41
- ...('lineOfCreditApplicationId' in props && {
42
- lineOfCreditApplicationId: props.lineOfCreditApplicationId,
43
- }),
44
- ...('additionalQueryParams' in props && props.additionalQueryParams),
40
+ ...('token' in props &&
41
+ props.token !== undefined &&
42
+ props.token !== null && { token: props.token }),
43
+ ...('partner' in props &&
44
+ props.partner !== undefined &&
45
+ props.partner !== null && { partner: props.partner }),
46
+ ...('externalBusinessId' in props &&
47
+ props.externalBusinessId !== undefined &&
48
+ props.externalBusinessId !== null && {
49
+ externalBusinessId: props.externalBusinessId,
50
+ }),
51
+ ...('orderId' in props &&
52
+ props.orderId !== undefined &&
53
+ props.orderId !== null && { orderId: props.orderId }),
54
+ ...('lineOfCreditApplicationId' in props &&
55
+ props.lineOfCreditApplicationId !== undefined &&
56
+ props.lineOfCreditApplicationId !== null && {
57
+ lineOfCreditApplicationId: props.lineOfCreditApplicationId,
58
+ }),
59
+ ...('inWebView' in props &&
60
+ props.inWebView !== undefined &&
61
+ props.inWebView !== null && { inWebView: props.inWebView.toString() }),
45
62
  ...Object.fromEntries(searchParams),
46
63
  }
64
+
47
65
  const url = `${origin}${route}?${new URLSearchParams(query).toString()}`
48
66
 
49
67
  if ('openInNewTab' in props && props.openInNewTab) {
50
68
  window.open(url, '_blank')
51
- return
69
+ return () => {} // noop
52
70
  }
53
71
 
54
72
  if ('mode' in props && props.mode === 'redirect') {
55
73
  window.location.href = url
56
- return
74
+ return () => {} // noop
57
75
  }
58
76
 
59
77
  const existingParafinDashboard =
@@ -87,27 +105,40 @@ export const openParafinDashboard = (
87
105
  throw new Error('Dashboard target element not found')
88
106
  }
89
107
 
108
+ const onExit = () => {
109
+ if ('orderId' in props) {
110
+ props.onExit?.(props.orderId)
111
+ } else if ('lineOfCreditApplicationId' in props) {
112
+ props.onExit?.(props.lineOfCreditApplicationId)
113
+ } else {
114
+ props.onExit?.()
115
+ }
116
+ }
117
+
118
+ let cleanedUp = false
119
+ const cleanup = ({ withOnExit }: { withOnExit: boolean }) => {
120
+ if (cleanedUp) return // noop
121
+ cleanedUp = true
122
+
123
+ window.removeEventListener('message', messageListener)
124
+ frame.style.opacity = '0'
125
+
126
+ setTimeout(() => {
127
+ if (withOnExit) onExit()
128
+ dashboardTargetElement.removeChild(frame)
129
+ document.body.style.removeProperty('overflow')
130
+ }, 200)
131
+ }
132
+
90
133
  const messageListener = async (event: MessageEvent) => {
91
134
  if (event.origin === origin) {
92
135
  switch (event.data?.message) {
93
136
  case 'close-dashboard':
94
- if ('orderId' in props) {
95
- props.onExit?.(props.orderId)
96
- } else if ('lineOfCreditApplicationId' in props) {
97
- props.onExit?.(props.lineOfCreditApplicationId)
98
- } else {
99
- props.onExit?.()
100
- }
101
- window.removeEventListener('message', messageListener)
102
- frame.style.opacity = '0'
103
- setTimeout(() => {
104
- dashboardTargetElement.removeChild(frame)
105
- document.body.style.removeProperty('overflow')
106
- }, 200)
137
+ cleanup({ withOnExit: true })
107
138
  break
108
139
  case 'link-opened':
109
- if (props.onLinkOpened && event.data?.url) {
110
- props.onLinkOpened(event.data.url)
140
+ if (props.onLinkOpened && event.data?.url && event.data?.metadata) {
141
+ props.onLinkOpened(event.data.url, event.data.metadata)
111
142
  }
112
143
  break
113
144
  }
@@ -119,8 +150,11 @@ export const openParafinDashboard = (
119
150
  setTimeout(() => {
120
151
  frame.style.opacity = '1'
121
152
  document.body.style.overflow = 'hidden'
122
- }, 1)
153
+ }, 0)
154
+
155
+ return () => cleanup({ withOnExit: false })
123
156
  } catch (error) {
124
157
  console.error('Error loading Parafin dashboard', error)
158
+ return () => {} // noop
125
159
  }
126
160
  }
package/out/index.d.ts CHANGED
@@ -1,16 +1,19 @@
1
+ export type LinkOpenedMetadata = {
2
+ type: 'auth' | 'document' | 'webpage' | 'phone' | 'email';
3
+ };
1
4
  type BaseProps = ({
2
5
  token: string;
3
6
  } | {
4
7
  partner: string;
5
8
  }) & {
6
9
  dashboardTargetElementId?: string;
7
- onLinkOpened?: (url: string) => void;
10
+ onLinkOpened?: (url: string, metadata: LinkOpenedMetadata) => void;
11
+ inWebView?: boolean;
8
12
  };
9
13
  type CapitalOrSpendProps = {
10
14
  product: 'capital' | 'spend_card' | 'cash_account';
11
15
  route?: string;
12
16
  externalBusinessId?: string;
13
- additionalQueryParams?: Record<string, string>;
14
17
  openInNewTab?: boolean;
15
18
  onExit?: () => Promise<void> | void;
16
19
  };
@@ -21,5 +24,5 @@ type BNPLProps = {
21
24
  lineOfCreditApplicationId?: string;
22
25
  onExit?: (id?: string) => Promise<void> | void;
23
26
  };
24
- export declare const openParafinDashboard: (props: BaseProps & (CapitalOrSpendProps | BNPLProps)) => void;
27
+ export declare const openParafinDashboard: (props: BaseProps & (CapitalOrSpendProps | BNPLProps)) => () => void;
25
28
  export {};
package/out/index.js CHANGED
@@ -7,26 +7,38 @@ export const openParafinDashboard = (props) => {
7
7
  const query = {
8
8
  product: props.product,
9
9
  referrer: 'partner',
10
- ...('token' in props && { token: props.token }),
11
- ...('partner' in props && { partner: props.partner }),
12
- ...('externalBusinessId' in props && {
10
+ ...('token' in props &&
11
+ props.token !== undefined &&
12
+ props.token !== null && { token: props.token }),
13
+ ...('partner' in props &&
14
+ props.partner !== undefined &&
15
+ props.partner !== null && { partner: props.partner }),
16
+ ...('externalBusinessId' in props &&
17
+ props.externalBusinessId !== undefined &&
18
+ props.externalBusinessId !== null && {
13
19
  externalBusinessId: props.externalBusinessId,
14
20
  }),
15
- ...('orderId' in props && { orderId: props.orderId }),
16
- ...('lineOfCreditApplicationId' in props && {
21
+ ...('orderId' in props &&
22
+ props.orderId !== undefined &&
23
+ props.orderId !== null && { orderId: props.orderId }),
24
+ ...('lineOfCreditApplicationId' in props &&
25
+ props.lineOfCreditApplicationId !== undefined &&
26
+ props.lineOfCreditApplicationId !== null && {
17
27
  lineOfCreditApplicationId: props.lineOfCreditApplicationId,
18
28
  }),
19
- ...('additionalQueryParams' in props && props.additionalQueryParams),
29
+ ...('inWebView' in props &&
30
+ props.inWebView !== undefined &&
31
+ props.inWebView !== null && { inWebView: props.inWebView.toString() }),
20
32
  ...Object.fromEntries(searchParams),
21
33
  };
22
34
  const url = `${origin}${route}?${new URLSearchParams(query).toString()}`;
23
35
  if ('openInNewTab' in props && props.openInNewTab) {
24
36
  window.open(url, '_blank');
25
- return;
37
+ return () => { }; // noop
26
38
  }
27
39
  if ('mode' in props && props.mode === 'redirect') {
28
40
  window.location.href = url;
29
- return;
41
+ return () => { }; // noop
30
42
  }
31
43
  const existingParafinDashboard = document.getElementById('parafin-dashboard');
32
44
  if (existingParafinDashboard) {
@@ -54,29 +66,40 @@ export const openParafinDashboard = (props) => {
54
66
  if (!dashboardTargetElement) {
55
67
  throw new Error('Dashboard target element not found');
56
68
  }
69
+ const onExit = () => {
70
+ if ('orderId' in props) {
71
+ props.onExit?.(props.orderId);
72
+ }
73
+ else if ('lineOfCreditApplicationId' in props) {
74
+ props.onExit?.(props.lineOfCreditApplicationId);
75
+ }
76
+ else {
77
+ props.onExit?.();
78
+ }
79
+ };
80
+ let cleanedUp = false;
81
+ const cleanup = ({ withOnExit }) => {
82
+ if (cleanedUp)
83
+ return; // noop
84
+ cleanedUp = true;
85
+ window.removeEventListener('message', messageListener);
86
+ frame.style.opacity = '0';
87
+ setTimeout(() => {
88
+ if (withOnExit)
89
+ onExit();
90
+ dashboardTargetElement.removeChild(frame);
91
+ document.body.style.removeProperty('overflow');
92
+ }, 200);
93
+ };
57
94
  const messageListener = async (event) => {
58
95
  if (event.origin === origin) {
59
96
  switch (event.data?.message) {
60
97
  case 'close-dashboard':
61
- if ('orderId' in props) {
62
- props.onExit?.(props.orderId);
63
- }
64
- else if ('lineOfCreditApplicationId' in props) {
65
- props.onExit?.(props.lineOfCreditApplicationId);
66
- }
67
- else {
68
- props.onExit?.();
69
- }
70
- window.removeEventListener('message', messageListener);
71
- frame.style.opacity = '0';
72
- setTimeout(() => {
73
- dashboardTargetElement.removeChild(frame);
74
- document.body.style.removeProperty('overflow');
75
- }, 200);
98
+ cleanup({ withOnExit: true });
76
99
  break;
77
100
  case 'link-opened':
78
- if (props.onLinkOpened && event.data?.url) {
79
- props.onLinkOpened(event.data.url);
101
+ if (props.onLinkOpened && event.data?.url && event.data?.metadata) {
102
+ props.onLinkOpened(event.data.url, event.data.metadata);
80
103
  }
81
104
  break;
82
105
  }
@@ -87,9 +110,11 @@ export const openParafinDashboard = (props) => {
87
110
  setTimeout(() => {
88
111
  frame.style.opacity = '1';
89
112
  document.body.style.overflow = 'hidden';
90
- }, 1);
113
+ }, 0);
114
+ return () => cleanup({ withOnExit: false });
91
115
  }
92
116
  catch (error) {
93
117
  console.error('Error loading Parafin dashboard', error);
118
+ return () => { }; // noop
94
119
  }
95
120
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parafin/core",
3
- "version": "2.2.0",
3
+ "version": "2.3.1",
4
4
  "description": "Parafin embedded core",
5
5
  "author": "Parafin (https://www.parafin.com)",
6
6
  "module": "out/index.js",