@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/dist/index.js
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
// 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 (utf8Match?.[1]) {
|
|
16
|
+
try {
|
|
17
|
+
return decodeURIComponent(utf8Match[1]);
|
|
18
|
+
} catch {
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const plainMatch = header.match(/filename="([^"]*)"/i);
|
|
22
|
+
return plainMatch?.[1] ?? null;
|
|
23
|
+
}
|
|
24
|
+
|
|
1
25
|
// src/base.ts
|
|
2
26
|
var Base = class {
|
|
3
27
|
apiKey;
|
|
@@ -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
|
-
|
|
69
|
+
const contentType = 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 = 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 =
|
|
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
|
-
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
|
|
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 });
|
|
168
|
+
const { data, error } = await this.request(
|
|
169
|
+
"GET",
|
|
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
|
folders;
|
|
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
|
export {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darseen/vaze",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Vaze Self-Hosted file storage and hosting service
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Vaze Self-Hosted file storage and hosting service SDK.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
7
7
|
"sdk",
|
|
@@ -25,10 +25,22 @@
|
|
|
25
25
|
"type": "module",
|
|
26
26
|
"exports": {
|
|
27
27
|
".": {
|
|
28
|
-
"import":
|
|
29
|
-
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./dist/index.d.cts",
|
|
34
|
+
"default": "./dist/index.cjs"
|
|
35
|
+
}
|
|
30
36
|
}
|
|
31
37
|
},
|
|
38
|
+
"main": "./dist/index.cjs",
|
|
39
|
+
"module": "./dist/index.js",
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"files": [
|
|
42
|
+
"dist"
|
|
43
|
+
],
|
|
32
44
|
"devDependencies": {
|
|
33
45
|
"@types/node": "^25.5.0",
|
|
34
46
|
"rimraf": "^6.1.3",
|