Skip to content

Commit 18d97a9

Browse files
committed
make emails with react component render text aswell
1 parent b767b51 commit 18d97a9

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/batch/batch.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import type {
1010
} from './interfaces/create-batch-options.interface';
1111

1212
export class Batch {
13-
private renderAsync?: (component: React.ReactElement) => Promise<string>;
13+
private renderAsync?: (
14+
component: React.ReactElement,
15+
options?: { plainText?: boolean },
16+
) => Promise<string>;
1417
constructor(private readonly resend: Resend) {}
1518

1619
async send(
@@ -38,8 +41,14 @@ export class Batch {
3841
);
3942
}
4043
}
41-
42-
email.html = await this.renderAsync(email.react as React.ReactElement);
44+
const [html, text] = await Promise.all([
45+
this.renderAsync(email.react as React.ReactElement),
46+
this.renderAsync(email.react as React.ReactElement, {
47+
plainText: true,
48+
}),
49+
]);
50+
email.html = html;
51+
email.text = text;
4352
email.react = undefined;
4453
}
4554

src/emails/emails.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import type {
2222
} from './interfaces/update-email-options.interface';
2323

2424
export class Emails {
25-
private renderAsync?: (component: React.ReactElement) => Promise<string>;
25+
private renderAsync?: (
26+
component: React.ReactElement,
27+
options?: { plainText?: boolean },
28+
) => Promise<string>;
2629
constructor(private readonly resend: Resend) {}
2730

2831
async send(
@@ -48,9 +51,15 @@ export class Emails {
4851
}
4952
}
5053

51-
payload.html = await this.renderAsync(
52-
payload.react as React.ReactElement,
53-
);
54+
const reactElement = payload.react as React.ReactElement;
55+
const [html, text] = await Promise.all([
56+
this.renderAsync(reactElement),
57+
this.renderAsync(reactElement, {
58+
plainText: true,
59+
}),
60+
]);
61+
payload.text = text;
62+
payload.html = html;
5463
}
5564

5665
const data = await this.resend.post<CreateEmailResponseSuccess>(

0 commit comments

Comments
 (0)