node-datalith 0.1.0 → 0.1.2

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/file.d.ts CHANGED
@@ -11,6 +11,7 @@ export declare class File {
11
11
  readonly date: Date;
12
12
  readonly contentType: string;
13
13
  readonly contentLength: number;
14
+ readonly contentDisposition: string;
14
15
  readonly data: ReadableStream;
15
16
  readonly imageSize?: (ImageSize | null) | undefined;
16
17
  /**
@@ -21,10 +22,11 @@ export declare class File {
21
22
  * @param {Date} date The date and time when the file was created.
22
23
  * @param {string} contentType The MIME type of the file.
23
24
  * @param {number} contentLength The size of the file in bytes.
25
+ * @param {string} contentDisposition The file name and the expected usage.
24
26
  * @param {ReadableStream} data The file content as a readable stream.
25
27
  * @param {imageSize} [imageSize] (Optional) The size of the image in pixels, or `null` if not applicable, or `undefined` if this file is not an image.
26
28
  */
27
- constructor(resource: TimeoutResponse, etag: string, date: Date, contentType: string, contentLength: number, data: ReadableStream, imageSize?: (ImageSize | null) | undefined);
29
+ constructor(resource: TimeoutResponse, etag: string, date: Date, contentType: string, contentLength: number, contentDisposition: string, data: ReadableStream, imageSize?: (ImageSize | null) | undefined);
28
30
  /**
29
31
  * If you want to cancel the data, use this function instead of `data.cancel()` or `data.getReader().cancel()`.
30
32
  */
package/lib/file.js CHANGED
@@ -9,6 +9,7 @@ export class File {
9
9
  date;
10
10
  contentType;
11
11
  contentLength;
12
+ contentDisposition;
12
13
  data;
13
14
  imageSize;
14
15
  /**
@@ -19,15 +20,17 @@ export class File {
19
20
  * @param {Date} date The date and time when the file was created.
20
21
  * @param {string} contentType The MIME type of the file.
21
22
  * @param {number} contentLength The size of the file in bytes.
23
+ * @param {string} contentDisposition The file name and the expected usage.
22
24
  * @param {ReadableStream} data The file content as a readable stream.
23
25
  * @param {imageSize} [imageSize] (Optional) The size of the image in pixels, or `null` if not applicable, or `undefined` if this file is not an image.
24
26
  */
25
- constructor(resource, etag, date, contentType, contentLength, data, imageSize) {
27
+ constructor(resource, etag, date, contentType, contentLength, contentDisposition, data, imageSize) {
26
28
  this.resource = resource;
27
29
  this.etag = etag;
28
30
  this.date = date;
29
31
  this.contentType = contentType;
30
32
  this.contentLength = contentLength;
33
+ this.contentDisposition = contentDisposition;
31
34
  this.data = data;
32
35
  this.imageSize = imageSize;
33
36
  // do nothing
package/lib/lib.d.ts CHANGED
@@ -133,8 +133,19 @@ export interface ImagePutOptions extends WithBodyTimeoutOptions {
133
133
  */
134
134
  idleTimeout?: number | null;
135
135
  }
136
- export type ResourceGetOptions = WithBodyTimeoutOptions;
137
- export interface ImageGetOptions extends WithBodyTimeoutOptions {
136
+ export interface FileGetOptions extends WithBodyTimeoutOptions {
137
+ /**
138
+ * Whether to download as a file. It will affect the `contentDisposition` field of the returned `File` instance.
139
+ *
140
+ * If not provided (`undefined`), Datalith will default to `false`.
141
+ *
142
+ * @default undefined (Datalith defaults to `false`)
143
+ */
144
+ download?: boolean;
145
+ }
146
+ export type ResourceGetOptions = FileGetOptions;
147
+ export type Resolution = `${1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10}x` | "original";
148
+ export interface ImageGetOptions extends FileGetOptions {
138
149
  /**
139
150
  * The desired resolution of the image.
140
151
  *
@@ -144,7 +155,7 @@ export interface ImageGetOptions extends WithBodyTimeoutOptions {
144
155
  *
145
156
  * @default undefined (Datalith defaults to `"1x"`)
146
157
  */
147
- resolution?: `${1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10}x` | "original";
158
+ resolution?: Resolution;
148
159
  /**
149
160
  * Whether to use a fallback image (in PNG/JPEG format instead of WebP).
150
161
  *
@@ -237,8 +248,9 @@ export declare class Datalith {
237
248
  }
238
249
  /**
239
250
  * Validates if the input string is a valid center crop string (in the format of `"<float>:<float>"`).
240
- *
241
- * @param {string}
242
- * @returns {boolean}
243
251
  */
244
252
  export declare const validateCenterCrop: (centerCrop?: string) => boolean;
253
+ /**
254
+ * Validates if the given resolution is either `undefined`, `"original"`, or follows the format of `"<positive integer>x"`.
255
+ */
256
+ export declare const validateResolution: (resolution?: string) => resolution is Resolution;
package/lib/lib.js CHANGED
@@ -159,6 +159,10 @@ export class Datalith {
159
159
  */
160
160
  async getResource(id, options = {}) {
161
161
  const url = new URL(id, this._apiFetch);
162
+ const searchParams = url.searchParams;
163
+ if (options.download) {
164
+ searchParams.append("download", "1");
165
+ }
162
166
  const response = await timeoutFetch(url, {
163
167
  method: "GET",
164
168
  requestTimeout: typeof options.reqeustTimeout !== "undefined" ? options.reqeustTimeout : DEFAULT_REQUEST_TIMEOUT,
@@ -176,8 +180,10 @@ export class Datalith {
176
180
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
177
181
  const contentLength = parseInt(response.headers.get("content-length"));
178
182
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
183
+ const contentDisposition = response.headers.get("content-disposition");
184
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
179
185
  const body = response.body;
180
- return new File(response, etag, date, contentType, contentLength, body);
186
+ return new File(response, etag, date, contentType, contentLength, contentDisposition, body);
181
187
  }
182
188
  case 400:
183
189
  await response.cancelBody();
@@ -199,6 +205,9 @@ export class Datalith {
199
205
  async getImage(id, options = {}) {
200
206
  const url = new URL(id, this._apiFetchImage);
201
207
  const searchParams = url.searchParams;
208
+ if (options.download) {
209
+ searchParams.append("download", "1");
210
+ }
202
211
  if (typeof options.resolution !== "undefined") {
203
212
  searchParams.append("resolution", options.resolution);
204
213
  }
@@ -222,6 +231,8 @@ export class Datalith {
222
231
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
223
232
  const contentLength = parseInt(response.headers.get("content-length"));
224
233
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
234
+ const contentDisposition = response.headers.get("content-disposition");
235
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
225
236
  const body = response.body;
226
237
  const getNullableNumber = (fieldName) => {
227
238
  const s = response.headers.get(fieldName);
@@ -239,7 +250,7 @@ export class Datalith {
239
250
  height: imageHeight,
240
251
  };
241
252
  }
242
- return new File(response, etag, date, contentType, contentLength, body, imageSize);
253
+ return new File(response, etag, date, contentType, contentLength, contentDisposition, body, imageSize);
243
254
  }
244
255
  case 400:
245
256
  await response.cancelBody();
@@ -341,9 +352,6 @@ export class Datalith {
341
352
  }
342
353
  /**
343
354
  * Validates if the input string is a valid center crop string (in the format of `"<float>:<float>"`).
344
- *
345
- * @param {string}
346
- * @returns {boolean}
347
355
  */
348
356
  export const validateCenterCrop = (centerCrop) => {
349
357
  if (typeof centerCrop === "undefined") {
@@ -351,3 +359,15 @@ export const validateCenterCrop = (centerCrop) => {
351
359
  }
352
360
  return (/^-?\d+\.?\d*:-?\d+\.?\d*$/).test(centerCrop);
353
361
  };
362
+ /**
363
+ * Validates if the given resolution is either `undefined`, `"original"`, or follows the format of `"<positive integer>x"`.
364
+ */
365
+ export const validateResolution = (resolution) => {
366
+ if (typeof resolution === "undefined") {
367
+ return true;
368
+ }
369
+ if (resolution === "original") {
370
+ return true;
371
+ }
372
+ return (/^[1-9][0-9]*x$/).test(resolution);
373
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-datalith",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
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",