@swan-admin/swan-ai-measurements 1.0.63 → 1.0.65

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.
@@ -0,0 +1,34 @@
1
+ import { AxiosResponse } from "axios";
2
+ interface RegisterUserParams {
3
+ email: string;
4
+ appVerifyUrl: string;
5
+ gender?: string;
6
+ height?: number;
7
+ username?: string;
8
+ }
9
+ interface AddUserParams {
10
+ scanId: string;
11
+ email: string;
12
+ name?: string;
13
+ height: number;
14
+ gender: string;
15
+ offsetMarketingConsent?: boolean;
16
+ }
17
+ interface AuthSocketParams {
18
+ email: string;
19
+ scanId: string;
20
+ onError?: (event: Event) => void;
21
+ onSuccess?: (data: any) => void;
22
+ onClose?: () => void;
23
+ onOpen?: () => void;
24
+ }
25
+ export default class Auth {
26
+ #private;
27
+ constructor(accessKey: string);
28
+ registerUser({ email, appVerifyUrl, gender, height, username }: RegisterUserParams): Promise<AxiosResponse>;
29
+ verifyToken(token: string): Promise<AxiosResponse>;
30
+ addUser({ scanId, email, name, height, gender, offsetMarketingConsent }: AddUserParams): Promise<AxiosResponse>;
31
+ getUserDetail(email: string): Promise<AxiosResponse>;
32
+ handleAuthSocket({ email, scanId, onError, onSuccess, onClose, onOpen }: AuthSocketParams): void;
33
+ }
34
+ export {};
@@ -0,0 +1,83 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ 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");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ 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");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _Auth_socketRef, _Auth_accessKey;
13
+ import axios from "axios";
14
+ import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_AUTH_WEBSOCKET_URL, APP_BASE_URL, REQUIRED_MESSAGE } from "./constants";
15
+ import { checkParameters } from "./utils";
16
+ class Auth {
17
+ constructor(accessKey) {
18
+ _Auth_socketRef.set(this, void 0);
19
+ _Auth_accessKey.set(this, void 0);
20
+ __classPrivateFieldSet(this, _Auth_accessKey, accessKey, "f");
21
+ }
22
+ registerUser({ email, appVerifyUrl, gender, height, username }) {
23
+ if (!checkParameters(email, appVerifyUrl)) {
24
+ throw new Error(REQUIRED_MESSAGE);
25
+ }
26
+ let body = { username, email, appVerifyUrl };
27
+ if (gender && height) {
28
+ body = Object.assign(Object.assign({}, body), { attributes: { gender, height } });
29
+ }
30
+ return axios.post(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.REGISTER_USER}`, body, {
31
+ headers: { "X-Api-Key": __classPrivateFieldGet(this, _Auth_accessKey, "f") },
32
+ });
33
+ }
34
+ verifyToken(token) {
35
+ if (!checkParameters(token)) {
36
+ throw new Error(REQUIRED_MESSAGE);
37
+ }
38
+ return axios.post(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.VERIFY_USER}`, null, {
39
+ params: { token },
40
+ headers: { "X-Api-Key": __classPrivateFieldGet(this, _Auth_accessKey, "f") },
41
+ });
42
+ }
43
+ addUser({ scanId, email, name, height, gender, offsetMarketingConsent }) {
44
+ if (!checkParameters(scanId, email, height, gender)) {
45
+ throw new Error(REQUIRED_MESSAGE);
46
+ }
47
+ return axios.post(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.ADD_USER}`, { scan_id: scanId, email, name, offsetMarketingConsent, attributes: JSON.stringify({ height, gender }) }, { headers: { "X-Api-Key": __classPrivateFieldGet(this, _Auth_accessKey, "f") } });
48
+ }
49
+ getUserDetail(email) {
50
+ if (!checkParameters(email)) {
51
+ throw new Error(REQUIRED_MESSAGE);
52
+ }
53
+ return axios.get(`${APP_BASE_URL}${API_ENDPOINTS.GET_USER_DETAIL}/${email}`, {
54
+ headers: { "X-Api-Key": __classPrivateFieldGet(this, _Auth_accessKey, "f") },
55
+ });
56
+ }
57
+ handleAuthSocket({ email, scanId, onError, onSuccess, onClose, onOpen }) {
58
+ if (!checkParameters(email, scanId)) {
59
+ throw new Error(REQUIRED_MESSAGE);
60
+ }
61
+ if (__classPrivateFieldGet(this, _Auth_socketRef, "f"))
62
+ __classPrivateFieldGet(this, _Auth_socketRef, "f").close();
63
+ __classPrivateFieldSet(this, _Auth_socketRef, new WebSocket(`${APP_AUTH_WEBSOCKET_URL}${API_ENDPOINTS.AUTH}`), "f");
64
+ const detailObj = { email, scanId };
65
+ __classPrivateFieldGet(this, _Auth_socketRef, "f").onopen = () => {
66
+ var _a;
67
+ (_a = __classPrivateFieldGet(this, _Auth_socketRef, "f")) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify(detailObj));
68
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
69
+ };
70
+ __classPrivateFieldGet(this, _Auth_socketRef, "f").onmessage = (event) => {
71
+ const data = JSON.parse(event.data);
72
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
73
+ };
74
+ __classPrivateFieldGet(this, _Auth_socketRef, "f").onclose = () => {
75
+ onClose === null || onClose === void 0 ? void 0 : onClose();
76
+ };
77
+ __classPrivateFieldGet(this, _Auth_socketRef, "f").onerror = (event) => {
78
+ onError === null || onError === void 0 ? void 0 : onError(event);
79
+ };
80
+ }
81
+ }
82
+ _Auth_socketRef = new WeakMap(), _Auth_accessKey = new WeakMap();
83
+ export default Auth;
@@ -0,0 +1,31 @@
1
+ export declare const APP_BASE_URL: string;
2
+ export declare const APP_AUTH_BASE_URL: string;
3
+ export declare const APP_AUTH_WEBSOCKET_URL: string;
4
+ export declare const APP_TRY_ON_WEBSOCKET_URL: string;
5
+ export declare const APP_RECOMMENDATION_WEBSOCKET_URL: string;
6
+ export declare const APP_POSE_DETECTION_WEBSOCKET_URL: string;
7
+ export declare const UPPY_FILE_UPLOAD_ENDPOINT: {
8
+ UPLOAD_START: string;
9
+ UPLOAD_COMPLETE: string;
10
+ UPLOAD_SIGN_PART: string;
11
+ UPLOAD_ABORT: string;
12
+ };
13
+ export declare const API_ENDPOINTS: {
14
+ [key: string]: string;
15
+ };
16
+ export interface ObjMetaData {
17
+ gender: string;
18
+ scan_id: string;
19
+ email: string;
20
+ focal_length: string;
21
+ height: string;
22
+ customer_store_url: string;
23
+ clothes_fit: string;
24
+ scan_type: string;
25
+ callback_url: string;
26
+ }
27
+ type RequiredMetaDataKeys = keyof ObjMetaData;
28
+ export declare const requiredMetaData: RequiredMetaDataKeys[];
29
+ export declare const REQUIRED_MESSAGE: string;
30
+ export declare const REQUIRED_MESSAGE_FOR_META_DATA: string;
31
+ export {};
@@ -0,0 +1,40 @@
1
+ export const APP_BASE_URL = "https://fitview-server-staging.ft2a64raup4pg.us-east-1.cs.amazonlightsail.com";
2
+ export const APP_AUTH_BASE_URL = "https://staging.api.getswan.co";
3
+ export const APP_AUTH_WEBSOCKET_URL = "wss://staging.wsnotify.api.getswan.co";
4
+ export const APP_TRY_ON_WEBSOCKET_URL = "wss://bucbzczxjk.execute-api.ap-south-1.amazonaws.com";
5
+ export const APP_RECOMMENDATION_WEBSOCKET_URL = "wss://staging.wsnotify.api.getswan.co/scanning";
6
+ export const APP_POSE_DETECTION_WEBSOCKET_URL = "https://posedetect-service-staging.ft2a64raup4pg.us-east-1.cs.amazonlightsail.com";
7
+ export const UPPY_FILE_UPLOAD_ENDPOINT = {
8
+ UPLOAD_START: "/upload/start",
9
+ UPLOAD_COMPLETE: "/upload/complete",
10
+ UPLOAD_SIGN_PART: "/upload/signpart",
11
+ UPLOAD_ABORT: "/upload/abort",
12
+ };
13
+ export const API_ENDPOINTS = {
14
+ GET_USER_DETAIL: "/api/user",
15
+ REGISTER_USER: "/auth/register",
16
+ VERIFY_USER: "/auth/verify",
17
+ ADD_USER: "/user",
18
+ CUSTOM_CUSTOMER: "/customers/custom",
19
+ MODEL: "/model",
20
+ TRY_ON_SCAN: "/tryon/scan",
21
+ TRY_ON_IMAGE_UPLOAD: "/tryon/user-image-urls/upload",
22
+ TRY_ON_IMAGE_DOWNLOAD: "/tryon/user-image-urls/download",
23
+ TRY_ON_IMAGE_URLS: "/tryon/user-image-urls",
24
+ TRY_ON_RESULT_IMAGE_DOWNLOAD: "/tryon/result-image-urls/download",
25
+ TRY_ON: "/tryon",
26
+ AUTH: "/auth",
27
+ };
28
+ export const requiredMetaData = [
29
+ "gender",
30
+ "scan_id",
31
+ "email",
32
+ "focal_length",
33
+ "height",
34
+ "customer_store_url",
35
+ "scan_type",
36
+ "callback_url",
37
+ "clothes_fit",
38
+ ];
39
+ export const REQUIRED_MESSAGE = "Please verify required parameters";
40
+ export const REQUIRED_MESSAGE_FOR_META_DATA = "Please verify required parameters in meta data";
@@ -0,0 +1,8 @@
1
+ import { AxiosResponse } from "axios";
2
+ declare class Custom {
3
+ #private;
4
+ constructor(accessKey: string);
5
+ getCustomCustomerConfig: (store_url: string) => Promise<AxiosResponse<any>>;
6
+ getModelUrl: (id: string) => Promise<AxiosResponse<any>>;
7
+ }
8
+ export default Custom;
@@ -0,0 +1,38 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ 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");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ 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");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _Custom_accessKey;
13
+ import axios from "axios";
14
+ import { API_ENDPOINTS, APP_AUTH_BASE_URL, REQUIRED_MESSAGE } from "./constants";
15
+ import { checkParameters } from "./utils";
16
+ class Custom {
17
+ constructor(accessKey) {
18
+ _Custom_accessKey.set(this, void 0);
19
+ this.getCustomCustomerConfig = (store_url) => {
20
+ if (checkParameters(store_url) === false) {
21
+ throw new Error(REQUIRED_MESSAGE);
22
+ }
23
+ return axios.get(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.CUSTOM_CUSTOMER}`, {
24
+ params: { store_url },
25
+ headers: { "X-Api-Key": __classPrivateFieldGet(this, _Custom_accessKey, "f") },
26
+ });
27
+ };
28
+ this.getModelUrl = (id) => {
29
+ if (checkParameters(id) === false) {
30
+ throw new Error(REQUIRED_MESSAGE);
31
+ }
32
+ return axios.get(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.MODEL}/${id}`, { headers: { "X-Api-Key": __classPrivateFieldGet(this, _Custom_accessKey, "f") } });
33
+ };
34
+ __classPrivateFieldSet(this, _Custom_accessKey, accessKey, "f");
35
+ }
36
+ }
37
+ _Custom_accessKey = new WeakMap();
38
+ export default Custom;
@@ -0,0 +1,22 @@
1
+ interface ObjMetaData {
2
+ gender: string;
3
+ scan_id: string;
4
+ email: string;
5
+ focal_length: string;
6
+ height: string;
7
+ customer_store_url: string;
8
+ clothes_fit: string;
9
+ scan_type: string;
10
+ callback_url: string;
11
+ }
12
+ interface UploadOptions {
13
+ file: any;
14
+ arrayMetaData: ObjMetaData[];
15
+ scanId: string;
16
+ }
17
+ export default class FileUpload {
18
+ #private;
19
+ constructor(accessKey: string);
20
+ uploadFile({ file, arrayMetaData, scanId }: UploadOptions): Promise<unknown>;
21
+ }
22
+ export {};
@@ -0,0 +1,122 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
11
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
12
+ 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");
13
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
14
+ };
15
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
16
+ if (kind === "m") throw new TypeError("Private method is not writable");
17
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
18
+ 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");
19
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
20
+ };
21
+ var _FileUpload_instances, _FileUpload_uppyIns, _FileUpload_accessKey, _FileUpload_Uppy, _FileUpload_AwsS3Multipart, _FileUpload_initializeModules;
22
+ import { REQUIRED_MESSAGE, REQUIRED_MESSAGE_FOR_META_DATA, UPPY_FILE_UPLOAD_ENDPOINT } from "./constants";
23
+ import { checkMetaDataValue, checkParameters, fetchData } from "./utils";
24
+ class FileUpload {
25
+ constructor(accessKey) {
26
+ _FileUpload_instances.add(this);
27
+ _FileUpload_uppyIns.set(this, void 0);
28
+ _FileUpload_accessKey.set(this, void 0);
29
+ _FileUpload_Uppy.set(this, void 0);
30
+ _FileUpload_AwsS3Multipart.set(this, void 0);
31
+ __classPrivateFieldGet(this, _FileUpload_instances, "m", _FileUpload_initializeModules).call(this);
32
+ __classPrivateFieldSet(this, _FileUpload_accessKey, accessKey, "f");
33
+ }
34
+ uploadFile(_a) {
35
+ return __awaiter(this, arguments, void 0, function* ({ file, arrayMetaData, scanId }) {
36
+ if (checkParameters(file, arrayMetaData, scanId) === false) {
37
+ throw new Error(REQUIRED_MESSAGE);
38
+ }
39
+ if (checkMetaDataValue(arrayMetaData) === false) {
40
+ throw new Error(REQUIRED_MESSAGE_FOR_META_DATA);
41
+ }
42
+ if (!__classPrivateFieldGet(this, _FileUpload_Uppy, "f") || !__classPrivateFieldGet(this, _FileUpload_AwsS3Multipart, "f")) {
43
+ yield __classPrivateFieldGet(this, _FileUpload_instances, "m", _FileUpload_initializeModules).call(this);
44
+ }
45
+ return new Promise((resolve, reject) => {
46
+ if (__classPrivateFieldGet(this, _FileUpload_uppyIns, "f")) {
47
+ __classPrivateFieldGet(this, _FileUpload_uppyIns, "f").close();
48
+ }
49
+ __classPrivateFieldSet(this, _FileUpload_uppyIns, new (__classPrivateFieldGet(this, _FileUpload_Uppy, "f"))({ autoProceed: true }), "f");
50
+ __classPrivateFieldGet(this, _FileUpload_uppyIns, "f").use(__classPrivateFieldGet(this, _FileUpload_AwsS3Multipart, "f"), {
51
+ limit: 10,
52
+ retryDelays: [0, 1000, 3000, 5000],
53
+ getChunkSize: () => 5 * 1024 * 1024,
54
+ createMultipartUpload: (file) => {
55
+ const objectKey = `${scanId}.${file.extension}`;
56
+ return fetchData({
57
+ path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_START,
58
+ apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
59
+ body: {
60
+ objectKey,
61
+ contentType: file.type,
62
+ objectMetadata: arrayMetaData,
63
+ },
64
+ });
65
+ },
66
+ completeMultipartUpload: (file, { uploadId, key, parts }) => fetchData({
67
+ path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
68
+ apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
69
+ body: {
70
+ uploadId,
71
+ objectKey: key,
72
+ parts,
73
+ originalFileName: file.name,
74
+ },
75
+ }),
76
+ signPart: (file, partData) => fetchData({
77
+ path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
78
+ apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
79
+ body: {
80
+ objectKey: partData.key,
81
+ uploadId: partData.uploadId,
82
+ partNumber: partData.partNumber,
83
+ },
84
+ }),
85
+ abortMultipartUpload: (file, { uploadId, key }) => fetchData({
86
+ path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_ABORT,
87
+ apiKey: __classPrivateFieldGet(this, _FileUpload_accessKey, "f"),
88
+ body: {
89
+ uploadId,
90
+ objectKey: key,
91
+ originalFileName: file.name,
92
+ },
93
+ }),
94
+ });
95
+ __classPrivateFieldGet(this, _FileUpload_uppyIns, "f").addFile({
96
+ source: "manual",
97
+ name: file.name,
98
+ type: file.type,
99
+ data: file,
100
+ });
101
+ __classPrivateFieldGet(this, _FileUpload_uppyIns, "f").on("upload-error", (file, error, response) => {
102
+ reject(error);
103
+ });
104
+ __classPrivateFieldGet(this, _FileUpload_uppyIns, "f").on("upload-success", () => {
105
+ resolve({ message: "file uploaded successfully" });
106
+ });
107
+ __classPrivateFieldGet(this, _FileUpload_uppyIns, "f").on("complete", (result) => {
108
+ if (__classPrivateFieldGet(this, _FileUpload_uppyIns, "f")) {
109
+ __classPrivateFieldGet(this, _FileUpload_uppyIns, "f").close();
110
+ }
111
+ });
112
+ });
113
+ });
114
+ }
115
+ }
116
+ _FileUpload_uppyIns = new WeakMap(), _FileUpload_accessKey = new WeakMap(), _FileUpload_Uppy = new WeakMap(), _FileUpload_AwsS3Multipart = new WeakMap(), _FileUpload_instances = new WeakSet(), _FileUpload_initializeModules = function _FileUpload_initializeModules() {
117
+ return __awaiter(this, void 0, void 0, function* () {
118
+ __classPrivateFieldSet(this, _FileUpload_Uppy, (yield import("@uppy/core")).default, "f");
119
+ __classPrivateFieldSet(this, _FileUpload_AwsS3Multipart, (yield import("@uppy/aws-s3-multipart")).default, "f");
120
+ });
121
+ };
122
+ export default FileUpload;
@@ -0,0 +1,17 @@
1
+ import Auth from "./auth";
2
+ import Custom from "./custom";
3
+ import FileUpload from "./fileUpload";
4
+ import Measurement from "./measurement";
5
+ import PoseDetection from "./poseDetection";
6
+ import TryOn from "./tryOn";
7
+ declare class Swan {
8
+ #private;
9
+ auth: Auth;
10
+ custom: Custom;
11
+ fileUpload: FileUpload;
12
+ measurement: Measurement;
13
+ poseDetection: PoseDetection;
14
+ tryOn: TryOn;
15
+ constructor(accessKey: string);
16
+ }
17
+ export default Swan;
@@ -0,0 +1,32 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ 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");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ 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");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _Swan_accessKey;
13
+ import Auth from "./auth";
14
+ import Custom from "./custom";
15
+ import FileUpload from "./fileUpload";
16
+ import Measurement from "./measurement";
17
+ import PoseDetection from "./poseDetection";
18
+ import TryOn from "./tryOn";
19
+ class Swan {
20
+ constructor(accessKey) {
21
+ _Swan_accessKey.set(this, void 0);
22
+ __classPrivateFieldSet(this, _Swan_accessKey, accessKey, "f");
23
+ this.auth = new Auth(__classPrivateFieldGet(this, _Swan_accessKey, "f"));
24
+ this.custom = new Custom(__classPrivateFieldGet(this, _Swan_accessKey, "f"));
25
+ this.fileUpload = new FileUpload(__classPrivateFieldGet(this, _Swan_accessKey, "f"));
26
+ this.measurement = new Measurement(__classPrivateFieldGet(this, _Swan_accessKey, "f"));
27
+ this.poseDetection = new PoseDetection(__classPrivateFieldGet(this, _Swan_accessKey, "f"));
28
+ this.tryOn = new TryOn(__classPrivateFieldGet(this, _Swan_accessKey, "f"));
29
+ }
30
+ }
31
+ _Swan_accessKey = new WeakMap();
32
+ export default Swan;
@@ -0,0 +1,26 @@
1
+ import { AxiosResponse } from "axios";
2
+ interface TryOnSocketOptions {
3
+ shopDomain: string;
4
+ scanId: string;
5
+ productName: string;
6
+ onError?: (error: any) => void;
7
+ onSuccess?: (data: any) => void;
8
+ onClose?: () => void;
9
+ onOpen?: () => void;
10
+ }
11
+ interface MeasurementSocketOptions {
12
+ scanId: string;
13
+ onError?: (error: any) => void;
14
+ onSuccess?: (data: any) => void;
15
+ onClose?: () => void;
16
+ onOpen?: () => void;
17
+ }
18
+ declare class Measurement {
19
+ #private;
20
+ constructor(accessKey: string);
21
+ getMeasurementResult(scanId: string): Promise<AxiosResponse<any>>;
22
+ getTryOnMeasurements({ scanId, shopDomain, productName }: TryOnSocketOptions): Promise<AxiosResponse<any>>;
23
+ handleTryOnSocket(options: TryOnSocketOptions): void;
24
+ handleMeasurementSocket(options: MeasurementSocketOptions): void;
25
+ }
26
+ export default Measurement;
@@ -0,0 +1,170 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
11
+ if (kind === "m") throw new TypeError("Private method is not writable");
12
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
13
+ 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");
14
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
15
+ };
16
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
17
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
20
+ };
21
+ var _Measurement_instances, _Measurement_tryOnSocketRef, _Measurement_measurementSocketRef, _Measurement_timerPollingRef, _Measurement_timerWaitingRef, _Measurement_count, _Measurement_accessKey, _Measurement_getMeasurementsCheck, _Measurement_handlePolling, _Measurement_disconnectSocket, _Measurement_handleTimeOut;
22
+ import axios from "axios";
23
+ import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_RECOMMENDATION_WEBSOCKET_URL, APP_TRY_ON_WEBSOCKET_URL, REQUIRED_MESSAGE } from "./constants";
24
+ import { checkParameters } from "./utils";
25
+ class Measurement {
26
+ constructor(accessKey) {
27
+ _Measurement_instances.add(this);
28
+ _Measurement_tryOnSocketRef.set(this, null);
29
+ _Measurement_measurementSocketRef.set(this, null);
30
+ _Measurement_timerPollingRef.set(this, null);
31
+ _Measurement_timerWaitingRef.set(this, null);
32
+ _Measurement_count.set(this, 1);
33
+ _Measurement_accessKey.set(this, void 0);
34
+ __classPrivateFieldSet(this, _Measurement_accessKey, accessKey, "f");
35
+ }
36
+ getMeasurementResult(scanId) {
37
+ if (!checkParameters(scanId)) {
38
+ throw new Error(REQUIRED_MESSAGE);
39
+ }
40
+ const url = `${APP_AUTH_BASE_URL}/measurements?scanId=${scanId}`;
41
+ return axios.get(url, {
42
+ headers: { "X-Api-Key": __classPrivateFieldGet(this, _Measurement_accessKey, "f") },
43
+ });
44
+ }
45
+ getTryOnMeasurements({ scanId, shopDomain, productName }) {
46
+ if (!checkParameters(scanId, shopDomain, productName)) {
47
+ throw new Error(REQUIRED_MESSAGE);
48
+ }
49
+ const tryOnUrl = `${APP_AUTH_BASE_URL}${API_ENDPOINTS.TRY_ON_SCAN}/${scanId}/shop/${shopDomain}/product/${productName}`;
50
+ return axios.get(tryOnUrl, { headers: { "X-Api-Key": __classPrivateFieldGet(this, _Measurement_accessKey, "f") } });
51
+ }
52
+ handleTryOnSocket(options) {
53
+ var _a;
54
+ const { shopDomain, scanId, productName, onError, onSuccess, onClose, onOpen } = options;
55
+ if (!checkParameters(shopDomain, scanId, productName)) {
56
+ throw new Error(REQUIRED_MESSAGE);
57
+ }
58
+ (_a = __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f")) === null || _a === void 0 ? void 0 : _a.close();
59
+ const url = `${APP_TRY_ON_WEBSOCKET_URL}/develop?store_url=${shopDomain}&product_name=${productName}&scan_id=${scanId}`;
60
+ __classPrivateFieldSet(this, _Measurement_tryOnSocketRef, new WebSocket(url), "f");
61
+ __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onopen = () => {
62
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
63
+ };
64
+ __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onmessage = (event) => {
65
+ const data = JSON.parse(event.data);
66
+ if ((data === null || data === void 0 ? void 0 : data.tryOnProcessStatus) === "available") {
67
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
68
+ }
69
+ else {
70
+ onError === null || onError === void 0 ? void 0 : onError({ message: "failed to get image urls" });
71
+ }
72
+ };
73
+ __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onclose = () => {
74
+ onClose === null || onClose === void 0 ? void 0 : onClose();
75
+ };
76
+ __classPrivateFieldGet(this, _Measurement_tryOnSocketRef, "f").onerror = (event) => {
77
+ onError === null || onError === void 0 ? void 0 : onError(event);
78
+ };
79
+ }
80
+ handleMeasurementSocket(options) {
81
+ const { scanId, onError, onSuccess, onClose, onOpen } = options;
82
+ if (!checkParameters(scanId)) {
83
+ throw new Error(REQUIRED_MESSAGE);
84
+ }
85
+ setTimeout(() => {
86
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_disconnectSocket).call(this);
87
+ const url = `${APP_RECOMMENDATION_WEBSOCKET_URL}?scanId=${scanId}`;
88
+ __classPrivateFieldSet(this, _Measurement_measurementSocketRef, new WebSocket(url), "f");
89
+ __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onopen = () => {
90
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
91
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handleTimeOut).call(this, { scanId, onSuccess, onError });
92
+ };
93
+ __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onmessage = (event) => {
94
+ const data = JSON.parse(event.data);
95
+ if ((data === null || data === void 0 ? void 0 : data.code) === 200 && (data === null || data === void 0 ? void 0 : data.scanStatus) === "success") {
96
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
97
+ }
98
+ else {
99
+ onError === null || onError === void 0 ? void 0 : onError(data);
100
+ }
101
+ if (__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f")) {
102
+ clearTimeout(__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f"));
103
+ }
104
+ };
105
+ __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onclose = () => {
106
+ onClose === null || onClose === void 0 ? void 0 : onClose();
107
+ };
108
+ __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onerror = (event) => {
109
+ onError === null || onError === void 0 ? void 0 : onError(event);
110
+ };
111
+ }, 5000);
112
+ }
113
+ }
114
+ _Measurement_tryOnSocketRef = new WeakMap(), _Measurement_measurementSocketRef = new WeakMap(), _Measurement_timerPollingRef = new WeakMap(), _Measurement_timerWaitingRef = new WeakMap(), _Measurement_count = new WeakMap(), _Measurement_accessKey = new WeakMap(), _Measurement_instances = new WeakSet(), _Measurement_getMeasurementsCheck = function _Measurement_getMeasurementsCheck(options) {
115
+ return __awaiter(this, void 0, void 0, function* () {
116
+ var _a;
117
+ var _b;
118
+ const { scanId, onSuccess, onError } = options;
119
+ try {
120
+ const res = yield this.getMeasurementResult(scanId);
121
+ if ((res === null || res === void 0 ? void 0 : res.data) && ((_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.isMeasured) === true) {
122
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(res.data);
123
+ if (__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f")) {
124
+ clearInterval(__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f"));
125
+ }
126
+ }
127
+ else {
128
+ if (__classPrivateFieldGet(this, _Measurement_count, "f") < 8) {
129
+ __classPrivateFieldSet(this, _Measurement_count, (_b = __classPrivateFieldGet(this, _Measurement_count, "f"), _b++, _b), "f");
130
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handlePolling).call(this, { scanId, onSuccess, onError });
131
+ }
132
+ else {
133
+ __classPrivateFieldSet(this, _Measurement_count, 1, "f");
134
+ if (__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f")) {
135
+ clearInterval(__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f"));
136
+ }
137
+ onError === null || onError === void 0 ? void 0 : onError({ scanStatus: "failed", message: "Scan not found", isMeasured: false });
138
+ }
139
+ }
140
+ }
141
+ catch (e) {
142
+ if (__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f")) {
143
+ clearInterval(__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f"));
144
+ }
145
+ onError === null || onError === void 0 ? void 0 : onError(e);
146
+ }
147
+ });
148
+ }, _Measurement_handlePolling = function _Measurement_handlePolling(options) {
149
+ const { scanId, onSuccess, onError } = options;
150
+ if (__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f")) {
151
+ clearInterval(__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f"));
152
+ }
153
+ __classPrivateFieldSet(this, _Measurement_timerPollingRef, setTimeout(() => {
154
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_getMeasurementsCheck).call(this, { scanId, onSuccess, onError });
155
+ }, __classPrivateFieldGet(this, _Measurement_count, "f") * 5000), "f");
156
+ }, _Measurement_disconnectSocket = function _Measurement_disconnectSocket() {
157
+ var _a;
158
+ (_a = __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f")) === null || _a === void 0 ? void 0 : _a.close();
159
+ if (__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f")) {
160
+ clearTimeout(__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f"));
161
+ }
162
+ }, _Measurement_handleTimeOut = function _Measurement_handleTimeOut(options) {
163
+ const { scanId, onSuccess, onError } = options;
164
+ __classPrivateFieldSet(this, _Measurement_count, 1, "f");
165
+ __classPrivateFieldSet(this, _Measurement_timerWaitingRef, setTimeout(() => {
166
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handlePolling).call(this, { scanId, onSuccess, onError });
167
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_disconnectSocket).call(this);
168
+ }, 2 * 60000), "f");
169
+ };
170
+ export default Measurement;
@@ -0,0 +1,15 @@
1
+ interface VideoEmitOptions {
2
+ image: string;
3
+ scanId: string;
4
+ }
5
+ type PoseStatusCallback = (data: any) => void;
6
+ declare class PoseDetection {
7
+ #private;
8
+ constructor(accessKey: string);
9
+ connect(): Promise<string>;
10
+ videoEmit({ image, scanId }: VideoEmitOptions): void;
11
+ disconnect(): void;
12
+ poseStatus(callBack: PoseStatusCallback): void;
13
+ connected(): boolean;
14
+ }
15
+ export default PoseDetection;
@@ -0,0 +1,70 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ 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");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ 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");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _PoseDetection_socketRef, _PoseDetection_accessKey;
13
+ import { io } from "socket.io-client";
14
+ import { APP_POSE_DETECTION_WEBSOCKET_URL } from "./constants";
15
+ class PoseDetection {
16
+ constructor(accessKey) {
17
+ _PoseDetection_socketRef.set(this, null);
18
+ _PoseDetection_accessKey.set(this, void 0);
19
+ __classPrivateFieldSet(this, _PoseDetection_accessKey, accessKey, "f");
20
+ }
21
+ connect() {
22
+ return new Promise((resolve, reject) => {
23
+ __classPrivateFieldSet(this, _PoseDetection_socketRef, io(APP_POSE_DETECTION_WEBSOCKET_URL, {
24
+ auth: {
25
+ token: __classPrivateFieldGet(this, _PoseDetection_accessKey, "f"),
26
+ },
27
+ }), "f");
28
+ __classPrivateFieldGet(this, _PoseDetection_socketRef, "f").on("connect", () => {
29
+ var _a;
30
+ const socketId = (_a = __classPrivateFieldGet(this, _PoseDetection_socketRef, "f")) === null || _a === void 0 ? void 0 : _a.id;
31
+ if (socketId) {
32
+ resolve(socketId);
33
+ }
34
+ else {
35
+ reject(new Error("Failed to obtain socket ID."));
36
+ }
37
+ });
38
+ __classPrivateFieldGet(this, _PoseDetection_socketRef, "f").on("connect_error", (err) => {
39
+ reject(err);
40
+ });
41
+ });
42
+ }
43
+ videoEmit({ image, scanId }) {
44
+ if (!__classPrivateFieldGet(this, _PoseDetection_socketRef, "f")) {
45
+ throw new Error("Socket is not connected");
46
+ }
47
+ __classPrivateFieldGet(this, _PoseDetection_socketRef, "f").emit("video", {
48
+ image,
49
+ user_unique_key: scanId,
50
+ });
51
+ }
52
+ disconnect() {
53
+ var _a;
54
+ (_a = __classPrivateFieldGet(this, _PoseDetection_socketRef, "f")) === null || _a === void 0 ? void 0 : _a.disconnect();
55
+ }
56
+ poseStatus(callBack) {
57
+ if (!__classPrivateFieldGet(this, _PoseDetection_socketRef, "f")) {
58
+ throw new Error("Socket is not connected");
59
+ }
60
+ __classPrivateFieldGet(this, _PoseDetection_socketRef, "f").on("pose_status", (data) => {
61
+ callBack === null || callBack === void 0 ? void 0 : callBack(data);
62
+ });
63
+ }
64
+ connected() {
65
+ var _a;
66
+ return !!((_a = __classPrivateFieldGet(this, _PoseDetection_socketRef, "f")) === null || _a === void 0 ? void 0 : _a.connected);
67
+ }
68
+ }
69
+ _PoseDetection_socketRef = new WeakMap(), _PoseDetection_accessKey = new WeakMap();
70
+ export default PoseDetection;
@@ -0,0 +1,40 @@
1
+ import { AxiosResponse } from "axios";
2
+ interface UploadFileParams {
3
+ files: File[];
4
+ userId: string;
5
+ }
6
+ interface DeleteImageParams {
7
+ userId: string;
8
+ fileName: string;
9
+ }
10
+ interface HandleTryOnWebSocketParams {
11
+ shopDomain: string;
12
+ userId: string;
13
+ productName: string;
14
+ onError?: (error: any) => void;
15
+ onSuccess?: (data: any) => void;
16
+ onClose?: () => void;
17
+ onOpen?: () => void;
18
+ }
19
+ interface HandleForLatestImageParams {
20
+ shopDomain: string;
21
+ userId: string;
22
+ productName: string;
23
+ onError?: (error: any) => void;
24
+ }
25
+ interface GetTryOnResultParams {
26
+ shopDomain: string;
27
+ userId: string;
28
+ productName: string;
29
+ }
30
+ declare class TryOn {
31
+ #private;
32
+ constructor(accessKey: string);
33
+ uploadFile({ files, userId }: UploadFileParams): Promise<string>;
34
+ getUploadedFiles(userId: string): Promise<AxiosResponse<any>>;
35
+ deleteImage({ userId, fileName }: DeleteImageParams): Promise<AxiosResponse<any>>;
36
+ handleTryOnWebSocket: ({ shopDomain, userId, productName, onError, onSuccess, onClose, onOpen }: HandleTryOnWebSocketParams) => void;
37
+ handleForLatestImage: ({ userId, shopDomain, productName, onError }: HandleForLatestImageParams) => Promise<any>;
38
+ getTryOnResult: ({ userId, shopDomain, productName }: GetTryOnResultParams) => Promise<AxiosResponse<any>>;
39
+ }
40
+ export default TryOn;
@@ -0,0 +1,180 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
11
+ if (kind === "m") throw new TypeError("Private method is not writable");
12
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
13
+ 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");
14
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
15
+ };
16
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
17
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
20
+ };
21
+ var _TryOn_instances, _TryOn_tryOnSocketRef, _TryOn_timerWaitingRef, _TryOn_accessKey, _TryOn_getSignedUrl, _TryOn_s3Upload, _TryOn_disconnectSocket, _TryOn_handleTimeOut, _TryOn_handleGetTryOnResult;
22
+ import axios from "axios";
23
+ import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_AUTH_WEBSOCKET_URL, REQUIRED_MESSAGE } from "./constants";
24
+ import { checkParameters } from "./utils";
25
+ class TryOn {
26
+ constructor(accessKey) {
27
+ _TryOn_instances.add(this);
28
+ _TryOn_tryOnSocketRef.set(this, null);
29
+ _TryOn_timerWaitingRef.set(this, null);
30
+ _TryOn_accessKey.set(this, void 0);
31
+ _TryOn_disconnectSocket.set(this, () => {
32
+ var _a;
33
+ (_a = __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f")) === null || _a === void 0 ? void 0 : _a.close();
34
+ if (__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f")) {
35
+ clearTimeout(__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f"));
36
+ }
37
+ });
38
+ _TryOn_handleTimeOut.set(this, ({ onSuccess, onError, shopDomain, userId, productName }) => {
39
+ __classPrivateFieldSet(this, _TryOn_timerWaitingRef, setTimeout(() => {
40
+ __classPrivateFieldGet(this, _TryOn_handleGetTryOnResult, "f").call(this, { shopDomain, userId, productName, onSuccess, onError });
41
+ __classPrivateFieldGet(this, _TryOn_disconnectSocket, "f").call(this);
42
+ }, 120000), "f");
43
+ });
44
+ this.handleTryOnWebSocket = ({ shopDomain, userId, productName, onError, onSuccess, onClose, onOpen }) => {
45
+ if (checkParameters(shopDomain, userId, productName) === false) {
46
+ throw new Error(REQUIRED_MESSAGE);
47
+ }
48
+ __classPrivateFieldGet(this, _TryOn_disconnectSocket, "f").call(this);
49
+ const url = `${APP_AUTH_WEBSOCKET_URL}${API_ENDPOINTS.TRY_ON}/?store_url=${shopDomain}&product_name=${productName}&scan_id=${userId}`;
50
+ __classPrivateFieldSet(this, _TryOn_tryOnSocketRef, new WebSocket(url), "f");
51
+ __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onopen = () => __awaiter(this, void 0, void 0, function* () {
52
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
53
+ __classPrivateFieldGet(this, _TryOn_handleTimeOut, "f").call(this, { onSuccess, onError, shopDomain, userId, productName });
54
+ try {
55
+ yield this.handleForLatestImage({ shopDomain, userId, productName, onError });
56
+ }
57
+ catch (error) {
58
+ onError === null || onError === void 0 ? void 0 : onError(error);
59
+ }
60
+ });
61
+ __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onmessage = (event) => {
62
+ const data = JSON.parse(event.data);
63
+ if ((data === null || data === void 0 ? void 0 : data.status) === "success") {
64
+ __classPrivateFieldGet(this, _TryOn_handleGetTryOnResult, "f").call(this, { shopDomain, userId, productName, onError, onSuccess });
65
+ }
66
+ else {
67
+ onError === null || onError === void 0 ? void 0 : onError(data);
68
+ }
69
+ if (__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f")) {
70
+ clearTimeout(__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f"));
71
+ }
72
+ };
73
+ __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onclose = () => {
74
+ onClose === null || onClose === void 0 ? void 0 : onClose();
75
+ };
76
+ __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onerror = (event) => {
77
+ onError === null || onError === void 0 ? void 0 : onError(event);
78
+ if (__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f")) {
79
+ clearTimeout(__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f"));
80
+ }
81
+ };
82
+ };
83
+ this.handleForLatestImage = (_a) => __awaiter(this, [_a], void 0, function* ({ userId, shopDomain, productName, onError }) {
84
+ var _b;
85
+ if (checkParameters(shopDomain, userId, productName) === false) {
86
+ throw new Error(REQUIRED_MESSAGE);
87
+ }
88
+ try {
89
+ const url = `${APP_AUTH_BASE_URL}${API_ENDPOINTS.TRY_ON}/?scan_id=${userId}&store_url=${shopDomain}&product_name=${productName}`;
90
+ const res = yield axios.post(url, null, {
91
+ headers: { "X-Api-Key": __classPrivateFieldGet(this, _TryOn_accessKey, "f") },
92
+ });
93
+ if (((_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.tryOnProcessStatus) === "failed") {
94
+ __classPrivateFieldGet(this, _TryOn_disconnectSocket, "f").call(this);
95
+ throw res.data;
96
+ }
97
+ else {
98
+ return res.data;
99
+ }
100
+ }
101
+ catch (error) {
102
+ onError === null || onError === void 0 ? void 0 : onError(error);
103
+ __classPrivateFieldGet(this, _TryOn_disconnectSocket, "f").call(this);
104
+ throw error;
105
+ }
106
+ });
107
+ _TryOn_handleGetTryOnResult.set(this, (_c) => __awaiter(this, [_c], void 0, function* ({ onSuccess, onError, shopDomain, userId, productName }) {
108
+ try {
109
+ const data = yield this.getTryOnResult({ shopDomain, userId, productName });
110
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data.data);
111
+ }
112
+ catch (error) {
113
+ onError === null || onError === void 0 ? void 0 : onError(error);
114
+ }
115
+ }));
116
+ this.getTryOnResult = ({ userId, shopDomain, productName }) => {
117
+ if (checkParameters(shopDomain, userId, productName) === false) {
118
+ throw new Error(REQUIRED_MESSAGE);
119
+ }
120
+ const url = `${APP_AUTH_BASE_URL}${API_ENDPOINTS.TRY_ON_RESULT_IMAGE_DOWNLOAD}?scan_id=${userId}&store_url=${shopDomain}&product_name=${productName}`;
121
+ return axios.post(url, null, {
122
+ headers: { "X-Api-Key": __classPrivateFieldGet(this, _TryOn_accessKey, "f") },
123
+ });
124
+ };
125
+ __classPrivateFieldSet(this, _TryOn_accessKey, accessKey, "f");
126
+ }
127
+ uploadFile(_a) {
128
+ return __awaiter(this, arguments, void 0, function* ({ files, userId }) {
129
+ var _b;
130
+ try {
131
+ const payload = {
132
+ userId,
133
+ userImages: [(_b = files[0]) === null || _b === void 0 ? void 0 : _b.name],
134
+ };
135
+ if (files[1]) {
136
+ payload.userImages.push(files[1].name);
137
+ }
138
+ const signedUrlRes = yield __classPrivateFieldGet(this, _TryOn_instances, "m", _TryOn_getSignedUrl).call(this, payload);
139
+ for (const file of files) {
140
+ yield __classPrivateFieldGet(this, _TryOn_instances, "m", _TryOn_s3Upload).call(this, signedUrlRes.data.uploadUrls[file.name].url, file);
141
+ }
142
+ return "uploaded successfully!";
143
+ }
144
+ catch (error) {
145
+ throw error;
146
+ }
147
+ });
148
+ }
149
+ getUploadedFiles(userId) {
150
+ if (checkParameters(userId) === false) {
151
+ throw new Error(REQUIRED_MESSAGE);
152
+ }
153
+ return axios.post(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.TRY_ON_IMAGE_DOWNLOAD}?userId=${userId}`, null, {
154
+ headers: { "X-Api-Key": __classPrivateFieldGet(this, _TryOn_accessKey, "f") },
155
+ });
156
+ }
157
+ deleteImage({ userId, fileName }) {
158
+ if (checkParameters(userId, fileName) === false) {
159
+ throw new Error(REQUIRED_MESSAGE);
160
+ }
161
+ return axios.delete(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.TRY_ON_IMAGE_URLS}?userId=${userId}&file=${fileName}`, {
162
+ headers: { "X-Api-Key": __classPrivateFieldGet(this, _TryOn_accessKey, "f") },
163
+ });
164
+ }
165
+ }
166
+ _TryOn_tryOnSocketRef = new WeakMap(), _TryOn_timerWaitingRef = new WeakMap(), _TryOn_accessKey = new WeakMap(), _TryOn_disconnectSocket = new WeakMap(), _TryOn_handleTimeOut = new WeakMap(), _TryOn_handleGetTryOnResult = new WeakMap(), _TryOn_instances = new WeakSet(), _TryOn_getSignedUrl = function _TryOn_getSignedUrl(payload) {
167
+ return axios.post(`${APP_AUTH_BASE_URL}${API_ENDPOINTS.TRY_ON_IMAGE_UPLOAD}`, payload, {
168
+ headers: {
169
+ "Content-Type": "application/json",
170
+ "X-Api-Key": __classPrivateFieldGet(this, _TryOn_accessKey, "f"),
171
+ },
172
+ });
173
+ }, _TryOn_s3Upload = function _TryOn_s3Upload(url, file) {
174
+ return axios.put(url, file, {
175
+ headers: {
176
+ "Content-Type": file.type,
177
+ },
178
+ });
179
+ };
180
+ export default TryOn;
@@ -0,0 +1,12 @@
1
+ import { ObjMetaData } from "./constants";
2
+ export interface FetchDataOptions {
3
+ path: string;
4
+ body?: any;
5
+ queryParams?: string;
6
+ baseUrl?: string;
7
+ apiKey?: string;
8
+ headers?: Record<string, string>;
9
+ }
10
+ export declare function fetchData(options: FetchDataOptions): Promise<any>;
11
+ export declare function checkParameters(...args: any[]): boolean;
12
+ export declare function checkMetaDataValue(arr: ObjMetaData[]): boolean;
@@ -0,0 +1,61 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import axios from "axios";
11
+ import { APP_AUTH_BASE_URL, requiredMetaData } from "./constants";
12
+ export function fetchData(options) {
13
+ return __awaiter(this, void 0, void 0, function* () {
14
+ const { path, body, queryParams, baseUrl = APP_AUTH_BASE_URL, apiKey = "", headers = { "X-Api-Key": apiKey, "Content-Type": "application/json" }, } = options;
15
+ const apiUrl = `${baseUrl}${path}${queryParams ? `?${new URLSearchParams(queryParams)}` : ""}`;
16
+ try {
17
+ const res = yield axios.post(apiUrl, body, { headers });
18
+ if (res.status >= 200 && res.status < 300) {
19
+ return res.data;
20
+ }
21
+ console.error(`Error: Unexpected response status ${res.status}`);
22
+ return {};
23
+ }
24
+ catch (error) {
25
+ console.error(error, "while uploading");
26
+ return {};
27
+ }
28
+ });
29
+ }
30
+ export function checkParameters(...args) {
31
+ for (const element of args) {
32
+ if (!element) {
33
+ return false;
34
+ }
35
+ }
36
+ return true;
37
+ }
38
+ export function checkMetaDataValue(arr) {
39
+ for (const key of requiredMetaData) {
40
+ let hasRequiredKey = false;
41
+ for (const obj of arr) {
42
+ if (obj.hasOwnProperty(key) && obj[key] !== undefined && obj[key] !== null && obj[key] !== "" && typeof obj[key] !== "number") {
43
+ hasRequiredKey = true;
44
+ break;
45
+ }
46
+ }
47
+ if (!hasRequiredKey) {
48
+ return false;
49
+ }
50
+ }
51
+ let correctFormat = false;
52
+ for (const obj of arr) {
53
+ if (obj.callback_url && obj.callback_url.startsWith("https")) {
54
+ correctFormat = true;
55
+ }
56
+ }
57
+ if (!correctFormat) {
58
+ return false;
59
+ }
60
+ return true;
61
+ }
package/package.json CHANGED
@@ -1,9 +1,16 @@
1
1
  {
2
2
  "name": "@swan-admin/swan-ai-measurements",
3
- "version": "1.0.63",
3
+ "version": "1.0.65",
4
4
  "description": "provides ai measurement suggestion",
5
5
  "main": "dist/index.js",
6
- "module": "dist/index.esm.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "require": "./dist/index.js",
11
+ "import": "./dist/esm/index.js"
12
+ }
13
+ },
7
14
  "dependencies": {
8
15
  "@types/axios": "^0.14.0",
9
16
  "@types/node": "^20.12.12",
@@ -17,7 +24,7 @@
17
24
  },
18
25
  "scripts": {
19
26
  "start": "nodemon dist/index.js",
20
- "build": "tsc",
27
+ "build": "tsc && tsc --project tsconfig.esm.json",
21
28
  "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
22
29
  "start:dev": "nodemon src/index.ts",
23
30
  "start:debug": "nodemon --inspect-brk src/index.ts",
@@ -44,7 +51,6 @@
44
51
  "url": "https://github.com/paras-swan/swan-ai-measurements/issues"
45
52
  },
46
53
  "homepage": "https://github.com/paras-swan/swan-ai-measurements#readme",
47
- "types": "dist/index.d.ts",
48
54
  "devDependencies": {
49
55
  "nodemon": "^3.1.0"
50
56
  }
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es6",
4
+ "module": "esnext",
5
+ "moduleResolution": "node",
6
+ "outDir": "./dist/esm",
7
+ "declaration": true,
8
+ "strict": true,
9
+ "esModuleInterop": true,
10
+ "skipLibCheck": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "resolveJsonModule": true,
13
+ "allowSyntheticDefaultImports": true
14
+ },
15
+ "include": ["src/**/*.ts"],
16
+ "exclude": ["node_modules"]
17
+ }
package/tsconfig.json CHANGED
@@ -2,15 +2,16 @@
2
2
  "compilerOptions": {
3
3
  "target": "es6",
4
4
  "module": "commonjs",
5
+ "moduleResolution": "node",
5
6
  "outDir": "./dist",
6
7
  "declaration": true,
7
8
  "strict": true,
8
9
  "esModuleInterop": true,
9
10
  "skipLibCheck": true,
10
- "forceConsistentCasingInFileNames": true
11
+ "forceConsistentCasingInFileNames": true,
12
+ "resolveJsonModule": true,
13
+ "allowSyntheticDefaultImports": true
11
14
  },
12
- "include": [
13
- "src/**/*.ts"
14
- ],
15
+ "include": ["src/**/*.ts"],
15
16
  "exclude": ["node_modules"]
16
17
  }