-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Bu Kinoshita <[email protected]>
- Loading branch information
1 parent
4bb4f65
commit ae19ac7
Showing
5 changed files
with
154 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { enableFetchMocks } from 'jest-fetch-mock'; | ||
import { Resend } from '../resend'; | ||
|
||
enableFetchMocks(); | ||
|
||
const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop'); | ||
|
||
describe('Batch', () => { | ||
afterEach(() => fetchMock.resetMocks()); | ||
|
||
describe('create', () => { | ||
it('sends multiple emails', async () => { | ||
const payload = [ | ||
{ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Hello World', | ||
html: '<h1>Hello world</h1>', | ||
}, | ||
{ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Olá mundo', | ||
html: '<h1>olá mundo</h1>', | ||
}, | ||
{ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Hi there', | ||
html: '<h1>Hi there</h1>', | ||
}, | ||
]; | ||
|
||
fetchMock.mockOnce( | ||
JSON.stringify({ | ||
data: [{ id: '1234' }, { id: '4567' }, { id: '4242' }], | ||
}), | ||
{ | ||
status: 200, | ||
headers: { | ||
'content-type': 'application/json', | ||
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop', | ||
}, | ||
}, | ||
); | ||
|
||
const data = await resend.batch.create(payload); | ||
expect(data).toMatchObject({ | ||
data: [{ id: '1234' }, { id: '4567' }, { id: '4242' }], | ||
}); | ||
}); | ||
}); | ||
|
||
describe('send', () => { | ||
it('sends multiple emails', async () => { | ||
const payload = [ | ||
{ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Hello World', | ||
html: '<h1>Hello world</h1>', | ||
}, | ||
{ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Olá mundo', | ||
html: '<h1>olá mundo</h1>', | ||
}, | ||
{ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Hi there', | ||
html: '<h1>Hi there</h1>', | ||
}, | ||
]; | ||
|
||
fetchMock.mockOnce( | ||
JSON.stringify({ | ||
data: [{ id: '1234' }, { id: '4567' }, { id: '4242' }], | ||
}), | ||
{ | ||
status: 200, | ||
headers: { | ||
'content-type': 'application/json', | ||
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop', | ||
}, | ||
}, | ||
); | ||
|
||
const data = await resend.batch.send(payload); | ||
expect(data).toMatchObject({ | ||
data: [{ id: '1234' }, { id: '4567' }, { id: '4242' }], | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { render } from '@react-email/render'; | ||
import * as React from 'react'; | ||
import { Resend } from '../resend'; | ||
import { | ||
CreateBatchOptions, | ||
CreateBatchRequestOptions, | ||
CreateBatchResponse, | ||
} from './interfaces'; | ||
|
||
export class Batch { | ||
constructor(private readonly resend: Resend) {} | ||
|
||
async send( | ||
payload: CreateBatchOptions, | ||
options: CreateBatchRequestOptions = {}, | ||
): Promise<CreateBatchResponse> { | ||
return this.create(payload, options); | ||
} | ||
|
||
async create( | ||
payload: CreateBatchOptions, | ||
options: CreateBatchRequestOptions = {}, | ||
): Promise<CreateBatchResponse> { | ||
for (const email of payload) { | ||
if (email.react) { | ||
email.html = render(email.react as React.ReactElement); | ||
delete email.react; | ||
} | ||
} | ||
|
||
const data = await this.resend.post<CreateBatchResponse>( | ||
'/emails/batch', | ||
payload, | ||
options, | ||
); | ||
|
||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { PostOptions } from '../../common/interfaces'; | ||
import { CreateEmailOptions } from '../../emails/interfaces/create-email-options.interface'; | ||
|
||
export type CreateBatchOptions = CreateEmailOptions[]; | ||
|
||
export interface CreateBatchRequestOptions extends PostOptions {} | ||
|
||
export interface CreateBatchResponse { | ||
data: { | ||
/** The ID of the newly created email. */ | ||
id: string; | ||
}[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './create-batch-options.interface'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters