@swan-admin/swan-ai-measurements 1.1.21 → 1.1.23

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/src/custom.ts CHANGED
@@ -1,57 +1,57 @@
1
- import axios, { AxiosResponse } from "axios";
2
- import { API_ENDPOINTS, APP_AUTH_BASE_URL, REQUIRED_MESSAGE } from "./constants.js";
3
- import { checkParameters, getUrl } from "./utils.js";
4
-
5
- interface CreateCustomer {
6
- name: string;
7
- storeUrl: string;
8
- location: string;
9
- email: string;
10
- emailsTier_1?: string;
11
- emailsTier_2?: string;
12
- }
13
- class Custom {
14
- #accessKey: string;
15
- #stagingUrl: boolean;
16
- constructor(accessKey: string, stagingUrl = false) {
17
- this.#accessKey = accessKey;
18
- this.#stagingUrl = stagingUrl;
19
- }
20
-
21
- createCustomer(payload: CreateCustomer): Promise<AxiosResponse<any>> {
22
- if (checkParameters(payload.name, payload.storeUrl, payload.email, payload.location) === false) {
23
- throw new Error(REQUIRED_MESSAGE);
24
- }
25
- return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.CREATE_CUSTOMER}`, {
26
- ...payload,
27
- headers: { "X-Api-Key": this.#accessKey },
28
- });
29
- }
30
-
31
- getCustomCustomerConfig: (store_url: string) => Promise<AxiosResponse<any>> = (store_url: string) => {
32
- if (checkParameters(store_url) === false) {
33
- throw new Error(REQUIRED_MESSAGE);
34
- }
35
- return axios.get(
36
- `${getUrl({
37
- urlName: APP_AUTH_BASE_URL,
38
- stagingUrl: this.#stagingUrl,
39
- })}${API_ENDPOINTS.CUSTOM_CUSTOMER}`,
40
- {
41
- params: { store_url },
42
- headers: { "X-Api-Key": this.#accessKey },
43
- }
44
- );
45
- };
46
-
47
- getModelUrl: (id: string) => Promise<AxiosResponse<any>> = (id: string) => {
48
- if (checkParameters(id) === false) {
49
- throw new Error(REQUIRED_MESSAGE);
50
- }
51
- return axios.get(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.MODEL}/${id}`, {
52
- headers: { "X-Api-Key": this.#accessKey },
53
- });
54
- };
55
- }
56
-
57
- export default Custom;
1
+ import axios, { AxiosResponse } from "axios";
2
+ import { API_ENDPOINTS, APP_AUTH_BASE_URL, REQUIRED_MESSAGE } from "./constants.js";
3
+ import { checkParameters, getUrl } from "./utils.js";
4
+
5
+ interface CreateCustomer {
6
+ name: string;
7
+ storeUrl: string;
8
+ location: string;
9
+ email: string;
10
+ emailsTier_1?: string;
11
+ emailsTier_2?: string;
12
+ }
13
+ class Custom {
14
+ #accessKey: string;
15
+ #stagingUrl: boolean;
16
+ constructor(accessKey: string, stagingUrl = false) {
17
+ this.#accessKey = accessKey;
18
+ this.#stagingUrl = stagingUrl;
19
+ }
20
+
21
+ createCustomer(payload: CreateCustomer): Promise<AxiosResponse<any>> {
22
+ if (checkParameters(payload.name, payload.storeUrl, payload.email, payload.location) === false) {
23
+ throw new Error(REQUIRED_MESSAGE);
24
+ }
25
+ return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.CREATE_CUSTOMER}`, {
26
+ ...payload,
27
+ headers: { "X-Api-Key": this.#accessKey },
28
+ });
29
+ }
30
+
31
+ getCustomCustomerConfig: (store_url: string) => Promise<AxiosResponse<any>> = (store_url: string) => {
32
+ if (checkParameters(store_url) === false) {
33
+ throw new Error(REQUIRED_MESSAGE);
34
+ }
35
+ return axios.get(
36
+ `${getUrl({
37
+ urlName: APP_AUTH_BASE_URL,
38
+ stagingUrl: this.#stagingUrl,
39
+ })}${API_ENDPOINTS.CUSTOM_CUSTOMER}`,
40
+ {
41
+ params: { store_url },
42
+ headers: { "X-Api-Key": this.#accessKey },
43
+ }
44
+ );
45
+ };
46
+
47
+ getModelUrl: (id: string) => Promise<AxiosResponse<any>> = (id: string) => {
48
+ if (checkParameters(id) === false) {
49
+ throw new Error(REQUIRED_MESSAGE);
50
+ }
51
+ return axios.get(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.MODEL}/${id}`, {
52
+ headers: { "X-Api-Key": this.#accessKey },
53
+ });
54
+ };
55
+ }
56
+
57
+ export default Custom;
package/src/fileUpload.ts CHANGED
@@ -1,175 +1,183 @@
1
- import axios from "axios";
2
- import { REQUIRED_MESSAGE, REQUIRED_MESSAGE_FOR_META_DATA, FILE_UPLOAD_ENDPOINT, APP_AUTH_BASE_URL, REQUIRED_ERROR_MESSAGE_INVALID_EMAIL } from "./constants.js";
3
- import { addScanType, checkMetaDataValue, checkParameters, fetchData, getFileChunks, getUrl } from "./utils.js";
4
- const Uppy = require("fix-esm").require("@uppy/core");
5
- const AwsS3Multipart = require("fix-esm").require("@uppy/aws-s3-multipart");
6
- interface ObjMetaData {
7
- gender: string;
8
- scan_id?: string;
9
- email: string;
10
- focal_length: string;
11
- height: string;
12
- customer_store_url: string;
13
- clothes_fit: string;
14
- scan_type?: string;
15
- callback_url: string;
16
- }
17
-
18
- interface UploadOptions {
19
- file: File;
20
- arrayMetaData: Partial<ObjMetaData>[];
21
- scanId: string;
22
- email: string;
23
- }
24
-
25
- export default class FileUpload {
26
- #uppyIns: any;
27
- #accessKey: string;
28
- #stagingUrl: boolean;
29
-
30
- constructor(accessKey: string, stagingUrl = false) {
31
- this.#accessKey = accessKey;
32
- this.#stagingUrl = stagingUrl;
33
- }
34
-
35
- async uploadFileFrontend({ file, arrayMetaData, scanId, email }: UploadOptions) {
36
- if (!checkParameters(file, arrayMetaData, scanId)) {
37
- throw new Error(REQUIRED_MESSAGE);
38
- }
39
- if (!checkMetaDataValue(arrayMetaData)) {
40
- throw new Error(REQUIRED_MESSAGE_FOR_META_DATA);
41
- }
42
- if(!email.trim()){
43
- throw new Error(REQUIRED_ERROR_MESSAGE_INVALID_EMAIL);
44
- }
45
- arrayMetaData = addScanType(arrayMetaData, scanId, email);
46
- return new Promise((resolve, reject) => {
47
- if (this.#uppyIns) {
48
- this.#uppyIns.close();
49
- }
50
- this.#uppyIns = new Uppy.default({ autoProceed: true });
51
- this.#uppyIns.use(AwsS3Multipart.default, {
52
- limit: 10,
53
- retryDelays: [0, 1000, 3000, 5000],
54
- companionUrl: getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl }),
55
- getChunkSize: () => 5 * 1024 * 1024,
56
- createMultipartUpload: (file: any) => {
57
- const objectKey = `${scanId}.${file.extension}`;
58
- return fetchData({
59
- path: FILE_UPLOAD_ENDPOINT.UPLOAD_START,
60
- apiKey: this.#accessKey,
61
- stagingUrl: this.#stagingUrl,
62
- body: {
63
- objectKey,
64
- contentType: file.type,
65
- objectMetadata: arrayMetaData,
66
- },
67
- });
68
- },
69
- completeMultipartUpload: (file: any, { uploadId, key, parts }: { uploadId: string | number; key: string | number; parts: any }) =>
70
- fetchData({
71
- path: FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
72
- apiKey: this.#accessKey,
73
- stagingUrl: this.#stagingUrl,
74
- body: {
75
- uploadId,
76
- objectKey: key,
77
- parts,
78
- originalFileName: file.name,
79
- },
80
- }),
81
-
82
- signPart: (file: any, partData: any) =>
83
- fetchData({
84
- path: FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
85
- stagingUrl: this.#stagingUrl,
86
- apiKey: this.#accessKey,
87
- body: {
88
- objectKey: partData.key,
89
- uploadId: partData.uploadId,
90
- partNumber: partData.partNumber,
91
- },
92
- }),
93
- });
94
-
95
- this.#uppyIns.addFile({
96
- source: "manual",
97
- name: file.name,
98
- type: file.type,
99
- data: file,
100
- });
101
-
102
- this.#uppyIns.on("upload-error", (file: any, error: any, response: any) => {
103
- reject(error);
104
- });
105
- this.#uppyIns.on("upload-success", () => {
106
- resolve({ message: "file uploaded successfully" });
107
- });
108
- this.#uppyIns.on("complete", (result: any) => {
109
- if (this.#uppyIns) {
110
- this.#uppyIns.close();
111
- }
112
- });
113
- });
114
- }
115
-
116
- async uploadFile({ file, arrayMetaData, scanId, email }: UploadOptions) {
117
- if (!checkParameters(file, arrayMetaData, scanId)) {
118
- throw new Error(REQUIRED_MESSAGE);
119
- }
120
- if (!checkMetaDataValue(arrayMetaData)) {
121
- throw new Error(REQUIRED_MESSAGE_FOR_META_DATA);
122
- }
123
- if(!email.trim()){
124
- throw new Error(REQUIRED_ERROR_MESSAGE_INVALID_EMAIL);
125
- }
126
- arrayMetaData = addScanType(arrayMetaData, scanId, email);
127
- return new Promise(async (resolve, reject) => {
128
- try {
129
- const res: { key: string; uploadId: string } = await fetchData({
130
- path: FILE_UPLOAD_ENDPOINT.UPLOAD_START,
131
- apiKey: this.#accessKey,
132
- stagingUrl: this.#stagingUrl,
133
- body: {
134
- objectKey: file.name,
135
- contentType: file.type,
136
- objectMetadata: arrayMetaData,
137
- },
138
- throwError: true,
139
- });
140
- const totalChunks = getFileChunks(file);
141
- const parts = [];
142
- for (let i = 0; i < totalChunks.length; i++) {
143
- const data: { url: string } = await fetchData({
144
- path: FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
145
- apiKey: this.#accessKey,
146
- stagingUrl: this.#stagingUrl,
147
- body: {
148
- objectKey: res?.key,
149
- uploadId: res?.uploadId,
150
- partNumber: i + 1,
151
- },
152
- throwError: true,
153
- });
154
- const val = await axios.put(data?.url, totalChunks[i], { headers: { "Content-Type": file.type, "X-Api-Key": this.#accessKey } });
155
- parts.push({ PartNumber: i + 1, ETag: val?.headers?.etag });
156
- }
157
- const completeValue = await fetchData({
158
- path: FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
159
- apiKey: this.#accessKey,
160
- stagingUrl: this.#stagingUrl,
161
- body: {
162
- uploadId: res?.uploadId,
163
- objectKey: res?.key,
164
- parts,
165
- originalFileName: file.name,
166
- },
167
- throwError: true,
168
- });
169
- resolve({ message: "successfully uploaded", data: completeValue });
170
- } catch (error: any) {
171
- reject(error);
172
- }
173
- });
174
- }
175
- }
1
+ import axios from "axios";
2
+ import {
3
+ REQUIRED_MESSAGE,
4
+ REQUIRED_MESSAGE_FOR_META_DATA,
5
+ FILE_UPLOAD_ENDPOINT,
6
+ APP_AUTH_BASE_URL,
7
+ REQUIRED_ERROR_MESSAGE_INVALID_EMAIL,
8
+ } from "./constants.js";
9
+ import { addScanType, checkMetaDataValue, checkParameters, fetchData, getFileChunks, getUrl, isValidEmail } from "./utils.js";
10
+ const Uppy = require("fix-esm").require("@uppy/core");
11
+ const AwsS3Multipart = require("fix-esm").require("@uppy/aws-s3-multipart");
12
+ interface ObjMetaData {
13
+ gender: string;
14
+ scan_id?: string;
15
+ email: string;
16
+ focal_length: string;
17
+ height: string;
18
+ customer_store_url: string;
19
+ clothes_fit: string;
20
+ scan_type?: string;
21
+ callback_url: string;
22
+ }
23
+
24
+ interface UploadOptions {
25
+ file: File;
26
+ arrayMetaData: Partial<ObjMetaData>[];
27
+ scanId: string;
28
+ email: string;
29
+ }
30
+
31
+ export default class FileUpload {
32
+ #uppyIns: any;
33
+ #accessKey: string;
34
+ #stagingUrl: boolean;
35
+
36
+ constructor(accessKey: string, stagingUrl = false) {
37
+ this.#accessKey = accessKey;
38
+ this.#stagingUrl = stagingUrl;
39
+ }
40
+
41
+ async uploadFileFrontend({ file, arrayMetaData, scanId, email }: UploadOptions) {
42
+ email = email.trim();
43
+ if (!checkParameters(file, arrayMetaData, scanId, email)) {
44
+ throw new Error(REQUIRED_MESSAGE);
45
+ }
46
+ if (!isValidEmail(email)) {
47
+ throw new Error(REQUIRED_ERROR_MESSAGE_INVALID_EMAIL);
48
+ }
49
+ if (!checkMetaDataValue(arrayMetaData)) {
50
+ throw new Error(REQUIRED_MESSAGE_FOR_META_DATA);
51
+ }
52
+ arrayMetaData = addScanType(arrayMetaData, scanId, email);
53
+ return new Promise((resolve, reject) => {
54
+ if (this.#uppyIns) {
55
+ this.#uppyIns.close();
56
+ }
57
+ this.#uppyIns = new Uppy.default({ autoProceed: true });
58
+ this.#uppyIns.use(AwsS3Multipart.default, {
59
+ limit: 10,
60
+ retryDelays: [0, 1000, 3000, 5000],
61
+ companionUrl: getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl }),
62
+ getChunkSize: () => 5 * 1024 * 1024,
63
+ createMultipartUpload: (file: any) => {
64
+ const objectKey = `${scanId}.${file.extension}`;
65
+ return fetchData({
66
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_START,
67
+ apiKey: this.#accessKey,
68
+ stagingUrl: this.#stagingUrl,
69
+ body: {
70
+ objectKey,
71
+ contentType: file.type,
72
+ objectMetadata: arrayMetaData,
73
+ },
74
+ });
75
+ },
76
+ completeMultipartUpload: (file: any, { uploadId, key, parts }: { uploadId: string | number; key: string | number; parts: any }) =>
77
+ fetchData({
78
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
79
+ apiKey: this.#accessKey,
80
+ stagingUrl: this.#stagingUrl,
81
+ body: {
82
+ uploadId,
83
+ objectKey: key,
84
+ parts,
85
+ originalFileName: file.name,
86
+ },
87
+ }),
88
+
89
+ signPart: (file: any, partData: any) =>
90
+ fetchData({
91
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
92
+ stagingUrl: this.#stagingUrl,
93
+ apiKey: this.#accessKey,
94
+ body: {
95
+ objectKey: partData.key,
96
+ uploadId: partData.uploadId,
97
+ partNumber: partData.partNumber,
98
+ },
99
+ }),
100
+ });
101
+
102
+ this.#uppyIns.addFile({
103
+ source: "manual",
104
+ name: file.name,
105
+ type: file.type,
106
+ data: file,
107
+ });
108
+
109
+ this.#uppyIns.on("upload-error", (file: any, error: any, response: any) => {
110
+ reject(error);
111
+ });
112
+ this.#uppyIns.on("upload-success", () => {
113
+ resolve({ message: "file uploaded successfully" });
114
+ });
115
+ this.#uppyIns.on("complete", (result: any) => {
116
+ if (this.#uppyIns) {
117
+ this.#uppyIns.close();
118
+ }
119
+ });
120
+ });
121
+ }
122
+
123
+ async uploadFile({ file, arrayMetaData, scanId, email }: UploadOptions) {
124
+ email = email.trim();
125
+ if (!checkParameters(file, arrayMetaData, scanId, email)) {
126
+ throw new Error(REQUIRED_MESSAGE);
127
+ }
128
+ if (!isValidEmail(email)) {
129
+ throw new Error(REQUIRED_ERROR_MESSAGE_INVALID_EMAIL);
130
+ }
131
+ if (!checkMetaDataValue(arrayMetaData)) {
132
+ throw new Error(REQUIRED_MESSAGE_FOR_META_DATA);
133
+ }
134
+ arrayMetaData = addScanType(arrayMetaData, scanId, email);
135
+ return new Promise(async (resolve, reject) => {
136
+ try {
137
+ const res: { key: string; uploadId: string } = await fetchData({
138
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_START,
139
+ apiKey: this.#accessKey,
140
+ stagingUrl: this.#stagingUrl,
141
+ body: {
142
+ objectKey: file.name,
143
+ contentType: file.type,
144
+ objectMetadata: arrayMetaData,
145
+ },
146
+ throwError: true,
147
+ });
148
+ const totalChunks = getFileChunks(file);
149
+ const parts = [];
150
+ for (let i = 0; i < totalChunks.length; i++) {
151
+ const data: { url: string } = await fetchData({
152
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
153
+ apiKey: this.#accessKey,
154
+ stagingUrl: this.#stagingUrl,
155
+ body: {
156
+ objectKey: res?.key,
157
+ uploadId: res?.uploadId,
158
+ partNumber: i + 1,
159
+ },
160
+ throwError: true,
161
+ });
162
+ const val = await axios.put(data?.url, totalChunks[i], { headers: { "Content-Type": file.type, "X-Api-Key": this.#accessKey } });
163
+ parts.push({ PartNumber: i + 1, ETag: val?.headers?.etag });
164
+ }
165
+ const completeValue = await fetchData({
166
+ path: FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
167
+ apiKey: this.#accessKey,
168
+ stagingUrl: this.#stagingUrl,
169
+ body: {
170
+ uploadId: res?.uploadId,
171
+ objectKey: res?.key,
172
+ parts,
173
+ originalFileName: file.name,
174
+ },
175
+ throwError: true,
176
+ });
177
+ resolve({ message: "successfully uploaded", data: completeValue });
178
+ } catch (error: any) {
179
+ reject(error);
180
+ }
181
+ });
182
+ }
183
+ }
package/src/index.ts CHANGED
@@ -1,30 +1,30 @@
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
-
8
- class Swan {
9
- #accessKey: string;
10
- #stagingUrl: boolean;
11
- auth: Auth;
12
- custom: Custom;
13
- fileUpload: FileUpload;
14
- measurement: Measurement;
15
- poseDetection: PoseDetection;
16
- tryOn: TryOn;
17
-
18
- constructor(accessKey: string, stagingUrl = false) {
19
- this.#accessKey = accessKey;
20
- this.#stagingUrl = stagingUrl;
21
- this.auth = new Auth(this.#accessKey, this.#stagingUrl);
22
- this.custom = new Custom(this.#accessKey, this.#stagingUrl);
23
- this.fileUpload = new FileUpload(this.#accessKey, this.#stagingUrl);
24
- this.measurement = new Measurement(this.#accessKey, this.#stagingUrl);
25
- this.poseDetection = new PoseDetection(this.#accessKey, this.#stagingUrl);
26
- this.tryOn = new TryOn(this.#accessKey, this.#stagingUrl);
27
- }
28
- }
29
-
30
- export default Swan;
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
+
8
+ class Swan {
9
+ #accessKey: string;
10
+ #stagingUrl: boolean;
11
+ auth: Auth;
12
+ custom: Custom;
13
+ fileUpload: FileUpload;
14
+ measurement: Measurement;
15
+ poseDetection: PoseDetection;
16
+ tryOn: TryOn;
17
+
18
+ constructor(accessKey: string, stagingUrl = false) {
19
+ this.#accessKey = accessKey;
20
+ this.#stagingUrl = stagingUrl;
21
+ this.auth = new Auth(this.#accessKey, this.#stagingUrl);
22
+ this.custom = new Custom(this.#accessKey, this.#stagingUrl);
23
+ this.fileUpload = new FileUpload(this.#accessKey, this.#stagingUrl);
24
+ this.measurement = new Measurement(this.#accessKey, this.#stagingUrl);
25
+ this.poseDetection = new PoseDetection(this.#accessKey, this.#stagingUrl);
26
+ this.tryOn = new TryOn(this.#accessKey, this.#stagingUrl);
27
+ }
28
+ }
29
+
30
+ export default Swan;