@swan-admin/swan-ai-measurements 1.1.41 → 1.1.43

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/dist/auth.js CHANGED
@@ -6,6 +6,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const axios_1 = __importDefault(require("axios"));
7
7
  const constants_js_1 = require("./constants.js");
8
8
  const utils_js_1 = require("./utils.js");
9
+ const ws_1 = __importDefault(require("ws"));
10
+ // Conditionally import ws for Node.js
11
+ let WebSocketClient;
12
+ if (typeof window !== "undefined" && window.WebSocket) {
13
+ WebSocketClient = window.WebSocket;
14
+ }
15
+ else {
16
+ WebSocketClient = ws_1.default;
17
+ }
9
18
  class Auth {
10
19
  #socketRef;
11
20
  #accessKey;
@@ -55,22 +64,32 @@ class Auth {
55
64
  }
56
65
  if (this.#socketRef)
57
66
  this.#socketRef.close();
58
- this.#socketRef = new WebSocket(`${(0, utils_js_1.getUrl)({ urlName: constants_js_1.APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${constants_js_1.API_ENDPOINTS.AUTH}`);
67
+ this.#socketRef = new WebSocketClient(`${(0, utils_js_1.getUrl)({ urlName: constants_js_1.APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${constants_js_1.API_ENDPOINTS.AUTH}`);
59
68
  const detailObj = { email, scanId };
60
- this.#socketRef.onopen = () => {
61
- this.#socketRef?.send(JSON.stringify(detailObj));
62
- onOpen?.();
63
- };
64
- this.#socketRef.onmessage = (event) => {
65
- const data = JSON.parse(event.data);
66
- onSuccess?.(data);
67
- };
68
- this.#socketRef.onclose = () => {
69
- onClose?.();
70
- };
71
- this.#socketRef.onerror = (event) => {
72
- onError?.(event);
73
- };
69
+ if (this.#socketRef) {
70
+ this.#socketRef.onopen = () => {
71
+ this.#socketRef?.send(JSON.stringify(detailObj));
72
+ onOpen?.();
73
+ };
74
+ this.#socketRef.onmessage = (event) => {
75
+ let data;
76
+ try {
77
+ data = JSON.parse(event.data);
78
+ }
79
+ catch (error) {
80
+ console.log(data, error, "noy correct format for data");
81
+ return;
82
+ }
83
+ data = JSON.parse(event.data);
84
+ onSuccess?.(data);
85
+ };
86
+ this.#socketRef.onclose = () => {
87
+ onClose?.();
88
+ };
89
+ this.#socketRef.onerror = (event) => {
90
+ onError?.(event);
91
+ };
92
+ }
74
93
  }
75
94
  }
76
95
  exports.default = Auth;
package/dist/esm/auth.js CHANGED
@@ -13,6 +13,15 @@ var _Auth_socketRef, _Auth_accessKey, _Auth_stagingUrl;
13
13
  import axios from "axios";
14
14
  import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_BASE_WEBSOCKET_URL, REQUIRED_MESSAGE } from "./constants.js";
15
15
  import { checkParameters, getUrl } from "./utils.js";
