@parafin/core 2.3.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 +44 -24
- package/out/index.d.ts +1 -1
- package/out/index.js +42 -24
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -34,23 +34,31 @@ export const openParafinDashboard = (
|
|
|
34
34
|
)
|
|
35
35
|
const route = 'route' in props ? props.route : '/'
|
|
36
36
|
|
|
37
|
-
const addPropIfExists = (key: string) => {
|
|
38
|
-
if (key in props && props[key] !== undefined && props[key] !== null) {
|
|
39
|
-
return { [key]: props[key] }
|
|
40
|
-
} else {
|
|
41
|
-
return {}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
37
|
const query = {
|
|
46
38
|
product: props.product,
|
|
47
39
|
referrer: 'partner',
|
|
48
|
-
...
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
...
|
|
52
|
-
|
|
53
|
-
|
|
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() }),
|
|
54
62
|
...Object.fromEntries(searchParams),
|
|
55
63
|
}
|
|
56
64
|
|
|
@@ -58,12 +66,12 @@ export const openParafinDashboard = (
|
|
|
58
66
|
|
|
59
67
|
if ('openInNewTab' in props && props.openInNewTab) {
|
|
60
68
|
window.open(url, '_blank')
|
|
61
|
-
return
|
|
69
|
+
return () => {} // noop
|
|
62
70
|
}
|
|
63
71
|
|
|
64
72
|
if ('mode' in props && props.mode === 'redirect') {
|
|
65
73
|
window.location.href = url
|
|
66
|
-
return
|
|
74
|
+
return () => {} // noop
|
|
67
75
|
}
|
|
68
76
|
|
|
69
77
|
const existingParafinDashboard =
|
|
@@ -107,17 +115,26 @@ export const openParafinDashboard = (
|
|
|
107
115
|
}
|
|
108
116
|
}
|
|
109
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
|
+
|
|
110
133
|
const messageListener = async (event: MessageEvent) => {
|
|
111
134
|
if (event.origin === origin) {
|
|
112
135
|
switch (event.data?.message) {
|
|
113
136
|
case 'close-dashboard':
|
|
114
|
-
|
|
115
|
-
frame.style.opacity = '0'
|
|
116
|
-
setTimeout(() => {
|
|
117
|
-
onExit()
|
|
118
|
-
dashboardTargetElement.removeChild(frame)
|
|
119
|
-
document.body.style.removeProperty('overflow')
|
|
120
|
-
}, 200)
|
|
137
|
+
cleanup({ withOnExit: true })
|
|
121
138
|
break
|
|
122
139
|
case 'link-opened':
|
|
123
140
|
if (props.onLinkOpened && event.data?.url && event.data?.metadata) {
|
|
@@ -133,8 +150,11 @@ export const openParafinDashboard = (
|
|
|
133
150
|
setTimeout(() => {
|
|
134
151
|
frame.style.opacity = '1'
|
|
135
152
|
document.body.style.overflow = 'hidden'
|
|
136
|
-
},
|
|
153
|
+
}, 0)
|
|
154
|
+
|
|
155
|
+
return () => cleanup({ withOnExit: false })
|
|
137
156
|
} catch (error) {
|
|
138
157
|
console.error('Error loading Parafin dashboard', error)
|
|
158
|
+
return () => {} // noop
|
|
139
159
|
}
|
|
140
160
|
}
|
package/out/index.d.ts
CHANGED
|
@@ -24,5 +24,5 @@ type BNPLProps = {
|
|
|
24
24
|
lineOfCreditApplicationId?: string;
|
|
25
25
|
onExit?: (id?: string) => Promise<void> | void;
|
|
26
26
|
};
|
|
27
|
-
export declare const openParafinDashboard: (props: BaseProps & (CapitalOrSpendProps | BNPLProps)) => void;
|
|
27
|
+
export declare const openParafinDashboard: (props: BaseProps & (CapitalOrSpendProps | BNPLProps)) => () => void;
|
|
28
28
|
export {};
|
package/out/index.js
CHANGED
|
@@ -4,33 +4,41 @@ export const openParafinDashboard = (props) => {
|
|
|
4
4
|
// @ts-ignore
|
|
5
5
|
props.dashboardUrlOverride ?? 'https://app.parafin.com');
|
|
6
6
|
const route = 'route' in props ? props.route : '/';
|
|
7
|
-
const addPropIfExists = (key) => {
|
|
8
|
-
if (key in props && props[key] !== undefined && props[key] !== null) {
|
|
9
|
-
return { [key]: props[key] };
|
|
10
|
-
}
|
|
11
|
-
else {
|
|
12
|
-
return {};
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
7
|
const query = {
|
|
16
8
|
product: props.product,
|
|
17
9
|
referrer: 'partner',
|
|
18
|
-
...
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
...
|
|
22
|
-
|
|
23
|
-
|
|
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 && {
|
|
19
|
+
externalBusinessId: props.externalBusinessId,
|
|
20
|
+
}),
|
|
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 && {
|
|
27
|
+
lineOfCreditApplicationId: props.lineOfCreditApplicationId,
|
|
28
|
+
}),
|
|
29
|
+
...('inWebView' in props &&
|
|
30
|
+
props.inWebView !== undefined &&
|
|
31
|
+
props.inWebView !== null && { inWebView: props.inWebView.toString() }),
|
|
24
32
|
...Object.fromEntries(searchParams),
|
|
25
33
|
};
|
|
26
34
|
const url = `${origin}${route}?${new URLSearchParams(query).toString()}`;
|
|
27
35
|
if ('openInNewTab' in props && props.openInNewTab) {
|
|
28
36
|
window.open(url, '_blank');
|
|
29
|
-
return;
|
|
37
|
+
return () => { }; // noop
|
|
30
38
|
}
|
|
31
39
|
if ('mode' in props && props.mode === 'redirect') {
|
|
32
40
|
window.location.href = url;
|
|
33
|
-
return;
|
|
41
|
+
return () => { }; // noop
|
|
34
42
|
}
|
|
35
43
|
const existingParafinDashboard = document.getElementById('parafin-dashboard');
|
|
36
44
|
if (existingParafinDashboard) {
|
|
@@ -69,17 +77,25 @@ export const openParafinDashboard = (props) => {
|
|
|
69
77
|
props.onExit?.();
|
|
70
78
|
}
|
|
71
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
|
+
};
|
|
72
94
|
const messageListener = async (event) => {
|
|
73
95
|
if (event.origin === origin) {
|
|
74
96
|
switch (event.data?.message) {
|
|
75
97
|
case 'close-dashboard':
|
|
76
|
-
|
|
77
|
-
frame.style.opacity = '0';
|
|
78
|
-
setTimeout(() => {
|
|
79
|
-
onExit();
|
|
80
|
-
dashboardTargetElement.removeChild(frame);
|
|
81
|
-
document.body.style.removeProperty('overflow');
|
|
82
|
-
}, 200);
|
|
98
|
+
cleanup({ withOnExit: true });
|
|
83
99
|
break;
|
|
84
100
|
case 'link-opened':
|
|
85
101
|
if (props.onLinkOpened && event.data?.url && event.data?.metadata) {
|
|
@@ -94,9 +110,11 @@ export const openParafinDashboard = (props) => {
|
|
|
94
110
|
setTimeout(() => {
|
|
95
111
|
frame.style.opacity = '1';
|
|
96
112
|
document.body.style.overflow = 'hidden';
|
|
97
|
-
},
|
|
113
|
+
}, 0);
|
|
114
|
+
return () => cleanup({ withOnExit: false });
|
|
98
115
|
}
|
|
99
116
|
catch (error) {
|
|
100
117
|
console.error('Error loading Parafin dashboard', error);
|
|
118
|
+
return () => { }; // noop
|
|
101
119
|
}
|
|
102
120
|
};
|