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
1 change: 1 addition & 0 deletions src/emails/receiving/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './get-inbound-email.interface';
export * from './list-inbound-emails.interface';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { PaginationOptions } from '../../../common/interfaces';
import type { ErrorResponse } from '../../../interfaces';
import type { GetInboundEmailResponseSuccess } from './get-inbound-email.interface';

export type ListInboundEmailsOptions = PaginationOptions;

export type ListInboundEmail = Omit<
GetInboundEmailResponseSuccess,
'html' | 'text' | 'headers' | 'object'
>;

export interface ListInboundEmailsResponseSuccess {
object: 'list';
has_more: boolean;
data: ListInboundEmail[];
}

export type ListInboundEmailsResponse =
| {
data: ListInboundEmailsResponseSuccess;
error: null;
}
| {
data: null;
error: ErrorResponse;
};
220 changes: 218 additions & 2 deletions src/emails/receiving/receiving.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Receiving', () => {
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 @@ -30,7 +30,7 @@ describe('Receiving', () => {
{
"data": null,
"error": {
"message": "Inbound email not found",
"message": "Email not found",
"name": "not_found",
},
}
Expand Down Expand Up @@ -170,4 +170,220 @@ describe('Receiving', () => {
});
});
});

describe('list', () => {
describe('when no inbound emails found', () => {
it('returns empty list', async () => {
const response = {
object: 'list' as const,
has_more: false,
data: [],
};

fetchMock.mockOnce(JSON.stringify(response), {
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});

const result = await resend.emails.receiving.list();

expect(result).toMatchInlineSnapshot(`
{
"data": {
"data": [],
"has_more": false,
"object": "list",
},
"error": null,
}
`);
});
});

describe('when inbound emails found', () => {
it('returns list of inbound emails with transformed fields', async () => {
const apiResponse = {
object: 'list' as const,
has_more: true,
data: [
{
id: '67d9bcdb-5a02-42d7-8da9-0d6feea18cff',
to: ['received@example.com'],
from: 'sender@example.com',
created_at: '2023-04-07T23:13:52.669661+00:00',
subject: 'Test inbound email 1',
bcc: null,
cc: ['cc@example.com'],
reply_to: ['reply@example.com'],
attachments: [
{
id: 'att_123',
filename: 'document.pdf',
content_type: 'application/pdf',
content_id: 'cid_123',
content_disposition: 'attachment' as const,
size: 12345,
},
],
},
{
id: '87e9bcdb-6b03-43e8-9ea0-1e7gffa19d00',
to: ['another@example.com'],
from: 'sender2@example.com',
created_at: '2023-04-08T10:20:30.123456+00:00',
subject: 'Test inbound email 2',
bcc: ['bcc@example.com'],
cc: null,
reply_to: null,
attachments: [],
},
],
};

fetchMock.mockOnce(JSON.stringify(apiResponse), {
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});

const result = await resend.emails.receiving.list();

expect(result).toMatchInlineSnapshot(`
{
"data": {
"data": [
{
"attachments": [
{
"content_disposition": "attachment",
"content_id": "cid_123",
"content_type": "application/pdf",
"filename": "document.pdf",
"id": "att_123",
"size": 12345,
},
],
"bcc": null,
"cc": [
"cc@example.com",
],
"created_at": "2023-04-07T23:13:52.669661+00:00",
"from": "sender@example.com",
"id": "67d9bcdb-5a02-42d7-8da9-0d6feea18cff",
"reply_to": [
"reply@example.com",
],
"subject": "Test inbound email 1",
"to": [
"received@example.com",
],
},
{
"attachments": [],
"bcc": [
"bcc@example.com",
],
"cc": null,
"created_at": "2023-04-08T10:20:30.123456+00:00",
"from": "sender2@example.com",
"id": "87e9bcdb-6b03-43e8-9ea0-1e7gffa19d00",
"reply_to": null,
"subject": "Test inbound email 2",
"to": [
"another@example.com",
],
},
],
"has_more": true,
"object": "list",
},
"error": null,
}
`);
});

it('supports pagination with limit parameter', async () => {
const apiResponse = {
object: 'list' as const,
has_more: true,
data: [
{
id: '67d9bcdb-5a02-42d7-8da9-0d6feea18cff',
to: ['received@example.com'],
from: 'sender@example.com',
created_at: '2023-04-07T23:13:52.669661+00:00',
subject: 'Test inbound email',
bcc: null,
cc: null,
reply_to: null,
attachments: [],
},
],
};

fetchMock.mockOnce(JSON.stringify(apiResponse), {
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});

await resend.emails.receiving.list({ limit: 10 });

expect(fetchMock.mock.calls[0][0]).toBe(
'https://api.resend.com/emails/receiving?limit=10',
);
});

it('supports pagination with after parameter', async () => {
const apiResponse = {
object: 'list' as const,
has_more: false,
data: [],
};

fetchMock.mockOnce(JSON.stringify(apiResponse), {
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});

await resend.emails.receiving.list({ after: 'cursor123' });

expect(fetchMock.mock.calls[0][0]).toBe(
'https://api.resend.com/emails/receiving?after=cursor123',
);
});

it('supports pagination with before parameter', async () => {
const apiResponse = {
object: 'list' as const,
has_more: false,
data: [],
};

fetchMock.mockOnce(JSON.stringify(apiResponse), {
status: 200,
headers: {
'content-type': 'application/json',
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});

await resend.emails.receiving.list({ before: 'cursor456' });

expect(fetchMock.mock.calls[0][0]).toBe(
'https://api.resend.com/emails/receiving?before=cursor456',
);
});
});
});
});
19 changes: 19 additions & 0 deletions src/emails/receiving/receiving.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { buildPaginationQuery } from '../../common/utils/build-pagination-query';
import type { Resend } from '../../resend';
import type {
GetInboundEmailResponse,
GetInboundEmailResponseSuccess,
} from './interfaces/get-inbound-email.interface';
import type {
ListInboundEmailsOptions,
ListInboundEmailsResponse,
ListInboundEmailsResponseSuccess,
} from './interfaces/list-inbound-emails.interface';

export class Receiving {
constructor(private readonly resend: Resend) {}
Expand All @@ -14,4 +20,17 @@ export class Receiving {

return data;
}

async list(
options: ListInboundEmailsOptions = {},
): Promise<ListInboundEmailsResponse> {
const queryString = buildPaginationQuery(options);
const url = queryString
? `/emails/receiving?${queryString}`
: '/emails/receiving';

const data = await this.resend.get<ListInboundEmailsResponseSuccess>(url);

return data;
}
}
Loading