@swan-admin/swan-ai-measurements 1.0.30 → 1.0.32
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/constants.js +4 -0
- package/index.js +4 -0
- package/measurement.js +5 -5
- package/package.json +1 -1
- package/tryOn.js +143 -0
package/constants.js
CHANGED
|
@@ -22,6 +22,10 @@ export const API_ENDPOINTS = {
|
|
|
22
22
|
CUSTOM_CUSTOMER: "/customers/custom",
|
|
23
23
|
MODEL: "/model",
|
|
24
24
|
TRY_ON: "/tryon/scan",
|
|
25
|
+
TRY_ON_IMAGE_UPLOAD: "/tryon/user-image-urls/upload",
|
|
26
|
+
TRY_ON_IMAGE_DOWNLOAD: "/tryon/user-image-urls/download",
|
|
27
|
+
TRY_ON_IMAGE_URLS: "/tryon/user-image-urls",
|
|
28
|
+
TRY_ON_RESULT_IMAGE_DOWNLOAD: "/tryon/result-image-urls/download",
|
|
25
29
|
};
|
|
26
30
|
|
|
27
31
|
export const REQUIRED_MESSAGE = "Please verify required parameters";
|
package/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import Custom from "./custom.js";
|
|
|
3
3
|
import FileUpload from "./fileUpload.js";
|
|
4
4
|
import Measurement from "./measurement.js";
|
|
5
5
|
import PoseDetection from "./poseDetection.js";
|
|
6
|
+
import TryOn from "./tryOn.js";
|
|
6
7
|
export default class Swan {
|
|
7
8
|
static accessKey;
|
|
8
9
|
constructor(key) {
|
|
@@ -21,4 +22,7 @@ export default class Swan {
|
|
|
21
22
|
measurement = new Measurement(Swan.accessKey);
|
|
22
23
|
|
|
23
24
|
poseDetection = new PoseDetection(Swan.accessKey);
|
|
25
|
+
|
|
26
|
+
tryOn = new TryOn(Swan.accessKey);
|
|
24
27
|
}
|
|
28
|
+
const a = new Swan(9876543210);
|
package/measurement.js
CHANGED
|
@@ -11,13 +11,13 @@ import { checkParameters } from "./utils.js";
|
|
|
11
11
|
|
|
12
12
|
export default class Measurement {
|
|
13
13
|
static accessKey;
|
|
14
|
-
constructor(key) {
|
|
15
|
-
Measurement.accessKey = key;
|
|
16
|
-
}
|
|
17
14
|
static tryOnSocketRef = null;
|
|
18
15
|
static measurementSocketRef = null;
|
|
19
16
|
static timerPollingRef = null;
|
|
20
17
|
static timerWaitingRef = null;
|
|
18
|
+
constructor(key) {
|
|
19
|
+
Measurement.accessKey = key;
|
|
20
|
+
}
|
|
21
21
|
|
|
22
22
|
getMeasurementStatus(scanId) {
|
|
23
23
|
if (!scanId) {
|
|
@@ -70,7 +70,7 @@ export default class Measurement {
|
|
|
70
70
|
|
|
71
71
|
static getMeasurementsCheck = async (onSuccess, onError, scanId) => {
|
|
72
72
|
try {
|
|
73
|
-
const res = await getMeasurementStatus(scanId);
|
|
73
|
+
const res = await this.getMeasurementStatus(scanId);
|
|
74
74
|
if (res?.data && res?.data?.[0]?.isMeasured === true) {
|
|
75
75
|
onSuccess?.(res?.data);
|
|
76
76
|
clearInterval(Measurement.timerPollingRef);
|
|
@@ -96,7 +96,7 @@ export default class Measurement {
|
|
|
96
96
|
static handleTimeOut = (onSuccess, onError) => {
|
|
97
97
|
Measurement.timerWaitingRef = setTimeout(() => {
|
|
98
98
|
Measurement.handlePolling(onSuccess, onError);
|
|
99
|
-
disconnectSocket();
|
|
99
|
+
Measurement.disconnectSocket();
|
|
100
100
|
}, 2 * 60000);
|
|
101
101
|
};
|
|
102
102
|
|
package/package.json
CHANGED
package/tryOn.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { API_ENDPOINTS, APP_AUTH_BASE_URL, REQUIRED_MESSAGE } from "./constants";
|
|
3
|
+
import { checkParameters } from "./utils";
|
|
4
|
+
|
|
5
|
+
class TryOn {
|
|
6
|
+
static accessKey;
|
|
7
|
+
static tryOnSocketRef = null;
|
|
8
|
+
static timerWaitingRef = null;
|
|
9
|
+
constructor(accessKey) {
|
|
10
|
+
TryOn.accessKey = accessKey;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
uploadFile(files, userId) {
|
|
14
|
+
return new Promise(async (resolve, reject) => {
|
|
15
|
+
try {
|
|
16
|
+
const payload = {
|
|
17
|
+
userId,
|
|
18
|
+
userImages: [files?.[0]?.name],
|
|
19
|
+
};
|
|
20
|
+
if (files?.[1]) {
|
|
21
|
+
payload.userImages.push(files?.[1]?.name);
|
|
22
|
+
}
|
|
23
|
+
const signedUrlRes = await TryOn.getSignedUrl(payload);
|
|
24
|
+
for (const file of files) {
|
|
25
|
+
await TryOn.s3Upload(signedUrlRes?.data?.uploadUrls?.[file.name]?.url, file);
|
|
26
|
+
}
|
|
27
|
+
resolve("uploaded successfully!");
|
|
28
|
+
} catch (error) {
|
|
29
|
+
reject(error);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static getSignedUrl(payload) {
|
|
35
|
+
return axios.post(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.TRY_ON_IMAGE_UPLOAD}`, payload, {
|
|
36
|
+
headers: {
|
|
37
|
+
"Content-Type": "application/json",
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static s3Upload(url, file) {
|
|
43
|
+
return axios.put(url, file, {
|
|
44
|
+
headers: {
|
|
45
|
+
"Content-Type": file.type,
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
getUploadedFiles(userId) {
|
|
51
|
+
return axios.post(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.TRY_ON_IMAGE_DOWNLOAD}?userId=${userId}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
deleteImage(userId, name) {
|
|
55
|
+
return axios.delete(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.TRY_ON_IMAGE_URLS}?userId=${userId}&file=${name}`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static disconnectSocket = () => {
|
|
59
|
+
TryOn.tryOnSocketRef?.close();
|
|
60
|
+
clearTimeout(TryOn.timerWaitingRef);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
static handleTimeOut = ({ onSuccess, onError, shopDomain, userId, productName }) => {
|
|
64
|
+
TryOn.timerWaitingRef = setTimeout(() => {
|
|
65
|
+
this.getTryOnResult({ shopDomain, userId, productName });
|
|
66
|
+
TryOn.disconnectSocket();
|
|
67
|
+
}, 78000);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
handleTryOnWebSocket = ({ shopDomain, userId, productName, onError, onSuccess, onClose, onOpen }) => {
|
|
71
|
+
if (checkParameters(shopDomain, userId, productName) === false) {
|
|
72
|
+
throw new Error(REQUIRED_MESSAGE);
|
|
73
|
+
}
|
|
74
|
+
TryOn.disconnectSocket();
|
|
75
|
+
TryOn.tryOnSocketRef = new WebSocket(url);
|
|
76
|
+
TryOn.tryOnSocketRef.onopen = async () => {
|
|
77
|
+
onOpen?.();
|
|
78
|
+
TryOn.handleTimeOut({ onSuccess, onError, shopDomain, userId, productName });
|
|
79
|
+
try {
|
|
80
|
+
await this.handleForLatestImage({ shopDomain, userId, productName });
|
|
81
|
+
} catch (error) {
|
|
82
|
+
onError?.(error);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
TryOn.tryOnSocketRef.onmessage = (event) => {
|
|
86
|
+
const data = JSON.parse(event.data);
|
|
87
|
+
if (data?.status === "success") {
|
|
88
|
+
this.getTryOnResult({ shopDomain, userId, productName });
|
|
89
|
+
} else {
|
|
90
|
+
onError?.(data);
|
|
91
|
+
}
|
|
92
|
+
clearTimeout(TryOn.tryOnSocketRef);
|
|
93
|
+
};
|
|
94
|
+
TryOn.tryOnSocketRef.onclose = () => {
|
|
95
|
+
onclose?.();
|
|
96
|
+
};
|
|
97
|
+
TryOn.tryOnSocketRef.onerror = (event) => {
|
|
98
|
+
onError?.(event);
|
|
99
|
+
clearTimeout(TryOn.tryOnSocketRef);
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
handleForLatestImage = async ({ userId, shopDomain, productName, onError }) => {
|
|
104
|
+
if (checkParameters(shopDomain, userId, productName) === false) {
|
|
105
|
+
throw new Error(REQUIRED_MESSAGE);
|
|
106
|
+
}
|
|
107
|
+
return new Promise(async (resolve, reject) => {
|
|
108
|
+
try {
|
|
109
|
+
const url = `${APP_AUTH_BASE_URL}/tryon/?scan_id=${id}&store_url=${shopDomain}&product_name=${productName}`;
|
|
110
|
+
const res = await axios.post(url);
|
|
111
|
+
if (res?.data?.tryOnProcessStatus === "failed") {
|
|
112
|
+
TryOn.disconnectSocket();
|
|
113
|
+
reject(res?.data);
|
|
114
|
+
} else {
|
|
115
|
+
resolve(res?.data);
|
|
116
|
+
}
|
|
117
|
+
} catch (error) {
|
|
118
|
+
onError?.(error);
|
|
119
|
+
TryOn.disconnectSocket();
|
|
120
|
+
reject(error);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
handleGetTryOnResult = async ({ onSuccess, onError, shopDomain, userId, productName }) => {
|
|
126
|
+
try {
|
|
127
|
+
const data = await this.getTryOnResult({ shopDomain, userId, productName });
|
|
128
|
+
onSuccess?.(data?.data);
|
|
129
|
+
} catch (error) {
|
|
130
|
+
onError?.(error);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
getTryOnResult = async ({ userId, shopDomain, productName }) => {
|
|
135
|
+
if (checkParameters(shopDomain, userId, productName) === false) {
|
|
136
|
+
throw new Error(REQUIRED_MESSAGE);
|
|
137
|
+
}
|
|
138
|
+
const url = `${APP_AUTH_BASE_URL}${API_ENDPOINTS.TRY_ON_RESULT_IMAGE_DOWNLOAD}?scan_id=${userId}&store_url=${shopDomain}&product_name=${productName}`;
|
|
139
|
+
return axios.post(url);
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export default TryOn;
|