Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 4 additions & 78 deletions src/attachments/attachments.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,10 @@
import type { Resend } from '../resend';
import type {
GetAttachmentApiResponse,
GetAttachmentOptions,
GetAttachmentResponse,
GetAttachmentResponseSuccess,
} from './interfaces/get-attachment.interface';
import type {
ListAttachmentsApiResponse,
ListAttachmentsOptions,
ListAttachmentsResponse,
} from './interfaces/list-attachments.interface';
import { Receiving } from './receiving/receiving';

export class Attachments {
constructor(private readonly resend: Resend) {}
readonly receiving: Receiving;

async get(options: GetAttachmentOptions): Promise<GetAttachmentResponse> {
const { inboundId, id } = options;

const data = await this.resend.get<GetAttachmentApiResponse>(
`/emails/inbound/${inboundId}/attachments/${id}`,
);

if (data.error) {
return {
data: null,
error: data.error,
};
}

const apiResponse = data.data;

const transformedData: GetAttachmentResponseSuccess = {
object: apiResponse.object,
data: {
id: apiResponse.data.id,
filename: apiResponse.data.filename,
contentType: apiResponse.data.content_type,
contentDisposition: apiResponse.data.content_disposition,
contentId: apiResponse.data.content_id,
content: apiResponse.data.content,
},
};

return {
data: transformedData,
error: null,
};
}

async list(
options: ListAttachmentsOptions,
): Promise<ListAttachmentsResponse> {
const { inboundId } = options;

const data = await this.resend.get<ListAttachmentsApiResponse>(
`/emails/inbound/${inboundId}/attachments`,
);

if (data.error) {
return {
data: null,
error: data.error,
};
}

const apiResponse = data.data;

// Transform snake_case to camelCase and return array directly
const transformedData = apiResponse.data.map((attachment) => ({
id: attachment.id,
filename: attachment.filename,
contentType: attachment.content_type,
contentDisposition: attachment.content_disposition,
contentId: attachment.content_id,
content: attachment.content,
}));

return {
data: transformedData,
error: null,
};
constructor(resend: Resend) {
this.receiving = new Receiving(resend);
}
}
29 changes: 0 additions & 29 deletions src/attachments/interfaces/list-attachments.interface.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export interface InboundAttachment {
id: string;
filename?: string;
contentType: string;
contentDisposition: 'inline' | 'attachment';
contentId?: string;
content_type: string;
content_disposition: 'inline' | 'attachment';
content_id?: string;
content: string; // base64
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ErrorResponse } from '../../interfaces';
import type { ErrorResponse } from '../../../interfaces';
import type { InboundAttachment } from './attachment';

export interface GetAttachmentOptions {
inboundId: string;
emailId: string;
id: string;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { ErrorResponse } from '../../../interfaces';
import type { InboundAttachment } from './attachment';

export interface ListAttachmentsOptions {
emailId: string;
}

export interface ListAttachmentsResponseSuccess {
object: 'attachment';
data: InboundAttachment[];
}

export type ListAttachmentsResponse =
| {
data: ListAttachmentsResponseSuccess;
error: null;
}
| {
data: null;
error: ErrorResponse;
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { ErrorResponse } from '../interfaces';
import { Resend } from '../resend';
import type { ErrorResponse } from '../../interfaces';
import { Resend } from '../../resend';

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');

describe('Attachments', () => {
describe('Receiving', () => {
afterEach(() => fetchMock.resetMocks());

describe('get', () => {
Expand All @@ -22,8 +22,8 @@ describe('Attachments', () => {
},
});

const result = await resend.attachments.get({
inboundId: '61cda979-919d-4b9d-9638-c148b93ff410',
const result = await resend.attachments.receiving.get({
emailId: '61cda979-919d-4b9d-9638-c148b93ff410',
id: 'att_123',
});

Expand Down Expand Up @@ -61,8 +61,8 @@ describe('Attachments', () => {
},
});

const result = await resend.attachments.get({
inboundId: '67d9bcdb-5a02-42d7-8da9-0d6feea18cff',
const result = await resend.attachments.receiving.get({
emailId: '67d9bcdb-5a02-42d7-8da9-0d6feea18cff',
id: 'att_123',
});

Expand All @@ -71,9 +71,9 @@ describe('Attachments', () => {
"data": {
"data": {
"content": "base64encodedcontent==",
"contentDisposition": "attachment",
"contentId": "cid_123",
"contentType": "application/pdf",
"content_disposition": "attachment",
"content_id": "cid_123",
"content_type": "application/pdf",
"filename": "document.pdf",
"id": "att_123",
},
Expand Down Expand Up @@ -106,8 +106,8 @@ describe('Attachments', () => {
},
});

const result = await resend.attachments.get({
inboundId: '67d9bcdb-5a02-42d7-8da9-0d6feea18cff',
const result = await resend.attachments.receiving.get({
emailId: '67d9bcdb-5a02-42d7-8da9-0d6feea18cff',
id: 'att_456',
});

Expand All @@ -116,9 +116,9 @@ describe('Attachments', () => {
"data": {
"data": {
"content": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
"contentDisposition": "inline",
"contentId": "cid_456",
"contentType": "image/png",
"content_disposition": "inline",
"content_id": "cid_456",
"content_type": "image/png",
"filename": "image.png",
"id": "att_456",
},
Expand Down Expand Up @@ -150,8 +150,8 @@ describe('Attachments', () => {
},
});

const result = await resend.attachments.get({
inboundId: '67d9bcdb-5a02-42d7-8da9-0d6feea18cff',
const result = await resend.attachments.receiving.get({
emailId: '67d9bcdb-5a02-42d7-8da9-0d6feea18cff',
id: 'att_789',
});

Expand All @@ -160,10 +160,8 @@ describe('Attachments', () => {
"data": {
"data": {
"content": "base64content",
"contentDisposition": "attachment",
"contentId": undefined,
"contentType": "text/plain",
"filename": undefined,
"content_disposition": "attachment",
"content_type": "text/plain",
"id": "att_789",
},
"object": "attachment",
Expand All @@ -180,7 +178,7 @@ describe('Attachments', () => {
it('returns error', async () => {
const response: ErrorResponse = {
name: 'not_found',
message: 'Inbound email not found',
message: 'Email not found',
};

fetchMock.mockOnce(JSON.stringify(response), {
Expand All @@ -191,24 +189,16 @@ describe('Attachments', () => {
},
});

const result = await resend.attachments.list({
inboundId: '61cda979-919d-4b9d-9638-c148b93ff410',
const result = await resend.attachments.receiving.list({
emailId: '61cda979-919d-4b9d-9638-c148b93ff410',
});

expect(result).toMatchInlineSnapshot(`
{
"data": null,
"error": {
"message": "Inbound email not found",
"name": "not_found",
},
}
`);
expect(result).toEqual({ data: null, error: response });
});
});

describe('when attachments found', () => {
it('returns multiple attachments with transformed fields', async () => {
it('returns multiple attachments', async () => {
const apiResponse = {
object: 'attachment' as const,
data: [
Expand Down Expand Up @@ -239,33 +229,11 @@ describe('Attachments', () => {
},
});

const result = await resend.attachments.list({
inboundId: '67d9bcdb-5a02-42d7-8da9-0d6feea18cff',
const result = await resend.attachments.receiving.list({
emailId: '67d9bcdb-5a02-42d7-8da9-0d6feea18cff',
});

expect(result).toMatchInlineSnapshot(`
{
"data": [
{
"content": "base64encodedcontent==",
"contentDisposition": "attachment",
"contentId": "cid_123",
"contentType": "application/pdf",
"filename": "document.pdf",
"id": "att_123",
},
{
"content": "imagebase64==",
"contentDisposition": "inline",
"contentId": "cid_456",
"contentType": "image/png",
"filename": "image.png",
"id": "att_456",
},
],
"error": null,
}
`);
expect(result).toEqual({ data: apiResponse, error: null });
});

it('returns empty array when no attachments', async () => {
Expand All @@ -282,16 +250,11 @@ describe('Attachments', () => {
},
});

const result = await resend.attachments.list({
inboundId: '67d9bcdb-5a02-42d7-8da9-0d6feea18cff',
const result = await resend.attachments.receiving.list({
emailId: '67d9bcdb-5a02-42d7-8da9-0d6feea18cff',
});

expect(result).toMatchInlineSnapshot(`
{
"data": [],
"error": null,
}
`);
expect(result).toEqual({ data: apiResponse, error: null });
});
});
});
Expand Down
37 changes: 37 additions & 0 deletions src/attachments/receiving/receiving.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Resend } from '../../resend';
import type {
GetAttachmentOptions,
GetAttachmentResponse,
GetAttachmentResponseSuccess,
} from './interfaces/get-attachment.interface';
import type {
ListAttachmentsOptions,
ListAttachmentsResponse,
ListAttachmentsResponseSuccess,
} from './interfaces/list-attachments.interface';

export class Receiving {
constructor(private readonly resend: Resend) {}

async get(options: GetAttachmentOptions): Promise<GetAttachmentResponse> {
const { emailId, id } = options;

const data = await this.resend.get<GetAttachmentResponseSuccess>(
`/emails/inbound/${emailId}/attachments/${id}`,
);

return data;
}

async list(
options: ListAttachmentsOptions,
): Promise<ListAttachmentsResponse> {
const { emailId } = options;

const data = await this.resend.get<ListAttachmentsResponseSuccess>(
`/emails/inbound/${emailId}/attachments`,
);

return data;
}
}
Loading
Loading