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

feature(email): send email with react component will now render text if text is undefined #469

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions src/batch/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import type {
} from './interfaces/create-batch-options.interface';

export class Batch {
private renderAsync?: (component: React.ReactElement) => Promise<string>;
private renderAsync?: (
component: React.ReactElement,
options?: { plainText?: boolean },
) => Promise<string>;
constructor(private readonly resend: Resend) {}

async send(
Expand Down Expand Up @@ -38,8 +41,13 @@ export class Batch {
);
}
}

email.html = await this.renderAsync(email.react as React.ReactElement);
const reactElement = email.react as React.ReactElement;
email.html = await this.renderAsync(reactElement);
if (email.text === undefined) {
email.text = await this.renderAsync(reactElement, {
plainText: true,
});
}
email.react = undefined;
}

Expand Down
15 changes: 11 additions & 4 deletions src/emails/emails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import type {
} from './interfaces/update-email-options.interface';

export class Emails {
private renderAsync?: (component: React.ReactElement) => Promise<string>;
private renderAsync?: (
component: React.ReactElement,
options?: { plainText?: boolean },
) => Promise<string>;
constructor(private readonly resend: Resend) {}

async send(
Expand All @@ -48,9 +51,13 @@ export class Emails {
}
}

payload.html = await this.renderAsync(
payload.react as React.ReactElement,
);
const reactElement = payload.react as React.ReactElement;
payload.html = await this.renderAsync(reactElement);
if (payload.text === undefined) {
payload.text = await this.renderAsync(reactElement, {
plainText: true,
});
}
}

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