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

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
+ // Conditionally import ws for Node.js
10
+ let WebSocketClient;
11
+ if (typeof window !== "undefined" && window.WebSocket) {
12
+ WebSocketClient = window.WebSocket;
13
+ }
14
+ else {
15
+ const WS = require("ws");
16
+ WebSocketClient = WS;
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
+ // Conditionally import ws for Node.js
17
+ let WebSocketClient;
18
+ if (typeof window !== "undefined" && window.WebSocket) {
19
+ WebSocketClient = window.WebSocket;
20
+ }
21
+ else {
22
+ const WS = require("ws");
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,15 @@ 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
+ let WebSocketClient;
26
+ if (typeof window !== 'undefined' && window.WebSocket) {
27
+ WebSocketClient = window.WebSocket;
28
+ }
29
+ else {
30
+ const WS = require('ws');
31
+ console.log(WS);
32
+ WebSocketClient = WS;
33
+ }
25
34
  class Measurement {
26
35
  constructor(accessKey, stagingUrl = false) {
27
36
  _Measurement_instances.add(this);
@@ -68,25 +77,34 @@ class Measurement {
68
77
  urlName: APP_BASE_WEBSOCKET_URL,
69
78
  stagingUrl: __classPrivateFieldGet(this, _Measurement_stagingUrl, "f"),
70
79
  })}/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
- };
80
+ __classPrivateFieldSet(this, _Measurement_tryOnSocketRef, new WebSocketClient(url), "f");
81
+ if (__classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f")) {
82
+ __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onopen = () => {
83
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
84
+ };
85
+ __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onmessage = (event) => {
86
+ let data;
87
+ try {
88
+ data = JSON.parse(event.data);
89
+ }
90
+ catch (error) {
91
+ console.log(data, error, "noy correct format for data");
92
+ return;
93
+ }
94
+ if ((data === null || data === void 0 ? void 0 : data.tryOnProcessStatus) === "available") {
95
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
96
+ }
97
+ else {
98
+ onError === null || onError === void 0 ? void 0 : onError({ message: "failed to get image urls" });
99
+ }
100
+ };
101
+ __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onclose = () => {
102
+ onClose === null || onClose === void 0 ? void 0 : onClose();
103
+ };
104
+ __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onerror = (event) => {
105
+ onError === null || onError === void 0 ? void 0 : onError(event);
106
+ };
107
+ }
90
108
  }
