diff --git a/src/endpoints/email.spec.ts b/src/endpoints/email.spec.ts index 9fb66fe..5560f0a 100644 --- a/src/endpoints/email.spec.ts +++ b/src/endpoints/email.spec.ts @@ -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('

First

') + .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 diff --git a/src/endpoints/email.ts b/src/endpoints/email.ts index a55a0f3..fc94ebe 100644 --- a/src/endpoints/email.ts +++ b/src/endpoints/email.ts @@ -338,6 +338,22 @@ export class EmailEndpoint extends Endpoint { ? { headers: { 'Idempotency-Key': this.idempotencyKeyValue } } : undefined; - return this.httpClient.post('/send', this.payload, config); + const response = await this.httpClient.post('/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; } }