@parafin/core 2.1.0 → 2.3.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.
- package/index.ts +50 -26
- package/out/index.d.ts +5 -1
- package/out/index.js +43 -27
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -1,12 +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
|
|
7
|
+
onLinkOpened?: (url: string, metadata: LinkOpenedMetadata) => void
|
|
8
|
+
inWebView?: boolean
|
|
3
9
|
}
|
|
4
10
|
|
|
5
11
|
type CapitalOrSpendProps = {
|
|
6
12
|
product: 'capital' | 'spend_card' | 'cash_account'
|
|
7
13
|
route?: string
|
|
8
14
|
externalBusinessId?: string
|
|
9
|
-
additionalQueryParams?: Record<string, string>
|
|
10
15
|
openInNewTab?: boolean
|
|
11
16
|
onExit?: () => Promise<void> | void
|
|
12
17
|
}
|
|
@@ -28,21 +33,27 @@ export const openParafinDashboard = (
|
|
|
28
33
|
props.dashboardUrlOverride ?? 'https://app.parafin.com'
|
|
29
34
|
)
|
|
30
35
|
const route = 'route' in props ? props.route : '/'
|
|
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
|
+
|
|
31
45
|
const query = {
|
|
32
46
|
product: props.product,
|
|
33
47
|
referrer: 'partner',
|
|
34
|
-
...('token'
|
|
35
|
-
...('partner'
|
|
36
|
-
...('externalBusinessId'
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
...('
|
|
40
|
-
...('lineOfCreditApplicationId' in props && {
|
|
41
|
-
lineOfCreditApplicationId: props.lineOfCreditApplicationId,
|
|
42
|
-
}),
|
|
43
|
-
...('additionalQueryParams' in props && props.additionalQueryParams),
|
|
48
|
+
...addPropIfExists('token'),
|
|
49
|
+
...addPropIfExists('partner'),
|
|
50
|
+
...addPropIfExists('externalBusinessId'),
|
|
51
|
+
...addPropIfExists('orderId'),
|
|
52
|
+
...addPropIfExists('lineOfCreditApplicationId'),
|
|
53
|
+
...addPropIfExists('inWebView'),
|
|
44
54
|
...Object.fromEntries(searchParams),
|
|
45
55
|
}
|
|
56
|
+
|
|
46
57
|
const url = `${origin}${route}?${new URLSearchParams(query).toString()}`
|
|
47
58
|
|
|
48
59
|
if ('openInNewTab' in props && props.openInNewTab) {
|
|
@@ -86,25 +97,38 @@ export const openParafinDashboard = (
|
|
|
86
97
|
throw new Error('Dashboard target element not found')
|
|
87
98
|
}
|
|
88
99
|
|
|
89
|
-
const
|
|
90
|
-
if (
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
100
|
+
const onExit = () => {
|
|
101
|
+
if ('orderId' in props) {
|
|
102
|
+
props.onExit?.(props.orderId)
|
|
103
|
+
} else if ('lineOfCreditApplicationId' in props) {
|
|
104
|
+
props.onExit?.(props.lineOfCreditApplicationId)
|
|
105
|
+
} else {
|
|
106
|
+
props.onExit?.()
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const messageListener = async (event: MessageEvent) => {
|
|
111
|
+
if (event.origin === origin) {
|
|
112
|
+
switch (event.data?.message) {
|
|
113
|
+
case 'close-dashboard':
|
|
114
|
+
window.removeEventListener('message', messageListener)
|
|
115
|
+
frame.style.opacity = '0'
|
|
116
|
+
setTimeout(() => {
|
|
117
|
+
onExit()
|
|
118
|
+
dashboardTargetElement.removeChild(frame)
|
|
119
|
+
document.body.style.removeProperty('overflow')
|
|
120
|
+
}, 200)
|
|
121
|
+
break
|
|
122
|
+
case 'link-opened':
|
|
123
|
+
if (props.onLinkOpened && event.data?.url && event.data?.metadata) {
|
|
124
|
+
props.onLinkOpened(event.data.url, event.data.metadata)
|
|
125
|
+
}
|
|
126
|
+
break
|
|
97
127
|
}
|
|
98
|
-
window.removeEventListener('message', closeParafinDashboard)
|
|
99
|
-
frame.style.opacity = '0'
|
|
100
|
-
setTimeout(() => {
|
|
101
|
-
dashboardTargetElement.removeChild(frame)
|
|
102
|
-
document.body.style.removeProperty('overflow')
|
|
103
|
-
}, 200)
|
|
104
128
|
}
|
|
105
129
|
}
|
|
106
130
|
|
|
107
|
-
window.addEventListener('message',
|
|
131
|
+
window.addEventListener('message', messageListener)
|
|
108
132
|
dashboardTargetElement.appendChild(frame)
|
|
109
133
|
setTimeout(() => {
|
|
110
134
|
frame.style.opacity = '1'
|
package/out/index.d.ts
CHANGED
|
@@ -1,15 +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;
|
|
10
|
+
onLinkOpened?: (url: string, metadata: LinkOpenedMetadata) => void;
|
|
11
|
+
inWebView?: boolean;
|
|
7
12
|
};
|
|
8
13
|
type CapitalOrSpendProps = {
|
|
9
14
|
product: 'capital' | 'spend_card' | 'cash_account';
|
|
10
15
|
route?: string;
|
|
11
16
|
externalBusinessId?: string;
|
|
12
|
-
additionalQueryParams?: Record<string, string>;
|
|
13
17
|
openInNewTab?: boolean;
|
|
14
18
|
onExit?: () => Promise<void> | void;
|
|
15
19
|
};
|
package/out/index.js
CHANGED
|
@@ -4,19 +4,23 @@ 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
|
+
};
|
|
7
15
|
const query = {
|
|
8
16
|
product: props.product,
|
|
9
17
|
referrer: 'partner',
|
|
10
|
-
...('token'
|
|
11
|
-
...('partner'
|
|
12
|
-
...('externalBusinessId'
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
...('
|
|
16
|
-
...('lineOfCreditApplicationId' in props && {
|
|
17
|
-
lineOfCreditApplicationId: props.lineOfCreditApplicationId,
|
|
18
|
-
}),
|
|
19
|
-
...('additionalQueryParams' in props && props.additionalQueryParams),
|
|
18
|
+
...addPropIfExists('token'),
|
|
19
|
+
...addPropIfExists('partner'),
|
|
20
|
+
...addPropIfExists('externalBusinessId'),
|
|
21
|
+
...addPropIfExists('orderId'),
|
|
22
|
+
...addPropIfExists('lineOfCreditApplicationId'),
|
|
23
|
+
...addPropIfExists('inWebView'),
|
|
20
24
|
...Object.fromEntries(searchParams),
|
|
21
25
|
};
|
|
22
26
|
const url = `${origin}${route}?${new URLSearchParams(query).toString()}`;
|
|
@@ -54,26 +58,38 @@ export const openParafinDashboard = (props) => {
|
|
|
54
58
|
if (!dashboardTargetElement) {
|
|
55
59
|
throw new Error('Dashboard target element not found');
|
|
56
60
|
}
|
|
57
|
-
const
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
const onExit = () => {
|
|
62
|
+
if ('orderId' in props) {
|
|
63
|
+
props.onExit?.(props.orderId);
|
|
64
|
+
}
|
|
65
|
+
else if ('lineOfCreditApplicationId' in props) {
|
|
66
|
+
props.onExit?.(props.lineOfCreditApplicationId);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
props.onExit?.();
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const messageListener = async (event) => {
|
|
73
|
+
if (event.origin === origin) {
|
|
74
|
+
switch (event.data?.message) {
|
|
75
|
+
case 'close-dashboard':
|
|
76
|
+
window.removeEventListener('message', messageListener);
|
|
77
|
+
frame.style.opacity = '0';
|
|
78
|
+
setTimeout(() => {
|
|
79
|
+
onExit();
|
|
80
|
+
dashboardTargetElement.removeChild(frame);
|
|
81
|
+
document.body.style.removeProperty('overflow');
|
|
82
|
+
}, 200);
|
|
83
|
+
break;
|
|
84
|
+
case 'link-opened':
|
|
85
|
+
if (props.onLinkOpened && event.data?.url && event.data?.metadata) {
|
|
86
|
+
props.onLinkOpened(event.data.url, event.data.metadata);
|
|
87
|
+
}
|
|
88
|
+
break;
|
|
67
89
|
}
|
|
68
|
-
window.removeEventListener('message', closeParafinDashboard);
|
|
69
|
-
frame.style.opacity = '0';
|
|
70
|
-
setTimeout(() => {
|
|
71
|
-
dashboardTargetElement.removeChild(frame);
|
|
72
|
-
document.body.style.removeProperty('overflow');
|
|
73
|
-
}, 200);
|
|
74
90
|
}
|
|
75
91
|
};
|
|
76
|
-
window.addEventListener('message',
|
|
92
|
+
window.addEventListener('message', messageListener);
|
|
77
93
|
dashboardTargetElement.appendChild(frame);
|
|
78
94
|
setTimeout(() => {
|
|
79
95
|
frame.style.opacity = '1';
|