@swan-admin/swan-ai-measurements 1.0.12 → 1.0.14
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 +38 -8
- package/constants.js +6 -0
- package/custom.js +7 -0
- package/index.js +45 -7
- package/measurement.js +127 -0
- package/package.json +1 -1
package/auth.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
|
-
import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_BASE_URL } from "./constants.js";
|
|
2
|
+
import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_AUTH_WEBSOCKET_URL, APP_BASE_URL } from "./constants.js";
|
|
3
|
+
let socketRef;
|
|
3
4
|
|
|
4
5
|
export const registerUser = ({ email, appVerifyUrl, gender, height, username = "", accessKey }) => {
|
|
5
6
|
let body = {
|
|
@@ -13,20 +14,49 @@ export const registerUser = ({ email, appVerifyUrl, gender, height, username = "
|
|
|
13
14
|
return axios.post(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.REGISTER_USER}`, body);
|
|
14
15
|
};
|
|
15
16
|
|
|
16
|
-
export
|
|
17
|
-
|
|
17
|
+
export const verifyToken = (token, accessKey) =>
|
|
18
|
+
axios.post(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.VERIFY_USER}`, null, {
|
|
18
19
|
params: { token },
|
|
19
20
|
});
|
|
20
|
-
}
|
|
21
21
|
|
|
22
|
-
export const addUser =
|
|
22
|
+
export const addUser = ({ scanId, email, name, height, gender, accessKey, offsetMarketingConsent }) =>
|
|
23
23
|
axios.post(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.ADD_USER}`, {
|
|
24
24
|
scan_id: scanId,
|
|
25
25
|
email,
|
|
26
26
|
name,
|
|
27
|
+
offsetMarketingConsent,
|
|
27
28
|
attributes: JSON.stringify({ height, gender }),
|
|
28
29
|
});
|
|
29
30
|
|
|
30
|
-
export
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
export const getUserDetail = (email, accessKey) =>
|
|
32
|
+
axios.get(`${APP_BASE_URL}${API_ENDPOINTS.GET_USER_DETAIL}/${email}`);
|
|
33
|
+
|
|
34
|
+
export const handleAuthSocket = ({ email, scanId, accessKey, onError, onSuccess, onClose, onOpen }) => {
|
|
35
|
+
if (socketRef) {
|
|
36
|
+
socketRef.close();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
socketRef = new WebSocket(APP_AUTH_WEBSOCKET_URL);
|
|
40
|
+
const detailObj = {
|
|
41
|
+
email,
|
|
42
|
+
scanId,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
socketRef.onopen = () => {
|
|
46
|
+
onOpen();
|
|
47
|
+
socketRef.send(JSON.stringify(detailObj));
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
socketRef.onmessage = (event) => {
|
|
51
|
+
const data = JSON.parse(event.data);
|
|
52
|
+
onSuccess(data);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
socketRef.onclose = () => {
|
|
56
|
+
onClose();
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
socketRef.current.onerror = (event) => {
|
|
60
|
+
onError(event);
|
|
61
|
+
};
|
|
62
|
+
};
|
package/constants.js
CHANGED
|
@@ -2,6 +2,9 @@ export const FILE_UPLOAD_URL = "https://9xc8mxwlkj.execute-api.us-east-1.amazona
|
|
|
2
2
|
export const FILE_UPLOAD_KEY = "7a8ccc86249849be911b0d7dbc20290e";
|
|
3
3
|
export const APP_BASE_URL = "https://fitview-server-staging.ft2a64raup4pg.us-east-1.cs.amazonlightsail.com";
|
|
4
4
|
export const APP_AUTH_BASE_URL = "https://staging.api.getswan.co";
|
|
5
|
+
export const APP_AUTH_WEBSOCKET_URL = "wss://develop.wsnotify.api.getswan.co/auth";
|
|
6
|
+
export const APP_TRY_ON_WEBSOCKET_URL = "wss://bucbzczxjk.execute-api.ap-south-1.amazonaws.com";
|
|
7
|
+
export const APP_RECOMMENDATION_WEBSOCKET_URL = "wss://9cfbimvp2e.execute-api.us-east-1.amazonaws.com/staging";
|
|
5
8
|
|
|
6
9
|
export const UPPY_FILE_UPLOAD_ENDPOINT = {
|
|
7
10
|
UPLOAD_START: "/upload/start",
|
|
@@ -15,4 +18,7 @@ export const API_ENDPOINTS = {
|
|
|
15
18
|
REGISTER_USER: "/auth/register",
|
|
16
19
|
VERIFY_USER: "/auth/verify",
|
|
17
20
|
ADD_USER: "/user",
|
|
21
|
+
CUSTOM_CUSTOMER: "/customers/custom",
|
|
22
|
+
MODEL: "/model",
|
|
23
|
+
TRY_ON: "/tryon/scan",
|
|
18
24
|
};
|
package/custom.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { API_ENDPOINTS, APP_AUTH_BASE_URL } from "./constants.js";
|
|
3
|
+
|
|
4
|
+
export const getCustomCustomerConfig = (store_url, accessKey) =>
|
|
5
|
+
axios.get(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.CUSTOM_CUSTOMER}`, { params: { store_url } });
|
|
6
|
+
|
|
7
|
+
export const getModelUrl = (id, accessKey) => axios.get(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.MODEL}/${id}`);
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { addUser, getUserDetail, registerUser, verifyToken } from "./auth.js";
|
|
2
|
+
import { getCustomCustomerConfig } from "./custom.js";
|
|
3
3
|
import uploadFile from "./fileUpload.js";
|
|
4
4
|
export default class Swan {
|
|
5
5
|
constructor(key) {
|
|
@@ -8,16 +8,17 @@ export default class Swan {
|
|
|
8
8
|
}
|
|
9
9
|
this.accessKey = key;
|
|
10
10
|
}
|
|
11
|
-
|
|
11
|
+
|
|
12
|
+
registerUser({ email, appVerifyUrl, gender, height, username }) {
|
|
12
13
|
return registerUser({ email, appVerifyUrl, gender, height, username, accessKey: this.accessKey });
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
return
|
|
16
|
+
verifyToken(token) {
|
|
17
|
+
return verifyToken(token, this.accessKey);
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
addUser({ scanId, email, name
|
|
20
|
-
return addUser({ scanId, email, name, height, gender, accessKey: this.accessKey });
|
|
20
|
+
addUser({ scanId, email, name, height, gender, offsetMarketingConsent }) {
|
|
21
|
+
return addUser({ scanId, email, name, height, gender, offsetMarketingConsent, accessKey: this.accessKey });
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
getUserDetail(email) {
|
|
@@ -27,4 +28,41 @@ export default class Swan {
|
|
|
27
28
|
uploadFile({ file, objMetaData, scanId }) {
|
|
28
29
|
return uploadFile({ file, objMetaData, scanId, accessKey: this.accessKey });
|
|
29
30
|
}
|
|
31
|
+
|
|
32
|
+
getModelUrl(id) {
|
|
33
|
+
return getModelUrl(id, this.accessKey);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
getCustomCustomerConfig(storeUrl) {
|
|
37
|
+
return getCustomCustomerConfig(storeUrl, this.accessKey);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getMeasurementStatus(scanId) {
|
|
41
|
+
return getMeasurementStatus(scanId, this.accessKey);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
getTryOnMeasurements({ scanId, shopDomain, productName }) {
|
|
45
|
+
return getTryOnMeasurements({ scanId, shopDomain, productName, accessKey: this.accessKey });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
handleTryOnSocket({ shopDomain, scanId, productName, onError, onSuccess, onClose, onOpen }) {
|
|
49
|
+
return handleTryOnSocket({
|
|
50
|
+
shopDomain,
|
|
51
|
+
scanId,
|
|
52
|
+
productName,
|
|
53
|
+
accessKey: this.accessKey,
|
|
54
|
+
onError,
|
|
55
|
+
onSuccess,
|
|
56
|
+
onClose,
|
|
57
|
+
onOpen,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
handleMeasurementSocket({ scanId, onError, onSuccess, onClose, onOpen }) {
|
|
62
|
+
return handleMeasurementSocket({ scanId, accessKey: this.accessKey, onError, onSuccess, onClose, onOpen });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
handleAuthSocket({ email, scanId, onError, onSuccess, onClose, onOpen }) {
|
|
66
|
+
return handleAuthSocket({ email, scanId, accessKey: this.accessKey, onError, onSuccess, onClose, onOpen });
|
|
67
|
+
}
|
|
30
68
|
}
|
package/measurement.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
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
|
+
FILE_UPLOAD_KEY,
|
|
8
|
+
FILE_UPLOAD_URL,
|
|
9
|
+
} from "./constants.js";
|
|
10
|
+
|
|
11
|
+
let tryOnSocketRef;
|
|
12
|
+
let measurementSocketRef;
|
|
13
|
+
let timerPollingRef;
|
|
14
|
+
let timerWaitingRef;
|
|
15
|
+
|
|
16
|
+
export const getMeasurementStatus = (scanId, accessKey) => {
|
|
17
|
+
const url = `${FILE_UPLOAD_URL}/measurements?scanId=${scanId}`;
|
|
18
|
+
return axios.get(url, {
|
|
19
|
+
headers: { "X-Api-Key": FILE_UPLOAD_KEY },
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const getTryOnMeasurements = ({ scanId, shopDomain, productName, accessKey }) => {
|
|
24
|
+
const tryOnUrl = `${APP_AUTH_BASE_URL}${API_ENDPOINTS.TRY_ON}/${scanId}/shop/${shopDomain}/product/${productName}`;
|
|
25
|
+
return axios.get(tryOnUrl);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const handleTryOnSocket = ({
|
|
29
|
+
shopDomain,
|
|
30
|
+
scanId,
|
|
31
|
+
productName,
|
|
32
|
+
accessKey,
|
|
33
|
+
onError,
|
|
34
|
+
onSuccess,
|
|
35
|
+
onClose,
|
|
36
|
+
onOpen,
|
|
37
|
+
}) => {
|
|
38
|
+
if (tryOnSocketRef) {
|
|
39
|
+
tryOnSocketRef.close();
|
|
40
|
+
}
|
|
41
|
+
const url = `${APP_TRY_ON_WEBSOCKET_URL}/develop?store_url=${shopDomain}&product_name=${productName}&scan_id=${scanId}`;
|
|
42
|
+
tryOnSocketRef = new WebSocket(url);
|
|
43
|
+
|
|
44
|
+
tryOnSocketRef.onopen = () => {
|
|
45
|
+
onOpen();
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
tryOnSocketRef.onmessage = (event) => {
|
|
49
|
+
const data = JSON.parse(event.data);
|
|
50
|
+
if (data?.tryOnProcessStatus === "available") {
|
|
51
|
+
onSuccess(data);
|
|
52
|
+
} else {
|
|
53
|
+
onError({ message: "failed to get image urls" });
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
tryOnSocketRef.onclose = () => {
|
|
58
|
+
onClose();
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
tryOnSocketRef.onerror = (event) => {
|
|
62
|
+
onError(event);
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const getMeasurementsCheck = async (onSuccess, onError, scanId, accessKey) => {
|
|
67
|
+
try {
|
|
68
|
+
const res = await getMeasurementStatus(scanId, accessKey);
|
|
69
|
+
if (res?.data && res?.data?.[0]?.isMeasured === true) {
|
|
70
|
+
onSuccess(res?.data);
|
|
71
|
+
clearInterval(timerPollingRef);
|
|
72
|
+
}
|
|
73
|
+
} catch (e) {
|
|
74
|
+
clearInterval(timerPollingRef);
|
|
75
|
+
onError(e);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const handlePolling = (onSuccess, onError) => {
|
|
80
|
+
clearInterval(timerPollingRef);
|
|
81
|
+
timerPollingRef = setInterval(() => {
|
|
82
|
+
getMeasurementsCheck(onSuccess, onError);
|
|
83
|
+
}, 5000);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const disconnectSocket = () => {
|
|
87
|
+
if (measurementSocketRef) {
|
|
88
|
+
measurementSocketRef.close();
|
|
89
|
+
clearTimeout(timerWaitingRef);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const handleTimeOut = (onSuccess, onError) => {
|
|
94
|
+
timerWaitingRef = setTimeout(() => {
|
|
95
|
+
handlePolling(onSuccess, onError);
|
|
96
|
+
disconnectSocket();
|
|
97
|
+
}, 2 * 60000);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export const handleMeasurementSocket = ({ scanId, accessKey, onError, onSuccess, onClose, onOpen }) => {
|
|
101
|
+
disconnectSocket();
|
|
102
|
+
const url = `${APP_RECOMMENDATION_WEBSOCKET_URL}?scanId=${scanId}`;
|
|
103
|
+
measurementSocketRef = new WebSocket(url);
|
|
104
|
+
|
|
105
|
+
measurementSocketRef.onopen = () => {
|
|
106
|
+
onOpen();
|
|
107
|
+
handleTimeOut(onSuccess, onError);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
measurementSocketRef.onmessage = (event) => {
|
|
111
|
+
const data = JSON.parse(event.data);
|
|
112
|
+
if (data?.code === 200 && data?.scanStatus === "success") {
|
|
113
|
+
onSuccess(data);
|
|
114
|
+
} else {
|
|
115
|
+
onError(data);
|
|
116
|
+
}
|
|
117
|
+
clearTimeout(timerWaitingRef);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
measurementSocketRef.onclose = () => {
|
|
121
|
+
onClose();
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
measurementSocketRef.current.onerror = (event) => {
|
|
125
|
+
onError(event);
|
|
126
|
+
};
|
|
127
|
+
};
|