Skip to content

Commit 7ed63ca

Browse files
committed
fix: use try/catch and unify attachment download
1 parent bcce4b4 commit 7ed63ca

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

src/attachments/receiving/receiving.ts

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,45 @@ import type {
1212
ListAttachmentsResponse,
1313
} from './interfaces/list-attachments.interface';
1414

15+
type DownloadAttachmentResult =
16+
| {
17+
type: 'error';
18+
message: string;
19+
}
20+
| {
21+
type: 'success';
22+
base64Content: string;
23+
};
24+
1525
export class Receiving {
1626
constructor(private readonly resend: Resend) {}
1727

28+
private async downloadAttachment(
29+
url: string,
30+
): Promise<DownloadAttachmentResult> {
31+
try {
32+
const content = await fetch(url);
33+
if (!content.ok) {
34+
return {
35+
type: 'error',
36+
message: 'Failed to download attachment content',
37+
};
38+
}
39+
40+
const arrayBuffer = await content.arrayBuffer();
41+
const buffer = Buffer.from(arrayBuffer);
42+
return {
43+
type: 'success',
44+
base64Content: buffer.toString('base64'),
45+
};
46+
} catch {
47+
return {
48+
type: 'error',
49+
message: 'An error occurred while downloading attachment content',
50+
};
51+
}
52+
}
53+
1854
async get(options: GetAttachmentOptions): Promise<GetAttachmentResponse> {
1955
const { emailId, id } = options;
2056

@@ -26,27 +62,23 @@ export class Receiving {
2662
return apiResponse;
2763
}
2864

29-
const downloadResponse = await fetch(apiResponse.data.data.download_url);
30-
if (!downloadResponse.ok) {
65+
const { download_url, ...otherFields } = apiResponse.data.data;
66+
const downloadResult = await this.downloadAttachment(download_url);
67+
if (downloadResult.type === 'error') {
3168
return {
3269
data: null,
3370
error: {
3471
name: 'application_error',
35-
message: 'Failed to download attachment content',
72+
message: downloadResult.message,
3673
},
3774
};
3875
}
3976

40-
const arrayBuffer = await downloadResponse.arrayBuffer();
41-
const buffer = Buffer.from(arrayBuffer);
42-
const base64Content = buffer.toString('base64');
43-
44-
const { download_url, ...otherFields } = apiResponse.data.data;
4577
const responseData: GetAttachmentResponseSuccess = {
4678
object: 'attachment',
4779
data: {
4880
...otherFields,
49-
content: base64Content,
81+
content: downloadResult.base64Content,
5082
},
5183
};
5284

@@ -74,25 +106,21 @@ export class Receiving {
74106

75107
const attachmentsWithContent = [];
76108
for (const attachment of apiResponse.data.data) {
77-
const downloadResponse = await fetch(attachment.download_url);
78-
if (!downloadResponse.ok) {
109+
const { download_url, ...otherFields } = attachment;
110+
const downloadResult = await this.downloadAttachment(download_url);
111+
if (downloadResult.type === 'error') {
79112
return {
80113
data: null,
81114
error: {
82115
name: 'application_error',
83-
message: `Failed to download attachment ${attachment.id}`,
116+
message: downloadResult.message,
84117
},
85118
};
86119
}
87120

88-
const arrayBuffer = await downloadResponse.arrayBuffer();
89-
const buffer = Buffer.from(arrayBuffer);
90-
const base64Content = buffer.toString('base64');
91-
92-
const { download_url, ...otherFields } = attachment;
93121
attachmentsWithContent.push({
94122
...otherFields,
95-
content: base64Content,
123+
content: downloadResult.base64Content,
96124
});
97125
}
98126

0 commit comments

Comments
 (0)