16
+ import WS from "ws";
17
+ // Conditionally import ws for Node.js
18
+ let WebSocketClient;
19
+ if (typeof window !== "undefined" && window.WebSocket) {
20
+ WebSocketClient = window.WebSocket;
21
+ }
22
+ else {
23
+ WebSocketClient = WS;
24
+ }
16
25
  class Auth {
17
26
  constructor(accessKey, stagingUrl = false) {
18
27
  _Auth_socketRef.set(this, void 0);
@@ -62,23 +71,33 @@ class Auth {
62
71
  }
63
72
  if (__classPrivateFieldGet(this, _Auth_socketRef, "f"))
64
73
  __classPrivateFieldGet(this, _Auth_socketRef, "f").close();
65
- __classPrivateFieldSet(this, _Auth_socketRef, new WebSocket(`${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: __classPrivateFieldGet(this, _Auth_stagingUrl, "f") })}${API_ENDPOINTS.AUTH}`), "f");
74
+ __classPrivateFieldSet(this, _Auth_socketRef, new WebSocketClient(`${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: __classPrivateFieldGet(this, _Auth_stagingUrl, "f") })}${API_ENDPOINTS.AUTH}`), "f");
66
75
  const detailObj = { email, scanId };
67
- __classPrivateFieldGet(this, _Auth_socketRef, "f").onopen = () => {
68
- var _a;
69
- (_a = __classPrivateFieldGet(this, _Auth_socketRef, "f")) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify(detailObj));
70
- onOpen === null || onOpen === void 0 ? void 0 : onOpen();
71
- };
72
- __classPrivateFieldGet(this, _Auth_socketRef, "f").onmessage = (event) => {
73
- const data = JSON.parse(event.data);
74
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
75
- };
76
- __classPrivateFieldGet(this, _Auth_socketRef, "f").onclose = () => {
77
- onClose === null || onClose === void 0 ? void 0 : onClose();
78
- };
79
- __classPrivateFieldGet(this, _Auth_socketRef, "f").onerror = (event) => {
80
- onError === null || onError === void 0 ? void 0 : onError(event);
81
- };
76
+ if (__classPrivateFieldGet(this, _Auth_socketRef, "f")) {
77
+ __classPrivateFieldGet(this, _Auth_socketRef, "f").onopen = () => {
78
+ var _a;
79
+ (_a = __classPrivateFieldGet(this, _Auth_socketRef, "f")) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify(detailObj));
80
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
81
+ };
82
+ __classPrivateFieldGet(this, _Auth_socketRef, "f").onmessage = (event) => {
83
+ let data;
84
+ try {
85
+ data = JSON.parse(event.data);
86
+ }
87
+ catch (error) {
88
+ console.log(data, error, "noy correct format for data");
89
+ return;
90
+ }
91
+ data = JSON.parse(event.data);
92
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
93
+ };
94
+ __classPrivateFieldGet(this, _Auth_socketRef, "f").onclose = () => {
95
+ onClose === null || onClose === void 0 ? void 0 : onClose();
96
+ };
97
+ __classPrivateFieldGet(this, _Auth_socketRef, "f").onerror = (event) => {
98
+ onError === null || onError === void 0 ? void 0 : onError(event);
99
+ };
100
+ }
82
101
  }
83
102
  }
84
103
  _Auth_socketRef = new WeakMap(), _Auth_accessKey = new WeakMap(), _Auth_stagingUrl = new WeakMap();
@@ -22,6 +22,14 @@ var _Measurement_instances, _Measurement_tryOnSocketRef, _Measurement_measuremen
22
22
  import axios from "axios";
23
23
  import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_BASE_WEBSOCKET_URL, REQUIRED_MESSAGE } from "./constants.js";
24
24
  import { checkParameters, getUrl } from "./utils.js";
