@darseen/vaze 1.1.1 → 1.2.1

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/README.md CHANGED
@@ -61,7 +61,7 @@ The SDK is divided into two main modules: `files` and `folders`. All methods are
61
61
  ```ts
62
62
  const { data, error } = await vaze.files.getAll({
63
63
  limit: 10,
64
- orderBy: "created_at",
64
+ orderBy: "createdAt",
65
65
  orderDirection: "DESC",
66
66
  });
67
67
 
@@ -76,7 +76,7 @@ if (data) {
76
76
  const { data, error } = await vaze.files.getById("file-id");
77
77
  ```
78
78
 
79
- `getByName(name, options?)` Search for files by name.
79
+ `getByName(name, options?)` Search for files by name. Returns an empty `files` array when nothing matches.
80
80
 
81
81
  ```ts
82
82
  const { data, error } = await vaze.files.getByName("document.pdf", {
@@ -87,8 +87,8 @@ const { data, error } = await vaze.files.getByName("document.pdf", {
87
87
  `upload(data)` Upload one or multiple files to your Vaze instance.
88
88
 
89
89
  - data:
90
- - `files` (File[]): An array of standard `File` objects.
91
- - `folder?` (string): The ID or name of the target folder.
90
+ - `files` (File[]): An array of standard web [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) objects.
91
+ - `folder?` (string): The path of the target folder, created if it doesn't exist.
92
92
 
93
93
  ```ts
