node-datalith 0.0.3 → 0.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.
- package/lib/errors.d.ts +3 -0
- package/lib/errors.js +6 -0
- package/lib/lib.d.ts +8 -0
- package/lib/lib.js +47 -1
- package/package.json +1 -1
package/lib/errors.d.ts
CHANGED
package/lib/errors.js
CHANGED
|
@@ -4,6 +4,12 @@ export class BadRequestError extends Error {
|
|
|
4
4
|
this.name = "BadRequestError";
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
|
+
export class NotFoundError extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super("NotFound");
|
|
10
|
+
this.name = "NotFoundError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
7
13
|
export class PayloadTooLargeError extends Error {
|
|
8
14
|
constructor() {
|
|
9
15
|
super("PayloadTooLarge");
|
package/lib/lib.d.ts
CHANGED
|
@@ -226,4 +226,12 @@ export declare class Datalith {
|
|
|
226
226
|
*/
|
|
227
227
|
deleteImage(id: string, options?: DeleteOptions): Promise<boolean>;
|
|
228
228
|
delete(id: string, apiURL: URL, options?: DeleteOptions): Promise<boolean>;
|
|
229
|
+
/**
|
|
230
|
+
* Convert a resource into an image.
|
|
231
|
+
*
|
|
232
|
+
* @throws {NotFoundError}
|
|
233
|
+
* @throws {BadRequestError}
|
|
234
|
+
* @throws {Error}
|
|
235
|
+
*/
|
|
236
|
+
convertResourceToImage(id: string, options: DeleteOptions & Pick<ImagePutOptions, "maxWidth" | "maxHeight" | "centerCrop">): Promise<Image>;
|
|
229
237
|
}
|
package/lib/lib.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Readable } from "node:stream";
|
|
2
2
|
import { nodeReadableToWebReadableStream, timeoutFetch } from "fetch-helper-x";
|
|
3
|
-
import { BadRequestError, PayloadTooLargeError } from "./errors.js";
|
|
3
|
+
import { BadRequestError, NotFoundError, PayloadTooLargeError } from "./errors.js";
|
|
4
4
|
import { File } from "./file.js";
|
|
5
5
|
import { Image } from "./image.js";
|
|
6
6
|
import { Resource } from "./resource.js";
|
|
@@ -292,4 +292,50 @@ export class Datalith {
|
|
|
292
292
|
await response.cancelBody();
|
|
293
293
|
}
|
|
294
294
|
}
|
|
295
|
+
/**
|
|
296
|
+
* Convert a resource into an image.
|
|
297
|
+
*
|
|
298
|
+
* @throws {NotFoundError}
|
|
299
|
+
* @throws {BadRequestError}
|
|
300
|
+
* @throws {Error}
|
|
301
|
+
*/
|
|
302
|
+
async convertResourceToImage(id, options) {
|
|
303
|
+
const url = new URL(id, this._apiOperate);
|
|
304
|
+
const searchParams = url.searchParams;
|
|
305
|
+
searchParams.append("convert-image", "");
|
|
306
|
+
if (typeof options.maxWidth !== "undefined") {
|
|
307
|
+
searchParams.append("max_width", options.maxWidth.toString());
|
|
308
|
+
}
|
|
309
|
+
if (typeof options.maxHeight !== "undefined") {
|
|
310
|
+
searchParams.append("max_height", options.maxHeight.toString());
|
|
311
|
+
}
|
|
312
|
+
if (typeof options.centerCrop !== "undefined") {
|
|
313
|
+
searchParams.append("center_crop", options.centerCrop);
|
|
314
|
+
}
|
|
315
|
+
const response = await timeoutFetch(url, {
|
|
316
|
+
method: "DELETE",
|
|
317
|
+
requestTimeout: typeof options.reqeustTimeout !== "undefined" ? options.reqeustTimeout : DEFAULT_DELETE_REQUEST_TIMEOUT,
|
|
318
|
+
});
|
|
319
|
+
try {
|
|
320
|
+
switch (response.status) {
|
|
321
|
+
case 200:
|
|
322
|
+
break;
|
|
323
|
+
case 400:
|
|
324
|
+
throw new BadRequestError();
|
|
325
|
+
case 404:
|
|
326
|
+
throw new NotFoundError();
|
|
327
|
+
default:
|
|
328
|
+
throw new Error("unknown error");
|
|
329
|
+
}
|
|
330
|
+
const json = await response.json();
|
|
331
|
+
return new Image(json.id, new Date(json.created_at), json.image_stem, {
|
|
332
|
+
width: json.image_width,
|
|
333
|
+
height: json.image_height,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
catch (error) {
|
|
337
|
+
await response.cancelBody();
|
|
338
|
+
throw error;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
295
341
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-datalith",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Datalith is a file management system powered by SQLite for metadata storage and the file system for file storage. This library can help you conmunicate with Datalith in Node.js.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": "./lib/lib.js",
|