@allanfsouza/aether-sdk 2.4.2 → 2.4.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.
package/src/storage.ts CHANGED
@@ -12,18 +12,48 @@ export class StorageModule {
12
12
  this.http = http;
13
13
  }
14
14
 
15
+ // =================================================================
16
+ // COMPATIBILIDADE: MÈTODO .from() (Estilo Supabase/Drizzle)
17
+ // =================================================================
18
+
19
+ /**
20
+ * Seleciona um Bucket para operar.
21
+ * Ex: storage.from('avatars').upload(...)
22
+ */
23
+ from(bucketId: string) {
24
+ return {
25
+ upload: async (path: string, file: File | Blob | Buffer) => {
26
+ // Tenta inferir o contentType se for File/Blob
27
+ let contentType = "application/octet-stream";
28
+ if (typeof File !== "undefined" && file instanceof File) {
29
+ contentType = file.type;
30
+ } else if (typeof Blob !== "undefined" && file instanceof Blob) {
31
+ contentType = file.type;
32
+ }
33
+
34
+ // Chama o método upload original passando o bucket como folder/prefix
35
+ // O path vira o nome do arquivo final
36
+ return this.upload(file, path, contentType, bucketId);
37
+ },
38
+
39
+ list: async () => {
40
+ return this.list(bucketId);
41
+ }
42
+ };
43
+ }
44
+
45
+ // =================================================================
46
+ // MÉTODOS ORIGINAIS
47
+ // =================================================================
48
+
15
49
  /**
16
50
  * Faz o upload de um arquivo.
17
- * @param file Arquivo (Browser: File, Node: Buffer)
18
- * @param fileName Nome do arquivo (ex: 'foto.jpg')
19
- * @param contentType MIME Type (ex: 'image/jpeg')
20
- * @param folder (Opcional) Pasta de destino (ex: 'usuarios/123/')
21
51
  */
22
52
  async upload(
23
53
  file: File | Buffer | Blob,
24
54
  fileName: string,
25
55
  contentType: string,
26
- folder?: string // [NOVO]
56
+ folder?: string
27
57
  ) {
28
58
  // Calcula tamanho de forma segura para Browser e Node
29
59
  let size = 0;
@@ -32,7 +62,6 @@ export class StorageModule {
32
62
  } else if (typeof Blob !== "undefined" && file instanceof Blob) {
33
63
  size = file.size;
34
64
  } else if (file instanceof Buffer) {
35
- // Node.js
36
65
  size = file.length;
37
66
  }
38
67
 
@@ -41,40 +70,38 @@ export class StorageModule {
41
70
  fileName,
42
71
  contentType,
43
72
  size,
44
- prefix: folder || "", // [NOVO] Envia o prefixo para o backend
73
+ prefix: folder || "",
45
74
  });
46
75
 
47
76
  const presign = presignData.data;
48
77
  if (!presign?.url) throw new Error("Falha ao obter URL de upload.");
49
78
 
50
- // 2. Upload direto para S3
79
+ // 2. Upload direto para S3 (PUT na URL assinada)
51
80
  await axios.put(presign.url, file, {
52
81
  headers: { "Content-Type": contentType },
53
82
  });
54
83
 
55
84
  return {
56
- id: presign.objectId,
57
- key: presign.key,
58
- downloadUrl: presign.downloadUrl,
59
- url: presign.downloadUrl, // Alias amigável
85
+ data: {
86
+ id: presign.objectId,
87
+ key: presign.key,
88
+ path: presign.key, // Alias para facilitar o frontend
89
+ downloadUrl: presign.downloadUrl,
90
+ url: presign.downloadUrl,
91
+ },
92
+ error: null
60
93
  };
61
94
  }
62
95
 
63
- /**
64
- * Lista arquivos de uma pasta.
65
- */
66
96
  async list(folder = "") {
67
97
  const { data } = await this.http.get("/storage/list", {
68
98
  params: { projectId: this.client.projectId, prefix: folder },
69
99
  });
70
- return data.data; // Retorna { files: [], folders: [] }
100
+ return data.data;
71
101
  }
72
102
 
73
- /**
74
- * Deleta um arquivo pelo ID.
75
- */
76
103
  async delete(fileId: string) {
77
104
  await this.http.delete("/storage/delete", { data: { objectId: fileId } });
78
105
  return true;
79
106
  }
80
- }
107
+ }