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: Ensure either text or html is a required field #130

Merged
merged 2 commits into from
Jul 10, 2023
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
18 changes: 15 additions & 3 deletions src/emails/interfaces/create-email-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { ReactElement } from 'react';
import { PostOptions } from '../../common/interfaces';

export interface CreateEmailOptions {
interface CreateEmailBaseOptions {
attachments?: Attachment[];
bcc?: string | string[];
cc?: string | string[];
from: string;
html?: string;
react?: ReactElement | null;
reply_to?: string | string[];
subject: string;
tags?: Tag[];
text?: string;
to: string | string[];
}

interface CreateEmailWithHtmlOptions extends CreateEmailBaseOptions {
html: string;
text?: string;
}

interface CreateEmailWithTextOptions extends CreateEmailBaseOptions {
html?: string;
text: string;
}

export type CreateEmailOptions =
| CreateEmailWithHtmlOptions
| CreateEmailWithTextOptions;

export interface CreateEmailRequestOptions extends PostOptions {}

export interface CreateEmailResponse {
Expand Down
19 changes: 13 additions & 6 deletions src/resend.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Resend } from './resend';
import MockAdapater from 'axios-mock-adapter';
import axios from 'axios';
import { CreateEmailOptions } from './emails/interfaces';

const mock = new MockAdapater(axios);
const resend = new Resend('re_924b3rjh2387fbewf823');
Expand All @@ -13,10 +14,11 @@ describe('Resend', () => {
});

it('sends email', async () => {
const payload = {
const payload: CreateEmailOptions = {
from: 'bu@resend.com',
to: 'zeno@resend.com',
subject: 'Hello World',
html: '<h1>Hello world</h1>',
};
mock.onPost('/emails', payload).replyOnce(200, {
id: '1234',
Expand All @@ -37,10 +39,11 @@ describe('Resend', () => {
});

it('sends email with multiple recipients', async () => {
const payload = {
const payload: CreateEmailOptions = {
from: 'admin@resend.com',
to: ['bu@resend.com', 'zeno@resend.com'],
subject: 'Hello World',
text: 'Hello world',
};
mock.onPost('/emails', payload).replyOnce(200, {
id: '1234',
Expand All @@ -64,11 +67,12 @@ describe('Resend', () => {
});

it('sends email with multiple bcc recipients', async () => {
const payload = {
const payload: CreateEmailOptions = {
from: 'admin@resend.com',
to: 'bu@resend.com',
bcc: ['foo@resend.com', 'bar@resend.com'],
subject: 'Hello World',
text: 'Hello world',
};
mock.onPost('/emails', payload).replyOnce(200, {
id: '1234',
Expand All @@ -94,11 +98,12 @@ describe('Resend', () => {
});

it('sends email with multiple cc recipients', async () => {
const payload = {
const payload: CreateEmailOptions = {
from: 'admin@resend.com',
to: 'bu@resend.com',
cc: ['foo@resend.com', 'bar@resend.com'],
subject: 'Hello World',
text: 'Hello world',
};
mock.onPost('/emails', payload).replyOnce(200, {
id: '1234',
Expand All @@ -124,11 +129,12 @@ describe('Resend', () => {
});

it('sends email with multiple replyTo emails', async () => {
const apiPayload = {
const apiPayload: CreateEmailOptions = {
from: 'admin@resend.com',
to: 'bu@resend.com',
reply_to: ['foo@resend.com', 'bar@resend.com'],
subject: 'Hello World',
text: 'Hello world',
};

mock.onPost('/emails', apiPayload).replyOnce(200, {
Expand All @@ -139,11 +145,12 @@ describe('Resend', () => {
created_at: '123',
});

const payload = {
const payload: CreateEmailOptions = {
from: 'admin@resend.com',
to: 'bu@resend.com',
reply_to: ['foo@resend.com', 'bar@resend.com'],
subject: 'Hello World',
text: 'Hello world',
};

const data = await resend.emails.send(payload);
Expand Down