@darseen/vaze 1.1.1 → 2.0.0
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 +31 -7
- package/dist/index.cjs +141 -63
- package/dist/index.d.cts +763 -93
- package/dist/index.d.ts +763 -93
- package/dist/index.js +140 -62
- package/package.json +16 -4
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: "
|
|
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
|
|
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:
|
|
@@ -120,7 +132,7 @@ Folders API Access folder-related methods via `vaze.folders`. `get(options?)` Re
|
|
|
120
132
|
|
|
121
133
|
- options (optional):
|
|
122
134
|
- `id`
|
|
123
|
-
- `
|
|
135
|
+
- `key` example: "projects/work"
|
|
124
136
|
- `limit`
|
|
125
137
|
- `offset`
|
|
126
138
|
- `orderBy`
|
|
@@ -128,7 +140,7 @@ Folders API Access folder-related methods via `vaze.folders`. `get(options?)` Re
|
|
|
128
140
|
|
|
129
141
|
```ts
|
|
130
142
|
const { data, error } = await vaze.folders.get({
|
|
131
|
-
|
|
143
|
+
key: "documents/work",
|
|
132
144
|
limit: 20,
|
|
133
145
|
});
|
|
134
146
|
|
|
@@ -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 `
|
|
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})
|
|
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
|
|
68
|
+
});
|
|
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;
|
|
86
|
+
} catch (error) {
|
|
87
|
+
return {
|
|
88
|
+
data: null,
|
|
89
|
+
error: {
|
|
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 }
|
|
35
99
|
});
|
|
36
|
-
|
|
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
|
+
};
|
|
37
121
|
} catch (error) {
|
|
38
|
-
console.log("Request error:", error);
|
|
39
122
|
return {
|
|
40
123
|
data: null,
|
|
41
124
|
error: {
|
|
42
|
-
message: "An error occurred while making the request."
|
|
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 =
|
|
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
|
|
138
|
+
url
|
|
72
139
|
);
|
|
73
140
|
return {
|
|
74
141
|
error,
|
|
@@ -81,11 +148,26 @@ var Files = class extends Base {
|
|
|
81
148
|
};
|
|
82
149
|
}
|
|
83
150
|
async getById(id) {
|
|
84
|
-
const url =
|
|
85
|
-
|
|
151
|
+
const url = this.apiUrl("/api/files", { id });
|
|
152
|
+
const { data, error } = await this.request(
|
|
153
|
+
"GET",
|
|
154
|
+
url
|
|
155
|
+
);
|
|
156
|
+
return {
|
|
157
|
+
error,
|
|
158
|
+
data: data ? {
|
|
159
|
+
file: constructFileUrls({
|
|
160
|
+
vazeUrl: this.vazeUrl,
|
|
161
|
+
files: [data.file]
|
|
162
|
+
})[0]
|
|
163
|
+
} : null
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
async getByKey(key) {
|
|
167
|
+
const url = this.apiUrl("/api/files", { key });
|
|
86
168
|
const { data, error } = await this.request(
|
|
87
169
|
"GET",
|
|
88
|
-
url
|
|
170
|
+
url
|
|
89
171
|
);
|
|
90
172
|
return {
|
|
91
173
|
error,
|
|
@@ -98,16 +180,10 @@ var Files = class extends Base {
|
|
|
98
180
|
};
|
|
99
181
|
}
|
|
100
182
|
async getByName(name, options) {
|
|
101
|
-
const url =
|
|
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
|
-
}
|
|
183
|
+
const url = this.apiUrl("/api/files", { name, ...options });
|
|
108
184
|
const { error, data } = await this.request(
|
|
109
185
|
"GET",
|
|
110
|
-
url
|
|
186
|
+
url
|
|
111
187
|
);
|
|
112
188
|
return {
|
|
113
189
|
error,
|
|
@@ -121,17 +197,13 @@ var Files = class extends Base {
|
|
|
121
197
|
}
|
|
122
198
|
async upload(data) {
|
|
123
199
|
const { files, folder } = data;
|
|
124
|
-
const url =
|
|
200
|
+
const url = this.apiUrl("/api/files");
|
|
125
201
|
const formData = new FormData();
|
|
126
202
|
if (folder) formData.append("folder", folder);
|
|
127
203
|
files.forEach((file) => {
|
|
128
204
|
formData.append("files", file);
|
|
129
205
|
});
|
|
130
|
-
const { error, data: responseData } = await this.request(
|
|
131
|
-
"POST",
|
|
132
|
-
url.toString(),
|
|
133
|
-
formData
|
|
134
|
-
);
|
|
206
|
+
const { error, data: responseData } = await this.request("POST", url, formData);
|
|
135
207
|
return {
|
|
136
208
|
error,
|
|
137
209
|
data: responseData ? {
|
|
@@ -142,26 +214,25 @@ var Files = class extends Base {
|
|
|
142
214
|
} : null
|
|
143
215
|
};
|
|
144
216
|
}
|
|
217
|
+
async download(id) {
|
|
218
|
+
const url = this.apiUrl(`/api/files/download/${encodeURIComponent(id)}`);
|
|
219
|
+
return await this.requestBlob(url);
|
|
220
|
+
}
|
|
145
221
|
async rename(data) {
|
|
146
|
-
|
|
147
|
-
return await this.request("PUT", url.toString(), data);
|
|
222
|
+
return await this.request("PUT", this.apiUrl("/api/files"), data);
|
|
148
223
|
}
|
|
149
224
|
async delete(id) {
|
|
150
|
-
|
|
151
|
-
|
|
225
|
+
return await this.request("DELETE", this.apiUrl("/api/files"), {
|
|
226
|
+
id
|
|
227
|
+
});
|
|
152
228
|
}
|
|
153
229
|
};
|
|
154
230
|
|
|
155
231
|
// src/folders.ts
|
|
156
232
|
var Folders = class extends Base {
|
|
157
233
|
async get(options) {
|
|
158
|
-
const url =
|
|
159
|
-
|
|
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());
|
|
234
|
+
const url = this.apiUrl("/api/folders", options);
|
|
235
|
+
const { error, data } = await this.request("GET", url);
|
|
165
236
|
return {
|
|
166
237
|
error,
|
|
167
238
|
data: data ? {
|
|
@@ -174,18 +245,19 @@ var Folders = class extends Base {
|
|
|
174
245
|
};
|
|
175
246
|
}
|
|
176
247
|
async create(folder) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
248
|
+
return await this.request(
|
|
249
|
+
"POST",
|
|
250
|
+
this.apiUrl("/api/folders"),
|
|
251
|
+
{ folder }
|
|
252
|
+
);
|
|
181
253
|
}
|
|
182
254
|
async rename(data) {
|
|
183
|
-
|
|
184
|
-
return await this.request("PUT", url.toString(), data);
|
|
255
|
+
return await this.request("PUT", this.apiUrl("/api/folders"), data);
|
|
185
256
|
}
|
|
186
257
|
async delete(id) {
|
|
187
|
-
|
|
188
|
-
|
|
258
|
+
return await this.request("DELETE", this.apiUrl("/api/folders"), {
|
|
259
|
+
id
|
|
260
|
+
});
|
|
189
261
|
}
|
|
190
262
|
};
|
|
191
263
|
|
|
@@ -195,8 +267,14 @@ var Vaze = class extends Base {
|
|
|
195
267
|
|
|
196
268
|
constructor(options = {}) {
|
|
197
269
|
super(options);
|
|
198
|
-
this.files = new Files();
|
|
199
|
-
this.folders = new Folders();
|
|
270
|
+
this.files = new Files(options);
|
|
271
|
+
this.folders = new Folders(options);
|
|
272
|
+
}
|
|
273
|
+
async health() {
|
|
274
|
+
return await this.request(
|
|
275
|
+
"GET",
|
|
276
|
+
this.apiUrl("/api/health")
|
|
277
|
+
);
|
|
200
278
|
}
|
|
201
279
|
};
|
|
202
280
|
|