@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.
Files changed (41) hide show
  1. package/README.md +62 -0
  2. package/dist/cdn-index.d.ts +2 -0
  3. package/dist/cdn-index.d.ts.map +1 -0
  4. package/dist/cdn-index.js +8 -0
  5. package/dist/common/enums/index.d.ts +25 -0
  6. package/dist/common/enums/index.d.ts.map +1 -0
  7. package/dist/common/enums/index.js +29 -0
  8. package/dist/config/default-config.d.ts +3 -0
  9. package/dist/config/default-config.d.ts.map +1 -0
  10. package/dist/config/default-config.js +25 -0
  11. package/dist/core/index.d.ts +178 -0
  12. package/dist/core/index.d.ts.map +1 -0
  13. package/dist/core/index.js +629 -0
  14. package/dist/index.d.ts +6 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +10 -0
  17. package/dist/interfaces/index.d.ts +77 -0
  18. package/dist/interfaces/index.d.ts.map +1 -0
  19. package/dist/interfaces/index.js +2 -0
  20. package/dist/interfaces/post-message.interfaces.d.ts +17 -0
  21. package/dist/interfaces/post-message.interfaces.d.ts.map +1 -0
  22. package/dist/interfaces/post-message.interfaces.js +2 -0
  23. package/dist/interfaces/utils.interfaces.d.ts +4 -0
  24. package/dist/interfaces/utils.interfaces.d.ts.map +1 -0
  25. package/dist/interfaces/utils.interfaces.js +3 -0
  26. package/dist/services/default-auth-provider.d.ts +17 -0
  27. package/dist/services/default-auth-provider.d.ts.map +1 -0
  28. package/dist/services/default-auth-provider.js +225 -0
  29. package/dist/utils/common.utils.d.ts +27 -0
  30. package/dist/utils/common.utils.d.ts.map +1 -0
  31. package/dist/utils/common.utils.js +102 -0
  32. package/dist/utils/date.utils.d.ts +2 -0
  33. package/dist/utils/date.utils.d.ts.map +1 -0
  34. package/dist/utils/date.utils.js +29 -0
  35. package/dist/utils/iframe-url.utils.d.ts +6 -0
  36. package/dist/utils/iframe-url.utils.d.ts.map +1 -0
  37. package/dist/utils/iframe-url.utils.js +37 -0
  38. package/dist/utils/object.utils.d.ts +56 -0
  39. package/dist/utils/object.utils.d.ts.map +1 -0
  40. package/dist/utils/object.utils.js +214 -0
  41. package/package.json +44 -0
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # 🧩 WebSDK
2
+
3
+ The **Nuvei WebSDK** allows third-party applications to easily embed Micro Frontends into their applications.
4
+
5
+ ## 📦 Installation
6
+
7
+ Install the SDK from npm:
8
+
9
+ ```bash
10
+ npm install nc-web-sdk
11
+ ```
12
+
13
+ ## 🚀 Usage
14
+
15
+ Import and mount the SDK component:
16
+
17
+ ```typescript
18
+ import { NuveiSDK } from "nc-web-sdk";
19
+
20
+ const instance = await NuveiSDK.mount({
21
+ componentName: moduleName,
22
+ container: containerRef.current,
23
+
24
+ config: {
25
+ environment: "preprod",
26
+ },
27
+ });
28
+ ```
29
+
30
+ ### 🔐 Authentication
31
+
32
+ By default, the SDK **handles authentication automatically**. No authentication configuration is required from the consuming application.
33
+
34
+ If the consuming application wants to manage authentication itself, it can provide authentication callbacks. These callbacks **override the SDK's default authentication behavior**.
35
+
36
+ ```typescript
37
+ const instance = await NuveiSDK.mount({
38
+ componentName: moduleName,
39
+ container: containerRef.current,
40
+
41
+ config: {
42
+ environment: "preprod",
43
+ },
44
+
45
+ fetchClientSession: getSessionId,
46
+ refreshClientSession: refreshSessionId,
47
+ destroySession: handleSessionDestroy,
48
+ });
49
+ ```
50
+
51
+ ### ⚙️ Configuration
52
+
53
+ | Property | Required | Description |
54
+ | ---------------------- | -------- | --------------------------------------------------- |
55
+ | `componentName` | Yes | Micro Frontend component to load |
56
+ | `container` | Yes | DOM element where the component will be mounted |
57
+ | `config.environment` | Yes | SDK environment (`local`, `dev`, `preprod`, `prod`) |
58
+ | `fetchClientSession` | No | Custom authentication/session callback |
59
+ | `refreshClientSession` | No | Custom session refresh callback |
60
+ | `destroySession` | No | Custom session destroy/logout callback |
61
+
62
+ > **Note:** Authentication callbacks are optional. If they are not provided, the SDK uses its built-in authentication flow.
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=cdn-index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cdn-index.d.ts","sourceRoot":"","sources":["../src/cdn-index.ts"],"names":[],"mappings":""}
@@ -0,0 +1,8 @@
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
+ var index_1 = __importDefault(require("./core/index"));
7
+ // Attach to window/globalThis
8
+ window.NCWebSDK = index_1.default;
@@ -0,0 +1,25 @@
1
+ export declare enum ModulesNameEnum {
2
+ ONBOARDING_PARTNER = "ONBOARDING_PARTNER",
3
+ ONBOARDING_MERCHANT = "ONBOARDING_MERCHANT",
4
+ DOCUTILITY = "DOCUTILITY"
5
+ }
6
+ export type ModulesNameType = keyof typeof ModulesNameEnum;
7
+ export declare enum PostMessageActionEnum {
8
+ UPDATE_TOKEN = "UPDATE_TOKEN",
9
+ NAVIGATE = "NAVIGATE",
10
+ CUSTOM_DATA = "CUSTOM_DATA"
11
+ }
12
+ export type PostMessageActionType = keyof typeof PostMessageActionEnum;
13
+ export declare enum AcknowledgeMessageActionEnum {
14
+ IFRAME_READY = "IFRAME_READY"
15
+ }
16
+ export type AcknowledgeMessageActionType = keyof typeof AcknowledgeMessageActionEnum;
17
+ export declare enum EnvironmentEnum {
18
+ LOCAL = "local",
19
+ DEV = "dev",
20
+ PREPROD = "preprod",
21
+ UAT = "uat",
22
+ PROD = "prod"
23
+ }
24
+ export type EnvironmentType = keyof typeof EnvironmentEnum;
25
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/common/enums/index.ts"],"names":[],"mappings":"AACA,oBAAY,eAAe;IACzB,kBAAkB,uBAAuB;IACzC,mBAAmB,wBAAwB;IAC3C,UAAU,eAAe;CAC1B;AACD,MAAM,MAAM,eAAe,GAAG,MAAM,OAAO,eAAe,CAAC;AAG3D,oBAAY,qBAAqB;IAC/B,YAAY,iBAAiB;IAC7B,QAAQ,aAAa;IACrB,WAAW,gBAAgB;CAC5B;AACD,MAAM,MAAM,qBAAqB,GAAG,MAAM,OAAO,qBAAqB,CAAC;AAGvE,oBAAY,4BAA4B;IACtC,YAAY,iBAAiB;CAC9B;AACD,MAAM,MAAM,4BAA4B,GAAG,MAAM,OAAO,4BAA4B,CAAC;AAErF,oBAAY,eAAe;IACzB,KAAK,UAAU;IACf,GAAG,QAAQ;IACX,OAAO,YAAY;IACnB,GAAG,QAAQ;IACX,IAAI,SAAS;CACd;AACD,MAAM,MAAM,eAAe,GAAG,MAAM,OAAO,eAAe,CAAC"}
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EnvironmentEnum = exports.AcknowledgeMessageActionEnum = exports.PostMessageActionEnum = exports.ModulesNameEnum = void 0;
4
+ // Define the Enum for component names
5
+ var ModulesNameEnum;
6
+ (function (ModulesNameEnum) {
7
+ ModulesNameEnum["ONBOARDING_PARTNER"] = "ONBOARDING_PARTNER";
8
+ ModulesNameEnum["ONBOARDING_MERCHANT"] = "ONBOARDING_MERCHANT";
9
+ ModulesNameEnum["DOCUTILITY"] = "DOCUTILITY";
10
+ })(ModulesNameEnum || (exports.ModulesNameEnum = ModulesNameEnum = {}));
11
+ ;
12
+ var PostMessageActionEnum;
13
+ (function (PostMessageActionEnum) {
14
+ PostMessageActionEnum["UPDATE_TOKEN"] = "UPDATE_TOKEN";
15
+ PostMessageActionEnum["NAVIGATE"] = "NAVIGATE";
16
+ PostMessageActionEnum["CUSTOM_DATA"] = "CUSTOM_DATA";
17
+ })(PostMessageActionEnum || (exports.PostMessageActionEnum = PostMessageActionEnum = {}));
18
+ var AcknowledgeMessageActionEnum;
19
+ (function (AcknowledgeMessageActionEnum) {
20
+ AcknowledgeMessageActionEnum["IFRAME_READY"] = "IFRAME_READY";
21
+ })(AcknowledgeMessageActionEnum || (exports.AcknowledgeMessageActionEnum = AcknowledgeMessageActionEnum = {}));
22
+ var EnvironmentEnum;
23
+ (function (EnvironmentEnum) {
24
+ EnvironmentEnum["LOCAL"] = "local";
25
+ EnvironmentEnum["DEV"] = "dev";
26
+ EnvironmentEnum["PREPROD"] = "preprod";
27
+ EnvironmentEnum["UAT"] = "uat";
28
+ EnvironmentEnum["PROD"] = "prod";
29
+ })(EnvironmentEnum || (exports.EnvironmentEnum = EnvironmentEnum = {}));
@@ -0,0 +1,3 @@
1
+ import { INuveiSdkConfig } from "../interfaces/index";
2
+ export declare const DEFAULT_CONFIG: INuveiSdkConfig;
3
+ //# sourceMappingURL=default-config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default-config.d.ts","sourceRoot":"","sources":["../../src/config/default-config.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGpD,eAAO,MAAM,cAAc,EAAE,eAmB5B,CAAC"}
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_CONFIG = void 0;
4
+ var enums_1 = require("../common/enums");
5
+ // Configuration Constants
6
+ exports.DEFAULT_CONFIG = {
7
+ iframeId: 'nc-web-sdk-iframe',
8
+ containerId: 'websdk-container',
9
+ iframeStyle: {
10
+ width: '100%',
11
+ height: '100vh',
12
+ border: 'none',
13
+ display: 'block'
14
+ },
15
+ sessionValidation: function (token, tokenExpiry) {
16
+ if (!token || !tokenExpiry) {
17
+ return false;
18
+ }
19
+ var expiryDate = new Date(tokenExpiry);
20
+ var currentDate = new Date();
21
+ return expiryDate > currentDate;
22
+ },
23
+ sessionCheckInterval: 10 * 60 * 1000, // 5 minutes
24
+ environment: enums_1.EnvironmentEnum.UAT,
25
+ };
@@ -0,0 +1,178 @@
1
+ import { ModulesNameEnum } from '../common/enums';
2
+ import { HTMLIFrameElement, INuveiInitParams } from '../interfaces';
3
+ export default class NuveiSDK {
4
+ private sdkConfig;
5
+ private currentIframe;
6
+ private iframeUrl;
7
+ private isIframeReady;
8
+ private messageQueue;
9
+ private cachedSession;
10
+ private sessionCheckInterval;
11
+ private expiryTimeout;
12
+ private retryAttempts;
13
+ private readonly MAX_RETRIES;
14
+ private REFRESH_BUFFER_MS;
15
+ private authProvider;
16
+ private componentName?;
17
+ private container?;
18
+ private isListeningToIframeEvents;
19
+ private isDestroyed;
20
+ /**
21
+ * Initializes the Nuvei SDK instance
22
+ * @param {INuveiInitParams} params - Initialization parameters
23
+ * @param {() => Promise<IClientSession>} params.fetchClientSession - Function to fetch the client session
24
+ * @param {() => Promise<IClientRefreshSession>} params.refreshClientSession - Function to refresh the client session
25
+ * @param {() => Promise<boolean>} params.destroySession - Function to destroy the client session
26
+ * @param {INuveiSdkConfig} [params.config] - Optional configuration overrides
27
+ */
28
+ constructor(params: INuveiInitParams);
29
+ private onReady?;
30
+ private onError?;
31
+ private onMessage?;
32
+ /**
33
+ * Creates and initializes an SDK instance in one call.
34
+ */
35
+ static readonly mount: (params: INuveiInitParams) => Promise<NuveiSDK>;
36
+ /**
37
+ * Alias for mount, useful for integrations that prefer create-style naming.
38
+ */
39
+ static readonly create: (params: INuveiInitParams) => Promise<NuveiSDK>;
40
+ /**
41
+ * Initializes the configured component.
42
+ *
43
+ * This is a convenience wrapper for integrations that only mount one
44
+ * microfrontend and do not need to call loadComponent manually.
45
+ */
46
+ initialize: () => Promise<HTMLIFrameElement | void>;
47
+ /**
48
+ * Establishes a connection to a specified component by creating and loading an iframe.
49
+ *
50
+ * This asynchronous function initializes the WebSDK with a valid session, validates
51
+ * the session token, determines the appropriate iframe URL based on the given
52
+ * component name, and creates the iframe with the specified source URL.
53
+ *
54
+ * @param {string} componentName - The name of the component to connect to.
55
+ * Valid values are defined in the ComponentNameEnum.
56
+ *
57
+ * @returns {Promise<HTMLIFrameElement | void>} A promise that resolves to the created
58
+ * iframe element, or void if an error occurs.
59
+ *
60
+ * @throws {Error} Throws an error if the session token is invalid or if the
61
+ * specified component name is unknown.
62
+ */
63
+ loadComponent: (componentName: ModulesNameEnum) => Promise<HTMLIFrameElement | void>;
64
+ /**
65
+ * Destroys the current iframe if it exists, removing it from the DOM.
66
+ * Also clears the session check interval associated with the iframe.
67
+ */
68
+ destroyIframe: () => void;
69
+ /**
70
+ * Fully destroys the SDK instance's mounted iframe and browser listeners.
71
+ */
72
+ destroy: () => void;
73
+ /**
74
+ * Logout of the WebSDK.
75
+ *
76
+ * Logs out the current session, revoking the access token. If the logout is
77
+ * successful, the SDK will destroy the iframe.
78
+ *
79
+ * @throws {Error} Throws an error if something goes wrong during logout.
80
+ */
81
+ logout: () => Promise<void>;
82
+ /**
83
+ * Sends a message to the iframe to navigate to the given path.
84
+ * @param {string} path - The path to navigate to.
85
+ */
86
+ updateIframeNavigation: (path: string) => void;
87
+ /**
88
+ * Sends custom data to the iframe.
89
+ * @param {any} data - The custom data to be sent to the iframe.
90
+ * @example sendCustomDataToIframe({ name: 'John Doe', age: 30 });
91
+ */
92
+ sendCustomDataToIframe: (data: any) => void;
93
+ /**
94
+ * Retrieves and validates the current client session.
95
+ *
96
+ * This asynchronous function fetches a new client session using
97
+ * `fetchClientSession`, validates the session's access token using
98
+ * `sessionValidation`, and updates the current session ID and
99
+ * token expiry time. If the session token is invalid, an error is thrown.
100
+ *
101
+ * @returns {Promise<string>} The current session ID if validation is successful.
102
+ * @throws {Error} If session validation fails or any error occurs during
103
+ * the fetching or validation process.
104
+ */
105
+ private getClientSession;
106
+ /**
107
+ * Refreshes the current client session.
108
+ *
109
+ * @returns {Promise<IClientRefreshSession>} The current session ID if validation is successful.
110
+ * @throws {Error} If session validation fails or any error occurs during
111
+ * the fetching or validation process.
112
+ */
113
+ private refreshedClientSession;
114
+ /**
115
+ * Creates an iframe with the given URL and appends it to the container with the given ID.
116
+ * If an iframe with the same ID already exists, it is reused.
117
+ *
118
+ * @param {string} url The URL to load in the iframe
119
+ * @returns {Promise<HTMLIFrameElement>} A promise that resolves to the created iframe element
120
+ * @throws {Error} If the container element is not found or the iframe fails to load
121
+ */
122
+ private createIframeWithSource;
123
+ private getContainerElement;
124
+ private getIframeId;
125
+ /**
126
+ * Schedules a session expiry check in the future based on the provided
127
+ * {@link tokenExpiryTime}. If the token has already expired, the check
128
+ * is retried up to {@link MAX_RETRIES} times. If all retries fail, the
129
+ * session check is stopped and the iframe is destroyed.
130
+ *
131
+ * @throws {Error} If the session check fails with an error, the iframe is
132
+ * destroyed as a fallback.
133
+ */
134
+ private startSessionExpiryCheck;
135
+ /**
136
+ * Stops the session expiry check scheduled by {@link startSessionExpiryCheck}.
137
+ * This is useful when the iframe is destroyed or the session is manually
138
+ * invalidated.
139
+ */
140
+ private stopSessionExpiryCheck;
141
+ /**
142
+ * Sends a message to the microfrontend running in the iframe to update the session token.
143
+ * @param {string} data - The new session token to be used by the microfrontend.
144
+ */
145
+ private updateIframeToken;
146
+ /**
147
+ * Enqueues a message to be sent to the microfrontend running in the iframe.
148
+ * @param {PostMessageActionType} action - The action to be performed by the microfrontend.
149
+ * @param {any} data - The data to be sent to the microfrontend.
150
+ * @example enqueueMessage('UPDATE_TOKEN', 'my-access-token');
151
+ */
152
+ private enqueueMessage;
153
+ /**
154
+ * Flushes the message queue to send all queued messages to the microfrontend.
155
+ * This is called when the iframe is ready to receive messages.
156
+ * @example flushMessageQueue();
157
+ */
158
+ private flushMessageQueue;
159
+ /**
160
+ * Listens to messages from the microfrontend running in the iframe.
161
+ * @example listenToIframeEvents();
162
+ */
163
+ private listenToIframeEvents;
164
+ private handleIframeMessage;
165
+ /**
166
+ * Handles the iframe ready event.
167
+ * @example handleIframeReady();
168
+ */
169
+ private handleIframeReady;
170
+ /**
171
+ * Sends a post message to the microfrontend running in the iframe.
172
+ * This will fail silently if the iframe does not exist or does not have a contentWindow.
173
+ * @param {ISdkPostMessagePayload} payload - The payload to be sent as a post message.
174
+ * @example postMessageToIframe({ action: 'NAVIGATE', source: 'NUVEI_WEB_SDK', data: '/dashboard' });
175
+ */
176
+ private postMessageToIframe;
177
+ }
178
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,eAAe,EAGhB,MAAM,eAAe,CAAC;AAQvB,OAAO,EACL,iBAAiB,EAKjB,gBAAgB,EAEjB,MAAM,eAAe,CAAC;AAIvB,MAAM,CAAC,OAAO,OAAO,QAAQ;IAC3B,OAAO,CAAC,SAAS,CAA0C;IAE3D,OAAO,CAAC,aAAa,CAAkC;IACvD,OAAO,CAAC,SAAS,CAAc;IAC/B,OAAO,CAAC,aAAa,CAAkB;IACvC,OAAO,CAAC,YAAY,CAAgC;IAEpD,OAAO,CAAC,aAAa,CAA+B;IACpD,OAAO,CAAC,oBAAoB,CAA+C;IAC3E,OAAO,CAAC,aAAa,CAA8C;IACnE,OAAO,CAAC,aAAa,CAAK;IAC1B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAK;IACjC,OAAO,CAAC,iBAAiB,CAAa;IACtC,OAAO,CAAC,YAAY,CAAgB;IACpC,OAAO,CAAC,aAAa,CAAC,CAAkB;IACxC,OAAO,CAAC,SAAS,CAAC,CAAuB;IACzC,OAAO,CAAC,yBAAyB,CAAS;IAC1C,OAAO,CAAC,WAAW,CAAS;IAE5B;;;;;;;OAOG;gBACS,MAAM,EAAE,gBAAgB;IAwDpC,OAAO,CAAC,OAAO,CAAC,CAAqD;IACrE,OAAO,CAAC,OAAO,CAAC,CAA2B;IAC3C,OAAO,CAAC,SAAS,CAAC,CAAyB;IAI3C;;OAEG;IACH,gBAAuB,KAAK,GAAU,QAAQ,gBAAgB,KAAG,OAAO,CAAC,QAAQ,CAAC,CAIhF;IAEF;;OAEG;IACH,gBAAuB,MAAM,WATiB,gBAAgB,KAAG,OAAO,CAAC,QAAQ,CAAC,CASnC;IAE/C;;;;;OAKG;IACI,UAAU,QAAa,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC,CAgB7D;IAEF;;;;;;;;;;;;;;;OAeG;IACI,aAAa,GAClB,eAAe,eAAe,KAC7B,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC,CA2BlC;IAEF;;;OAGG;IACI,aAAa,aAOlB;IAEF;;OAEG;IACI,OAAO,aAOZ;IAEF;;;;;;;OAOG;IACI,MAAM,sBASX;IAEF;;;OAGG;IACI,sBAAsB,GAAI,MAAM,MAAM,UAE3C;IAEF;;;;OAIG;IACI,sBAAsB,GAAI,MAAM,GAAG,UAExC;IAIF;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,gBAAgB,CA4BtB;IAEF;;;;;;OAMG;IACH,OAAO,CAAC,sBAAsB,CAoC5B;IAEF;;;;;;;OAOG;IACH,OAAO,CAAC,sBAAsB,CAgD5B;IAEF,OAAO,CAAC,mBAAmB,CAUzB;IAEF,OAAO,CAAC,WAAW,CAGjB;IAEF;;;;;;;;OAQG;IACH,OAAO,CAAC,uBAAuB,CA2D7B;IAEF;;;;OAIG;IACH,OAAO,CAAC,sBAAsB,CAW5B;IAEF;;;OAGG;IACH,OAAO,CAAC,iBAAiB,CAEvB;IAEF;;;;;OAKG;IACH,OAAO,CAAC,cAAc,CAcpB;IAEF;;;;OAIG;IACH,OAAO,CAAC,iBAAiB,CAOvB;IAEF;;;OAGG;IACH,OAAO,CAAC,oBAAoB,CAK1B;IAEF,OAAO,CAAC,mBAAmB,CAwBzB;IAEF;;;OAGG;IACH,OAAO,CAAC,iBAAiB,CAIvB;IAEF;;;;;OAKG;IACH,OAAO,CAAC,mBAAmB,CASzB;CACH"}