25
+ import WS from "ws";
26
+ let WebSocketClient;
27
+ if (typeof window !== 'undefined' && window.WebSocket) {
28
+ WebSocketClient = window.WebSocket;
29
+ }
30
+ else {
31
+ WebSocketClient = WS;
32
+ }
25
33
  class Measurement {
26
34
  constructor(accessKey, stagingUrl = false) {
27
35
  _Measurement_instances.add(this);
@@ -68,25 +76,34 @@ class Measurement {
68
76
  urlName: APP_BASE_WEBSOCKET_URL,
69
77
  stagingUrl: __classPrivateFieldGet(this, _Measurement_stagingUrl, "f"),
70
78
  })}/develop?store_url=${shopDomain}&product_name=${productName}&scan_id=${scanId}`;
71
- __classPrivateFieldSet(this, _Measurement_tryOnSocketRef, new WebSocket(url), "f");
72
- __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onopen = () => {
73
- onOpen === null || onOpen === void 0 ? void 0 : onOpen();
74
- };
75
- __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onmessage = (event) => {
76
- const data = JSON.parse(event.data);
77
- if ((data === null || data === void 0 ? void 0 : data.tryOnProcessStatus) === "available") {
78
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
79
- }
80
- else {
81
- onError === null || onError === void 0 ? void 0 : onError({ message: "failed to get image urls" });
82
- }
83
- };
84
- __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onclose = () => {
85
- onClose === null || onClose === void 0 ? void 0 : onClose();
86
- };
87
- __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onerror = (event) => {
88
- onError === null || onError === void 0 ? void 0 : onError(event);
89
- };
79
+ __classPrivateFieldSet(this, _Measurement_tryOnSocketRef, new WebSocketClient(url), "f");
80
+ if (__classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f")) {
81
+ __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onopen = () => {
82
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
83
+ };
84
+ __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onmessage = (event) => {
85
+ let data;
86
+ try {
87
+ data = JSON.parse(event.data);
88
+ }
89
+ catch (error) {
90
+ console.log(data, error, "noy correct format for data");
91
+ return;
92
+ }
93
+ if ((data === null || data === void 0 ? void 0 : data.tryOnProcessStatus) === "available") {
94
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
95
+ }
96
+ else {
97
+ onError === null || onError === void 0 ? void 0 : onError({ message: "failed to get image urls" });
98
+ }
99
+ };
100
+ __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onclose = () => {
101
+ onClose === null || onClose === void 0 ? void 0 : onClose();
102
+ };
103
+ __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onerror = (event) => {
104
+ onError === null || onError === void 0 ? void 0 : onError(event);
105
+ };
106
+ }
90
107
  }
91
108
  handleMeasurementSocket(options) {
92
109
  const { scanId, onError, onSuccess, onClose, onOpen } = options;
package/dist/esm/tryOn.js CHANGED
@@ -22,6 +22,14 @@ var _TryOn_instances, _TryOn_tryOnSocketRef, _TryOn_timerWaitingRef, _TryOn_acce
22
22
  import axios from "axios";
23
23
  import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_BASE_WEBSOCKET_URL, REQUIRED_ERROR_MESSAGE_INVALID_EMAIL, REQUIRED_MESSAGE } from "./constants.js";
24
24
  import { checkParameters, getUrl, isValidEmail } from "./utils.js";
25
+ import WS from "ws";
26
+ let WebSocketClient;
27
+ if (typeof window !== 'undefined' && window.WebSocket) {
28
+ WebSocketClient = window.WebSocket;
29
+ }
30
+ else {
31
+ WebSocketClient = WS;
32
+ }
25
33
  class TryOn {
26
34
  constructor(accessKey, stagingUrl = false) {
27
35
  _TryOn_instances.add(this);
@@ -51,32 +59,44 @@ class TryOn {
51
59
  }
52
60
  __classPrivateFieldGet(this, _TryOn_disconnectSocket, "f").call(this);
53
61
  const url = `${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: __classPrivateFieldGet(this, _TryOn_stagingUrl, "f") })}${API_ENDPOINTS.TRY_ON}?tryonId=${tryonId}`;
54
- __classPrivateFieldSet(this, _TryOn_tryOnSocketRef, new WebSocket(url), "f");
55
- __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onopen = () => __awaiter(this, void 0, void 0, function* () {
56
- onOpen === null || onOpen === void 0 ? void 0 : onOpen();
57
- __classPrivateFieldGet(this, _TryOn_handleTimeOut, "f").call(this, { onSuccess, onError, shopDomain, userEmail, productName });
58
- });
59
- __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onmessage = (event) => {
60
- const data = JSON.parse(event.data);
61
- if ((data === null || data === void 0 ? void 0 : data.status) === "success") {
62
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
63
- }
64
- else {
65
- onError === null || onError === void 0 ? void 0 : onError(data);
66
- }
67
- if (__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f")) {
68
- clearTimeout(__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f"));
69
- }
70
- };
71
- __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onclose = () => {
72
- onClose === null || onClose === void 0 ? void 0 : onClose();
73
- };
74
- __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onerror = (event) => {
75
- onError === null || onError === void 0 ? void 0 : onError(event);
76
- if (__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f")) {
77
- clearTimeout(__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f"));
78
- }
79
- };
62
+ __classPrivateFieldSet(this, _TryOn_tryOnSocketRef, new WebSocketClient(url), "f");
63
+ if (__classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f")) {
64
+ __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onopen = () => __awaiter(this, void 0, void 0, function* () {
65
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
66
+ __classPrivateFieldGet(this, _TryOn_handleTimeOut, "f").call(this, { onSuccess, onError, shopDomain, userEmail, productName });
67
+ });
68
+ __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onmessage = (event) => {
69
+ let data;
70
+ try {
71
+ data = JSON.parse(event.data);
72
+ }
73
+ catch (error) {
74
+ console.log(data, error, "noy correct format for data");
75
+ return;
76
+ }
77
+ if ((data === null || data === void 0 ? void 0 : data.status) === "success") {
78
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
79
+ }
80
+ else {
81
+ onError === null || onError === void 0 ? void 0 : onError(data);
82
+ }
83
+ if (__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f")) {
84
+ clearTimeout(__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f"));
85
+ }
86
+ };
87
+ __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onclose = () => {
88
+ onClose === null || onClose === void 0 ? void 0 : onClose();
89
+ };
90
+ __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onerror = (event) => {
91
+ onError === null || onError === void 0 ? void 0 : onError(event);
92
+ if (__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f")) {
93
+ clearTimeout(__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f"));
94
+ }
95
+ };
96
+ }
97
+ else {
98
+ console.log("no connection made for websocket");
99
+ }
80
100
  };
81
101
  _TryOn_handleGetTryOnResult.set(this, (_a) => __awaiter(this, [_a], void 0, function* ({ onSuccess, onError, shopDomain, userEmail, productName }) {
82
102
  try {
@@ -6,6 +6,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const axios_1 = __importDefault(require("axios"));
7
7
  const constants_js_1 = require("./constants.js");
8
8
  const utils_js_1 = require("./utils.js");
9
+ const ws_1 = __importDefault(require("ws"));
10
+ let WebSocketClient;
11
+ if (typeof window !== 'undefined' && window.WebSocket) {
12
+ WebSocketClient = window.WebSocket;
13
+ }
14
+ else {
15
+ WebSocketClient = ws_1.default;
16
+ }
9
17
  class Measurement {
10
18
  #tryOnSocketRef = null;
11
19
  #measurementSocketRef = null;
@@ -50,25 +58,34 @@ class Measurement {
50
58
  urlName: constants_js_1.APP_BASE_WEBSOCKET_URL,
51
59
  stagingUrl: this.#stagingUrl,
52
60
  })}/develop?store_url=${shopDomain}&product_name=${productName}&scan_id=${scanId}`;
