@swan-admin/swan-ai-measurements 1.1.67 → 1.1.69

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.
@@ -4,13 +4,17 @@ interface MeasurementRecommendation {
4
4
  scanId: string;
5
5
  productName: string;
6
6
  }
7
- interface MeasurementSocketOptions {
8
- scanId: string;
7
+ interface Callbacks {
9
8
  onError?: (error: any) => void;
10
9
  onSuccess?: (data: any) => void;
11
10
  onClose?: () => void;
12
11
  onOpen?: () => void;
13
- isFallback?: boolean;
12
+ }
13
+ interface MeasurementSocketOptions extends Callbacks {
14
+ scanId: string;
15
+ }
16
+ interface FaceScanSocketOptions extends Callbacks {
17
+ faceScanId: string;
14
18
  }
15
19
  declare class Measurement {
16
20
  #private;
@@ -18,5 +22,6 @@ declare class Measurement {
18
22
  getMeasurementResult(scanId: string): Promise<AxiosResponse<any>>;
19
23
  getMeasurementRecommendation({ scanId, shopDomain, productName }: MeasurementRecommendation): Promise<AxiosResponse<any>>;
20
24
  handleMeasurementSocket(options: MeasurementSocketOptions): void;
25
+ handlFaceScaneSocket(options: FaceScanSocketOptions): void;
21
26
  }
22
27
  export default Measurement;
@@ -18,7 +18,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
18
18
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
19
19
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
20
20
  };
21
- var _Measurement_instances, _Measurement_measurementSocketRef, _Measurement_timerPollingRef, _Measurement_timerWaitingRef, _Measurement_count, _Measurement_accessKey, _Measurement_stagingUrl, _Measurement_getMeasurementsCheck, _Measurement_handlePolling, _Measurement_disconnectSocket, _Measurement_handleTimeOut;
21
+ var _Measurement_instances, _Measurement_measurementSocketRef, _Measurement_timerPollingRef, _Measurement_timerWaitingRef, _Measurement_count, _Measurement_accessKey, _Measurement_stagingUrl, _Measurement_getMeasurementsCheck, _Measurement_handlePolling, _Measurement_disconnectSocket, _Measurement_handleTimeOut, _Measurement_handleSocket;
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";
@@ -47,45 +47,23 @@ class Measurement {
47
47
  if (!checkParameters(scanId, shopDomain, productName)) {
48
48
  throw new Error(REQUIRED_MESSAGE);
49
49
  }
50
- return axios.get(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: __classPrivateFieldGet(this, _Measurement_stagingUrl, "f") })}${API_ENDPOINTS.RECOMMENDATION}/scan/${scanId}/shop/${shopDomain}/product/${productName}`, { headers: { "X-Api-Key": __classPrivateFieldGet(this, _Measurement_accessKey, "f") } });
50
+ return axios.get(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: __classPrivateFieldGet(this, _Measurement_stagingUrl, "f") })}${API_ENDPOINTS.RECOMMENDATION}/scan/${scanId}/shop/${shopDomain}/product/${productName}`, {
51
+ headers: { "X-Api-Key": __classPrivateFieldGet(this, _Measurement_accessKey, "f") },
52
+ });
51
53
  }
