@swan-admin/swan-ai-measurements 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/constants.js ADDED
@@ -0,0 +1,9 @@
1
+ import dotenv from "dotenv";
2
+ export const { FILE_UPLOAD_URL } = dotenv.config().parsed;
3
+
4
+ export const UPPY_FILE_UPLOAD_ENDPOINT = {
5
+ UPLOAD_START: "/upload/start",
6
+ UPLOAD_COMPLETE: "/upload/complete",
7
+ UPLOAD_SIGN_PART: "/upload/signpart",
8
+ UPLOAD_ABORT: "/upload/abort",
9
+ };
package/index.js ADDED
@@ -0,0 +1,80 @@
1
+ import Uppy from "@uppy/core";
2
+ import { UPPY_FILE_UPLOAD_ENDPOINT } from "./constants.js";
3
+ import { fetchData } from "./utils.js";
4
+ import AwsS3Multipart from "@uppy/aws-s3-multipart";
5
+ export default class Swan {
6
+ constructor(key) {
7
+ if (key !== 12345) {
8
+ throw new Error({ message: "wrong access key" });
9
+ }
10
+ this.uppyIns = null;
11
+ }
12
+ uploadFile({ file, objMetaData, scanId }) {
13
+ this.uppyIns = new Uppy({ autoProceed: true });
14
+ this.uppyIns.use(AwsS3Multipart, {
15
+ limit: 10,
16
+ retryDelays: [0, 1000, 3000, 5000],
17
+ getChunkSize: () => 5 * 1024 * 1024,
18
+ createMultipartUpload: (file) => {
19
+ const objectKey = `${scanId}.${file.extension}`;
20
+ return fetchData({
21
+ path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_START,
22
+ body: {
23
+ objectKey,
24
+ contentType: file.type,
25
+ objectMetadata: objMetaData,
26
+ },
27
+ });
28
+ },
29
+ completeMultipartUpload: (file, { uploadId, key, parts }) =>
30
+ fetchData({
31
+ path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_COMPLETE,
32
+ body: {
33
+ uploadId,
34
+ objectKey: key,
35
+ parts,
36
+ originalFileName: file.name,
37
+ },
38
+ }),
39
+
40
+ signPart: (file, partData) =>
41
+ fetchData({
42
+ path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_SIGN_PART,
43
+ body: {
44
+ objectKey: partData.key,
45
+ uploadId: partData.uploadId,
46
+ partNumber: partData.partNumber,
47
+ },
48
+ }),
49
+
50
+ abortMultipartUpload: (file, { uploadId, key }) =>
51
+ fetchData({
52
+ path: UPPY_FILE_UPLOAD_ENDPOINT.UPLOAD_ABORT,
53
+ body: {
54
+ uploadId,
55
+ objectKey: key,
56
+ originalFileName: file.name,
57
+ },
58
+ }),
59
+ });
60
+
61
+ this.uppyIns.addFile({
62
+ source: "manual",
63
+ name: file.name,
64
+ type: file.type,
65
+ data: file,
66
+ });
67
+
68
+ this.uppyIns.on("upload-error", (file, error, response) => {
69
+ throw new Error({ message: "file uploading failed", error });
70
+ });
71
+ this.uppyIns.on("upload-success", () => {
72
+ return { message: "file uploaded successfully" };
73
+ });
74
+ this.uppyIns.on("complete", (result) => {
75
+ if (this.uppyIns) {
76
+ this.uppyIns.close();
77
+ }
78
+ });
79
+ }
80
+ }
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@swan-admin/swan-ai-measurements",
3
+ "version": "1.0.7",
4
+ "description": "provides ai measurement suggestion",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "dependencies": {
8
+ "@uppy/aws-s3-multipart": "^3.10.2",
9
+ "@uppy/core": "^3.9.3",
10
+ "axios": "^1.6.7",
11
+ "dotenv": "^16.4.5"
12
+ },
13
+ "devDependencies": {
14
+ "nodemon": "^3.1.0"
15
+ },
16
+ "scripts": {
17
+ "start": "nodemon index.js",
18
+ "test": "echo \"Error: no test specified\" && exit 1"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://paras-swan@github.com/paras-swan/swan-ai-measurements.git"
23
+ },
24
+ "keywords": [
25
+ "swan",
26
+ "ai",
27
+ "fitview"
28
+ ],
29
+ "author": "paras",
30
+ "license": "ISC",
31
+ "bugs": {
32
+ "url": "https://github.com/paras-swan/swan-ai-measurements/issues"
33
+ },
34
+ "homepage": "https://github.com/paras-swan/swan-ai-measurements#readme"
35
+ }
package/utils.js ADDED
@@ -0,0 +1,24 @@
1
+ import { FILE_UPLOAD_URL } from "./constants.js";
2
+ import axios from "axios";
3
+
4
+ export async function fetchData({
5
+ path,
6
+ body,
7
+ queryParams,
8
+ baseUrl = FILE_UPLOAD_URL,
9
+ apiKey = "",
10
+ headers = { "X-Api-Key": apiKey, "Content-Type": "application/json" },
11
+ }) {
12
+ const apiUrl = `${baseUrl}${path}${queryParams ? `?${new URLSearchParams(queryParams)}` : ""}`;
13
+ try {
14
+ const res = await axios.post(apiUrl, body, { headers });
15
+ if (res.status >= 200 && res.status < 300) {
16
+ return res?.data;
17
+ }
18
+ console.error(`Error: Unexpected response status ${res?.status}`);
19
+ return {};
20
+ } catch (error) {
21
+ console.error(error, "while uploading");
22
+ return {};
23
+ }
24
+ }