Skip to content

Commit

Permalink
fix: Ensure either text or html is a required field (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayanratna authored Jul 10, 2023
1 parent 3c43e31 commit b4f3548
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
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

0 comments on commit b4f3548

Please sign in to comment.