polgo-upload-kit 1.0.2 → 1.0.4

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.
@@ -3,42 +3,46 @@
3
3
  const axios = require("axios");
4
4
  const fileType = require("file-type");
5
5
  class PolgoUploadClient {
6
- constructor(apiUrl) {
7
- this.apiUrl = apiUrl || "https://mkgplyz3tc.execute-api.us-east-1.amazonaws.com/lambdaUploadProducao/arquivo/upload";
6
+ constructor(isProd, stack) {
7
+ isProd ? this.ambiente = "producao" : this.ambiente = "dev";
8
+ this.url = "https://mkgplyz3tc.execute-api.us-east-1.amazonaws.com/lambdaUploadProducao/arquivo/upload";
9
+ this.stack = stack;
8
10
  }
9
- uploadFile(uint8Array, options = {}) {
10
- let fileBlobPromise;
11
+ async uploadFile(uint8Array, options = {}) {
12
+ let fileBlob;
11
13
  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
- });
14
+ const mimeType = (await fileType.fromBuffer(uint8Array))?.mime || "application/octet-stream";
15
+ fileBlob = new Blob([uint8Array], {
16
+ type: mimeType
17
17
  });
18
18
  } else {
19
- return Promise.reject(new TypeError("The provided file is not a valid Uint8Array."));
19
+ throw new TypeError("O arquivo não é um Uint8Array válido.");
20
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, {
21
+ const queryParams = new URLSearchParams();
22
+ queryParams.append("ambiente", this.ambiente);
23
+ queryParams.append("stack", this.stack);
24
+ if (options.diretorio) {
25
+ queryParams.append("diretorio", options.diretorio);
26
+ }
27
+ if (options.nomeArquivo) {
28
+ queryParams.append("nomeArquivo", options.nomeArquivo);
29
+ }
30
+ const finalUrl = `${this.url}${queryParams.toString() ? `?${queryParams.toString()}` : ""}`;
31
+ const form = new FormData();
32
+ const fileName = options.nomeArquivo || "uploaded_file";
33
+ form.append("file", fileBlob, fileName);
34
+ try {
35
+ const response = await axios.post(finalUrl, form, {
34
36
  headers: {
35
- "Content-Type": "multipart/form-data"
37
+ "Content-Type": "multipart/form-data",
38
+ Authorization: `Bearer 025154864ea17d1f294b69eb1defe112b5fa9a3c2bd88a40e70ebd29991da8aa`
36
39
  }
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
40
  });
41
- });
41
+ return response.data;
42
+ } catch (error) {
43
+ console.error("Erro ao fazer upload do arquivo:", error.message);
44
+ throw new Error(`Falha ao realizar o upload do arquivo: ${error.message}`);
45
+ }
42
46
  }
43
47
  }
44
48
  module.exports = {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "polgo-upload-kit",
3
- "version": "1.0.2",
4
- "main": "dist/index.js",
3
+ "version": "1.0.4",
4
+ "main": "dist/polgoUploadClient.js",
5
5
  "scripts": {
6
6
  "build": "babel src --out-dir dist",
7
7
  "prepublishOnly": "npm run build"
package/.babelrc DELETED
@@ -1,12 +0,0 @@
1
- {
2
- "presets": [
3
- [
4
- "@babel/preset-env",
5
- {
6
- "targets": {
7
- "node": "20"
8
- }
9
- }
10
- ]
11
- ]
12
- }
@@ -1,57 +0,0 @@
1
- const axios = require("axios");
2
- const fileType = require("file-type");
3
-
4
- class PolgoUploadClient {
5
- constructor(apiUrl) {
6
- this.apiUrl =
7
- apiUrl ||
8
- "https://mkgplyz3tc.execute-api.us-east-1.amazonaws.com/lambdaUploadProducao/arquivo/upload";
9
- }
10
-
11
- uploadFile(uint8Array, options = {}) {
12
- let fileBlobPromise;
13
-
14
- if (uint8Array instanceof Uint8Array) {
15
- fileBlobPromise = fileType.fromBuffer(uint8Array).then((result) => {
16
- const mimeType = result?.mime || "application/octet-stream";
17
- return new Blob([uint8Array], { type: mimeType });
18
- });
19
- } else {
20
- return Promise.reject(
21
- new TypeError("The provided file is not a valid Uint8Array.")
22
- );
23
- }
24
-
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
- });
54
- }
55
- }
56
-
57
- module.exports = { PolgoUploadClient };