@parafin/core 2.1.0 → 2.2.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 CHANGED
@@ -1,5 +1,6 @@
1
1
  type BaseProps = ({ token: string } | { partner: string }) & {
2
2
  dashboardTargetElementId?: string
3
+ onLinkOpened?: (url: string) => void
3
4
  }
4
5
 
5
6
  type CapitalOrSpendProps = {
@@ -86,25 +87,34 @@ export const openParafinDashboard = (
86
87
  throw new Error('Dashboard target element not found')
87
88
  }
88
89
 
89
- const closeParafinDashboard = (e: MessageEvent) => {
90
- if (e.origin === origin && e.data?.message === 'close-dashboard') {
91
- if ('orderId' in props) {
92
- props.onExit?.(props.orderId)
93
- } else if ('lineOfCreditApplicationId' in props) {
94
- props.onExit?.(props.lineOfCreditApplicationId)
95
- } else {
96
- props.onExit?.()
90
+ const messageListener = async (event: MessageEvent) => {
91
+ if (event.origin === origin) {
92
+ switch (event.data?.message) {
93
+ 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)
107
+ break
108
+ case 'link-opened':
109
+ if (props.onLinkOpened && event.data?.url) {
110
+ props.onLinkOpened(event.data.url)
111
+ }
112
+ break
97
113
  }
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
114
  }
105
115
  }
106
116
 
107
- window.addEventListener('message', closeParafinDashboard)
117
+ window.addEventListener('message', messageListener)
108
118
  dashboardTargetElement.appendChild(frame)
109
119
  setTimeout(() => {
110
120
  frame.style.opacity = '1'
package/out/index.d.ts CHANGED
@@ -4,6 +4,7 @@ type BaseProps = ({
4
4
  partner: string;
5
5
  }) & {
6
6
  dashboardTargetElementId?: string;
7
+ onLinkOpened?: (url: string) => void;
7
8
  };
8
9
  type CapitalOrSpendProps = {
9
10
  product: 'capital' | 'spend_card' | 'cash_account';
package/out/index.js CHANGED
@@ -54,26 +54,35 @@ export const openParafinDashboard = (props) => {
54
54
  if (!dashboardTargetElement) {
55
55
  throw new Error('Dashboard target element not found');
56
56
  }
57
- const closeParafinDashboard = (e) => {
58
- if (e.origin === origin && e.data?.message === 'close-dashboard') {
59
- if ('orderId' in props) {
60
- props.onExit?.(props.orderId);
57
+ const messageListener = async (event) => {
58
+ if (event.origin === origin) {
59
+ switch (event.data?.message) {
60
+ 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);
76
+ break;
77
+ case 'link-opened':
78
+ if (props.onLinkOpened && event.data?.url) {
79
+ props.onLinkOpened(event.data.url);
80
+ }
81
+ break;
61
82
  }
62
- else if ('lineOfCreditApplicationId' in props) {
63
- props.onExit?.(props.lineOfCreditApplicationId);
64
- }
65
- else {
66
- props.onExit?.();
67
- }
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
83
  }
75
84
  };
76
- window.addEventListener('message', closeParafinDashboard);
85
+ window.addEventListener('message', messageListener);
77
86
  dashboardTargetElement.appendChild(frame);
78
87
  setTimeout(() => {
79
88
  frame.style.opacity = '1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parafin/core",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Parafin embedded core",
5
5
  "author": "Parafin (https://www.parafin.com)",
6
6
  "module": "out/index.js",