@swan-admin/swan-ai-measurements
Version:
provides ai measurement suggestion
230 lines (229 loc) • 14.2 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _TryOn_instances, _TryOn_socketMap, _TryOn_timerMap, _TryOn_accessKey, _TryOn_urlType, _TryOn_token, _TryOn_getHeaders, _TryOn_getSignedUrl, _TryOn_s3Upload, _TryOn_disconnectSocket, _TryOn_handleTimeOut, _TryOn_handleGetTryOnResult;
import axios from "axios";
import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_BASE_WEBSOCKET_URL, REQUIRED_ERROR_MESSAGE_INVALID_EMAIL, REQUIRED_MESSAGE } from "./constants.js";
import { checkParameters, getUrl, isValidEmail } from "./utils.js";
import { URLType } from "./enum.js";
class TryOn {
constructor(accessKey, urlType = URLType.PROD, token) {
_TryOn_instances.add(this);
_TryOn_socketMap.set(this, new Map());
_TryOn_timerMap.set(this, new Map());
_TryOn_accessKey.set(this, void 0);
_TryOn_urlType.set(this, void 0);
_TryOn_token.set(this, void 0);
_TryOn_disconnectSocket.set(this, (tryonId) => {
if (tryonId) {
const socket = __classPrivateFieldGet(this, _TryOn_socketMap, "f").get(tryonId);
const timer = __classPrivateFieldGet(this, _TryOn_timerMap, "f").get(tryonId);
socket === null || socket === void 0 ? void 0 : socket.close();
if (timer)
clearTimeout(timer);
__classPrivateFieldGet(this, _TryOn_socketMap, "f").delete(tryonId);
__classPrivateFieldGet(this, _TryOn_timerMap, "f").delete(tryonId);
}
else {
// Disconnect all
__classPrivateFieldGet(this, _TryOn_socketMap, "f").forEach((socket) => socket.close());
__classPrivateFieldGet(this, _TryOn_timerMap, "f").forEach((timer) => clearTimeout(timer));
__classPrivateFieldGet(this, _TryOn_socketMap, "f").clear();
__classPrivateFieldGet(this, _TryOn_timerMap, "f").clear();
}
});
_TryOn_handleTimeOut.set(this, ({ onSuccess, onError, tryonId }) => {
const timer = setTimeout(() => {
__classPrivateFieldGet(this, _TryOn_handleGetTryOnResult, "f").call(this, { onSuccess, onError, tryonId });
__classPrivateFieldGet(this, _TryOn_disconnectSocket, "f").call(this, tryonId);
}, 300000);
__classPrivateFieldGet(this, _TryOn_timerMap, "f").set(tryonId, timer);
});
this.handleTryOnWebSocket = ({ tryonId, onError, onSuccess, onClose, onOpen }) => {
if (checkParameters(tryonId) === false) {
throw new Error(REQUIRED_MESSAGE);
}
__classPrivateFieldGet(this, _TryOn_disconnectSocket, "f").call(this, tryonId);
const url = `${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, urlType: __classPrivateFieldGet(this, _TryOn_urlType, "f") })}${API_ENDPOINTS.TRY_ON}?tryonId=${tryonId}`;
const socket = new WebSocket(url);
__classPrivateFieldGet(this, _TryOn_socketMap, "f").set(tryonId, socket);
socket.onopen = () => {
onOpen === null || onOpen === void 0 ? void 0 : onOpen();
__classPrivateFieldGet(this, _TryOn_handleTimeOut, "f").call(this, { onSuccess, onError, tryonId });
};
socket.onmessage = (event) => {
let data;
try {
data = JSON.parse(event.data);
}
catch (error) {
console.log("Invalid JSON:", event.data);
return;
}
if ((data === null || data === void 0 ? void 0 : data.eventType) === "tryon.completed") {
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
}
else if ((data === null || data === void 0 ? void 0 : data.eventType) == "tryon.failed") {
onError === null || onError === void 0 ? void 0 : onError(data);
}
const timer = __classPrivateFieldGet(this, _TryOn_timerMap, "f").get(tryonId);
if (timer)
clearTimeout(timer);
__classPrivateFieldGet(this, _TryOn_timerMap, "f").delete(tryonId);
};
socket.onclose = () => {
onClose === null || onClose === void 0 ? void 0 : onClose();
};
socket.onerror = (event) => {
onError === null || onError === void 0 ? void 0 : onError(event);
};
};
_TryOn_handleGetTryOnResult.set(this, (_a) => __awaiter(this, [_a], void 0, function* ({ onSuccess, onError, tryonId }) {
try {
const data = yield this.getTryOnResult({ tryonId });
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data.data);
}
catch (error) {
onError === null || onError === void 0 ? void 0 : onError(error);
}
}));
this.getTryOnResult = ({ tryonId }) => {
if (checkParameters(tryonId) === false) {
throw new Error(REQUIRED_MESSAGE);
}
const url = `${getUrl({ urlName: APP_AUTH_BASE_URL, urlType: __classPrivateFieldGet(this, _TryOn_urlType, "f") })}${API_ENDPOINTS.TRY_ON_RESULT_IMAGE_DOWNLOAD}/${tryonId}`;
return axios.post(url, null, {
headers: __classPrivateFieldGet(this, _TryOn_instances, "m", _TryOn_getHeaders).call(this),
});
};
__classPrivateFieldSet(this, _TryOn_accessKey, accessKey, "f");
__classPrivateFieldSet(this, _TryOn_urlType, urlType, "f");
__classPrivateFieldSet(this, _TryOn_token, token, "f");
}
uploadFile(_a) {
return __awaiter(this, arguments, void 0, function* ({ files, userEmail, fileNoLimit = 2 }) {
if (checkParameters(files, userEmail) === false) {
throw new Error(REQUIRED_MESSAGE);
}
if (!isValidEmail(userEmail.trim())) {
throw new Error(REQUIRED_ERROR_MESSAGE_INVALID_EMAIL);
}
if (fileNoLimit <= 0) {
throw new Error(`Provide valid file number limit ${fileNoLimit}.`);
}
if ((files === null || files === void 0 ? void 0 : files.length) > fileNoLimit) {
throw new Error(`Cannot allow more than ${fileNoLimit} files.`);
}
try {
const payload = {
userEmail,
userImages: [],
};
files === null || files === void 0 ? void 0 : files.forEach((file) => {
payload.userImages.push(file.name);
});
const signedUrlRes = yield __classPrivateFieldGet(this, _TryOn_instances, "m", _TryOn_getSignedUrl).call(this, payload);
for (const file of files) {
yield __classPrivateFieldGet(this, _TryOn_instances, "m", _TryOn_s3Upload).call(this, signedUrlRes.data.uploadUrls[file.name].url, file);
}
return `uploaded successfully!`;
}
catch (error) {
throw error;
}
});
}
getUploadedFiles(userEmail) {
if (checkParameters(userEmail) === false) {
throw new Error(REQUIRED_MESSAGE);
}
if (!isValidEmail(userEmail.trim())) {
throw new Error(REQUIRED_ERROR_MESSAGE_INVALID_EMAIL);
}
const payload = {
userEmail,
};
return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, urlType: __classPrivateFieldGet(this, _TryOn_urlType, "f") })}${API_ENDPOINTS.TRY_ON_IMAGE_DOWNLOAD}`, payload, {
headers: __classPrivateFieldGet(this, _TryOn_instances, "m", _TryOn_getHeaders).call(this),
});
}
deleteImage({ userEmail, fileName }) {
if (checkParameters(userEmail, fileName) === false) {
throw new Error(REQUIRED_MESSAGE);
}
if (!isValidEmail(userEmail.trim())) {
throw new Error(REQUIRED_ERROR_MESSAGE_INVALID_EMAIL);
}
const payload = {
userEmail,
file: fileName,
};
return axios.delete(`${getUrl({ urlName: APP_AUTH_BASE_URL, urlType: __classPrivateFieldGet(this, _TryOn_urlType, "f") })}${API_ENDPOINTS.TRY_ON_IMAGE_URLS}`, {
headers: __classPrivateFieldGet(this, _TryOn_instances, "m", _TryOn_getHeaders).call(this),
data: payload,
});
}
handleTryOnSubmit({ shopDomain, products, selectedUserImages, requestSource, callbackUrl, openTryonId, selectedProductImageUrl, requestedTryonViews }) {
if (checkParameters(shopDomain, products) === false) {
throw new Error(REQUIRED_MESSAGE);
}
const payload = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ products, customerStoreUrl: shopDomain }, (selectedUserImages !== undefined && selectedUserImages !== null && { selectedUserImages })), (requestSource !== undefined && requestSource !== null && { requestSource })), (callbackUrl !== undefined && callbackUrl !== null && { callbackUrl })), (openTryonId !== undefined && openTryonId !== null && { openTryonId })), (selectedProductImageUrl !== undefined && selectedProductImageUrl !== null && { selectedProductImageUrl })), (requestedTryonViews && (requestedTryonViews === null || requestedTryonViews === void 0 ? void 0 : requestedTryonViews.length) > 0 && { requestedTryonViews }));
const url = `${getUrl({ urlName: APP_AUTH_BASE_URL, urlType: __classPrivateFieldGet(this, _TryOn_urlType, "f") })}${API_ENDPOINTS.TRY_ON}`;
return axios.post(url, payload, {
headers: __classPrivateFieldGet(this, _TryOn_instances, "m", _TryOn_getHeaders).call(this),
});
}
getShareLink(tryonId) {
return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, urlType: __classPrivateFieldGet(this, _TryOn_urlType, "f") })}${API_ENDPOINTS.TRY_ON_SHARE}`, { tryonId }, {
headers: __classPrivateFieldGet(this, _TryOn_instances, "m", _TryOn_getHeaders).call(this),
});
}
getProductImageEligibility({ storeUrl, productHandle, imageURL, productDescription }) {
if (checkParameters(storeUrl, productHandle, imageURL) === false) {
throw new Error(REQUIRED_MESSAGE);
}
const payload = { storeUrl, productHandle, imageURL, productDescription: productDescription !== null && productDescription !== void 0 ? productDescription : null };
const url = `${getUrl({ urlName: APP_AUTH_BASE_URL, urlType: __classPrivateFieldGet(this, _TryOn_urlType, "f") })}${API_ENDPOINTS.TRY_ON_PRODUCT_IMAGE_ELIGIBILTY}`;
return axios.post(url, payload, {
headers: __classPrivateFieldGet(this, _TryOn_instances, "m", _TryOn_getHeaders).call(this),
});
}
}
_TryOn_socketMap = new WeakMap(), _TryOn_timerMap = new WeakMap(), _TryOn_accessKey = new WeakMap(), _TryOn_urlType = new WeakMap(), _TryOn_token = new WeakMap(), _TryOn_disconnectSocket = new WeakMap(), _TryOn_handleTimeOut = new WeakMap(), _TryOn_handleGetTryOnResult = new WeakMap(), _TryOn_instances = new WeakSet(), _TryOn_getHeaders = function _TryOn_getHeaders(token, extraHeaders = {}) {
const requestToken = token !== null && token !== void 0 ? token : __classPrivateFieldGet(this, _TryOn_token, "f");
return Object.assign(Object.assign(Object.assign({}, extraHeaders), (__classPrivateFieldGet(this, _TryOn_accessKey, "f") ? { "X-Api-Key": __classPrivateFieldGet(this, _TryOn_accessKey, "f") } : {})), (requestToken ? { Authorization: `Bearer ${requestToken}` } : {}));
}, _TryOn_getSignedUrl = function _TryOn_getSignedUrl(payload) {
if (checkParameters(payload) === false) {
throw new Error(REQUIRED_MESSAGE);
}
return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, urlType: __classPrivateFieldGet(this, _TryOn_urlType, "f") })}${API_ENDPOINTS.TRY_ON_IMAGE_UPLOAD}`, payload, {
headers: __classPrivateFieldGet(this, _TryOn_instances, "m", _TryOn_getHeaders).call(this, undefined, { "Content-Type": "application/json" }),
});
}, _TryOn_s3Upload = function _TryOn_s3Upload(url, file) {
if (checkParameters(url, file) === false) {
throw new Error(REQUIRED_MESSAGE);
}
return axios.put(url, file, {
headers: {
"Content-Type": file.type,
},
});
};
export default TryOn;