Skip to content
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
66 changes: 66 additions & 0 deletions src/endpoints/email.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,72 @@ describe('EmailEndpoint', () => {
);
});

it('should reset all payload properties after send', async () => {
// First email with all properties set
await emailEndpoint
.from('sender@example.com')
.to('recipient@example.com')
.subject('First Subject')
.html('<p>First</p>')
.text('First')
.cc('cc@example.com')
.bcc('bcc@example.com')
.replyTo('reply@example.com')
.headers({ 'X-Custom': 'Value' })
.attach('file.txt', 'base64content')
.route('first-route')
.metadata({ key: 'value' })
.tag('first-tag')
.idempotencyKey('key-1')
.send();

// Second email with only required properties
await emailEndpoint
.from('other@example.com')
.to('other-recipient@example.com')
.subject('Second Subject')
.text('Second')
.send();

// Verify second email does NOT contain any properties from the first
expect(client.post).toHaveBeenLastCalledWith(
'/send',
{
from: 'other@example.com',
to: ['other-recipient@example.com'],
subject: 'Second Subject',
text: 'Second',
},
undefined
);
});

it('should not accumulate attachments across sends', async () => {
await emailEndpoint
.from('sender@example.com')
.to('recipient@example.com')
.subject('First')
.text('First')
.attach('file1.txt', 'content1')
.send();

await emailEndpoint
.from('sender@example.com')
.to('recipient@example.com')
.subject('Second')
.text('Second')
.attach('file2.txt', 'content2')
.send();

expect(client.post).toHaveBeenLastCalledWith(
'/send',
expect.objectContaining({
attachments: [{ filename: 'file2.txt', content: 'content2' }],
}),
undefined
);
});

it('should set the idempotency key in the request headers', async () => {
// Set up a basic email with an idempotency key
emailEndpoint
Expand Down
18 changes: 17 additions & 1 deletion src/endpoints/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,22 @@ export class EmailEndpoint extends Endpoint {
? { headers: { 'Idempotency-Key': this.idempotencyKeyValue } }
: undefined;

return this.httpClient.post<SendEmailResponse>('/send', this.payload, config);
const response = await this.httpClient.post<SendEmailResponse>('/send', this.payload, config);

this.reset();

return response;
}

/**
* Reset the payload and idempotency key to their initial state
*/
private reset(): void {
this.payload = {
from: '',
to: [],
subject: '',
};
this.idempotencyKeyValue = undefined;
}
}