91
109
  handleMeasurementSocket(options) {
92
110
  const { scanId, onError, onSuccess, onClose, onOpen } = options;
package/dist/esm/tryOn.js CHANGED
@@ -22,6 +22,16 @@ 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
+ // Conditionally import ws for Node.js
26
+ let WebSocketClient;
27
+ if (typeof window !== 'undefined' && window.WebSocket) {
28
+ WebSocketClient = window.WebSocket;
29
+ }
30
+ else {
31
+ const WS = require('ws');
32
+ console.log(WS);
33
+ WebSocketClient = WS;
34
+ }
25
35
  class TryOn {
26
36
  constructor(accessKey, stagingUrl = false) {
27
37
  _TryOn_instances.add(this);
@@ -51,32 +61,44 @@ class TryOn {
51
61
  }
52
62
  __classPrivateFieldGet(this, _TryOn_disconnectSocket, "f").call(this);
53
63
  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
- };
64
+ __classPrivateFieldSet(this, _TryOn_tryOnSocketRef, new WebSocketClient(url), "f");
65
+ if (__classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f")) {
66
+ __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onopen = () => __awaiter(this, void 0, void 0, function* () {
67
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
68
+ __classPrivateFieldGet(this, _TryOn_handleTimeOut, "f").call(this, { onSuccess, onError, shopDomain, userEmail, productName });
69
+ });
70
+ __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onmessage = (event) => {
71
+ let data;
72
+ try {
73
+ data = JSON.parse(event.data);
74
+ }
75
+ catch (error) {
76
+ console.log(data, error, "noy correct format for data");
77
+ return;
78
+ }
79
+ if ((data === null || data === void 0 ? void 0 : data.status) === "success") {
80
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
81
+ }
82
+ else {
83
+ onError === null || onError === void 0 ? void 0 : onError(data);
84
+ }
85
+ if (__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f")) {
86
+ clearTimeout(__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f"));
87
+ }
88
+ };
89
+ __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onclose = () => {
90
+ onClose === null || onClose === void 0 ? void 0 : onClose();
91
+ };
92
+ __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onerror = (event) => {
93
+ onError === null || onError === void 0 ? void 0 : onError(event);
94
+ if (__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f")) {
95
+ clearTimeout(__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f"));
96
+ }
97
+ };
98
+ }
99
+ else {
100
+ console.log("no connection made for websocket");
101
+ }
80
102
  };
81
103
  _TryOn_handleGetTryOnResult.set(this, (_a) => __awaiter(this, [_a], void 0, function* ({ onSuccess, onError, shopDomain, userEmail, productName }) {
82
104
  try {
@@ -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
+ let WebSocketClient;
10
+ if (typeof window !== 'undefined' && window.WebSocket) {
11
+ WebSocketClient = window.WebSocket;
12
+ }
13
+ else {
14
+ const WS = require('ws');
15
+ console.log(WS);
16
+ WebSocketClient = WS;
17
+ }
9
18
  class Measurement {
10
19
  #tryOnSocketRef = null;
11
20
  #measurementSocketRef = null;
@@ -50,25 +59,34 @@ class Measurement {
50
59
  urlName: constants_js_1.APP_BASE_WEBSOCKET_URL,
51
60
  stagingUrl: this.#stagingUrl,
52
61
  })}/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
- };
62
+ this.#tryOnSocketRef = new WebSocketClient(url);
63
+ if (this.#tryOnSocketRef) {
64
+ this.#tryOnSocketRef.onopen = () => {
65
+ onOpen?.();
66
+ };
67
+ this.#tryOnSocketRef.onmessage = (event) => {
68
+ let data;
69
+ try {
70
+ data = JSON.parse(event.data);
71
+ }
72
+ catch (error) {
73
+ console.log(data, error, "noy correct format for data");
74
+ return;
75
+ }
76
+ if (data?.tryOnProcessStatus === "available") {
77
+ onSuccess?.(data);
78
+ }
79
+ else {
80
+ onError?.({ message: "failed to get image urls" });
81
+ }
82
+ };
83
+ this.#tryOnSocketRef.onclose = () => {
84
+ onClose?.();
85
+ };
86
+ this.#tryOnSocketRef.onerror = (event) => {
87
+ onError?.(event);
88
+ };
89
+ }
72
90
  }
73
91
  async #getMeasurementsCheck(options) {
74
92
  const { scanId, onSuccess, onError } = options;
package/dist/tryOn.js CHANGED
@@ -6,6 +6,16 @@ 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
+ // Conditionally import ws for Node.js
10
+ let WebSocketClient;
11
+ if (typeof window !== 'undefined' && window.WebSocket) {
12
+ WebSocketClient = window.WebSocket;
13
+ }
14
+ else {
15
+ const WS = require('ws');
16
+ console.log(WS);
17
+ WebSocketClient = WS;
18
+ }
9
19
  class TryOn {
10
20
  #tryOnSocketRef = null;
11
21
  #timerWaitingRef = null;
@@ -115,32 +125,44 @@ class TryOn {
115
125
  }
116
126
  this.#disconnectSocket();
117
127
  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
- };
128
+ this.#tryOnSocketRef = new WebSocketClient(url);
129
+ if (this.#tryOnSocketRef) {
130
+ this.#tryOnSocketRef.onopen = async () => {
131
+ onOpen?.();
132
+ this.#handleTimeOut({ onSuccess, onError, shopDomain, userEmail, productName });
133
+ };
134
+ this.#tryOnSocketRef.onmessage = (event) => {
135
+ let data;
136
+ try {
137
+ data = JSON.parse(event.data);
138
+ }
139
+ catch (error) {
140
+ console.log(data, error, "noy correct format for data");
141
+ return;
142
+ }
143
+ if (data?.status === "success") {
144
+ onSuccess?.(data);
145
+ }
146
+ else {
147
+ onError?.(data);
148
+ }
149
+ if (this.#timerWaitingRef) {
150
+ clearTimeout(this.#timerWaitingRef);
151
+ }
152
+ };
153
+ this.#tryOnSocketRef.onclose = () => {
154
+ onClose?.();
155
+ };
156
+ this.#tryOnSocketRef.onerror = (event) => {
157
+ onError?.(event);
158
+ if (this.#timerWaitingRef) {
159
+ clearTimeout(this.#timerWaitingRef);
160
+ }
161
+ };
162
+ }
163
+ else {
164
+ console.log("no connection made for websocket");
165
+ }
144
166
  };
