@nuvei-connect/nc-websdk 1.0.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/README.md +62 -0
- package/dist/cdn-index.d.ts +2 -0
- package/dist/cdn-index.d.ts.map +1 -0
- package/dist/cdn-index.js +8 -0
- package/dist/common/enums/index.d.ts +25 -0
- package/dist/common/enums/index.d.ts.map +1 -0
- package/dist/common/enums/index.js +29 -0
- package/dist/config/default-config.d.ts +3 -0
- package/dist/config/default-config.d.ts.map +1 -0
- package/dist/config/default-config.js +25 -0
- package/dist/core/index.d.ts +178 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +629 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/interfaces/index.d.ts +77 -0
- package/dist/interfaces/index.d.ts.map +1 -0
- package/dist/interfaces/index.js +2 -0
- package/dist/interfaces/post-message.interfaces.d.ts +17 -0
- package/dist/interfaces/post-message.interfaces.d.ts.map +1 -0
- package/dist/interfaces/post-message.interfaces.js +2 -0
- package/dist/interfaces/utils.interfaces.d.ts +4 -0
- package/dist/interfaces/utils.interfaces.d.ts.map +1 -0
- package/dist/interfaces/utils.interfaces.js +3 -0
- package/dist/services/default-auth-provider.d.ts +17 -0
- package/dist/services/default-auth-provider.d.ts.map +1 -0
- package/dist/services/default-auth-provider.js +225 -0
- package/dist/utils/common.utils.d.ts +27 -0
- package/dist/utils/common.utils.d.ts.map +1 -0
- package/dist/utils/common.utils.js +102 -0
- package/dist/utils/date.utils.d.ts +2 -0
- package/dist/utils/date.utils.d.ts.map +1 -0
- package/dist/utils/date.utils.js +29 -0
- package/dist/utils/iframe-url.utils.d.ts +6 -0
- package/dist/utils/iframe-url.utils.d.ts.map +1 -0
- package/dist/utils/iframe-url.utils.js +37 -0
- package/dist/utils/object.utils.d.ts +56 -0
- package/dist/utils/object.utils.d.ts.map +1 -0
- package/dist/utils/object.utils.js +214 -0
- package/package.json +44 -0
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
24
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
var enums_1 = require("../common/enums");
|
|
54
|
+
var default_auth_provider_1 = __importDefault(require("../services/default-auth-provider"));
|
|
55
|
+
var object_utils_1 = require("../utils/object.utils");
|
|
56
|
+
var default_config_1 = require("../config/default-config");
|
|
57
|
+
var date_utils_1 = require("../utils/date.utils");
|
|
58
|
+
var iframe_url_utils_1 = require("../utils/iframe-url.utils");
|
|
59
|
+
var NuveiSDK = /** @class */ (function () {
|
|
60
|
+
/**
|
|
61
|
+
* Initializes the Nuvei SDK instance
|
|
62
|
+
* @param {INuveiInitParams} params - Initialization parameters
|
|
63
|
+
* @param {() => Promise<IClientSession>} params.fetchClientSession - Function to fetch the client session
|
|
64
|
+
* @param {() => Promise<IClientRefreshSession>} params.refreshClientSession - Function to refresh the client session
|
|
65
|
+
* @param {() => Promise<boolean>} params.destroySession - Function to destroy the client session
|
|
66
|
+
* @param {INuveiSdkConfig} [params.config] - Optional configuration overrides
|
|
67
|
+
*/
|
|
68
|
+
function NuveiSDK(params) {
|
|
69
|
+
var _this = this;
|
|
70
|
+
this.sdkConfig = __assign({}, default_config_1.DEFAULT_CONFIG);
|
|
71
|
+
// iframe
|
|
72
|
+
this.currentIframe = null;
|
|
73
|
+
this.iframeUrl = '';
|
|
74
|
+
this.isIframeReady = false;
|
|
75
|
+
this.messageQueue = [];
|
|
76
|
+
// session
|
|
77
|
+
this.cachedSession = null;
|
|
78
|
+
this.sessionCheckInterval = null;
|
|
79
|
+
this.expiryTimeout = null;
|
|
80
|
+
this.retryAttempts = 0;
|
|
81
|
+
this.MAX_RETRIES = 3;
|
|
82
|
+
this.REFRESH_BUFFER_MS = 60 * 1000;
|
|
83
|
+
this.isListeningToIframeEvents = false;
|
|
84
|
+
this.isDestroyed = false;
|
|
85
|
+
/**
|
|
86
|
+
* Initializes the configured component.
|
|
87
|
+
*
|
|
88
|
+
* This is a convenience wrapper for integrations that only mount one
|
|
89
|
+
* microfrontend and do not need to call loadComponent manually.
|
|
90
|
+
*/
|
|
91
|
+
this.initialize = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
92
|
+
var iframe, error_1;
|
|
93
|
+
var _b, _c;
|
|
94
|
+
return __generator(this, function (_d) {
|
|
95
|
+
switch (_d.label) {
|
|
96
|
+
case 0:
|
|
97
|
+
this.isDestroyed = false;
|
|
98
|
+
if (!this.componentName) {
|
|
99
|
+
throw new Error('componentName is required to initialize the SDK(Web SDK)');
|
|
100
|
+
}
|
|
101
|
+
_d.label = 1;
|
|
102
|
+
case 1:
|
|
103
|
+
_d.trys.push([1, 4, , 5]);
|
|
104
|
+
return [4 /*yield*/, this.loadComponent(this.componentName)];
|
|
105
|
+
case 2:
|
|
106
|
+
iframe = _d.sent();
|
|
107
|
+
if (this.isDestroyed)
|
|
108
|
+
return [2 /*return*/, iframe];
|
|
109
|
+
return [4 /*yield*/, ((_b = this.onReady) === null || _b === void 0 ? void 0 : _b.call(this, this))];
|
|
110
|
+
case 3:
|
|
111
|
+
_d.sent();
|
|
112
|
+
return [2 /*return*/, iframe];
|
|
113
|
+
case 4:
|
|
114
|
+
error_1 = _d.sent();
|
|
115
|
+
(_c = this.onError) === null || _c === void 0 ? void 0 : _c.call(this, error_1);
|
|
116
|
+
throw error_1;
|
|
117
|
+
case 5: return [2 /*return*/];
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}); };
|
|
121
|
+
/**
|
|
122
|
+
* Establishes a connection to a specified component by creating and loading an iframe.
|
|
123
|
+
*
|
|
124
|
+
* This asynchronous function initializes the WebSDK with a valid session, validates
|
|
125
|
+
* the session token, determines the appropriate iframe URL based on the given
|
|
126
|
+
* component name, and creates the iframe with the specified source URL.
|
|
127
|
+
*
|
|
128
|
+
* @param {string} componentName - The name of the component to connect to.
|
|
129
|
+
* Valid values are defined in the ComponentNameEnum.
|
|
130
|
+
*
|
|
131
|
+
* @returns {Promise<HTMLIFrameElement | void>} A promise that resolves to the created
|
|
132
|
+
* iframe element, or void if an error occurs.
|
|
133
|
+
*
|
|
134
|
+
* @throws {Error} Throws an error if the session token is invalid or if the
|
|
135
|
+
* specified component name is unknown.
|
|
136
|
+
*/
|
|
137
|
+
this.loadComponent = function (componentName) { return __awaiter(_this, void 0, void 0, function () {
|
|
138
|
+
var accessTokenData, iframe, error_2;
|
|
139
|
+
return __generator(this, function (_b) {
|
|
140
|
+
switch (_b.label) {
|
|
141
|
+
case 0:
|
|
142
|
+
console.log('Component Requested to Load(Web SDK): ', componentName);
|
|
143
|
+
_b.label = 1;
|
|
144
|
+
case 1:
|
|
145
|
+
_b.trys.push([1, 4, , 5]);
|
|
146
|
+
return [4 /*yield*/, this.getClientSession()];
|
|
147
|
+
case 2:
|
|
148
|
+
accessTokenData = _b.sent();
|
|
149
|
+
if (!this.sdkConfig.sessionValidation(accessTokenData === null || accessTokenData === void 0 ? void 0 : accessTokenData.accessToken, accessTokenData === null || accessTokenData === void 0 ? void 0 : accessTokenData.accessTokenExpiry)) {
|
|
150
|
+
throw new Error('Token Validation FailedLoad(Web SDK)');
|
|
151
|
+
}
|
|
152
|
+
this.iframeUrl = (0, iframe_url_utils_1.resolveIframeUrl)(componentName, this.sdkConfig);
|
|
153
|
+
// Create and load the iframe in the container
|
|
154
|
+
console.log('\nIframe URL(Web SDK): ', this.iframeUrl, '\n');
|
|
155
|
+
return [4 /*yield*/, this.createIframeWithSource(this.iframeUrl, componentName)];
|
|
156
|
+
case 3:
|
|
157
|
+
iframe = _b.sent();
|
|
158
|
+
this.currentIframe = iframe;
|
|
159
|
+
this.updateIframeToken(accessTokenData.accessToken);
|
|
160
|
+
return [2 /*return*/, iframe];
|
|
161
|
+
case 4:
|
|
162
|
+
error_2 = _b.sent();
|
|
163
|
+
this.destroyIframe();
|
|
164
|
+
throw error_2;
|
|
165
|
+
case 5: return [2 /*return*/];
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}); };
|
|
169
|
+
/**
|
|
170
|
+
* Destroys the current iframe if it exists, removing it from the DOM.
|
|
171
|
+
* Also clears the session check interval associated with the iframe.
|
|
172
|
+
*/
|
|
173
|
+
this.destroyIframe = function () {
|
|
174
|
+
if (_this.currentIframe) {
|
|
175
|
+
_this.currentIframe.remove();
|
|
176
|
+
_this.currentIframe = null;
|
|
177
|
+
}
|
|
178
|
+
_this.stopSessionExpiryCheck();
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* Fully destroys the SDK instance's mounted iframe and browser listeners.
|
|
182
|
+
*/
|
|
183
|
+
this.destroy = function () {
|
|
184
|
+
_this.isDestroyed = true;
|
|
185
|
+
_this.destroyIframe();
|
|
186
|
+
_this.messageQueue = [];
|
|
187
|
+
_this.isIframeReady = false;
|
|
188
|
+
window.removeEventListener('message', _this.handleIframeMessage);
|
|
189
|
+
_this.isListeningToIframeEvents = false;
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* Logout of the WebSDK.
|
|
193
|
+
*
|
|
194
|
+
* Logs out the current session, revoking the access token. If the logout is
|
|
195
|
+
* successful, the SDK will destroy the iframe.
|
|
196
|
+
*
|
|
197
|
+
* @throws {Error} Throws an error if something goes wrong during logout.
|
|
198
|
+
*/
|
|
199
|
+
this.logout = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
200
|
+
var logoutRes;
|
|
201
|
+
return __generator(this, function (_b) {
|
|
202
|
+
switch (_b.label) {
|
|
203
|
+
case 0: return [4 /*yield*/, this.authProvider.destroySession()];
|
|
204
|
+
case 1:
|
|
205
|
+
logoutRes = _b.sent();
|
|
206
|
+
if (logoutRes) {
|
|
207
|
+
this.stopSessionExpiryCheck();
|
|
208
|
+
this.destroyIframe();
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
console.error('Logout failed(Web SDK)');
|
|
212
|
+
}
|
|
213
|
+
return [2 /*return*/];
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
}); };
|
|
217
|
+
/**
|
|
218
|
+
* Sends a message to the iframe to navigate to the given path.
|
|
219
|
+
* @param {string} path - The path to navigate to.
|
|
220
|
+
*/
|
|
221
|
+
this.updateIframeNavigation = function (path) {
|
|
222
|
+
_this.enqueueMessage('NAVIGATE', path);
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* Sends custom data to the iframe.
|
|
226
|
+
* @param {any} data - The custom data to be sent to the iframe.
|
|
227
|
+
* @example sendCustomDataToIframe({ name: 'John Doe', age: 30 });
|
|
228
|
+
*/
|
|
229
|
+
this.sendCustomDataToIframe = function (data) {
|
|
230
|
+
_this.enqueueMessage('CUSTOM_DATA', data);
|
|
231
|
+
};
|
|
232
|
+
// =============================|| PRIVATE METHODS ||============================== //
|
|
233
|
+
/**
|
|
234
|
+
* Retrieves and validates the current client session.
|
|
235
|
+
*
|
|
236
|
+
* This asynchronous function fetches a new client session using
|
|
237
|
+
* `fetchClientSession`, validates the session's access token using
|
|
238
|
+
* `sessionValidation`, and updates the current session ID and
|
|
239
|
+
* token expiry time. If the session token is invalid, an error is thrown.
|
|
240
|
+
*
|
|
241
|
+
* @returns {Promise<string>} The current session ID if validation is successful.
|
|
242
|
+
* @throws {Error} If session validation fails or any error occurs during
|
|
243
|
+
* the fetching or validation process.
|
|
244
|
+
*/
|
|
245
|
+
this.getClientSession = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
246
|
+
var session, error_3;
|
|
247
|
+
var _b, _c, _d, _e;
|
|
248
|
+
return __generator(this, function (_f) {
|
|
249
|
+
switch (_f.label) {
|
|
250
|
+
case 0:
|
|
251
|
+
_f.trys.push([0, 4, , 5]);
|
|
252
|
+
if (!(this.cachedSession &&
|
|
253
|
+
((_b = this.sdkConfig) === null || _b === void 0 ? void 0 : _b.sessionValidation((_c = this.cachedSession) === null || _c === void 0 ? void 0 : _c.accessToken, (_d = this.cachedSession) === null || _d === void 0 ? void 0 : _d.accessTokenExpiry)))) return [3 /*break*/, 1];
|
|
254
|
+
return [2 /*return*/, this.cachedSession];
|
|
255
|
+
case 1: return [4 /*yield*/, this.authProvider.fetchClientSession()];
|
|
256
|
+
case 2:
|
|
257
|
+
session = _f.sent();
|
|
258
|
+
if (!session.isSuccess) {
|
|
259
|
+
throw new Error('Client Session Fetch Failed(Web SDK)');
|
|
260
|
+
}
|
|
261
|
+
if (!((_e = this.sdkConfig) === null || _e === void 0 ? void 0 : _e.sessionValidation(session.accessToken, session.accessTokenExpiry))) {
|
|
262
|
+
throw new Error('Client Session Validation Failed(Web SDK)');
|
|
263
|
+
}
|
|
264
|
+
this.cachedSession = session;
|
|
265
|
+
this.retryAttempts = 0;
|
|
266
|
+
return [2 /*return*/, session];
|
|
267
|
+
case 3: return [3 /*break*/, 5];
|
|
268
|
+
case 4:
|
|
269
|
+
error_3 = _f.sent();
|
|
270
|
+
throw error_3;
|
|
271
|
+
case 5: return [2 /*return*/];
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}); };
|
|
275
|
+
/**
|
|
276
|
+
* Refreshes the current client session.
|
|
277
|
+
*
|
|
278
|
+
* @returns {Promise<IClientRefreshSession>} The current session ID if validation is successful.
|
|
279
|
+
* @throws {Error} If session validation fails or any error occurs during
|
|
280
|
+
* the fetching or validation process.
|
|
281
|
+
*/
|
|
282
|
+
this.refreshedClientSession = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
283
|
+
var response, newSession, error_4;
|
|
284
|
+
var _b, _c;
|
|
285
|
+
return __generator(this, function (_d) {
|
|
286
|
+
switch (_d.label) {
|
|
287
|
+
case 0:
|
|
288
|
+
if (!((_b = this.cachedSession) === null || _b === void 0 ? void 0 : _b.refreshToken) || !this.cachedSession.accessToken) {
|
|
289
|
+
console.error('Invalid or missing refreshToken or accessToken in cashed session. Destroying iframe(Web SDK).');
|
|
290
|
+
this.destroyIframe();
|
|
291
|
+
throw new Error('No session found(Web SDK)');
|
|
292
|
+
}
|
|
293
|
+
_d.label = 1;
|
|
294
|
+
case 1:
|
|
295
|
+
_d.trys.push([1, 3, , 4]);
|
|
296
|
+
return [4 /*yield*/, this.authProvider.refreshClientSession({
|
|
297
|
+
accessToken: this.cachedSession.accessToken,
|
|
298
|
+
refreshToken: this.cachedSession.refreshToken,
|
|
299
|
+
})];
|
|
300
|
+
case 2:
|
|
301
|
+
response = _d.sent();
|
|
302
|
+
if (!response.isSuccess) {
|
|
303
|
+
console.log('Invalid refresh session token(Called From Web SDK)');
|
|
304
|
+
throw new Error('Refresh Client Session Failed');
|
|
305
|
+
}
|
|
306
|
+
if (!((_c = this.sdkConfig) === null || _c === void 0 ? void 0 : _c.sessionValidation(response.accessToken, response.accessTokenExpiry))) {
|
|
307
|
+
console.log('Invalid refresh session token(Called From Web SDK)');
|
|
308
|
+
throw new Error('Refresh Client Session Validation Failed');
|
|
309
|
+
}
|
|
310
|
+
newSession = __assign({}, this.cachedSession);
|
|
311
|
+
newSession.accessToken = response.accessToken;
|
|
312
|
+
newSession.accessTokenExpiry = response.accessTokenExpiry;
|
|
313
|
+
this.cachedSession = newSession;
|
|
314
|
+
this.retryAttempts = 0;
|
|
315
|
+
return [2 /*return*/, response];
|
|
316
|
+
case 3:
|
|
317
|
+
error_4 = _d.sent();
|
|
318
|
+
console.error('Session Refresh Failed(Web SDK):', error_4);
|
|
319
|
+
throw error_4;
|
|
320
|
+
case 4: return [2 /*return*/];
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
}); };
|
|
324
|
+
/**
|
|
325
|
+
* Creates an iframe with the given URL and appends it to the container with the given ID.
|
|
326
|
+
* If an iframe with the same ID already exists, it is reused.
|
|
327
|
+
*
|
|
328
|
+
* @param {string} url The URL to load in the iframe
|
|
329
|
+
* @returns {Promise<HTMLIFrameElement>} A promise that resolves to the created iframe element
|
|
330
|
+
* @throws {Error} If the container element is not found or the iframe fails to load
|
|
331
|
+
*/
|
|
332
|
+
this.createIframeWithSource = function (url, componentName) {
|
|
333
|
+
return new Promise(function (resolve, reject) {
|
|
334
|
+
var container = _this.getContainerElement();
|
|
335
|
+
if (!container) {
|
|
336
|
+
return reject(new Error('SDK container not found(Web SDK)'));
|
|
337
|
+
}
|
|
338
|
+
var iframeId = _this.getIframeId(componentName);
|
|
339
|
+
// Check if an iframe with the same ID already exists
|
|
340
|
+
var iframe = document.getElementById(iframeId);
|
|
341
|
+
if (iframe) {
|
|
342
|
+
console.log("Iframe with Id: ".concat(iframeId, " already exists, reusing it(Web SDK)"));
|
|
343
|
+
resolve(iframe);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
// If iframe doesn't exist, create it
|
|
347
|
+
iframe = document.createElement('iframe');
|
|
348
|
+
iframe.src = url;
|
|
349
|
+
iframe.id = iframeId;
|
|
350
|
+
// Apply styles
|
|
351
|
+
var iframeStyle = __assign({}, _this.sdkConfig.iframeStyle);
|
|
352
|
+
if (iframeStyle) {
|
|
353
|
+
Object.assign(iframe.style, iframeStyle);
|
|
354
|
+
}
|
|
355
|
+
// Set iframe permissions
|
|
356
|
+
iframe.setAttribute('allow', 'clipboard-write *');
|
|
357
|
+
// Event handlers
|
|
358
|
+
iframe.onload = function () {
|
|
359
|
+
console.log('Iframe loaded successfully(Web SDK)');
|
|
360
|
+
// Start session expiry check when iframe loads
|
|
361
|
+
_this.startSessionExpiryCheck();
|
|
362
|
+
resolve(iframe);
|
|
363
|
+
};
|
|
364
|
+
iframe.onerror = function () {
|
|
365
|
+
reject(new Error('Iframe failed to load(Web SDK)'));
|
|
366
|
+
};
|
|
367
|
+
container.appendChild(iframe);
|
|
368
|
+
});
|
|
369
|
+
};
|
|
370
|
+
this.getContainerElement = function () {
|
|
371
|
+
if (_this.container instanceof HTMLElement) {
|
|
372
|
+
return _this.container;
|
|
373
|
+
}
|
|
374
|
+
if (typeof _this.container === 'string') {
|
|
375
|
+
return document.querySelector(_this.container) || document.getElementById(_this.container);
|
|
376
|
+
}
|
|
377
|
+
return document.getElementById(_this.sdkConfig.containerId);
|
|
378
|
+
};
|
|
379
|
+
this.getIframeId = function (componentName) {
|
|
380
|
+
var componentKey = String(componentName).toLowerCase().replace(/_/g, '-');
|
|
381
|
+
return "".concat(_this.sdkConfig.iframeId, "-").concat(componentKey);
|
|
382
|
+
};
|
|
383
|
+
/**
|
|
384
|
+
* Schedules a session expiry check in the future based on the provided
|
|
385
|
+
* {@link tokenExpiryTime}. If the token has already expired, the check
|
|
386
|
+
* is retried up to {@link MAX_RETRIES} times. If all retries fail, the
|
|
387
|
+
* session check is stopped and the iframe is destroyed.
|
|
388
|
+
*
|
|
389
|
+
* @throws {Error} If the session check fails with an error, the iframe is
|
|
390
|
+
* destroyed as a fallback.
|
|
391
|
+
*/
|
|
392
|
+
this.startSessionExpiryCheck = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
393
|
+
var isRefreshExpired, now, expiryRaw, expiryTime, delay, performRefresh;
|
|
394
|
+
var _this = this;
|
|
395
|
+
var _b, _c;
|
|
396
|
+
return __generator(this, function (_d) {
|
|
397
|
+
isRefreshExpired = (0, date_utils_1.isDateExpired)((_b = this.cachedSession) === null || _b === void 0 ? void 0 : _b.refreshTokenExpiry);
|
|
398
|
+
if (isRefreshExpired) {
|
|
399
|
+
console.error('Refresh token is expired. Stopping session expiry check(Web SDK).');
|
|
400
|
+
this.destroyIframe();
|
|
401
|
+
return [2 /*return*/];
|
|
402
|
+
}
|
|
403
|
+
now = new Date();
|
|
404
|
+
expiryRaw = (_c = this.cachedSession) === null || _c === void 0 ? void 0 : _c.accessTokenExpiry;
|
|
405
|
+
expiryTime = expiryRaw ? new Date(expiryRaw) : null;
|
|
406
|
+
if (!expiryTime || isNaN(expiryTime.getTime())) {
|
|
407
|
+
console.error('Invalid or missing accessTokenExpiry in session. Stopping session expiry check(Web SDK).');
|
|
408
|
+
this.destroyIframe();
|
|
409
|
+
return [2 /*return*/];
|
|
410
|
+
}
|
|
411
|
+
delay = expiryTime.getTime() - now.getTime() - this.REFRESH_BUFFER_MS;
|
|
412
|
+
performRefresh = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
413
|
+
var refreshSession, err_1;
|
|
414
|
+
return __generator(this, function (_b) {
|
|
415
|
+
switch (_b.label) {
|
|
416
|
+
case 0:
|
|
417
|
+
_b.trys.push([0, 2, , 3]);
|
|
418
|
+
if (this.retryAttempts >= this.MAX_RETRIES) {
|
|
419
|
+
console.warn("Max retry attempts (".concat(this.MAX_RETRIES, ") reached. Stopping session check(Web SDK)."));
|
|
420
|
+
this.stopSessionExpiryCheck();
|
|
421
|
+
this.destroyIframe();
|
|
422
|
+
return [2 /*return*/];
|
|
423
|
+
}
|
|
424
|
+
return [4 /*yield*/, this.refreshedClientSession()];
|
|
425
|
+
case 1:
|
|
426
|
+
refreshSession = _b.sent();
|
|
427
|
+
this.updateIframeToken(refreshSession.accessToken);
|
|
428
|
+
// Success! Reset retries and start waiting for the next expiry
|
|
429
|
+
this.retryAttempts = 0;
|
|
430
|
+
this.startSessionExpiryCheck();
|
|
431
|
+
return [3 /*break*/, 3];
|
|
432
|
+
case 2:
|
|
433
|
+
err_1 = _b.sent();
|
|
434
|
+
console.error('Failed to perform session refresh(Web SDK):', err_1);
|
|
435
|
+
this.retryAttempts++;
|
|
436
|
+
this.stopSessionExpiryCheck();
|
|
437
|
+
this.expiryTimeout = setTimeout(performRefresh, 5000);
|
|
438
|
+
return [3 /*break*/, 3];
|
|
439
|
+
case 3: return [2 /*return*/];
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
}); };
|
|
443
|
+
// Ensure we don't accidentally create overlapping timeouts
|
|
444
|
+
this.stopSessionExpiryCheck();
|
|
445
|
+
if (delay <= 0) {
|
|
446
|
+
// It's already expired (or within the buffer window), so refresh immediately!
|
|
447
|
+
performRefresh();
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
this.expiryTimeout = setTimeout(performRefresh, delay);
|
|
451
|
+
}
|
|
452
|
+
return [2 /*return*/];
|
|
453
|
+
});
|
|
454
|
+
}); };
|
|
455
|
+
/**
|
|
456
|
+
* Stops the session expiry check scheduled by {@link startSessionExpiryCheck}.
|
|
457
|
+
* This is useful when the iframe is destroyed or the session is manually
|
|
458
|
+
* invalidated.
|
|
459
|
+
*/
|
|
460
|
+
this.stopSessionExpiryCheck = function () {
|
|
461
|
+
if (_this.expiryTimeout) {
|
|
462
|
+
clearTimeout(_this.expiryTimeout);
|
|
463
|
+
_this.expiryTimeout = null;
|
|
464
|
+
}
|
|
465
|
+
// Also cleanup interval if it still exists anywhere
|
|
466
|
+
if (_this.sessionCheckInterval) {
|
|
467
|
+
clearInterval(_this.sessionCheckInterval);
|
|
468
|
+
_this.sessionCheckInterval = null;
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
/**
|
|
472
|
+
* Sends a message to the microfrontend running in the iframe to update the session token.
|
|
473
|
+
* @param {string} data - The new session token to be used by the microfrontend.
|
|
474
|
+
*/
|
|
475
|
+
this.updateIframeToken = function (data) {
|
|
476
|
+
_this.enqueueMessage('UPDATE_TOKEN', data);
|
|
477
|
+
};
|
|
478
|
+
/**
|
|
479
|
+
* Enqueues a message to be sent to the microfrontend running in the iframe.
|
|
480
|
+
* @param {PostMessageActionType} action - The action to be performed by the microfrontend.
|
|
481
|
+
* @param {any} data - The data to be sent to the microfrontend.
|
|
482
|
+
* @example enqueueMessage('UPDATE_TOKEN', 'my-access-token');
|
|
483
|
+
*/
|
|
484
|
+
this.enqueueMessage = function (action, data) {
|
|
485
|
+
var payload = {
|
|
486
|
+
action: action,
|
|
487
|
+
source: 'NUVEI_WEB_SDK',
|
|
488
|
+
data: data,
|
|
489
|
+
};
|
|
490
|
+
if (!_this.isIframeReady) {
|
|
491
|
+
console.log('Iframe not ready, queueing message(Web SDK):', payload);
|
|
492
|
+
_this.messageQueue.push(payload);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
_this.postMessageToIframe(payload);
|
|
496
|
+
};
|
|
497
|
+
/**
|
|
498
|
+
* Flushes the message queue to send all queued messages to the microfrontend.
|
|
499
|
+
* This is called when the iframe is ready to receive messages.
|
|
500
|
+
* @example flushMessageQueue();
|
|
501
|
+
*/
|
|
502
|
+
this.flushMessageQueue = function () {
|
|
503
|
+
console.log('Flushing message queue(Web SDK):', _this.messageQueue);
|
|
504
|
+
while (_this.messageQueue.length > 0) {
|
|
505
|
+
var msg = _this.messageQueue.shift();
|
|
506
|
+
if (msg)
|
|
507
|
+
_this.postMessageToIframe(msg);
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
/**
|
|
511
|
+
* Listens to messages from the microfrontend running in the iframe.
|
|
512
|
+
* @example listenToIframeEvents();
|
|
513
|
+
*/
|
|
514
|
+
this.listenToIframeEvents = function () {
|
|
515
|
+
if (_this.isListeningToIframeEvents)
|
|
516
|
+
return;
|
|
517
|
+
window.addEventListener('message', _this.handleIframeMessage);
|
|
518
|
+
_this.isListeningToIframeEvents = true;
|
|
519
|
+
};
|
|
520
|
+
this.handleIframeMessage = function (event) {
|
|
521
|
+
if (!_this.iframeUrl)
|
|
522
|
+
return;
|
|
523
|
+
var expectedOrigin = new URL(_this.iframeUrl).origin;
|
|
524
|
+
if (event.origin !== expectedOrigin)
|
|
525
|
+
return;
|
|
526
|
+
var data = event.data;
|
|
527
|
+
if ((data === null || data === void 0 ? void 0 : data.source) !== 'NUVEI_IFRAME')
|
|
528
|
+
return;
|
|
529
|
+
if (_this.isDestroyed)
|
|
530
|
+
return;
|
|
531
|
+
console.log('Received from iframe:(Web SDK)', data);
|
|
532
|
+
if (_this.onMessage) {
|
|
533
|
+
_this.onMessage(data);
|
|
534
|
+
}
|
|
535
|
+
switch (data.action) {
|
|
536
|
+
case enums_1.AcknowledgeMessageActionEnum.IFRAME_READY:
|
|
537
|
+
_this.handleIframeReady();
|
|
538
|
+
break;
|
|
539
|
+
default:
|
|
540
|
+
break;
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
/**
|
|
544
|
+
* Handles the iframe ready event.
|
|
545
|
+
* @example handleIframeReady();
|
|
546
|
+
*/
|
|
547
|
+
this.handleIframeReady = function () {
|
|
548
|
+
console.log('Iframe is READY(Web SDK)');
|
|
549
|
+
_this.isIframeReady = true;
|
|
550
|
+
_this.flushMessageQueue();
|
|
551
|
+
};
|
|
552
|
+
/**
|
|
553
|
+
* Sends a post message to the microfrontend running in the iframe.
|
|
554
|
+
* This will fail silently if the iframe does not exist or does not have a contentWindow.
|
|
555
|
+
* @param {ISdkPostMessagePayload} payload - The payload to be sent as a post message.
|
|
556
|
+
* @example postMessageToIframe({ action: 'NAVIGATE', source: 'NUVEI_WEB_SDK', data: '/dashboard' });
|
|
557
|
+
*/
|
|
558
|
+
this.postMessageToIframe = function (payload) {
|
|
559
|
+
var _b;
|
|
560
|
+
if (!((_b = _this === null || _this === void 0 ? void 0 : _this.currentIframe) === null || _b === void 0 ? void 0 : _b.contentWindow)) {
|
|
561
|
+
console.warn('Iframe not available(Web SDK)');
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
var origin = new URL(_this.iframeUrl).origin;
|
|
565
|
+
console.log('Sending message to microfrontend(Web SDK):', payload);
|
|
566
|
+
_this.currentIframe.contentWindow.postMessage(payload, origin);
|
|
567
|
+
};
|
|
568
|
+
var fetchClientSession = params.fetchClientSession, refreshClientSession = params.refreshClientSession, destroySession = params.destroySession, componentName = params.componentName, container = params.container, authConfig = params.authConfig, config = params.config, onReady = params.onReady, onError = params.onError, onMessage = params.onMessage;
|
|
569
|
+
if (authConfig) {
|
|
570
|
+
var defaultAuthProvider = new default_auth_provider_1.default(authConfig, componentName);
|
|
571
|
+
this.authProvider = {
|
|
572
|
+
fetchClientSession: fetchClientSession !== null && fetchClientSession !== void 0 ? fetchClientSession : defaultAuthProvider.fetchClientSession,
|
|
573
|
+
refreshClientSession: refreshClientSession !== null && refreshClientSession !== void 0 ? refreshClientSession : defaultAuthProvider.refreshClientSession,
|
|
574
|
+
destroySession: destroySession !== null && destroySession !== void 0 ? destroySession : defaultAuthProvider.destroySession,
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
else if (fetchClientSession && refreshClientSession && destroySession) {
|
|
578
|
+
this.authProvider = {
|
|
579
|
+
fetchClientSession: fetchClientSession,
|
|
580
|
+
refreshClientSession: refreshClientSession,
|
|
581
|
+
destroySession: destroySession,
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
throw new Error('authConfig is required when auth callbacks are not fully provided(Web SDK)');
|
|
586
|
+
}
|
|
587
|
+
this.componentName = componentName;
|
|
588
|
+
this.container = container;
|
|
589
|
+
// Merged configuration
|
|
590
|
+
if (config) {
|
|
591
|
+
this.sdkConfig = (0, object_utils_1.deepMerge)(default_config_1.DEFAULT_CONFIG, config);
|
|
592
|
+
}
|
|
593
|
+
if (onReady) {
|
|
594
|
+
this.onReady = onReady;
|
|
595
|
+
}
|
|
596
|
+
if (onError) {
|
|
597
|
+
this.onError = onError;
|
|
598
|
+
}
|
|
599
|
+
if (onMessage) {
|
|
600
|
+
this.onMessage = onMessage;
|
|
601
|
+
}
|
|
602
|
+
this.listenToIframeEvents();
|
|
603
|
+
}
|
|
604
|
+
var _a;
|
|
605
|
+
_a = NuveiSDK;
|
|
606
|
+
// =============================|| PUBLIC METHODS ||============================== //
|
|
607
|
+
/**
|
|
608
|
+
* Creates and initializes an SDK instance in one call.
|
|
609
|
+
*/
|
|
610
|
+
NuveiSDK.mount = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
611
|
+
var instance;
|
|
612
|
+
return __generator(_a, function (_b) {
|
|
613
|
+
switch (_b.label) {
|
|
614
|
+
case 0:
|
|
615
|
+
instance = new _a(params);
|
|
616
|
+
return [4 /*yield*/, instance.initialize()];
|
|
617
|
+
case 1:
|
|
618
|
+
_b.sent();
|
|
619
|
+
return [2 /*return*/, instance];
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
}); };
|
|
623
|
+
/**
|
|
624
|
+
* Alias for mount, useful for integrations that prefer create-style naming.
|
|
625
|
+
*/
|
|
626
|
+
NuveiSDK.create = _a.mount;
|
|
627
|
+
return NuveiSDK;
|
|
628
|
+
}());
|
|
629
|
+
exports.default = NuveiSDK;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import NuveiSDK from "./core/index";
|
|
2
|
+
import { ModulesNameEnum } from "./common/enums/index";
|
|
3
|
+
import { IAuthConfig, IAuthProvider, INuveiInstance, INuveiInitParams, INuveiSdkConfig } from "./interfaces/index";
|
|
4
|
+
export default NuveiSDK;
|
|
5
|
+
export { ModulesNameEnum, IAuthConfig, IAuthProvider, INuveiInstance, INuveiSdkConfig, INuveiInitParams };
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,cAAc,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGlH,eAAe,QAAQ,CAAC;AAExB,OAAO,EACL,eAAe,EACf,WAAW,EACX,aAAa,EACb,cAAc,EACd,eAAe,EACf,gBAAgB,EACjB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ModulesNameEnum = void 0;
|
|
7
|
+
var index_1 = __importDefault(require("./core/index"));
|
|
8
|
+
var index_2 = require("./common/enums/index");
|
|
9
|
+
Object.defineProperty(exports, "ModulesNameEnum", { enumerable: true, get: function () { return index_2.ModulesNameEnum; } });
|
|
10
|
+
exports.default = index_1.default;
|