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
64 changes: 63 additions & 1 deletion src/endpoints/email.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ describe('EmailEndpoint', () => {
});

it('should set custom headers', () => {
const headers = { 'X-Custom': 'Value', 'X-Another': 'Another Value' };
const headers = {
'Message-ID': '<ticket-123@example.com>',
'X-LM-Preserve-Message-ID': 'true',
};
const result = emailEndpoint.headers(headers);

expect(result).toBe(emailEndpoint);
Expand Down Expand Up @@ -209,6 +212,47 @@ describe('EmailEndpoint', () => {
});
});

it('should add attachments with content_id and content_type', () => {
const result = emailEndpoint.attach('invite.ics', 'base64calendar', 'invite', 'text/calendar');

expect(result).toBe(emailEndpoint);

return emailEndpoint.send().then(() => {
expect(client.post).toHaveBeenCalledWith(
'/send',
expect.objectContaining({
attachments: [
{
filename: 'invite.ics',
content: 'base64calendar',
content_id: 'invite',
content_type: 'text/calendar',
},
],
}),
undefined
);
});
});

it('should set per-email settings', () => {
const settings = {
track_opens: false,
track_clicks: true,
tls: 'enforced' as const,
};

expect(emailEndpoint.settings(settings)).toBe(emailEndpoint);

return emailEndpoint.send().then(() => {
expect(client.post).toHaveBeenCalledWith(
'/send',
expect.objectContaining({ settings }),
undefined
);
});
});

it('should set the route', () => {
const result = emailEndpoint.route('test-route');

Expand Down Expand Up @@ -400,4 +444,22 @@ describe('EmailEndpoint', () => {
},
});
});

it('should set and reset the batch idempotency key', async () => {
const payload = [
{
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: 'Test Subject',
},
];

await emailEndpoint.idempotencyKey('batch-key').sendBatch(payload);
await emailEndpoint.sendBatch(payload);

expect(client.post).toHaveBeenNthCalledWith(1, '/send/batch', payload, {
headers: { 'Idempotency-Key': 'batch-key' },
});
expect(client.post).toHaveBeenNthCalledWith(2, '/send/batch', payload, undefined);
});
});
30 changes: 28 additions & 2 deletions src/endpoints/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,15 @@ export class EmailEndpoint extends Endpoint {
* @param filename The attachment filename
* @param content The base64-encoded file content
* @param content_id The Content-ID for inline attachments (optional)
* @param content_type The MIME type for the attachment (optional)
* @returns The current instance for chaining
*/
public attach(filename: string, content: string, content_id?: string): this {
public attach(
filename: string,
content: string,
content_id?: string,
content_type?: string
): this {
if (!this.payload.attachments) {
this.payload.attachments = [];
}
Expand All @@ -186,11 +192,23 @@ export class EmailEndpoint extends Endpoint {
filename,
content,
...(content_id && { content_id }),
...(content_type && { content_type }),
});

return this;
}

/**
* Set per-email delivery and tracking settings
*
* @param settings Settings that override the selected route for this email
* @returns The current instance for chaining
*/
public settings(settings: NonNullable<EmailPayload['settings']>): this {
this.payload.settings = settings;
return this;
}

/**
* Set metadata for the email
*
Expand Down Expand Up @@ -236,7 +254,15 @@ export class EmailEndpoint extends Endpoint {
}

public async sendBatch(payload: SendBatchMailRequest): Promise<SendBatchEmailResponse> {
return this.httpClient.post<SendBatchEmailResponse>('/send/batch', payload);
const config = this.idempotencyKeyValue
? { headers: { 'Idempotency-Key': this.idempotencyKeyValue } }
: undefined;

try {
return await this.httpClient.post<SendBatchEmailResponse>('/send/batch', payload, config);
} finally {
this.reset();
}
}

public async ping(): Promise<string> {
Expand Down
Loading