52
54
  handleMeasurementSocket(options) {
53
- const { scanId, onError, onSuccess, onClose, onOpen, isFallback = true } = options;
55
+ const { scanId, onError, onSuccess, onClose, onOpen } = options;
54
56
  if (!checkParameters(scanId)) {
55
57
  throw new Error(REQUIRED_MESSAGE);
56
58
  }
57
- setTimeout(() => {
58
- __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_disconnectSocket).call(this);
59
- const url = `${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: __classPrivateFieldGet(this, _Measurement_stagingUrl, "f") })}${API_ENDPOINTS.SCANNING}?scanId=${scanId}`;
60
- __classPrivateFieldSet(this, _Measurement_measurementSocketRef, new WebSocket(url), "f");
61
- __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onopen = () => {
62
- onOpen === null || onOpen === void 0 ? void 0 : onOpen();
63
- if (isFallback) {
64
- __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handleTimeOut).call(this, { scanId, onSuccess, onError });
65
- }
66
- };
67
- __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onmessage = (event) => {
68
- const data = JSON.parse(event.data);
69
- if ((data === null || data === void 0 ? void 0 : data.code) === 200 && (data === null || data === void 0 ? void 0 : data.scanStatus) === "success") {
70
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
71
- }
72
- else {
73
- if (__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f")) {
74
- clearTimeout(__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f"));
75
- }
76
- onError === null || onError === void 0 ? void 0 : onError(data);
77
- }
78
- if (__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f") && (data === null || data === void 0 ? void 0 : data.code) === 200 && (data === null || data === void 0 ? void 0 : data.scanStatus) === "success" && (data === null || data === void 0 ? void 0 : data.resultType) === "final") {
79
- clearTimeout(__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f"));
80
- }
81
- };
82
- __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onclose = () => {
83
- onClose === null || onClose === void 0 ? void 0 : onClose();
84
- };
85
- __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onerror = (event) => {
86
- // onError?.(event);
87
- };
88
- }, 5000);
59
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handleSocket).call(this, { onOpen, scanId, onSuccess, onError, onClose, paramsKey: "scanId", isFallback: true, delay: 5000 });
60
+ }
61
+ handlFaceScaneSocket(options) {
62
+ const { faceScanId, onError, onSuccess, onClose, onOpen } = options;
63
+ if (!checkParameters(faceScanId)) {
64
+ throw new Error(REQUIRED_MESSAGE);
65
+ }
66
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handleSocket).call(this, { onOpen, faceScanId, onSuccess, onError, onClose, paramsKey: "faceScanId", isFallback: false, delay: 0 });
89
67
  }
90
68
  }
91
69
  _Measurement_measurementSocketRef = new WeakMap(), _Measurement_timerPollingRef = new WeakMap(), _Measurement_timerWaitingRef = new WeakMap(), _Measurement_count = new WeakMap(), _Measurement_accessKey = new WeakMap(), _Measurement_stagingUrl = new WeakMap(), _Measurement_instances = new WeakSet(), _Measurement_getMeasurementsCheck = function _Measurement_getMeasurementsCheck(options) {
@@ -143,5 +121,38 @@ _Measurement_measurementSocketRef = new WeakMap(), _Measurement_timerPollingRef
143
121
  __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handlePolling).call(this, { scanId, onSuccess, onError });
144
122
  __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_disconnectSocket).call(this);
145
123
  }, 2 * 60000), "f");
124
+ }, _Measurement_handleSocket = function _Measurement_handleSocket({ onOpen, isFallback, scanId, onSuccess, onError, onClose, paramsKey, faceScanId, delay }) {
125
+ setTimeout(() => {
126
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_disconnectSocket).call(this);
127
+ const url = `${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: __classPrivateFieldGet(this, _Measurement_stagingUrl, "f") })}${API_ENDPOINTS.SCANNING}?${paramsKey}=${scanId || faceScanId}`;
128
+ __classPrivateFieldSet(this, _Measurement_measurementSocketRef, new WebSocket(url), "f");
129
+ __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onopen = () => {
130
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
131
+ if (isFallback && scanId) {
132
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handleTimeOut).call(this, { scanId, onSuccess, onError });
133
+ }
134
+ };
135
+ __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onmessage = (event) => {
136
+ const data = JSON.parse(event.data);
137
+ if ((data === null || data === void 0 ? void 0 : data.code) === 200 && (data === null || data === void 0 ? void 0 : data.scanStatus) === "success") {
138
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
139
+ }
140
+ else {
141
+ if (__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f")) {
142
+ clearTimeout(__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f"));
143
+ }
144
+ onError === null || onError === void 0 ? void 0 : onError(data);
145
+ }
146
+ if (__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f") && (data === null || data === void 0 ? void 0 : data.code) === 200 && (data === null || data === void 0 ? void 0 : data.scanStatus) === "success" && (data === null || data === void 0 ? void 0 : data.resultType) === "final") {
147
+ clearTimeout(__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f"));
148
+ }
149
+ };
150
+ __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onclose = () => {
151
+ onClose === null || onClose === void 0 ? void 0 : onClose();
152
+ };
153
+ __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onerror = (event) => {
154
+ // onError?.(event);
155
+ };
156
+ }, delay);
146
157
  };