53
- this.#tryOnSocketRef = new WebSocket(url);
54
- this.#tryOnSocketRef.onopen = () => {
55
- onOpen?.();
56
- };
57
- this.#tryOnSocketRef.onmessage = (event) => {
58
- const data = JSON.parse(event.data);
59
- if (data?.tryOnProcessStatus === "available") {
60
- onSuccess?.(data);
61
- }
62
- else {
63
- onError?.({ message: "failed to get image urls" });
64
- }
65
- };
66
- this.#tryOnSocketRef.onclose = () => {
67
- onClose?.();
68
- };
69
- this.#tryOnSocketRef.onerror = (event) => {
70
- onError?.(event);
71
- };
61
+ this.#tryOnSocketRef = new WebSocketClient(url);
62
+ if (this.#tryOnSocketRef) {
63
+ this.#tryOnSocketRef.onopen = () => {
64
+ onOpen?.();
65
+ };
66
+ this.#tryOnSocketRef.onmessage = (event) => {
67
+ let data;
68
+ try {
69
+ data = JSON.parse(event.data);
70
+ }
71
+ catch (error) {
72
+ console.log(data, error, "noy correct format for data");
73
+ return;
74
+ }
75
+ if (data?.tryOnProcessStatus === "available") {
76
+ onSuccess?.(data);
77
+ }
78
+ else {
79
+ onError?.({ message: "failed to get image urls" });
80
+ }
81
+ };
82
+ this.#tryOnSocketRef.onclose = () => {
83
+ onClose?.();
84
+ };
85
+ this.#tryOnSocketRef.onerror = (event) => {
86
+ onError?.(event);
87
+ };
88
+ }
72
89
  }
73
90
  async #getMeasurementsCheck(options) {
74
91
  const { scanId, onSuccess, onError } = options;
package/dist/tryOn.js CHANGED
@@ -6,6 +6,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const axios_1 = __importDefault(require("axios"));
7
7
  const constants_js_1 = require("./constants.js");
8
8
  const utils_js_1 = require("./utils.js");
9
+ const ws_1 = __importDefault(require("ws"));
10
+ let WebSocketClient;
11
+ if (typeof window !== 'undefined' && window.WebSocket) {
12
+ WebSocketClient = window.WebSocket;
13
+ }
14
+ else {
15
+ WebSocketClient = ws_1.default;
16
+ }
9
17
  class TryOn {
10
18
  #tryOnSocketRef = null;
11
19
  #timerWaitingRef = null;
@@ -115,32 +123,44 @@ class TryOn {
115
123
  }
116
124
  this.#disconnectSocket();
117
125
  const url = `${(0, utils_js_1.getUrl)({ urlName: constants_js_1.APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${constants_js_1.API_ENDPOINTS.TRY_ON}?tryonId=${tryonId}`;
118
- this.#tryOnSocketRef = new WebSocket(url);
119
- this.#tryOnSocketRef.onopen = async () => {
120
- onOpen?.();
121
- this.#handleTimeOut({ onSuccess, onError, shopDomain, userEmail, productName });
122
- };
123
- this.#tryOnSocketRef.onmessage = (event) => {
124
- const data = JSON.parse(event.data);
125
- if (data?.status === "success") {
126
- onSuccess?.(data);
127
- }
128
- else {
129
- onError?.(data);
130
- }
131
- if (this.#timerWaitingRef) {
132
- clearTimeout(this.#timerWaitingRef);
133
- }
134
- };
135
- this.#tryOnSocketRef.onclose = () => {
136
- onClose?.();
137
- };
138
- this.#tryOnSocketRef.onerror = (event) => {
139
- onError?.(event);
140
- if (this.#timerWaitingRef) {
141
- clearTimeout(this.#timerWaitingRef);
142
- }
143
- };
126
+ this.#tryOnSocketRef = new WebSocketClient(url);
127
+ if (this.#tryOnSocketRef) {
128
+ this.#tryOnSocketRef.onopen = async () => {
129
+ onOpen?.();
130
+ this.#handleTimeOut({ onSuccess, onError, shopDomain, userEmail, productName });
131
+ };
132
+ this.#tryOnSocketRef.onmessage = (event) => {
133
+ let data;
134
+ try {
135
+ data = JSON.parse(event.data);
136
+ }
137
+ catch (error) {
138
+ console.log(data, error, "noy correct format for data");
139
+ return;
140
+ }
141
+ if (data?.status === "success") {
142
+ onSuccess?.(data);
143
+ }
144
+ else {
145
+ onError?.(data);
146
+ }
147
+ if (this.#timerWaitingRef) {
148
+ clearTimeout(this.#timerWaitingRef);
149
+ }
150
+ };
151
+ this.#tryOnSocketRef.onclose = () => {
152
+ onClose?.();
153
+ };
154
+ this.#tryOnSocketRef.onerror = (event) => {
155
+ onError?.(event);
156
+ if (this.#timerWaitingRef) {
157
+ clearTimeout(this.#timerWaitingRef);
158
+ }
159
+ };
160
+ }
161
+ else {
162
+ console.log("no connection made for websocket");
163
+ }
144
164
  };
