@swan-admin/swan-ai-measurements 1.1.66 → 1.1.68

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