147
158
  export default Measurement;
@@ -4,13 +4,17 @@ interface MeasurementRecommendation {
4
4
  scanId: string;
5
5
  productName: string;
6
6
  }
7
- interface MeasurementSocketOptions {
8
- scanId: string;
7
+ interface Callbacks {
9
8
  onError?: (error: any) => void;
10
9
  onSuccess?: (data: any) => void;
11
10
  onClose?: () => void;
12
11
  onOpen?: () => void;
13
- isFallback?: boolean;
12
+ }
13
+ interface MeasurementSocketOptions extends Callbacks {
14
+ scanId: string;
15
+ }
16
+ interface FaceScanSocketOptions extends Callbacks {
17
+ faceScanId: string;
14
18
  }
15
19
  declare class Measurement {
16
20
  #private;
@@ -18,5 +22,6 @@ declare class Measurement {
18
22
  getMeasurementResult(scanId: string): Promise<AxiosResponse<any>>;
19
23
  getMeasurementRecommendation({ scanId, shopDomain, productName }: MeasurementRecommendation): Promise<AxiosResponse<any>>;
20
24
  handleMeasurementSocket(options: MeasurementSocketOptions): void;
25
+ handlFaceScaneSocket(options: FaceScanSocketOptions): void;
21
26
  }
22
27
  export default Measurement;
@@ -30,7 +30,9 @@ class Measurement {
30
30
  if (!(0, utils_js_1.checkParameters)(scanId, shopDomain, productName)) {
31
31
  throw new Error(constants_js_1.REQUIRED_MESSAGE);
32
32
  }
33
- return axios_1.default.get(`${(0, utils_js_1.getUrl)({ urlName: constants_js_1.APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${constants_js_1.API_ENDPOINTS.RECOMMENDATION}/scan/${scanId}/shop/${shopDomain}/product/${productName}`, { headers: { "X-Api-Key": this.#accessKey } });
33
+ return axios_1.default.get(`${(0, utils_js_1.getUrl)({ urlName: constants_js_1.APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${constants_js_1.API_ENDPOINTS.RECOMMENDATION}/scan/${scanId}/shop/${shopDomain}/product/${productName}`, {
34
+ headers: { "X-Api-Key": this.#accessKey },
35
+ });
34
36
  }
35
37
  async #getMeasurementsCheck(options) {
36
38
  const { scanId, onSuccess, onError } = options;
@@ -87,17 +89,27 @@ class Measurement {
87
89
  }, 2 * 60000);
88
90
  }
89
91
  handleMeasurementSocket(options) {
90
- const { scanId, onError, onSuccess, onClose, onOpen, isFallback = true } = options;
92
+ const { scanId, onError, onSuccess, onClose, onOpen } = options;
91
93
  if (!(0, utils_js_1.checkParameters)(scanId)) {
92
94
  throw new Error(constants_js_1.REQUIRED_MESSAGE);
93
95
  }
96
+ this.#handleSocket({ onOpen, scanId, onSuccess, onError, onClose, paramsKey: "scanId", isFallback: true, delay: 5000 });
97
+ }
98
+ handlFaceScaneSocket(options) {
99
+ const { faceScanId, onError, onSuccess, onClose, onOpen } = options;
100
+ if (!(0, utils_js_1.checkParameters)(faceScanId)) {
101
+ throw new Error(constants_js_1.REQUIRED_MESSAGE);
102
+ }
103
+ this.#handleSocket({ onOpen, faceScanId, onSuccess, onError, onClose, paramsKey: "faceScanId", isFallback: false, delay: 0 });
104
+ }
105
+ #handleSocket({ onOpen, isFallback, scanId, onSuccess, onError, onClose, paramsKey, faceScanId, delay }) {
94
106
  setTimeout(() => {
95
107
  this.#disconnectSocket();
96
- const url = `${(0, utils_js_1.getUrl)({ urlName: constants_js_1.APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${constants_js_1.API_ENDPOINTS.SCANNING}?scanId=${scanId}`;
108
+ const url = `${(0, utils_js_1.getUrl)({ urlName: constants_js_1.APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${constants_js_1.API_ENDPOINTS.SCANNING}?${paramsKey}=${scanId || faceScanId}`;
97
109
  this.#measurementSocketRef = new WebSocket(url);
98
110
  this.#measurementSocketRef.onopen = () => {
99
111
  onOpen?.();
100
- if (isFallback) {
112
+ if (isFallback && scanId) {
101
113
  this.#handleTimeOut({ scanId, onSuccess, onError });
102
114
  }
103
115
  };
@@ -122,7 +134,7 @@ class Measurement {
122
134
  this.#measurementSocketRef.onerror = (event) => {
123
135
  // onError?.(event);
124
136
  };
125
- }, 5000);
137
+ }, delay);
126
138
  }
127
139
  }