145
165
  handleTryOnSubmit({ userEmail, shopDomain, productName, firstImageName, secondImageName, }) {
146
166
  if ((0, utils_js_1.checkParameters)(shopDomain, userEmail, productName, firstImageName, secondImageName) === false) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swan-admin/swan-ai-measurements",
3
- "version": "1.1.41",
3
+ "version": "1.1.43",
4
4
  "description": "provides ai measurement suggestion",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -17,11 +17,12 @@
17
17
  "@uppy/aws-s3-multipart": "^3.11.1",
18
18
  "@uppy/core": "^3.11.3",
19
19
  "axios": "^1.6.7",
20
+ "fix-esm": "^1.0.1",
20
21
  "socket.io": "^4.7.5",
21
22
  "socket.io-client": "^4.7.5",
22
23
  "tslib": "^2.6.2",
23
24
  "typescript": "^5.4.5",
24
- "fix-esm": "^1.0.1"
25
+ "ws": "^8.18.0"
25
26
  },
26
27
  "scripts": {
27
28
  "start": "nodemon dist/index.js",
@@ -53,6 +54,7 @@
53
54
  },
54
55
  "homepage": "https://github.com/Syze/swan-ai-measurements.git#readme",
55
56
  "devDependencies": {
57
+ "@types/ws": "^8.5.12",
56
58
  "nodemon": "^3.1.0"
57
59
  }
58
60
  }
package/src/auth.ts CHANGED
@@ -1,116 +1,134 @@
1
1
  import axios, { AxiosResponse } from "axios";
2
2
  import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_BASE_WEBSOCKET_URL, REQUIRED_MESSAGE } from "./constants.js";
3
3
  import { checkParameters, getUrl } from "./utils.js";
4
+ import WS from "ws";
5
+ // Conditionally import ws for Node.js
6
+ let WebSocketClient: any;
7
+ if (typeof window !== "undefined" && window.WebSocket) {
8
+ WebSocketClient = window.WebSocket;
9
+ } else {
10
+
11
+ WebSocketClient = WS;
12
+ }
4
13
 
5
14
  interface RegisterUserParams {
6
- email: string;
7
- appVerifyUrl: string;
8
- gender?: string;
9
- height?: number;
10
- username?: string;
15
+ email: string;
16
+ appVerifyUrl: string;
17
+ gender?: string;
18
+ height?: number;
19
+ username?: string;
11
20
  }
12
21
 
13
22
  interface AddUserParams {
14
- scanId: string;
15
- email: string;
16
- name?: string;
17
- height: number;
18
- gender: string;
19
- offsetMarketingConsent?: boolean;
23
+ scanId: string;
24
+ email: string;
25
+ name?: string;
26
+ height: number;
27
+ gender: string;
28
+ offsetMarketingConsent?: boolean;
20
29
  }
21
30
 
22
31
  interface AuthSocketParams {
23
- email: string;
24
- scanId: string;
25
- onError?: (event: Event) => void;
26
- onSuccess?: (data: any) => void;
27
- onClose?: () => void;
28
- onOpen?: () => void;
32
+ email: string;
33
+ scanId: string;
34
+ onError?: (event: Event) => void;
35
+ onSuccess?: (data: any) => void;
36
+ onClose?: () => void;
37
+ onOpen?: () => void;
29
38
  }
