polgo-upload-kit 1.0.0 → 1.0.2

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/.babelrc ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "presets": [
3
+ [
4
+ "@babel/preset-env",
5
+ {
6
+ "targets": {
7
+ "node": "20"
8
+ }
9
+ }
10
+ ]
11
+ ]
12
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ const axios = require("axios");
4
+ const fileType = require("file-type");
5
+ class PolgoUploadClient {
6
+ constructor(apiUrl) {
7
+ this.apiUrl = apiUrl || "https://mkgplyz3tc.execute-api.us-east-1.amazonaws.com/lambdaUploadProducao/arquivo/upload";
8
+ }
9
+ uploadFile(uint8Array, options = {}) {
10
+ let fileBlobPromise;
11
+ if (uint8Array instanceof Uint8Array) {
12
+ fileBlobPromise = fileType.fromBuffer(uint8Array).then(result => {
13
+ const mimeType = result?.mime || "application/octet-stream";
14
+ return new Blob([uint8Array], {
15
+ type: mimeType
16
+ });
17
+ });
18
+ } else {
19
+ return Promise.reject(new TypeError("The provided file is not a valid Uint8Array."));
20
+ }
21
+ return fileBlobPromise.then(fileBlob => {
22
+ const queryParams = new URLSearchParams();
23
+ if (options.diretorio) {
24
+ queryParams.append("diretorio", options.diretorio);
25
+ }
26
+ if (options.nomeArquivo) {
27
+ queryParams.append("nomeArquivo", options.nomeArquivo);
28
+ }
29
+ const finalUrl = `${this.apiUrl}${queryParams.toString() ? `?${queryParams.toString()}` : ""}`;
30
+ const form = new FormData();
31
+ const fileName = options.nomeArquivo || "uploaded_file";
32
+ form.append("file", fileBlob, fileName);
33
+ return axios.post(finalUrl, form, {
34
+ headers: {
35
+ "Content-Type": "multipart/form-data"
36
+ }
37
+ }).then(response => response.data).catch(error => {
38
+ console.error("Error uploading file:", error.message);
39
+ throw new Error(`Failed to upload file: ${error.message}`);
40
+ });
41
+ });
42
+ }
43
+ }
44
+ module.exports = {
45
+ PolgoUploadClient
46
+ };
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "polgo-upload-kit",
3
- "version": "1.0.0",
4
- "main": "index.js",
3
+ "version": "1.0.2",
4
+ "main": "dist/index.js",
5
5
  "scripts": {
6
- "test": "echo \"Error: no test specified\" && exit 1"
6
+ "build": "babel src --out-dir dist",
7
+ "prepublishOnly": "npm run build"
7
8
  },
8
9
  "keywords": [],
9
10
  "author": "",
@@ -14,5 +15,10 @@
14
15
  "file-type": "^16.5.4",
15
16
  "form-data": "^4.0.1",
16
17
  "mime-types": "^2.1.35"
18
+ },
19
+ "devDependencies": {
20
+ "@babel/cli": "^7.25.9",
21
+ "@babel/core": "^7.25.9",
22
+ "@babel/preset-env": "^7.25.9"
17
23
  }
18
24
  }
@@ -8,45 +8,49 @@ class PolgoUploadClient {
8
8
  "https://mkgplyz3tc.execute-api.us-east-1.amazonaws.com/lambdaUploadProducao/arquivo/upload";
9
9
  }
10
10
 
11
- async uploadFile(uint8Array, options = {}) {
12
- let fileBlob;
11
+ uploadFile(uint8Array, options = {}) {
12
+ let fileBlobPromise;
13
13
 
14
14
  if (uint8Array instanceof Uint8Array) {
15
- const mimeType =
16
- (await fileType.fromBuffer(uint8Array))?.mime ||
17
- "application/octet-stream";
18
- fileBlob = new Blob([uint8Array], { type: mimeType });
15
+ fileBlobPromise = fileType.fromBuffer(uint8Array).then((result) => {
16
+ const mimeType = result?.mime || "application/octet-stream";
17
+ return new Blob([uint8Array], { type: mimeType });
18
+ });
19
19
  } else {
20
- throw new TypeError("The provided file is not a valid Uint8Array.");
21
- }
22
-
23
- const queryParams = new URLSearchParams();
24
- if (options.diretorio) {
25
- queryParams.append("diretorio", options.diretorio);
26
- }
27
- if (options.nomeArquivo) {
28
- queryParams.append("nomeArquivo", options.nomeArquivo);
20
+ return Promise.reject(
21
+ new TypeError("The provided file is not a valid Uint8Array.")
22
+ );
29
23
  }
30
24
 
31
- const finalUrl = `${this.apiUrl}${
32
- queryParams.toString() ? `?${queryParams.toString()}` : ""
33
- }`;
34
-
35
- const form = new FormData();
36
- const fileName = options.nomeArquivo || "uploaded_file";
37
- form.append("file", fileBlob, fileName);
38
-
39
- try {
40
- const response = await axios.post(finalUrl, form, {
41
- headers: {
42
- "Content-Type": "multipart/form-data",
43
- },
44
- });
45
- return response.data;
46
- } catch (error) {
47
- console.error("Error uploading file:", error.message);
48
- throw new Error(`Failed to upload file: ${error.message}`);
49
- }
25
+ return fileBlobPromise.then((fileBlob) => {
26
+ const queryParams = new URLSearchParams();
27
+ if (options.diretorio) {
28
+ queryParams.append("diretorio", options.diretorio);
29
+ }
30
+ if (options.nomeArquivo) {
31
+ queryParams.append("nomeArquivo", options.nomeArquivo);
32
+ }
33
+
34
+ const finalUrl = `${this.apiUrl}${
35
+ queryParams.toString() ? `?${queryParams.toString()}` : ""
36
+ }`;
37
+
38
+ const form = new FormData();
39
+ const fileName = options.nomeArquivo || "uploaded_file";
40
+ form.append("file", fileBlob, fileName);
41
+
42
+ return axios
43
+ .post(finalUrl, form, {
44
+ headers: {
45
+ "Content-Type": "multipart/form-data",
46
+ },
47
+ })
48
+ .then((response) => response.data)
49
+ .catch((error) => {
50
+ console.error("Error uploading file:", error.message);
51
+ throw new Error(`Failed to upload file: ${error.message}`);
52
+ });
53
+ });
50
54
  }
51
55
  }
52
56