@parafin/core 2.2.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 +35 -21
- package/out/index.d.ts +5 -2
- package/out/index.js +28 -21
- package/package.json +1 -1
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,21 +33,27 @@ export const openParafinDashboard = (
|
|
|
29
33
|
props.dashboardUrlOverride ?? 'https://app.parafin.com'
|
|
30
34
|
)
|
|
31
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
|
+
|
|
32
45
|
const query = {
|
|
33
46
|
product: props.product,
|
|
34
47
|
referrer: 'partner',
|
|
35
|
-
...('token'
|
|
36
|
-
...('partner'
|
|
37
|
-
...('externalBusinessId'
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
...('
|
|
41
|
-
...('lineOfCreditApplicationId' in props && {
|
|
42
|
-
lineOfCreditApplicationId: props.lineOfCreditApplicationId,
|
|
43
|
-
}),
|
|
44
|
-
...('additionalQueryParams' in props && props.additionalQueryParams),
|
|
48
|
+
...addPropIfExists('token'),
|
|
49
|
+
...addPropIfExists('partner'),
|
|
50
|
+
...addPropIfExists('externalBusinessId'),
|
|
51
|
+
...addPropIfExists('orderId'),
|
|
52
|
+
...addPropIfExists('lineOfCreditApplicationId'),
|
|
53
|
+
...addPropIfExists('inWebView'),
|
|
45
54
|
...Object.fromEntries(searchParams),
|
|
46
55
|
}
|
|
56
|
+
|
|
47
57
|
const url = `${origin}${route}?${new URLSearchParams(query).toString()}`
|
|
48
58
|
|
|
49
59
|
if ('openInNewTab' in props && props.openInNewTab) {
|
|
@@ -87,27 +97,31 @@ export const openParafinDashboard = (
|
|
|
87
97
|
throw new Error('Dashboard target element not found')
|
|
88
98
|
}
|
|
89
99
|
|
|
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
|
+
|
|
90
110
|
const messageListener = async (event: MessageEvent) => {
|
|
91
111
|
if (event.origin === origin) {
|
|
92
112
|
switch (event.data?.message) {
|
|
93
113
|
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
114
|
window.removeEventListener('message', messageListener)
|
|
102
115
|
frame.style.opacity = '0'
|
|
103
116
|
setTimeout(() => {
|
|
117
|
+
onExit()
|
|
104
118
|
dashboardTargetElement.removeChild(frame)
|
|
105
119
|
document.body.style.removeProperty('overflow')
|
|
106
120
|
}, 200)
|
|
107
121
|
break
|
|
108
122
|
case 'link-opened':
|
|
109
|
-
if (props.onLinkOpened && event.data?.url) {
|
|
110
|
-
props.onLinkOpened(event.data.url)
|
|
123
|
+
if (props.onLinkOpened && event.data?.url && event.data?.metadata) {
|
|
124
|
+
props.onLinkOpened(event.data.url, event.data.metadata)
|
|
111
125
|
}
|
|
112
126
|
break
|
|
113
127
|
}
|
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
|
};
|
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,29 +58,32 @@ export const openParafinDashboard = (props) => {
|
|
|
54
58
|
if (!dashboardTargetElement) {
|
|
55
59
|
throw new Error('Dashboard target element not found');
|
|
56
60
|
}
|
|
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
|
+
};
|
|
57
72
|
const messageListener = async (event) => {
|
|
58
73
|
if (event.origin === origin) {
|
|
59
74
|
switch (event.data?.message) {
|
|
60
75
|
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
76
|
window.removeEventListener('message', messageListener);
|
|
71
77
|
frame.style.opacity = '0';
|
|
72
78
|
setTimeout(() => {
|
|
79
|
+
onExit();
|
|
73
80
|
dashboardTargetElement.removeChild(frame);
|
|
74
81
|
document.body.style.removeProperty('overflow');
|
|
75
82
|
}, 200);
|
|
76
83
|
break;
|
|
77
84
|
case 'link-opened':
|
|
78
|
-
if (props.onLinkOpened && event.data?.url) {
|
|
79
|
-
props.onLinkOpened(event.data.url);
|
|
85
|
+
if (props.onLinkOpened && event.data?.url && event.data?.metadata) {
|
|
86
|
+
props.onLinkOpened(event.data.url, event.data.metadata);
|
|
80
87
|
}
|
|
81
88
|
break;
|
|
82
89
|
}
|