30
39
 
31
40
  interface AuthSocketDetail {
32
- email: string;
33
- scanId: string;
41
+ email: string;
42
+ scanId: string;
34
43
  }
35
44
 
36
45
  export default class Auth {
37
- #socketRef?: WebSocket;
38
- #accessKey: string;
39
- #stagingUrl: boolean;
40
-
41
- constructor(accessKey: string, stagingUrl = false) {
42
- this.#accessKey = accessKey;
43
- this.#stagingUrl = stagingUrl;
44
- }
45
-
46
- registerUser({ email, appVerifyUrl, gender, height, username }: RegisterUserParams): Promise<AxiosResponse> {
47
- if (!checkParameters(email, appVerifyUrl)) {
48
- throw new Error(REQUIRED_MESSAGE);
49
- }
50
- let body: Record<string, any> = { username, email, appVerifyUrl };
51
- if (gender && height) {
52
- body = { ...body, attributes: { gender, height } };
53
- }
54
- return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.REGISTER_USER}`, body, {
55
- headers: { "X-Api-Key": this.#accessKey },
56
- });
57
- }
58
-
59
- verifyToken(token: string): Promise<AxiosResponse> {
60
- if (!checkParameters(token)) {
61
- throw new Error(REQUIRED_MESSAGE);
62
- }
63
- return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.VERIFY_USER}`, null, {
64
- params: { token },
65
- headers: { "X-Api-Key": this.#accessKey },
66
- });
67
- }
68
-
69
- addUser({ scanId, email, name, height, gender, offsetMarketingConsent }: AddUserParams): Promise<AxiosResponse> {
70
- if (!checkParameters(scanId, email, height, gender)) {
71
- throw new Error(REQUIRED_MESSAGE);
72
- }
73
- return axios.post(
74
- `${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.ADD_USER}`,
75
- { scan_id: scanId, email, name, offsetMarketingConsent, attributes: JSON.stringify({ height, gender }) },
76
- { headers: { "X-Api-Key": this.#accessKey } }
77
- );
78
- }
79
-
80
- getUserDetail(email: string): Promise<AxiosResponse> {
81
- if (!checkParameters(email)) {
82
- throw new Error(REQUIRED_MESSAGE);
83
- }
84
- return axios.get(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.GET_USER_DETAIL}/${email}`, {
85
- headers: { "X-Api-Key": this.#accessKey },
86
- });
87
- }
88
-
89
- handleAuthSocket({ email, scanId, onError, onSuccess, onClose, onOpen }: AuthSocketParams): void {
90
- if (!checkParameters(email, scanId)) {
91
- throw new Error(REQUIRED_MESSAGE);
92
- }
93
- if (this.#socketRef) this.#socketRef.close();
94
-
95
- this.#socketRef = new WebSocket(`${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.AUTH}`);
96
- const detailObj: AuthSocketDetail = { email, scanId };
97
-
98
- this.#socketRef.onopen = () => {
99
- this.#socketRef?.send(JSON.stringify(detailObj));
100
- onOpen?.();
101
- };
102
-
103
- this.#socketRef.onmessage = (event: MessageEvent) => {
104
- const data = JSON.parse(event.data);
105
- onSuccess?.(data);
106
- };
107
-
108
- this.#socketRef.onclose = () => {
109
- onClose?.();
110
- };
111
-
112
- this.#socketRef.onerror = (event: Event) => {
113
- onError?.(event);
114
- };
115
- }
46
+ #socketRef?: WebSocket;
47
+ #accessKey: string;
48
+ #stagingUrl: boolean;
49
+
50
+ constructor(accessKey: string, stagingUrl = false) {
51
+ this.#accessKey = accessKey;
52
+ this.#stagingUrl = stagingUrl;
53
+ }
54
+
55
+ registerUser({ email, appVerifyUrl, gender, height, username }: RegisterUserParams): Promise<AxiosResponse> {
56
+ if (!checkParameters(email, appVerifyUrl)) {
57
+ throw new Error(REQUIRED_MESSAGE);
58
+ }
59
+ let body: Record<string, any> = { username, email, appVerifyUrl };
60
+ if (gender && height) {
61
+ body = { ...body, attributes: { gender, height } };
62
+ }
63
+ return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.REGISTER_USER}`, body, {
64
+ headers: { "X-Api-Key": this.#accessKey },
65
+ });
66
+ }
67
+
68
+ verifyToken(token: string): Promise<AxiosResponse> {
69
+ if (!checkParameters(token)) {
70
+ throw new Error(REQUIRED_MESSAGE);
71
+ }
72
+ return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.VERIFY_USER}`, null, {
73
+ params: { token },
74
+ headers: { "X-Api-Key": this.#accessKey },
75
+ });
76
+ }
77
+
78
+ addUser({ scanId, email, name, height, gender, offsetMarketingConsent }: AddUserParams): Promise<AxiosResponse> {
79
+ if (!checkParameters(scanId, email, height, gender)) {
80
+ throw new Error(REQUIRED_MESSAGE);
81
+ }
82
+ return axios.post(
83
+ `${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.ADD_USER}`,
84
+ { scan_id: scanId, email, name, offsetMarketingConsent, attributes: JSON.stringify({ height, gender }) },
85
+ { headers: { "X-Api-Key": this.#accessKey } },
86
+ );
87
+ }
88
+
89
+ getUserDetail(email: string): Promise<AxiosResponse> {
90
+ if (!checkParameters(email)) {
91
+ throw new Error(REQUIRED_MESSAGE);
92
+ }
93
+ return axios.get(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.GET_USER_DETAIL}/${email}`, {
94
+ headers: { "X-Api-Key": this.#accessKey },
95
+ });
96
+ }
97
+
98
+ handleAuthSocket({ email, scanId, onError, onSuccess, onClose, onOpen }: AuthSocketParams): void {
99
+ if (!checkParameters(email, scanId)) {
100
+ throw new Error(REQUIRED_MESSAGE);
101
+ }
102
+ if (this.#socketRef) this.#socketRef.close();
103
+
104
+ this.#socketRef = new WebSocketClient(`${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.AUTH}`);
105
+ const detailObj: AuthSocketDetail = { email, scanId };
106
+ if (this.#socketRef) {
107
+ this.#socketRef.onopen = () => {
108
+ this.#socketRef?.send(JSON.stringify(detailObj));
109
+ onOpen?.();
110
+ };
111
+
112
+ this.#socketRef.onmessage = (event: MessageEvent) => {
113
+ let data;
114
+ try {
115
+ data = JSON.parse(event.data);
116
+ } catch (error) {
117
+ console.log(data, error, "noy correct format for data");
118
+
119
+ return;
120
+ }
121
+ data = JSON.parse(event.data);
122
+ onSuccess?.(data);
123
+ };
124
+
125
+ this.#socketRef.onclose = () => {
126
+ onClose?.();
127
+ };
128
+
129
+ this.#socketRef.onerror = (event: Event) => {
130
+ onError?.(event);
131
+ };
132
+ }
133
+ }
116
134
  }
