@swan-admin/swan-ai-measurements 1.0.52 → 1.0.55

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/auth.js CHANGED
@@ -1,18 +1,39 @@
1
- import axios from "axios";
2
- import {
3
- API_ENDPOINTS,
4
- APP_AUTH_BASE_URL,
5
- APP_AUTH_WEBSOCKET_URL,
6
- APP_BASE_URL,
7
- REQUIRED_MESSAGE,
8
- } from "./constants.js";
9
- import { checkParameters } from "./utils.js";
1
+ const axios = require("axios");
2
+ const { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_AUTH_WEBSOCKET_URL, APP_BASE_URL, REQUIRED_MESSAGE } = require("./constants.js");
3
+ const { checkParameters } = require("./utils.js");
10
4
 
5
+ /**
6
+ * Represents a Auth class for handling authentication operations.
7
+ */
11
8
  class Auth {
12
9
  #socketRef;
13
10
 
14
- registerUser({ email, appVerifyUrl, gender, height, username, accessKey }) {
15
- if (checkParameters(email, appVerifyUrl, accessKey) === false) {
11
+ /**
12
+ * The access key used for authentication.
13
+ * @type {string}
14
+ * @private
15
+ */
16
+ #accessKey;
17
+
18
+ /**
19
+ * Constructs a new instance of the Auth class.
20
+ * @param {string} accessKey - The access key used for authentication.
21
+ */
22
+ constructor(accessKey) {
23
+ this.#accessKey = accessKey;
24
+ }
25
+ /**
26
+ * Register a new user.
27
+ * @param {Object} params - The parameters for user registration.
28
+ * @param {string} params.email - The email of the user.
29
+ * @param {string} params.appVerifyUrl - The verification URL.
30
+ * @param {string} [params.gender] - Optional. The gender of the user.
31
+ * @param {string} [params.height] - Optional. The height of the user.
32
+ * @param {string} params.username - Optional. The username of the user.
33
+ * @returns {Promise} - The axios response promise.
34
+ */
35
+ registerUser({ email, appVerifyUrl, gender, height, username }) {
36
+ if (checkParameters(email, appVerifyUrl) === false) {
16
37
  throw new Error(REQUIRED_MESSAGE);
17
38
  }
18
39
  let body = {
@@ -24,22 +45,40 @@ class Auth {
24
45
  body = { ...body, attributes: { gender, height } };
25
46
  }
26
47
  return axios.post(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.REGISTER_USER}`, body, {
27
- headers: { "X-Api-Key": accessKey },
48
+ headers: { "X-Api-Key": this.#accessKey },
28
49
  });
29
50
  }
30
51
 
52
+ /**
53
+ * Verify a user token.
54
+ * @param {string} token
55
+ * @returns {Promise}
56
+ */
57
+
31
58
  verifyToken = (token, accessKey) => {
32
- if (checkParameters(token, accessKey) === false) {
59
+ if (checkParameters(token) === false) {
33
60
  throw new Error(REQUIRED_MESSAGE);
34
61
  }
35
62
  return axios.post(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.VERIFY_USER}`, null, {
36
63
  params: { token },
37
- headers: { "X-Api-Key": accessKey },
64
+ headers: { "X-Api-Key": this.#accessKey },
38
65
  });
39
66
  };
40
67
 
41
- addUser = ({ scanId, email, name, height, gender, offsetMarketingConsent, accessKey }) => {
42
- if (checkParameters(scanId, email, height, gender, accessKey) === false) {
68
+ /**
69
+ * Add a user.
70
+ * @param {Object} params
71
+ * @param {string} params.scanId - The scan ID.
72
+ * @param {string} params.email - The email of the user.
73
+ * @param {string} params.name - The name of the user.
74
+ * @param {string} params.height - The height of the user.
75
+ * @param {string} params.gender - The gender of the user.
76
+ * @param {boolean} [params.offsetMarketingConsent] - Optional. The marketing consent offset.
77
+ * @returns {Promise} - The axios response promise.
78
+ */
79
+
80
+ addUser = ({ scanId, email, name, height, gender, offsetMarketingConsent }) => {
81
+ if (checkParameters(scanId, email, height, gender) === false) {
43
82
  throw new Error(REQUIRED_MESSAGE);
44
83
  }
45
84
  return axios.post(
@@ -51,21 +90,36 @@ class Auth {
51
90
  offsetMarketingConsent,
52
91
  attributes: JSON.stringify({ height, gender }),
53
92
  },
54
- { headers: { "X-Api-Key": accessKey } }
93
+ { headers: { "X-Api-Key": this.#accessKey } }
55
94
  );
56
95
  };
57
96
 
58
- getUserDetail = (email, accessKey) => {
59
- if (checkParameters(email, accessKey) === false) {
97
+ /**
98
+ * Get user details.
99
+ * @param {string} email
100
+ * @returns {Promise}
101
+ */
102
+
103
+ getUserDetail = (email) => {
104
+ if (checkParameters(email) === false) {
60
105
  throw new Error(REQUIRED_MESSAGE);
61
106
  }
62
107
  return axios.get(`${APP_BASE_URL}${API_ENDPOINTS.GET_USER_DETAIL}/${email}`, {
63
- headers: { "X-Api-Key": accessKey },
108
+ headers: { "X-Api-Key": this.#accessKey },
64
109
  });
65
110
  };
66
-
67
- handleAuthSocket = ({ email, scanId, onError, onSuccess, onClose, onOpen, accessKey }) => {
68
- if (checkParameters(email, scanId, accessKey) === false) {
111
+ /**
112
+ * Handle authentication via WebSocket.
113
+ * @param {Object} params
114
+ * @param {string} params.email - The email address of the user.
115
+ * @param {string} params.scanId - The scan ID associated with the user.
116
+ * @param {function} [params.onError] - Optional. Callback function to handle errors.
117
+ * @param {function} [params.onSuccess] - Optional. Callback function to handle successful authentication.
118
+ * @param {function} [params.onClose] - Optional. Callback function to handle the WebSocket close event.
119
+ * @param {function} [params.onOpen] - Optional. Callback function to handle the WebSocket open event.
120
+ */
121
+ handleAuthSocket = ({ email, scanId, onError, onSuccess, onClose, onOpen }) => {
122
+ if (checkParameters(email, scanId) === false) {
69
123
  throw new Error(REQUIRED_MESSAGE);
70
124
  }
71
125
  this.#socketRef?.close?.();
@@ -96,4 +150,4 @@ class Auth {
96
150
  };
97
151
  }
98
152
 
99
- export default Auth;
153
+ module.exports = Auth;
package/custom.js CHANGED
@@ -1,22 +1,54 @@
1
- import axios from "axios";
2
- import { API_ENDPOINTS, APP_AUTH_BASE_URL, REQUIRED_MESSAGE } from "./constants.js";
3
- import { checkParameters } from "./utils.js";
1
+ const axios = require("axios");
2
+ const { API_ENDPOINTS, APP_AUTH_BASE_URL, REQUIRED_MESSAGE } = require("./constants.js");
3
+ const { checkParameters } = require("./utils.js");
4
4
 
5
- export default class Custom {
6
- getCustomCustomerConfig = (store_url, accessKey) => {
7
- if (checkParameters(store_url, accessKey) === false) {
5
+ /**
6
+ * Represents a Custom class for handling custom operations.
7
+ */
8
+ class Custom {
9
+ /**
10
+ * The access key used for authentication.
11
+ * @type {string}
12
+ * @private
13
+ */
14
+ #accessKey;
15
+ /**
16
+ * Constructs a new instance of the Custom class.
17
+ * @param {string} accessKey - The access key used for authentication.
18
+ */
19
+ constructor(accessKey) {
20
+ this.#accessKey = accessKey;
21
+ }
22
+
23
+ /**
24
+ * Retrieves custom customer configuration based on the store URL.
25
+ * @param {string} store_url - The URL of the store.
26
+ * @returns {Promise} - A promise that resolves with the custom customer configuration.
27
+ * @throws {Error} - If required parameters are missing.
28
+ */
29
+
30
+ getCustomCustomerConfig = (store_url) => {
31
+ if (checkParameters(store_url) === false) {
8
32
  throw new Error(REQUIRED_MESSAGE);
9
33
  }
10
34
  return axios.get(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.CUSTOM_CUSTOMER}`, {
11
35
  params: { store_url },
12
- headers: { "X-Api-Key": accessKey },
36
+ headers: { "X-Api-Key": this.#accessKey },
13
37
  });
14
38
  };
15
39
 
16
- getModelUrl = (id, accessKey) => {
17
- if (checkParameters(id, accessKey) === false) {
40
+ /**
41
+ * Retrieves the model URL based on the model ID.
42
+ * @param {string} id - The ID of the model.
43
+ * @returns {Promise} - A promise that resolves with the model URL.
44
+ * @throws {Error} - If required parameters are missing.
45
+ */
46
+ getModelUrl = (id) => {
47
+ if (checkParameters(id) === false) {
18
48
  throw new Error(REQUIRED_MESSAGE);
19
49
  }
20
- return axios.get(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.MODEL}/${id}`, { headers: { "X-Api-Key": accessKey } });
50
+ return axios.get(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.MODEL}/${id}`, { headers: { "X-Api-Key": this.#accessKey } });
21
51
  };
22
52
  }
53
+
54
+ module.exports = Custom;
package/fileUpload.js CHANGED
@@ -1,24 +1,78 @@
1
- import AwsS3Multipart from "@uppy/aws-s3-multipart";
2
- import Uppy from "@uppy/core";
3
- import { REQUIRED_MESSAGE, REQUIRED_MESSAGE_FOR_META_DATA, UPPY_FILE_UPLOAD_ENDPOINT } from "./constants.js";
4
- import { checkMetaDataValue, checkParameters, fetchData } from "./utils.js";
5
-
6
- export default class FileUpload {
1
+ const { REQUIRED_MESSAGE, REQUIRED_MESSAGE_FOR_META_DATA, UPPY_FILE_UPLOAD_ENDPOINT } = require("./constants.js");
2
+ const { checkMetaDataValue, checkParameters, fetchData } = require("./utils.js");
3
+ /**
4
+ * Class representing a file uploader using Uppy for multipart uploads.
5
+ */
6
+ class FileUpload {
7
+ /**
8
+ * The Uppy instance.
9
+ * @type {Object}
10
+ * @private
11
+ */
7
12
  #uppyIns;
8
13
 
9
- uploadFile({ file, objMetaData, scanId, accessKey }) {
10
- if (checkParameters(file, objMetaData, scanId, accessKey) === false) {
14
+ /**
15
+ * Reference to the Uppy module.
16
+ * @type {Object}
17
+ * @private
18
+ */
19
+ #Uppy;
20
+ /**
21
+ * Reference to the AwsS3Multipart module.
22
+ * @type {Object}
23
+ * @private
24
+ */
25
+
26
+ #AwsS3Multipart;
27
+ /**
28
+ * The access key used for authentication.
29
+ * @type {string}
30
+ * @private
31
+ */
32
+ #accessKey;
33
+ /**
34
+ * Constructs a new instance of the FileUpload class.
35
+ * @param {string} accessKey - The access key used for authentication.
36
+ */
37
+ constructor(accessKey) {
38
+ this.initializeModules();
39
+ this.#accessKey = accessKey;
40
+ }
41
+ /**
42
+ * Asynchronously initializes the Uppy and AwsS3Multipart modules.
43
+ * @private
44
+ */
45
+ async initializeModules() {
46
+ this.#Uppy = (await import("@uppy/core")).default;
47
+ this.#AwsS3Multipart = (await import("@uppy/aws-s3-multipart")).default;
48
+ }
49
+ /**
50
+ * Uploads a file with optional metadata and scan ID.
51
+ * @param {Object} params - The parameters for file upload.
52
+ * @param {File} params.file - The file to upload.
53
+ * @param {Object} params.objMetaData - Optional. Metadata associated with the file.
54
+ * @param {string} params.scanId - Optional. The ID of the scan.
55
+ * @returns {Promise} - A promise that resolves when the file is uploaded successfully.
56
+ * @throws {Error} - If required parameters are missing or metadata value is invalid.
57
+ */
58
+ async uploadFile({ file, objMetaData, scanId }) {
59
+ if (checkParameters(file, objMetaData, scanId) === false) {
11
60
  throw new Error(REQUIRED_MESSAGE);
12
61
  }
13
62
  if (checkMetaDataValue(objMetaData) === false) {
14
63
  throw new Error(REQUIRED_MESSAGE_FOR_META_DATA);
15
64
  }
65
+
66
+ if (!this.#Uppy || !this.#AwsS3Multipart) {
67
+ await this.initializeModules();
68
+ }
69
+
16
70
  return new Promise((resolve, reject) => {
17
71
  if (this.#uppyIns) {
18
72
  this.#uppyIns.close();
19
73
  }
20
- this.#uppyIns = new Uppy({ autoProceed: true });
21
- this.#uppyIns.use(AwsS3Multipart, {
74
+ this.#uppyIns = new this.#Uppy({ autoProceed: true });
75
+ this.#uppyIns.use(this.#AwsS3Multipart, {
22
76
  limit: 10,
23
77
  retryDelays: [0, 1000, 3000, 5000],
24
78
  getChunkSize: () => 5 * 1024 * 1024,
@@ -26,7 +80,7 @@ export default class FileUpload {
26
80
  const objectKey = `${scanId}.${file.extension}`;
27
81
  return fetchData({
28
82
  path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_START,
29
- apiKey: accessKey,
83
+ apiKey: this.#accessKey,
30
84
  body: {
31
85
  objectKey,
32
86
  contentType: file.type,
@@ -37,7 +91,7 @@ export default class FileUpload {
37
91
  completeMultipartUpload: (file, { uploadId, key, parts }) =>
38
92
  fetchData({
39
93
  path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
40
- apiKey: accessKey,
94
+ apiKey: this.#accessKey,
41
95
  body: {
42
96
  uploadId,
43
97
  objectKey: key,
@@ -45,22 +99,20 @@ export default class FileUpload {
45
99
  originalFileName: file.name,
46
100
  },
47
101
  }),
48
-
49
102
  signPart: (file, partData) =>
50
103
  fetchData({
51
104
  path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
52
- apiKey: accessKey,
105
+ apiKey: this.#accessKey,
53
106
  body: {
54
107
  objectKey: partData.key,
55
108
  uploadId: partData.uploadId,
56
109
  partNumber: partData.partNumber,
57
110
  },
58
111
  }),
59
-
60
112
  abortMultipartUpload: (file, { uploadId, key }) =>
61
113
  fetchData({
62
114
  path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_ABORT,
63
- apiKey: accessKey,
115
+ apiKey: this.#accessKey,
64
116
  body: {
65
117
  uploadId,
66
118
  objectKey: key,
@@ -90,3 +142,5 @@ export default class FileUpload {
90
142
  });
91
143
  }
92
144
  }
145
+
146
+ module.exports = FileUpload;
package/index.js CHANGED
@@ -1,19 +1,25 @@
1
- import Auth from "./auth.js";
2
- import Custom from "./custom.js";
3
- import FileUpload from "./fileUpload.js";
4
- import Measurement from "./measurement.js";
5
- import PoseDetection from "./poseDetection.js";
6
- import TryOn from "./tryOn.js";
7
- export default class Swan {
8
- auth = new Auth();
1
+ const Auth = require("./auth.js");
2
+ const Custom = require("./custom.js");
3
+ const FileUpload = require("./fileUpload.js");
4
+ const Measurement = require("./measurement.js");
5
+ const PoseDetection = require("./poseDetection.js");
6
+ const TryOn = require("./tryOn.js");
7
+ class Swan {
8
+ #accessKey;
9
+ constructor(accessKey) {
10
+ this.#accessKey = accessKey;
11
+ }
12
+ auth = new Auth(this.#accessKey);
9
13
 
10
- custom = new Custom();
14
+ custom = new Custom(this.#accessKey);
11
15
 
12
- fileUpload = new FileUpload();
16
+ fileUpload = new FileUpload(this.#accessKey);
13
17
 
14
- measurement = new Measurement();
18
+ measurement = new Measurement(this.#accessKey);
15
19
 
16
- poseDetection = new PoseDetection();
20
+ poseDetection = new PoseDetection(this.#accessKey);
17
21
 
18
- tryOn = new TryOn();
22
+ tryOn = new TryOn(this.#accessKey);
19
23
  }
24
+
25
+ module.exports = Swan;
package/measurement.js CHANGED
@@ -1,39 +1,72 @@
1
- import axios from "axios";
2
- import {
3
- API_ENDPOINTS,
4
- APP_AUTH_BASE_URL,
5
- APP_RECOMMENDATION_WEBSOCKET_URL,
6
- APP_TRY_ON_WEBSOCKET_URL,
7
- REQUIRED_MESSAGE,
8
- } from "./constants.js";
9
- import { checkParameters } from "./utils.js";
10
-
11
- export default class Measurement {
1
+ const axios = require("axios");
2
+ const { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_RECOMMENDATION_WEBSOCKET_URL, APP_TRY_ON_WEBSOCKET_URL, REQUIRED_MESSAGE } = require("./constants.js");
3
+ const { checkParameters } = require("./utils.js");
4
+
5
+ /**
6
+ * Class representing measurement-related functionality.
7
+ */
8
+ class Measurement {
12
9
  #tryOnSocketRef = null;
13
10
  #measurementSocketRef = null;
14
11
  #timerPollingRef = null;
15
12
  #timerWaitingRef = null;
16
13
  #count = 1;
17
- getMeasurementStatus(scanId, accessKey) {
18
- if (checkParameters(scanId, accessKey) === false) {
14
+ #accessKey;
15
+
16
+ /**
17
+ * Constructs a new instance of the Measurement class.
18
+ * @param {string} accessKey - The access key used for authentication.
19
+ */
20
+ constructor(accessKey) {
21
+ this.#accessKey = accessKey;
22
+ }
23
+
24
+ /**
25
+ * Retrieves the measurement status for a given scan ID.
26
+ * @param {string} scanId - The ID of the scan.
27
+ * @returns {Promise} - The axios response promise.
28
+ * @throws {Error} - If the required parameter is missing.
29
+ */
30
+ getMeasurementStatus(scanId) {
31
+ if (checkParameters(scanId) === false) {
19
32
  throw new Error(REQUIRED_MESSAGE);
20
33
  }
21
34
  const url = `${APP_AUTH_BASE_URL}/measurements?scanId=${scanId}`;
22
35
  return axios.get(url, {
23
- headers: { "X-Api-Key": accessKey },
36
+ headers: { "X-Api-Key": this.#accessKey },
24
37
  });
25
38
  }
26
39
 
27
- getTryOnMeasurements({ scanId, shopDomain, productName, accessKey }) {
28
- if (checkParameters(scanId, shopDomain, productName, accessKey) === false) {
40
+ /**
41
+ * Retrieves the try-on measurements for a given scan ID, shop domain, and product name.
42
+ * @param {Object} params - The parameters for the try-on measurements.
43
+ * @param {string} params.scanId - The ID of the scan.
44
+ * @param {string} params.shopDomain - The shop domain.
45
+ * @param {string} params.productName - The product name.
46
+ * @returns {Promise} - The axios response promise.
47
+ * @throws {Error} - If the required parameters are missing.
48
+ */
49
+ getTryOnMeasurements({ scanId, shopDomain, productName }) {
50
+ if (checkParameters(scanId, shopDomain, productName) === false) {
29
51
  throw new Error(REQUIRED_MESSAGE);
30
52
  }
31
53
  const tryOnUrl = `${APP_AUTH_BASE_URL}${API_ENDPOINTS.TRY_ON_SCAN}/${scanId}/shop/${shopDomain}/product/${productName}`;
32
- return axios.get(tryOnUrl, { headers: { "X-Api-Key": accessKey } });
54
+ return axios.get(tryOnUrl, { headers: { "X-Api-Key": this.#accessKey } });
33
55
  }
34
56
 
35
- handleTryOnSocket({ shopDomain, scanId, productName, onError, onSuccess, onClose, onOpen, accessKey }) {
36
- if (checkParameters(shopDomain, scanId, productName, accessKey) === false) {
57
+ /**
58
+ * Handles the try-on WebSocket connection.
59
+ * @param {Object} params - The parameters for the WebSocket connection.
60
+ * @param {string} params.shopDomain - The shop domain.
61
+ * @param {string} params.scanId - The ID of the scan.
62
+ * @param {string} params.productName - The product name.
63
+ * @param {function} [params.onError] - Optional. Callback function to handle errors.
64
+ * @param {function} [params.onSuccess] - Optional. Callback function to handle successful messages.
65
+ * @param {function} [params.onClose] - Optional. Callback function to handle WebSocket close event.
66
+ * @param {function} [params.onOpen] - Optional. Callback function to handle WebSocket open event.
67
+ */
68
+ handleTryOnSocket({ shopDomain, scanId, productName, onError, onSuccess, onClose, onOpen }) {
69
+ if (checkParameters(shopDomain, scanId, productName) === false) {
37
70
  throw new Error(REQUIRED_MESSAGE);
38
71
  }
39
72
  this.#tryOnSocketRef?.close();
@@ -62,60 +95,96 @@ export default class Measurement {
62
95
  };
63
96
  }
64
97
 
65
- #getMeasurementsCheck = async ({ scanId, onSuccess, onError, accessKey }) => {
98
+ /**
99
+ * Checks the measurement status and handles polling.
100
+ * @param {Object} params - The parameters for checking measurements.
101
+ * @param {string} params.scanId - The ID of the scan.
102
+ * @param {function} [params.onSuccess] - Optional. Callback function to handle successful status check.
103
+ * @param {function} [params.onError] - Optional. Callback function to handle errors.
104
+ * @private
105
+ */
106
+ async #getMeasurementsCheck({ scanId, onSuccess, onError }) {
66
107
  try {
67
- const res = await this.getMeasurementStatus(scanId, accessKey);
108
+ const res = await this.getMeasurementStatus(scanId);
68
109
  if (res?.data && res?.data?.[0]?.isMeasured === true) {
69
110
  onSuccess?.(res?.data);
70
111
  clearInterval(this.#timerPollingRef);
71
112
  } else {
72
113
  if (this.#count < 8) {
73
114
  this.#count++;
74
- this.#handlePolling({ scanId, onSuccess, onError, accessKey });
115
+ this.#handlePolling({ scanId, onSuccess, onError });
75
116
  } else {
76
117
  this.#count = 1;
77
118
  clearInterval(this.#timerPollingRef);
78
- onError?.({ scanStatus: "failed", message: "Scan not found" });
119
+ onError?.({ scanStatus: "failed", message: "Scan not found", isMeasured: false });
79
120
  }
80
121
  }
81
122
  } catch (e) {
82
123
  clearInterval(this.#timerPollingRef);
83
124
  onError?.(e);
84
125
  }
85
- };
126
+ }
86
127
 
87
- #handlePolling({ scanId, onSuccess, onError, accessKey }) {
128
+ /**
129
+ * Handles polling for measurements.
130
+ * @param {Object} params - The parameters for polling.
131
+ * @param {string} params.scanId - The ID of the scan.
132
+ * @param {function} [params.onSuccess] - Optional. Callback function to handle successful polling.
133
+ * @param {function} [params.onError] - Optional. Callback function to handle errors.
134
+ * @private
135
+ */
136
+ #handlePolling({ scanId, onSuccess, onError }) {
88
137
  clearInterval(this.#timerPollingRef);
89
138
  this.#timerPollingRef = setTimeout(() => {
90
- this.#getMeasurementsCheck({ scanId, onSuccess, onError, accessKey });
139
+ this.#getMeasurementsCheck({ scanId, onSuccess, onError });
91
140
  }, this.#count * 5000);
92
141
  }
93
142
 
94
- #disconnectSocket = () => {
143
+ /**
144
+ * Disconnects the measurement WebSocket and clears the timeout.
145
+ * @private
146
+ */
147
+ #disconnectSocket() {
95
148
  this.#measurementSocketRef?.close();
96
149
  clearTimeout(this.#timerWaitingRef);
97
- };
150
+ }
98
151
 
99
- #handleTimeOut = ({ scanId, onSuccess, onError, accessKey }) => {
152
+ /**
153
+ * Handles the timeout for the measurement WebSocket.
154
+ * @param {Object} params - The parameters for handling timeout.
155
+ * @param {string} params.scanId - The ID of the scan.
156
+ * @param {function} [params.onSuccess] - Optional. Callback function to handle successful timeout.
157
+ * @param {function} [params.onError] - Optional. Callback function to handle errors.
158
+ * @private
159
+ */
160
+ #handleTimeOut({ scanId, onSuccess, onError }) {
100
161
  this.#count = 1;
101
162
  this.#timerWaitingRef = setTimeout(() => {
102
- this.#handlePolling({ scanId, onSuccess, onError, accessKey });
163
+ this.#handlePolling({ scanId, onSuccess, onError });
103
164
  this.#disconnectSocket();
104
165
  }, 2 * 60000);
105
- };
166
+ }
106
167
 
107
- handleMeasurementSocket = ({ scanId, onError, onSuccess, onClose, onOpen, accessKey }) => {
108
- if (checkParameters(scanId, accessKey) === false) {
168
+ /**
169
+ * Handles the measurement WebSocket connection.
170
+ * @param {Object} params - The parameters for the WebSocket connection.
171
+ * @param {string} params.scanId - The ID of the scan.
172
+ * @param {function} [params.onError] - Optional. Callback function to handle errors.
173
+ * @param {function} [params.onSuccess] - Optional. Callback function to handle successful messages.
174
+ * @param {function} [params.onClose] - Optional. Callback function to handle WebSocket close event.
175
+ * @param {function} [params.onOpen] - Optional. Callback function to handle WebSocket open event.
176
+ */
177
+ handleMeasurementSocket({ scanId, onError, onSuccess, onClose, onOpen }) {
178
+ if (checkParameters(scanId) === false) {
109
179
  throw new Error(REQUIRED_MESSAGE);
110
180
  }
111
181
  setTimeout(() => {
112
182
  this.#disconnectSocket();
113
183
  const url = `${APP_RECOMMENDATION_WEBSOCKET_URL}?scanId=${scanId}`;
114
184
  this.#measurementSocketRef = new WebSocket(url);
115
-
116
185
  this.#measurementSocketRef.onopen = () => {
117
186
  onOpen?.();
118
- this.#handleTimeOut({ scanId, onSuccess, onError, accessKey });
187
+ this.#handleTimeOut({ scanId, onSuccess, onError });
119
188
  };
120
189
 
121
190
  this.#measurementSocketRef.onmessage = (event) => {
@@ -136,5 +205,7 @@ export default class Measurement {
136
205
  onError?.(event);
137
206
  };
138
207
  }, 5000);
139
- };
208
+ }
140
209
  }
210
+
211
+ module.exports = Measurement;
package/package.json CHANGED
@@ -1,22 +1,38 @@
1
1
  {
2
2
  "name": "@swan-admin/swan-ai-measurements",
3
- "version": "1.0.52",
3
+ "version": "1.0.55",
4
4
  "description": "provides ai measurement suggestion",
5
5
  "main": "index.js",
6
- "type": "module",
6
+ "types": "types/index.d.ts",
7
+ "files": [
8
+ "index.js",
9
+ "auth.js",
10
+ "constant.js",
11
+ "custom.js",
12
+ "fileUpload.js",
13
+ "measurement.js",
14
+ "poseDetection.js",
15
+ "tryOn.js",
16
+ "utils.js",
17
+ "types/"
18
+ ],
7
19
  "dependencies": {
20
+ "@types/axios": "^0.14.0",
8
21
  "@uppy/aws-s3-multipart": "^3.10.2",
9
22
  "@uppy/core": "^3.9.3",
10
23
  "axios": "^1.6.7",
11
24
  "dotenv": "^16.4.5",
25
+ "socket.io": "^4.7.5",
12
26
  "socket.io-client": "^4.7.5"
13
27
  },
14
28
  "devDependencies": {
15
- "nodemon": "^3.1.0"
29
+ "nodemon": "^3.1.0",
30
+ "typescript": "^5.4.5"
16
31
  },
17
32
  "scripts": {
18
33
  "start": "nodemon index.js",
19
- "test": "echo \"Error: no test specified\" && exit 1"
34
+ "test": "echo \"Error: no test specified\" && exit 1",
35
+ "build:types": "tsc"
20
36
  },
21
37
  "repository": {
22
38
  "type": "git",