94
94
  const { data, error } = await vaze.files.upload({
@@ -97,6 +97,18 @@ const { data, error } = await vaze.files.upload({
97
97
  });
98
98
  ```
99
99
 
100
+ `download(id)` Download a file's contents by its ID.
101
+
102
+ ```ts
103
+ const { data, error } = await vaze.files.download("file-id-123");
104
+
105
+ if (data) {
106
+ console.log(data.filename); // original file name
107
+ console.log(data.contentType); // e.g. "image/png"
108
+ const buffer = Buffer.from(await data.blob.arrayBuffer());
109
+ }
110
+ ```
111
+
100
112
  `rename(data)` Rename an existing file.
101
113
 
102
114
  - data:
@@ -165,6 +177,18 @@ const { data, error } = await vaze.folders.rename({
165
177
  const { data, error } = await vaze.folders.delete("folder-id");
166
178
  ```
167
179
 
180
+ # Health Check
181
+
182
+ `vaze.health()` pings the instance to verify connectivity.
183
+
184
+ ```ts
185
+ const { data, error } = await vaze.health();
186
+
187
+ if (data) {
188
+ console.log(data.message); // "Vaze is running!"
189
+ }
190
+ ```
191
+
168
192
  # Types
169
193
 
170
- This SDK is built with TypeScript and exports all necessary types to ensure type safety in your applications. Types such as `FileWithUrl`, `Folder`, and `ApiResponse`.
194
+ This SDK is built with TypeScript and exports all necessary types to ensure type safety in your applications. Types such as `File`, `Folder`, `ApiResponse`, `ListOptions`, and `VazeOptions`.
package/dist/index.cjs CHANGED
@@ -1,4 +1,28 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/base.ts
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/utils/index.ts
2
+ function constructFileUrls({
3
+ vazeUrl,
4
+ files
5
+ }) {
6
+ const base = vazeUrl.endsWith("/") ? vazeUrl : `${vazeUrl}/`;
7
+ return files.map((file) => ({
8
+ ...file,
9
+ url: new URL(file.url, base).toString()
10
+ }));
11
+ }
12
+ function parseContentDispositionFilename(header) {
13
+ if (!header) return null;
14
+ const utf8Match = header.match(/filename\*=UTF-8''([^;]+)/i);
15
+ if (_optionalChain([utf8Match, 'optionalAccess', _ => _[1]])) {
16
+ try {
17
+ return decodeURIComponent(utf8Match[1]);
18
+ } catch (e) {
19
+ }
20
+ }
21
+ const plainMatch = header.match(/filename="([^"]*)"/i);
22
+ return _nullishCoalesce(_optionalChain([plainMatch, 'optionalAccess', _2 => _2[1]]), () => ( null));
23
+ }
24
+
25
+ // src/base.ts
2
26
  var Base = class {
3
27
 
4
28
 
@@ -16,7 +40,16 @@ var Base = class {
16
40
  );
17
41
  }
18
42
  this.apiKey = apiKey;
19
- this.vazeUrl = vazeUrl;
43
+ this.vazeUrl = vazeUrl.endsWith("/") ? vazeUrl : `${vazeUrl}/`;
44
+ }
45
+ apiUrl(path, params) {
46
+ const url = new URL(path.replace(/^\//, ""), this.vazeUrl);
47
+ if (params) {
48
+ for (const [key, value] of Object.entries(params)) {
49
+ if (value !== void 0) url.searchParams.set(key, String(value));
50
+ }
51
+ }
52
+ return url.toString();
20
53
  }
21
54
  async request(method, url, body, headers) {
22
55
  try {
@@ -25,50 +58,84 @@ var Base = class {
25
58
  ...headers
26
59
  };
27
60
  const isFormData = body instanceof FormData;
28
- if (!isFormData && !fetchHeaders["Content-Type"]) {
61
+ if (body !== void 0 && !isFormData && !fetchHeaders["Content-Type"]) {
29
62
  fetchHeaders["Content-Type"] = "application/json";
30
63
  }
31
64
  const response = await fetch(url, {
32
65
  method,
33
66
  headers: fetchHeaders,
34
- body: isFormData ? body : JSON.stringify(body)
67
+ body: isFormData ? body : body !== void 0 ? JSON.stringify(body) : null
35
68
  });
36
- return await response.json();
69
+ const contentType = _nullishCoalesce(response.headers.get("content-type"), () => ( ""));
70
+ if (!contentType.includes("application/json")) {
71
+ return {
72
+ data: null,
73
+ error: {
74
+ message: `Unexpected non-JSON response from server (status ${response.status}).`
75
+ }
76
+ };
77
+ }
78
+ const result = await response.json();
79
+ if (!response.ok && !result.error) {
80
+ return {
81
+ data: null,
82
+ error: { message: `Request failed with status ${response.status}.` }
83
+ };
84
+ }
85
+ return result;
37
86
  } catch (error) {
38
- console.log("Request error:", error);
39
87
  return {
40
88
  data: null,
41
89
  error: {
42
- message: "An error occurred while making the request."
90
+ message: error instanceof Error ? error.message : "An error occurred while making the request."
91
+ }
92
+ };
93
+ }
94
+ }
95
+ async requestBlob(url) {
96
+ try {
97
+ const response = await fetch(url, {
98
+ headers: { "API-Key": this.apiKey }
99
+ });
100
+ const contentType = _nullishCoalesce(response.headers.get("content-type"), () => ( ""));
101
+ if (!response.ok) {
102
+ if (contentType.includes("application/json")) {
103
+ const result = await response.json();
104
+ if (result.error) return { data: null, error: result.error };
105
+ }
106
+ return {
107
+ data: null,
108
+ error: { message: `Request failed with status ${response.status}.` }
109
+ };
110
+ }
111
+ return {
112
+ data: {
113
+ blob: await response.blob(),
114
+ filename: parseContentDispositionFilename(
115
+ response.headers.get("content-disposition")
116
+ ),
117
+ contentType
118
+ },
119
+ error: null
120
+ };
121
+ } catch (error) {
122
+ return {
123
+ data: null,
124
+ error: {
125
+ message: error instanceof Error ? error.message : "An error occurred while making the request."
43
126
  }
44
127
  };
45
128
  }
46
129
  }
47
130
  };
48
131
 
49
- // src/utils/index.ts
50
- function constructFileUrls({
51
- vazeUrl,
52
- files
53
- }) {
54
- return files.map((file) => ({
55
- ...file,
56
- url: `${vazeUrl}/${file.url}`
57
- }));
58
- }
59
-
60
132
  // src/files.ts
61
133
  var Files = class extends Base {
62
134
  async getAll(options) {
63
- const url = new URL("/api/files", this.vazeUrl);
64
- if (options) {
65
- for (const [key, value] of Object.entries(options)) {
66
- url.searchParams.set(key, value.toString());
67
- }
68
- }
135
+ const url = this.apiUrl("/api/files", options);
69
136
  const { error, data } = await this.request(
70
137
  "GET",
71
- url.toString()
138
+ url
72
139
  );
73
140
  return {
74
141
  error,
@@ -81,11 +148,10 @@ var Files = class extends Base {
81
148
  };
82
149
  }
83
150
  async getById(id) {
84
- const url = new URL(`/api/files`, this.vazeUrl);
85
- url.searchParams.set("id", id);
151
+ const url = this.apiUrl("/api/files", { id });
86
152
  const { data, error } = await this.request(
87
153
  "GET",
88
- url.toString()
154
+ url
89
155
  );
90
156
  return {
91
157
  error,
@@ -98,16 +164,10 @@ var Files = class extends Base {
98
164
  };
99
165
  }
100
166
  async getByName(name, options) {
101
- const url = new URL(`/api/files`, this.vazeUrl);
102
- url.searchParams.set("name", name);
103
- if (options) {
104
- for (const [key, value] of Object.entries(options)) {
105
- url.searchParams.set(key, value.toString());
106
- }
107
- }
167
+ const url = this.apiUrl("/api/files", { name, ...options });
108
168
  const { error, data } = await this.request(
109
169
  "GET",
110
- url.toString()
170
+ url
111
171
  );
112
172
  return {
113
173
  error,
@@ -121,17 +181,13 @@ var Files = class extends Base {
121
181
  }
122
182
  async upload(data) {
123
183
  const { files, folder } = data;
124
- const url = new URL("/api/files", this.vazeUrl);
184
+ const url = this.apiUrl("/api/files");
125
185
  const formData = new FormData();
126
186
  if (folder) formData.append("folder", folder);
127
187
  files.forEach((file) => {
128
188
  formData.append("files", file);
129
189
  });
130
- const { error, data: responseData } = await this.request(
131
- "POST",
132
- url.toString(),
133
- formData
134
- );
190
+ const { error, data: responseData } = await this.request("POST", url, formData);
135
191
  return {
136
192
  error,
137
193
  data: responseData ? {
@@ -142,26 +198,25 @@ var Files = class extends Base {
142
198
  } : null
143
199
  };
144
200
  }
201
+ async download(id) {
202
+ const url = this.apiUrl(`/api/files/download/${encodeURIComponent(id)}`);
203
+ return await this.requestBlob(url);
204
+ }
145
205
  async rename(data) {
146
- const url = new URL(`/api/files`, this.vazeUrl);
147
- return await this.request("PUT", url.toString(), data);
206
+ return await this.request("PUT", this.apiUrl("/api/files"), data);
148
207
  }
149
208
  async delete(id) {
150
- const url = new URL(`/api/files`, this.vazeUrl);
151
- return await this.request("DELETE", url.toString(), { id });
209
+ return await this.request("DELETE", this.apiUrl("/api/files"), {
210
+ id
211
+ });
152
212
  }
153
213
  };
154
214
 
155
215
  // src/folders.ts
156
216
  var Folders = class extends Base {
157
217
  async get(options) {
158
- const url = new URL("/api/folders", this.vazeUrl);
159
- if (options) {
160
- for (const [key, value] of Object.entries(options)) {
161
- url.searchParams.set(key, value.toString());
162
- }
163
- }
164
- const { error, data } = await this.request("GET", url.toString());
218
+ const url = this.apiUrl("/api/folders", options);
219
+ const { error, data } = await this.request("GET", url);
165
220
  return {
166
221
  error,
167
222
  data: data ? {
@@ -174,18 +229,19 @@ var Folders = class extends Base {
174
229
  };
175
230
  }
176
231
  async create(folder) {
177
- const url = new URL("/api/folders", this.vazeUrl);
178
- return await this.request("POST", url.toString(), {
179
- folder
180
- });
232
+ return await this.request(
233
+ "POST",
234
+ this.apiUrl("/api/folders"),
235
+ { folder }
236
+ );
181
237
  }
182
238
  async rename(data) {
183
- const url = new URL("/api/folders", this.vazeUrl);
184
- return await this.request("PUT", url.toString(), data);
239
+ return await this.request("PUT", this.apiUrl("/api/folders"), data);
185
240
  }
186
241
  async delete(id) {
187
- const url = new URL("/api/folders", this.vazeUrl);
188
- return await this.request("DELETE", url.toString(), { id });
242
+ return await this.request("DELETE", this.apiUrl("/api/folders"), {
243
+ id
244
+ });
189
245
  }
190
246
  };
191
247
 
@@ -195,8 +251,14 @@ var Vaze = class extends Base {
195
251
 
196
252
  constructor(options = {}) {
197
253
  super(options);
198
- this.files = new Files();
199
- this.folders = new Folders();
254
+ this.files = new Files(options);
255
+ this.folders = new Folders(options);
256
+ }
257
+ async health() {
258
+ return await this.request(
259
+ "GET",
260
+ this.apiUrl("/api/health")
261
+ );
200
262
  }
201
263
  };
202
264