@@ -1,7 +1,14 @@
1
1
  import axios, { AxiosResponse } from "axios";
2
2
  import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_BASE_WEBSOCKET_URL, REQUIRED_MESSAGE } from "./constants.js";
3
3
  import { checkParameters, getUrl } from "./utils.js";
4
+ import WS from "ws";
4
5
 
6
+ let WebSocketClient: any;
7
+ if (typeof window !== 'undefined' && window.WebSocket) {
8
+ WebSocketClient = window.WebSocket;
9
+ } else {
10
+ WebSocketClient = WS;
11
+ }
5
12
  interface TryOnSocketOptions {
6
13
  shopDomain: string;
7
14
  scanId: string;
@@ -101,28 +108,37 @@ class Measurement {
101
108
  urlName: APP_BASE_WEBSOCKET_URL,
102
109
  stagingUrl: this.#stagingUrl,
103
110
  })}/develop?store_url=${shopDomain}&product_name=${productName}&scan_id=${scanId}`;
104
- this.#tryOnSocketRef = new WebSocket(url);
105
-
106
- this.#tryOnSocketRef.onopen = () => {
107
- onOpen?.();
108
- };
109
-
110
- this.#tryOnSocketRef.onmessage = (event) => {
111
- const data = JSON.parse(event.data);
112
- if (data?.tryOnProcessStatus === "available") {
113
- onSuccess?.(data);
114
- } else {
115
- onError?.({ message: "failed to get image urls" });
116
- }
117
- };
118
-
119
- this.#tryOnSocketRef.onclose = () => {
120
- onClose?.();
121
- };
122
-
123
- this.#tryOnSocketRef.onerror = (event) => {
124
- onError?.(event);
125
- };
111
+ this.#tryOnSocketRef = new WebSocketClient(url);
112
+ if (this.#tryOnSocketRef) {
113
+ this.#tryOnSocketRef.onopen = () => {
114
+ onOpen?.();
115
+ };
116
+
117
+ this.#tryOnSocketRef.onmessage = (event) => {
118
+ let data;
119
+ try {
120
+ data = JSON.parse(event.data);
121
+ } catch (error) {
122
+ console.log(data, error, "noy correct format for data");
123
+
124
+ return;
125
+ }
126
+ if (data?.tryOnProcessStatus === "available") {
127
+ onSuccess?.(data);
128
+ } else {
129
+ onError?.({ message: "failed to get image urls" });
130
+ }
131
+ };
132
+
133
+ this.#tryOnSocketRef.onclose = () => {
134
+ onClose?.();
135
+ };
136
+
137
+ this.#tryOnSocketRef.onerror = (event) => {
138
+ onError?.(event);
139
+ };
140
+ }
141
+
126
142
  }
127
143
 
128
144
  async #getMeasurementsCheck(options: GetMeasurementsCheckOptions): Promise<void> {
package/src/tryOn.ts CHANGED
@@ -1,6 +1,14 @@
1
1
  import axios, { AxiosResponse } from "axios";
2
2
  import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_BASE_WEBSOCKET_URL, REQUIRED_ERROR_MESSAGE_INVALID_EMAIL, REQUIRED_MESSAGE } from "./constants.js";
3
3
  import { checkParameters, getUrl, isValidEmail } from "./utils.js";
4
+ import WS from "ws";
5
+
6
+ let WebSocketClient: any;
7
+ if (typeof window !== 'undefined' && window.WebSocket) {
8
+ WebSocketClient = window.WebSocket;
9
+ } else {
10
+ WebSocketClient = WS;
11
+ }
4
12
 
5
13
  interface UploadFileParams {
6
14
  files: File[];
@@ -166,31 +174,43 @@ class TryOn {
166
174
  }
167
175
  this.#disconnectSocket();
168
176
  const url = `${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.TRY_ON}?tryonId=${tryonId}`;
169
- this.#tryOnSocketRef = new WebSocket(url);
170
- this.#tryOnSocketRef.onopen = async () => {
171
- onOpen?.();
172
- this.#handleTimeOut({ onSuccess, onError, shopDomain, userEmail, productName });
173
- };
174
- this.#tryOnSocketRef.onmessage = (event) => {
175
- const data = JSON.parse(event.data);
176
- if (data?.status === "success") {
177
- onSuccess?.(data);
178
- } else {
179
- onError?.(data);
180
- }
181
- if (this.#timerWaitingRef) {
182
- clearTimeout(this.#timerWaitingRef);
183
- }
184
- };
185
- this.#tryOnSocketRef.onclose = () => {
186
- onClose?.();
187
- };
188
- this.#tryOnSocketRef.onerror = (event) => {
189
- onError?.(event);
190
- if (this.#timerWaitingRef) {
191
- clearTimeout(this.#timerWaitingRef);
192
- }
193
- };
177
+ this.#tryOnSocketRef = new WebSocketClient(url);
178
+ if(this.#tryOnSocketRef){
179
+ this.#tryOnSocketRef.onopen = async () => {
180
+ onOpen?.();
181
+ this.#handleTimeOut({ onSuccess, onError, shopDomain, userEmail, productName });
182
+ };
183
+ this.#tryOnSocketRef.onmessage = (event) => {
184
+ let data;
185
+ try {
186
+ data = JSON.parse(event.data);
187
+ } catch (error) {
188
+ console.log(data, error, "noy correct format for data");
189
+ return;
190
+ }
191
+ if (data?.status === "success") {
192
+ onSuccess?.(data);
193
+ } else {
194
+ onError?.(data);
195
+ }
196
+ if (this.#timerWaitingRef) {
197
+ clearTimeout(this.#timerWaitingRef);
198
+ }
199
+ };
200
+ this.#tryOnSocketRef.onclose = () => {
201
+ onClose?.();
202
+ };
203
+ this.#tryOnSocketRef.onerror = (event) => {
204
+ onError?.(event);
205
+ if (this.#timerWaitingRef) {
206
+ clearTimeout(this.#timerWaitingRef);
207
+ }
208
+ };
209
+ }else{
210
+ console.log("no connection made for websocket");
211
+
212
+ }
213
+
194
214
  };
195
215
 
196
216
  handleTryOnSubmit({
package/tsconfig.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "target": "esnext", // Ensure ESNext to support dynamic imports
4
- "module": "commonjs",
4
+ "module": "CommonJS",
5
5
  "moduleResolution": "node",
6
6
  "outDir": "./dist",
7
7
  "declaration": true,