@lunibee/builders 0.1.5 → 0.1.7

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/package.json CHANGED
@@ -1,10 +1,10 @@
1
- {
2
- "name": "@lunibee/builders",
3
- "version": "0.1.5",
4
- "type": "module",
5
- "module": "./src/index.ts",
6
- "types": "./src/index.ts",
7
- "exports": {
8
- ".": "./src/index.ts"
9
- }
10
- }
1
+ {
2
+ "name": "@lunibee/builders",
3
+ "version": "0.1.7",
4
+ "type": "module",
5
+ "module": "./src/index.ts",
6
+ "types": "./src/index.ts",
7
+ "exports": {
8
+ ".": "./src/index.ts"
9
+ }
10
+ }
package/src/attachment.ts CHANGED
@@ -1,67 +1,68 @@
1
- import { readFile } from "node:fs/promises";
2
- import { basename } from "node:path";
3
-
4
- export interface AttachmentData {
5
- name: string;
6
- description?: string;
7
- file: Uint8Array | ArrayBuffer | Blob | Buffer | string;
8
- }
9
-
10
- /** Builder for message attachments and file uploads. */
11
- export class AttachmentBuilder {
12
- public name: string;
13
- public description?: string;
14
- public file: Uint8Array | ArrayBuffer | Blob | Buffer | string;
15
-
16
- public constructor(
17
- file: Uint8Array | ArrayBuffer | Blob | Buffer | string,
18
- data?: Partial<Omit<AttachmentData, "file">> | string,
19
- ) {
20
- this.file = file;
21
- if (typeof data === "string") {
22
- this.name = data;
23
- } else {
24
- this.name =
25
- data?.name ?? (typeof file === "string" ? basename(file) : "file.bin");
26
- this.description = data?.description;
27
- }
28
- }
29
-
30
- /** Sets the attachment filename. */
31
- public setName(name: string): this {
32
- this.name = name;
33
- return this;
34
- }
35
-
36
- /** Sets the attachment description / alt text. */
37
- public setDescription(description: string): this {
38
- this.description = description;
39
- return this;
40
- }
41
-
42
- /** Sets the file contents. */
43
- public setFile(
44
- file: Uint8Array | ArrayBuffer | Blob | Buffer | string,
45
- ): this {
46
- this.file = file;
47
- return this;
48
- }
49
-
50
- /** Resolves the attachment payload to a binary Uint8Array. */
51
- public async toBuffer(): Promise<Uint8Array> {
52
- if (typeof this.file === "string") {
53
- const buffer = await readFile(this.file);
54
- return new Uint8Array(buffer);
55
- }
56
- if (this.file instanceof Uint8Array) return this.file;
57
- if (this.file instanceof ArrayBuffer) return new Uint8Array(this.file);
58
- if (this.file instanceof Blob) {
59
- const buf = await this.file.arrayBuffer();
60
- return new Uint8Array(buf);
61
- }
62
- if (typeof Buffer !== "undefined" && Buffer.isBuffer(this.file)) {
63
- return new Uint8Array(this.file);
64
- }
65
- return new Uint8Array();
66
- }
67
- }
1
+ import { readFile } from "node:fs/promises";
2
+ import { basename } from "node:path";
3
+
4
+ export interface AttachmentData {
5
+ name: string;
6
+ description?: string;
7
+ file: Uint8Array | ArrayBuffer | Blob | Buffer | string;
8
+ }
9
+
10
+ /** Builder for message attachments and file uploads. */
11
+ export class AttachmentBuilder {
12
+ public name: string;
13
+ public description?: string;
14
+ public file: Uint8Array | ArrayBuffer | Blob | Buffer | string;
15
+
16
+ public constructor(
17
+ file: Uint8Array | ArrayBuffer | Blob | Buffer | string,
18
+ data?: Partial<Omit<AttachmentData, "file">> | string,
19
+ ) {
20
+ this.file = file;
21
+ if (typeof data === "string") {
22
+ this.name = data;
23
+ } else {
24
+ this.name =
25
+ data?.name ??
26
+ (typeof file === "string" ? basename(file) : "file.bin");
27
+ this.description = data?.description;
28
+ }
29
+ }
30
+
31
+ /** Sets the attachment filename. */
32
+ public setName(name: string): this {
33
+ this.name = name;
34
+ return this;
35
+ }
36
+
37
+ /** Sets the attachment description / alt text. */
38
+ public setDescription(description: string): this {
39
+ this.description = description;
40
+ return this;
41
+ }
42
+
43
+ /** Sets the file contents. */
44
+ public setFile(
45
+ file: Uint8Array | ArrayBuffer | Blob | Buffer | string,
46
+ ): this {
47
+ this.file = file;
48
+ return this;
49
+ }
50
+
51
+ /** Resolves the attachment payload to a binary Uint8Array. */
52
+ public async toBuffer(): Promise<Uint8Array> {
53
+ if (typeof this.file === "string") {
54
+ const buffer = await readFile(this.file);
55
+ return new Uint8Array(buffer);
56
+ }
57
+ if (this.file instanceof Uint8Array) return this.file;
58
+ if (this.file instanceof ArrayBuffer) return new Uint8Array(this.file);
59
+ if (this.file instanceof Blob) {
60
+ const buf = await this.file.arrayBuffer();
61
+ return new Uint8Array(buf);
62
+ }
63
+ if (typeof Buffer !== "undefined" && Buffer.isBuffer(this.file)) {
64
+ return new Uint8Array(this.file);
65
+ }
66
+ throw new TypeError("Attachment file must be a supported binary value or file path.");
67
+ }
68
+ }