128
140
  exports.default = Measurement;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swan-admin/swan-ai-measurements",
3
- "version": "1.1.67",
3
+ "version": "1.1.69",
4
4
  "description": "provides ai measurement suggestion",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -3,174 +3,190 @@ import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_BASE_WEBSOCKET_URL, REQUIRED_MESS
3
3
  import { checkParameters, getUrl } from "./utils.js";
4
4
 
5
5
  interface MeasurementRecommendation {
6
- shopDomain: string;
7
- scanId: string;
8
- productName: string;
6
+ shopDomain: string;
7
+ scanId: string;
8
+ productName: string;
9
9
  }
10
- interface MeasurementSocketOptions {
11
- scanId: string;
12
- onError?: (error: any) => void;
13
- onSuccess?: (data: any) => void;
14
- onClose?: () => void;
15
- onOpen?: () => void;
16
- isFallback?:boolean
10
+ interface Callbacks {
11
+ onError?: (error: any) => void;
12
+ onSuccess?: (data: any) => void;
13
+ onClose?: () => void;
14
+ onOpen?: () => void;
15
+ }
16
+ interface MeasurementSocketOptions extends Callbacks {
17
+ scanId: string;
18
+ }
19
+ interface FaceScanSocketOptions extends Callbacks {
20
+ faceScanId: string;
21
+ }
22
+ interface HandleSocket extends Callbacks {
23
+ isFallback: boolean;
24
+ scanId?: string;
25
+ faceScanId?: string;
26
+ paramsKey?: string;
27
+ delay:number
17
28
  }
18
-
19
29
  interface GetMeasurementsCheckOptions {
20
- scanId: string;
21
- onSuccess?: (data: any) => void;
22
- onError?: (error: any) => void;
30
+ scanId: string;
31
+ onSuccess?: (data: any) => void;
32
+ onError?: (error: any) => void;
23
33
  }
24
34
 
25
35
  interface HandlePollingOptions {
26
- scanId: string;
27
- onSuccess?: (data: any) => void;
28
- onError?: (error: any) => void;
36
+ scanId: string;
37
+ onSuccess?: (data: any) => void;
38
+ onError?: (error: any) => void;
29
39
  }
30
40
 
31
41
  interface HandleTimeOutOptions {
32
- scanId: string;
33
- onSuccess?: (data: any) => void;
34
- onError?: (error: any) => void;
42
+ scanId: string;
43
+ onSuccess?: (data: any) => void;
44
+ onError?: (error: any) => void;
35
45
  }
36
46
 
37
47
  class Measurement {
38
- #measurementSocketRef: WebSocket | null = null;
39
- #timerPollingRef: NodeJS.Timeout | null = null;
40
- #timerWaitingRef: NodeJS.Timeout | null = null;
41
- #count: number = 1;
42
- #accessKey: string;
43
- #stagingUrl: boolean;
44
-
45
- constructor(accessKey: string, stagingUrl = false) {
46
- this.#accessKey = accessKey;
47
- this.#stagingUrl = stagingUrl;
48
- }
49
-
50
- getMeasurementResult(scanId: string): Promise<AxiosResponse<any>> {
51
- if (!checkParameters(scanId)) {
52
- throw new Error(REQUIRED_MESSAGE);
53
- }
54
- const url = `${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}/measurements?scanId=${scanId}`;
55
- return axios.get(url, {
56
- headers: { "X-Api-Key": this.#accessKey },
57
- });
58
- }
59
-
60
- getMeasurementRecommendation({ scanId, shopDomain, productName }: MeasurementRecommendation): Promise<AxiosResponse<any>> {
61
- if (!checkParameters(scanId, shopDomain, productName)) {
62
- throw new Error(REQUIRED_MESSAGE);
63
- }
64
- return axios.get(
65
- `${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${
66
- API_ENDPOINTS.RECOMMENDATION
67
- }/scan/${scanId}/shop/${shopDomain}/product/${productName}`,
68
- { headers: { "X-Api-Key": this.#accessKey } }
69
- );
70
- }
71
-
72
-
73
- async #getMeasurementsCheck(options: GetMeasurementsCheckOptions): Promise<void> {
74
- const { scanId, onSuccess, onError } = options;
75
-
76
- try {
77
- const res = await this.getMeasurementResult(scanId);
78
- if (res?.data && res?.data?.isMeasured === true) {
79
- onSuccess?.(res.data);
80
- if (this.#timerPollingRef) {
81
- clearInterval(this.#timerPollingRef);
82
- }
83
- } else {
84
- if (this.#count < 8) {
85
- this.#count++;
86
- this.#handlePolling({ scanId, onSuccess, onError });
87
- } else {
88
- this.#count = 1;
89
- if (this.#timerPollingRef) {
90
- clearInterval(this.#timerPollingRef);
91
- }
92
- onError?.({ scanStatus: "failed", message: "Scan not found", isMeasured: false });
93
- }
94
- }
95
- } catch (e) {
96
- if (this.#timerPollingRef) {
97
- clearInterval(this.#timerPollingRef);
98
- }
99
- onError?.(e);
100
- }
101
- }
102
-
103
- #handlePolling(options: HandlePollingOptions): void {
104
- const { scanId, onSuccess, onError } = options;
105
-
106
- if (this.#timerPollingRef) {
107
- clearInterval(this.#timerPollingRef);
108
- }
109
-
110
- this.#timerPollingRef = setTimeout(() => {
111
- this.#getMeasurementsCheck({ scanId, onSuccess, onError });
112
- }, this.#count * 5000);
113
- }
114
-
115
- #disconnectSocket(): void {
116
- this.#measurementSocketRef?.close();
117
- if (this.#timerWaitingRef) {
118
- clearTimeout(this.#timerWaitingRef);
119
- }
120
- }
121
-
122
- #handleTimeOut(options: HandleTimeOutOptions): void {
123
- const { scanId, onSuccess, onError } = options;
124
-
125
- this.#count = 1;
126
- this.#timerWaitingRef = setTimeout(() => {
127
- this.#handlePolling({ scanId, onSuccess, onError });
128
- this.#disconnectSocket();
129
- }, 2 * 60000);
130
- }
131
-
132
- handleMeasurementSocket(options: MeasurementSocketOptions): void {
133
- const { scanId, onError, onSuccess, onClose, onOpen,isFallback=true } = options;
134
-
135
- if (!checkParameters(scanId)) {
136
- throw new Error(REQUIRED_MESSAGE);
137
- }
138
-
139
- setTimeout(() => {
140
- this.#disconnectSocket();
141
- const url = `${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.SCANNING}?scanId=${scanId}`;
142
- this.#measurementSocketRef = new WebSocket(url);
143
- this.#measurementSocketRef.onopen = () => {
144
- onOpen?.();
145
- if (isFallback) {
146
- this.#handleTimeOut({ scanId, onSuccess, onError });
147
- }
148
- };
149
-
150
- this.#measurementSocketRef.onmessage = (event: MessageEvent) => {
151
- const data = JSON.parse(event.data);
152
- if (data?.code === 200 && data?.scanStatus === "success") {
153
- onSuccess?.(data);
154
- } else {
155
- if (this.#timerWaitingRef){
156
- clearTimeout(this.#timerWaitingRef);
157
- }
158
- onError?.(data);
159
- }
160
- if (this.#timerWaitingRef && data?.code === 200 && data?.scanStatus === "success" && data?.resultType === "final" ) {
161
- clearTimeout(this.#timerWaitingRef);
162
- }
163
- };
164
-
165
- this.#measurementSocketRef.onclose = () => {
166
- onClose?.();
167
- };
168
-
169
- this.#measurementSocketRef.onerror = (event: Event) => {
170
- // onError?.(event);
171
- };
172
- }, 5000);
173
- }
48
+ #measurementSocketRef: WebSocket | null = null;
49
+ #timerPollingRef: NodeJS.Timeout | null = null;
50
+ #timerWaitingRef: NodeJS.Timeout | null = null;
51
+ #count: number = 1;
52
+ #accessKey: string;
53
+ #stagingUrl: boolean;
54
+
55
+ constructor(accessKey: string, stagingUrl = false) {
56
+ this.#accessKey = accessKey;
57
+ this.#stagingUrl = stagingUrl;
58
+ }
59
+
60
+ getMeasurementResult(scanId: string): Promise<AxiosResponse<any>> {
61
+ if (!checkParameters(scanId)) {
62
+ throw new Error(REQUIRED_MESSAGE);
63
+ }
64
+ const url = `${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}/measurements?scanId=${scanId}`;
65
+ return axios.get(url, {
66
+ headers: { "X-Api-Key": this.#accessKey },
67
+ });
68
+ }
69
+
70
+ getMeasurementRecommendation({ scanId, shopDomain, productName }: MeasurementRecommendation): Promise<AxiosResponse<any>> {
71
+ if (!checkParameters(scanId, shopDomain, productName)) {
72
+ throw new Error(REQUIRED_MESSAGE);
73
+ }
74
+ return axios.get(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.RECOMMENDATION}/scan/${scanId}/shop/${shopDomain}/product/${productName}`, {
75
+ headers: { "X-Api-Key": this.#accessKey },
76
+ });
77
+ }
78
+
79
+ async #getMeasurementsCheck(options: GetMeasurementsCheckOptions): Promise<void> {
80
+ const { scanId, onSuccess, onError } = options;
81
+
82
+ try {
83
+ const res = await this.getMeasurementResult(scanId);
84
+ if (res?.data && res?.data?.isMeasured === true) {
85
+ onSuccess?.(res.data);
86
+ if (this.#timerPollingRef) {
87
+ clearInterval(this.#timerPollingRef);
88
+ }
89
+ } else {
90
+ if (this.#count < 8) {
91
+ this.#count++;
92
+ this.#handlePolling({ scanId, onSuccess, onError });
93
+ } else {
94
+ this.#count = 1;
95
+ if (this.#timerPollingRef) {
96
+ clearInterval(this.#timerPollingRef);
97
+ }
98
+ onError?.({ scanStatus: "failed", message: "Scan not found", isMeasured: false });
99
+ }
100
+ }
101
+ } catch (e) {
102
+ if (this.#timerPollingRef) {
103
+ clearInterval(this.#timerPollingRef);
104
+ }
105
+ onError?.(e);
106
+ }
107
+ }
108
+
109
+ #handlePolling(options: HandlePollingOptions): void {
110
+ const { scanId, onSuccess, onError } = options;
111
+
112
+ if (this.#timerPollingRef) {
113
+ clearInterval(this.#timerPollingRef);
114
+ }
115
+
116
+ this.#timerPollingRef = setTimeout(() => {
117
+ this.#getMeasurementsCheck({ scanId, onSuccess, onError });
118
+ }, this.#count * 5000);
119
+ }
120
+
121
+ #disconnectSocket(): void {
122
+ this.#measurementSocketRef?.close();
123
+ if (this.#timerWaitingRef) {
124
+ clearTimeout(this.#timerWaitingRef);
125
+ }
126
+ }
127
+
128
+ #handleTimeOut(options: HandleTimeOutOptions): void {
129
+ const { scanId, onSuccess, onError } = options;
130
+
131
+ this.#count = 1;
132
+ this.#timerWaitingRef = setTimeout(() => {
133
+ this.#handlePolling({ scanId, onSuccess, onError });
134
+ this.#disconnectSocket();
135
+ }, 2 * 60000);
136
+ }
137
+
138
+ handleMeasurementSocket(options: MeasurementSocketOptions): void {
139
+ const { scanId, onError, onSuccess, onClose, onOpen } = options;
140
+
141
+ if (!checkParameters(scanId)) {
142
+ throw new Error(REQUIRED_MESSAGE);
143
+ }
144
+ this.#handleSocket({ onOpen, scanId, onSuccess, onError, onClose, paramsKey: "scanId", isFallback: true,delay:5000 });
145
+ }
146
+ handlFaceScaneSocket(options: FaceScanSocketOptions): void {
147
+ const { faceScanId, onError, onSuccess, onClose, onOpen } = options;
148
+
149
+ if (!checkParameters(faceScanId)) {
150
+ throw new Error(REQUIRED_MESSAGE);
151
+ }
152
+ this.#handleSocket({ onOpen, faceScanId, onSuccess, onError, onClose, paramsKey: "faceScanId", isFallback: false,delay:0 });
153
+ }
154
+ #handleSocket({ onOpen, isFallback, scanId, onSuccess, onError, onClose, paramsKey, faceScanId,delay }: HandleSocket) {
155
+ setTimeout(() => {
156
+ this.#disconnectSocket();
157
+ const url = `${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.SCANNING}?${paramsKey}=${scanId || faceScanId}`;
158
+ this.#measurementSocketRef = new WebSocket(url);
159
+ this.#measurementSocketRef.onopen = () => {
160
+ onOpen?.();
161
+ if (isFallback && scanId) {
162
+ this.#handleTimeOut({ scanId, onSuccess, onError });
163
+ }
164
+ };
165
+
166
+ this.#measurementSocketRef.onmessage = (event: MessageEvent) => {
167
+ const data = JSON.parse(event.data);
168
+ if (data?.code === 200 && data?.scanStatus === "success") {
169
+ onSuccess?.(data);
170
+ } else {
171
+ if (this.#timerWaitingRef) {
172
+ clearTimeout(this.#timerWaitingRef);
173
+ }
174
+ onError?.(data);
175
+ }
176
+ if (this.#timerWaitingRef && data?.code === 200 && data?.scanStatus === "success" && data?.resultType === "final") {
177
+ clearTimeout(this.#timerWaitingRef);
178
+ }
179
+ };
180
+
181
+ this.#measurementSocketRef.onclose = () => {
182
+ onClose?.();
183
+ };
184
+
185
+ this.#measurementSocketRef.onerror = (event: Event) => {
186
+ // onError?.(event);
187
+ };
188
+ }, delay);
189
+ }
174
190
  }
175
191
 
176
192
  export default Measurement;