Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct email batch api call with reply_to #460

Merged
merged 5 commits into from
Jan 23, 2025
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
8 changes: 7 additions & 1 deletion src/batch/batch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type * as React from 'react';
import type { EmailApiOptions } from '../common/interfaces/email-api-options.interface';
import { parseEmailToApiOptions } from '../common/utils/parse-email-to-api-options';
import type { Resend } from '../resend';
import type {
CreateBatchOptions,
Expand All @@ -22,6 +24,8 @@ export class Batch {
payload: CreateBatchOptions,
options: CreateBatchRequestOptions = {},
): Promise<CreateBatchResponse> {
const emails: EmailApiOptions[] = [];

for (const email of payload) {
if (email.react) {
if (!this.renderAsync) {
Expand All @@ -38,11 +42,13 @@ export class Batch {
email.html = await this.renderAsync(email.react as React.ReactElement);
email.react = undefined;
}

emails.push(parseEmailToApiOptions(email));
}

const data = await this.resend.post<CreateBatchSuccessResponse>(
'/emails/batch',
payload,
emails,
options,
);

Expand Down
18 changes: 18 additions & 0 deletions src/common/interfaces/email-api-options.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Attachment } from '../../emails/interfaces/create-email-options.interface';
import type { Tag } from '../../interfaces';

export interface EmailApiOptions {
from: string;
to: string | string[];
subject: string;
region?: string;
headers?: Record<string, string>;
html?: string;
text?: string;
bcc?: string | string[];
cc?: string | string[];
reply_to?: string | string[];
scheduled_at?: string;
tags?: Tag[];
attachments?: Attachment[];
}
44 changes: 44 additions & 0 deletions src/common/utils/parse-email-to-api-options.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { CreateEmailOptions } from '../../emails/interfaces/create-email-options.interface';
import { parseEmailToApiOptions } from './parse-email-to-api-options';

describe('parseEmailToApiOptions', () => {
it('should handle minimal email with only required fields', () => {
const emailPayload: CreateEmailOptions = {
from: 'joao@resend.com',
to: 'bu@resend.com',
subject: 'Hey, there!',
html: '<h1>Hey, there!</h1>',
};

const apiOptions = parseEmailToApiOptions(emailPayload);

expect(apiOptions).toEqual({
from: 'joao@resend.com',
to: 'bu@resend.com',
subject: 'Hey, there!',
html: '<h1>Hey, there!</h1>',
});
});

it('should properly parse camel case to snake case', () => {
const emailPayload: CreateEmailOptions = {
from: 'joao@resend.com',
to: 'bu@resend.com',
subject: 'Hey, there!',
html: '<h1>Hey, there!</h1>',
replyTo: 'zeno@resend.com',
scheduledAt: 'in 1 min',
};

const apiOptions = parseEmailToApiOptions(emailPayload);

expect(apiOptions).toEqual({
from: 'joao@resend.com',
to: 'bu@resend.com',
subject: 'Hey, there!',
html: '<h1>Hey, there!</h1>',
reply_to: 'zeno@resend.com',
scheduled_at: 'in 1 min',
});
});
});
21 changes: 21 additions & 0 deletions src/common/utils/parse-email-to-api-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { CreateEmailOptions } from '../../emails/interfaces/create-email-options.interface';
import type { EmailApiOptions } from '../interfaces/email-api-options.interface';

export function parseEmailToApiOptions(
email: CreateEmailOptions,
): EmailApiOptions {
return {
attachments: email.attachments,
bcc: email.bcc,
cc: email.cc,
from: email.from,
headers: email.headers,
html: email.html,
reply_to: email.replyTo,
scheduled_at: email.scheduledAt,
subject: email.subject,
tags: email.tags,
text: email.text,
to: email.to,
};
}
16 changes: 2 additions & 14 deletions src/emails/emails.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as React from 'react';
import { parseEmailToApiOptions } from '../common/utils/parse-email-to-api-options';
import type { Resend } from '../resend';
import type {
CancelEmailResponse,
Expand Down Expand Up @@ -54,20 +55,7 @@ export class Emails {

const data = await this.resend.post<CreateEmailResponseSuccess>(
'/emails',
{
attachments: payload.attachments,
bcc: payload.bcc,
cc: payload.cc,
from: payload.from,
headers: payload.headers,
html: payload.html,
reply_to: payload.replyTo,
scheduled_at: payload.scheduledAt,
subject: payload.subject,
tags: payload.tags,
text: payload.text,
to: payload.to,
},
parseEmailToApiOptions(payload),
options,
);

Expand Down
2 changes: 1 addition & 1 deletion src/emails/interfaces/create-email-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export interface CreateEmailResponse {
error: ErrorResponse | null;
}

interface Attachment {
export interface Attachment {
/** Content of an attached file. */
content?: string | Buffer;
/** Name of attached file. */
Expand Down
Loading