@melio-eng/web-sdk 1.0.32 → 1.0.33-pr.61.157f964

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.
@@ -12,7 +12,7 @@ export declare class Flow implements FlowInstance {
12
12
  protected container: HTMLElement | null;
13
13
  private eventListeners;
14
14
  protected keepAliveInterval: number | null;
15
- private messageHandler;
15
+ protected messageHandler: ((event: MessageEvent) => void) | null;
16
16
  constructor(containerId: string, config: BaseFlowConfig, partnerName: string, environment: Environment, branchOverride?: string | undefined);
17
17
  /**
18
18
  * Initialize the flow by creating and injecting the iframe
@@ -26,7 +26,7 @@ export declare class Flow implements FlowInstance {
26
26
  * Create flow URL using partner name and environment
27
27
  */
28
28
  protected createFlowUrl(): string;
29
- private setupEventListeners;
29
+ protected setupEventListeners(): void;
30
30
  /**
31
31
  * Emit events to registered listeners
32
32
  */
@@ -80,12 +80,6 @@ export class Flow {
80
80
  case 'NAVIGATED_TO_TARGET':
81
81
  this.emit('navigated', data);
82
82
  break;
83
- case 'AUTHENTICATION_SUCCESS':
84
- this.emit('authenticationSucceeded');
85
- break;
86
- case 'AUTHENTICATION_ERROR':
87
- this.emit('authenticationFailed');
88
- break;
89
83
  case 'ONBOARDING_COMPLETED':
90
84
  this.emit('completed', { flowName: 'onboarding', ...data });
91
85
  break;
@@ -1,11 +1,19 @@
1
- import { InitConfig, Environment } from '../types.js';
2
- import { Flow } from './Flow.js';
1
+ import { InitConfig, Environment, FlowEventType, FlowEventCallback, InitFlowInstance } from '../types.js';
3
2
  /**
4
- * InitFlow subclass for handling initialization with callbacks
3
+ * Standalone InitFlow class for handling initialization with callbacks
5
4
  */
6
- export declare class InitFlow extends Flow {
5
+ export declare class InitFlow implements InitFlowInstance {
6
+ private iframe;
7
+ private container;
8
+ private eventListeners;
9
+ private keepAliveInterval;
10
+ private messageHandler;
7
11
  private authorizationCode;
8
- constructor(containerId: string, config: InitConfig, partnerName: string, environment: Environment, branchOverride?: string);
12
+ private config;
13
+ private partnerName;
14
+ private environment;
15
+ private branchOverride?;
16
+ constructor(config: InitConfig, partnerName: string, environment: Environment, branchOverride?: string);
9
17
  /**
10
18
  * Initialize the init flow by creating and injecting a hidden iframe
11
19
  */
@@ -13,6 +21,30 @@ export declare class InitFlow extends Flow {
13
21
  /**
14
22
  * Construct the specific flow URL for initialization
15
23
  */
16
- protected constructFlowUrl(baseUrl: string): string;
24
+ private constructFlowUrl;
25
+ /**
26
+ * Create flow URL using partner name and environment
27
+ */
28
+ private createFlowUrl;
29
+ /**
30
+ * Setup event listeners to only handle authentication events
31
+ */
32
+ private setupEventListeners;
33
+ /**
34
+ * Emit events to registered listeners
35
+ */
36
+ private emit;
37
+ /**
38
+ * Register an event listener
39
+ */
40
+ on(event: 'authenticationSucceeded' | 'authenticationFailed', callback: FlowEventCallback): void;
41
+ /**
42
+ * Remove an event listener
43
+ */
44
+ off(event: FlowEventType, callback: FlowEventCallback): void;
45
+ /**
46
+ * Close the flow and clean up resources
47
+ */
48
+ close(): void;
17
49
  setupKeepAlive(): void;
18
50
  }
@@ -1,15 +1,23 @@
1
- import { Flow } from './Flow.js';
2
- import { isEmptyString } from './utils.js';
1
+ import { isEmptyString, getBaseUrl } from './utils.js';
3
2
  /**
4
- * InitFlow subclass for handling initialization with callbacks
3
+ * Standalone InitFlow class for handling initialization with callbacks
5
4
  */
6
- export class InitFlow extends Flow {
7
- constructor(containerId, config, partnerName, environment, branchOverride) {
8
- super(containerId, config, partnerName, environment, branchOverride);
5
+ export class InitFlow {
6
+ constructor(config, partnerName, environment, branchOverride) {
7
+ this.iframe = null;
8
+ this.container = null;
9
+ this.eventListeners = new Map();
10
+ this.keepAliveInterval = null;
11
+ this.messageHandler = null;
9
12
  if (isEmptyString(config.authCode)) {
10
13
  throw new Error('Authorization code is required for init flow');
11
14
  }
12
15
  this.authorizationCode = config.authCode;
16
+ this.config = config;
17
+ this.partnerName = partnerName;
18
+ this.environment = environment;
19
+ this.branchOverride = branchOverride;
20
+ this.setupEventListeners();
13
21
  }
14
22
  /**
15
23
  * Initialize the init flow by creating and injecting a hidden iframe
@@ -35,6 +43,98 @@ export class InitFlow extends Flow {
35
43
  });
36
44
  return `${baseUrl}/${this.partnerName}/auth?${params.toString()}`;
37
45
  }
46
+ /**
47
+ * Create flow URL using partner name and environment
48
+ */
49
+ createFlowUrl() {
50
+ const baseUrl = getBaseUrl(this.environment);
51
+ let finalUrl = this.constructFlowUrl(baseUrl);
52
+ // Add cdn_branch_override parameter for non-production environments
53
+ if (this.branchOverride && this.environment !== 'production') {
54
+ const separator = finalUrl.includes('?') ? '&' : '?';
55
+ finalUrl += `${separator}cdn_branch_override=${this.branchOverride}`;
56
+ }
57
+ return finalUrl;
58
+ }
59
+ /**
60
+ * Setup event listeners to only handle authentication events
61
+ */
62
+ setupEventListeners() {
63
+ this.messageHandler = (event) => {
64
+ if (!/melio\.com|melioservices\.com/.test(event.origin)) {
65
+ return;
66
+ }
67
+ const { type, ...data } = event.data;
68
+ console.log('📬 Received message from iframe:', {
69
+ type,
70
+ data,
71
+ origin: event.origin,
72
+ });
73
+ switch (type) {
74
+ case 'AUTHENTICATION_SUCCESS':
75
+ this.emit('authenticationSucceeded');
76
+ break;
77
+ case 'AUTHENTICATION_ERROR':
78
+ this.emit('authenticationFailed');
79
+ break;
80
+ }
81
+ };
82
+ window.addEventListener('message', this.messageHandler);
83
+ }
84
+ /**
85
+ * Emit events to registered listeners
86
+ */
87
+ /* eslint-disable @typescript-eslint/no-explicit-any */
88
+ emit(event, data) {
89
+ const listeners = this.eventListeners.get(event);
90
+ if (listeners) {
91
+ listeners.forEach((callback) => {
92
+ try {
93
+ callback(data);
94
+ }
95
+ catch (error) {
96
+ console.error(`Error in ${event} event listener:`, error);
97
+ }
98
+ });
99
+ }
100
+ }
101
+ /**
102
+ * Register an event listener
103
+ */
104
+ on(event, callback) {
105
+ if (!this.eventListeners.has(event)) {
106
+ this.eventListeners.set(event, new Set());
107
+ }
108
+ this.eventListeners.get(event).add(callback);
109
+ }
110
+ /* eslint-enable no-dupe-class-members */
111
+ /**
112
+ * Remove an event listener
113
+ */
114
+ off(event, callback) {
115
+ const listeners = this.eventListeners.get(event);
116
+ if (listeners) {
117
+ listeners.delete(callback);
118
+ }
119
+ }
120
+ /**
121
+ * Close the flow and clean up resources
122
+ */
123
+ close() {
124
+ if (this.keepAliveInterval) {
125
+ clearInterval(this.keepAliveInterval);
126
+ this.keepAliveInterval = null;
127
+ }
128
+ if (this.iframe && this.container) {
129
+ this.container.removeChild(this.iframe);
130
+ this.iframe = null;
131
+ }
132
+ if (this.messageHandler) {
133
+ window.removeEventListener('message', this.messageHandler);
134
+ this.messageHandler = null;
135
+ }
136
+ this.eventListeners.clear();
137
+ }
38
138
  setupKeepAlive() {
39
139
  this.keepAliveInterval = window.setInterval(() => {
40
140
  if (this.iframe && this.iframe.contentWindow) {
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { InitOptions, OnboardingConfig, SettingsConfig, FlowInstance, PaymentsDashboardConfig, PayFlowConfig } from './types.js';
1
+ import { InitOptions, OnboardingConfig, SettingsConfig, FlowInstance, PaymentsDashboardConfig, PayFlowConfig, InitFlowInstance } from './types.js';
2
2
  /**
3
3
  * Main SDK implementation - now partner agnostic
4
4
  */
@@ -13,7 +13,7 @@ export declare class MelioSDK implements MelioSDK {
13
13
  /**
14
14
  * Initialize the SDK - triggers auth event without creating iframe
15
15
  */
16
- init(authenticationCode: string, options: InitOptions): FlowInstance;
16
+ init(authenticationCode: string, options: InitOptions): InitFlowInstance;
17
17
  /**
18
18
  * Launch the onboarding flow
19
19
  */
package/dist/index.js CHANGED
@@ -27,8 +27,7 @@ export class MelioSDK {
27
27
  partnerName: this.partnerName,
28
28
  environment: this.environment,
29
29
  });
30
- const initFlow = new InitFlow('', // no need container id for init flow as it is created inside the initialization
31
- { authCode: authenticationCode, containerId: '' }, this.partnerName, this.environment, this.branchOverride);
30
+ const initFlow = new InitFlow({ authCode: authenticationCode, containerId: '' }, this.partnerName, this.environment, this.branchOverride);
32
31
  initFlow.initialize();
33
32
  console.log('InitFlow initialized successfully');
34
33
  if (options.keepAlive)
package/dist/types.d.ts CHANGED
@@ -133,6 +133,25 @@ export interface FlowInstance {
133
133
  */
134
134
  close(): void;
135
135
  }
136
+ export interface InitFlowInstance {
137
+ /**
138
+ * Register an event listener for this flow
139
+ * @param event - The event type to listen for
140
+ * @param callback - The callback function to execute when the event occurs
141
+ */
142
+ on(event: 'authenticationSucceeded', callback: () => void): void;
143
+ on(event: 'authenticationFailed', callback: () => void): void;
144
+ /**
145
+ * Remove an event listener for this flow
146
+ * @param event - The event type to remove listener for
147
+ * @param callback - The callback function to remove
148
+ */
149
+ off(event: FlowEventType, callback: FlowEventCallback): void;
150
+ /**
151
+ * Close the flow and clean up resources
152
+ */
153
+ close(): void;
154
+ }
136
155
  /**
137
156
  * Main SDK interface
138
157
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@melio-eng/web-sdk",
3
- "version": "1.0.32",
3
+ "version": "1.0.33-pr.61.157f964",
4
4
  "description": "Melio Web SDK - Embed core Melio workflows directly into partner UI with minimal effort",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",