145
167
  handleTryOnSubmit({ userEmail, shopDomain, productName, firstImageName, secondImageName, }) {
146
168
  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.42",
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
@@ -2,115 +2,133 @@ 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
4
 
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
+ const WS = require("ws");
11
+ WebSocketClient = WS;
12
+ }
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
  }
@@ -2,6 +2,14 @@ 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
4
 
5
+ let WebSocketClient: any;
6
+ if (typeof window !== 'undefined' && window.WebSocket) {
7
+ WebSocketClient = window.WebSocket;
8
+ } else {
9
+ const WS = require('ws');
10
+ console.log(WS);
11
+ WebSocketClient = WS;
12
+ }
5
13
  interface TryOnSocketOptions {
6
14
  shopDomain: string;
7
15
  scanId: string;
@@ -101,28 +109,37 @@ class Measurement {
101
109
  urlName: APP_BASE_WEBSOCKET_URL,
102
110
  stagingUrl: this.#stagingUrl,
103
111
  })}/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
- };
112
+ this.#tryOnSocketRef = new WebSocketClient(url);
113
+ if (this.#tryOnSocketRef) {
114
+ this.#tryOnSocketRef.onopen = () => {
115
+ onOpen?.();
116
+ };
117
+
118
+ this.#tryOnSocketRef.onmessage = (event) => {
119
+ let data;
120
+ try {
121
+ data = JSON.parse(event.data);
122
+ } catch (error) {
123
+ console.log(data, error, "noy correct format for data");
124
+
125
+ return;
126
+ }
127
+ if (data?.tryOnProcessStatus === "available") {
128
+ onSuccess?.(data);
129
+ } else {
130
+ onError?.({ message: "failed to get image urls" });
131
+ }
132
+ };
133
+
134
+ this.#tryOnSocketRef.onclose = () => {
135
+ onClose?.();
136
+ };
137
+
138
+ this.#tryOnSocketRef.onerror = (event) => {
139
+ onError?.(event);
140
+ };
141
+ }
142
+
126
143
  }
127
144
 
128
145
  async #getMeasurementsCheck(options: GetMeasurementsCheckOptions): Promise<void> {
package/src/tryOn.ts CHANGED
@@ -2,6 +2,16 @@ 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
4
 
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
+ const WS = require('ws');
11
+ console.log(WS);
12
+ WebSocketClient = WS;
13
+ }
14
+
5
15
  interface UploadFileParams {
6
16
  files: File[];
7
17
  userEmail: string;
@@ -166,31 +176,43 @@ class TryOn {
166
176
  }
167
177
  this.#disconnectSocket();
168
178
  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
- };
179
+ this.#tryOnSocketRef = new WebSocketClient(url);
180
+ if(this.#tryOnSocketRef){
181
+ this.#tryOnSocketRef.onopen = async () => {
182
+ onOpen?.();
183
+ this.#handleTimeOut({ onSuccess, onError, shopDomain, userEmail, productName });
184
+ };
185
+ this.#tryOnSocketRef.onmessage = (event) => {
186
+ let data;
187
+ try {
188
+ data = JSON.parse(event.data);
189
+ } catch (error) {
190
+ console.log(data, error, "noy correct format for data");
191
+ return;
192
+ }
193
+ if (data?.status === "success") {
194
+ onSuccess?.(data);
195
+ } else {
196
+ onError?.(data);
197
+ }
198
+ if (this.#timerWaitingRef) {
199
+ clearTimeout(this.#timerWaitingRef);
200
+ }
201
+ };
202
+ this.#tryOnSocketRef.onclose = () => {
203
+ onClose?.();
204
+ };
205
+ this.#tryOnSocketRef.onerror = (event) => {
206
+ onError?.(event);
207
+ if (this.#timerWaitingRef) {
208
+ clearTimeout(this.#timerWaitingRef);
209
+ }
210
+ };
211
+ }else{
212
+ console.log("no connection made for websocket");
213
+
214
+ }
215
+
194
216
  };
195
217
 
196
218
  handleTryOnSubmit({