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
265 changes: 170 additions & 95 deletions src/batch/batch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Resend } from '../resend';
import { mockSuccessResponse } from '../test-utils/mock-fetch';
import type {
CreateBatchOptions,
CreateBatchSuccessResponse,
} from './interfaces/create-batch-options.interface';
import {
mockSuccessResponse,
mockSuccessWithStatusCode,
} from '../test-utils/mock-fetch';
import type { CreateBatchOptions } from './interfaces/create-batch-options.interface';

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');

Expand Down Expand Up @@ -32,18 +32,20 @@ describe('Batch', () => {
html: '<h1>Hi there</h1>',
},
];
const response: CreateBatchSuccessResponse = {
data: [
{ id: 'aabeeefc-bd13-474a-a440-0ee139b3a4cc' },
{ id: 'aebe1c6e-30ad-4257-993b-519f5affa626' },
{ id: 'b2bc2598-f98b-4da4-86c9-7b32881ef394' },
],
};
mockSuccessResponse(response, {
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
mockSuccessResponse(
{
data: [
{ id: 'aabeeefc-bd13-474a-a440-0ee139b3a4cc' },
{ id: 'aebe1c6e-30ad-4257-993b-519f5affa626' },
{ id: 'b2bc2598-f98b-4da4-86c9-7b32881ef394' },
],
},
});
{
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
},
);

const data = await resend.batch.create(payload);
expect(data).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -72,19 +74,20 @@ describe('Batch', () => {
});

it('does not send the Idempotency-Key header when idempotencyKey is not provided', async () => {
const response: CreateBatchSuccessResponse = {
data: [
{
id: 'not-idempotent-123',
mockSuccessResponse(
{
data: [
{
id: 'not-idempotent-123',
},
],
},
{
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
],
};

mockSuccessResponse(response, {
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});
);

const payload: CreateBatchOptions = [
{
Expand All @@ -101,29 +104,28 @@ describe('Batch', () => {
const lastCall = fetchMock.mock.calls[0];
expect(lastCall).toBeDefined();

//@ts-expect-error
const hasIdempotencyKey = lastCall[1]?.headers.has('Idempotency-Key');
expect(hasIdempotencyKey).toBeFalsy();
const request = lastCall[1];
expect(request).toBeDefined();

//@ts-expect-error
const usedIdempotencyKey = lastCall[1]?.headers.get('Idempotency-Key');
expect(usedIdempotencyKey).toBeNull();
const headers = new Headers(request?.headers);
expect(headers.has('Idempotency-Key')).toBeFalsy();
});

it('sends the Idempotency-Key header when idempotencyKey is provided', async () => {
const response: CreateBatchSuccessResponse = {
data: [
{
id: 'idempotent-123',
mockSuccessResponse(
{
data: [
{
id: 'idempotent-123',
},
],
},
{
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
],
};

mockSuccessResponse(response, {
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});
);

const payload: CreateBatchOptions = [
{
Expand Down Expand Up @@ -177,19 +179,21 @@ describe('Batch', () => {
html: '<h1>Hi there</h1>',
},
];
const response: CreateBatchSuccessResponse = {
data: [
{ id: 'aabeeefc-bd13-474a-a440-0ee139b3a4cc' },
{ id: 'aebe1c6e-30ad-4257-993b-519f5affa626' },
{ id: 'b2bc2598-f98b-4da4-86c9-7b32881ef394' },
],
};

mockSuccessResponse(response, {
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',

mockSuccessResponse(
{
data: [
{ id: 'aabeeefc-bd13-474a-a440-0ee139b3a4cc' },
{ id: 'aebe1c6e-30ad-4257-993b-519f5affa626' },
{ id: 'b2bc2598-f98b-4da4-86c9-7b32881ef394' },
],
},
});
{
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
},
);

const data = await resend.batch.send(payload);
expect(data).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -218,19 +222,20 @@ describe('Batch', () => {
});

it('does not send the Idempotency-Key header when idempotencyKey is not provided', async () => {
const response: CreateBatchSuccessResponse = {
data: [
{
id: 'not-idempotent-123',
mockSuccessResponse(
{
data: [
{
id: 'not-idempotent-123',
},
],
},
{
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
],
};

mockSuccessResponse(response, {
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});
);

const payload: CreateBatchOptions = [
{
Expand All @@ -246,30 +251,27 @@ describe('Batch', () => {
// Inspect the last fetch call and body
const lastCall = fetchMock.mock.calls[0];
expect(lastCall).toBeDefined();

//@ts-expect-error
const hasIdempotencyKey = lastCall[1]?.headers.has('Idempotency-Key');
expect(hasIdempotencyKey).toBeFalsy();

//@ts-expect-error
const usedIdempotencyKey = lastCall[1]?.headers.get('Idempotency-Key');
expect(usedIdempotencyKey).toBeNull();
const request = lastCall[1];
expect(request).toBeDefined();
const headers = new Headers(request?.headers);
expect(headers.has('Idempotency-Key')).toBe(false);
});

it('sends the Idempotency-Key header when idempotencyKey is provided', async () => {
const response: CreateBatchSuccessResponse = {
data: [
{
id: 'idempotent-123',
mockSuccessResponse(
{
data: [
{
id: 'idempotent-123',
},
],
},
{
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
],
};

mockSuccessResponse(response, {
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
});
);

const payload: CreateBatchOptions = [
{
Expand All @@ -286,18 +288,91 @@ describe('Batch', () => {
// Inspect the last fetch call and body
const lastCall = fetchMock.mock.calls[0];
expect(lastCall).toBeDefined();
const headers = new Headers(lastCall[1]?.headers);
expect(headers.has('Idempotency-Key')).toBeTruthy();
expect(headers.get('Idempotency-Key')).toBe(idempotencyKey);
});

// Check if headers contains Idempotency-Key
// In the mock, headers is an object with key-value pairs
expect(fetchMock.mock.calls[0][1]?.headers).toBeDefined();
it('handles batch response with errors field when permissive option is set', async () => {
mockSuccessWithStatusCode(
{
data: [],
errors: [{ index: 2, message: 'Invalid email address' }],
},
202,
{
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
},
);

//@ts-expect-error
const hasIdempotencyKey = lastCall[1]?.headers.has('Idempotency-Key');
expect(hasIdempotencyKey).toBeTruthy();
const payload: CreateBatchOptions = [];

//@ts-expect-error
const usedIdempotencyKey = lastCall[1]?.headers.get('Idempotency-Key');
expect(usedIdempotencyKey).toBe(idempotencyKey);
const result = await resend.batch.create(payload, {
batchValidation: 'permissive',
});

// Verify the header was passed correctly
const lastCall = fetchMock.mock.calls[0];
expect(lastCall).toBeDefined();
const request = lastCall[1];
expect(request).toBeDefined();
const headers = new Headers(request?.headers);
expect(headers.get('x-batch-validation')).toBe('permissive');

expect(result.data).toEqual({
data: [],
errors: [{ index: 2, message: 'Invalid email address' }],
});
expect(result.error).toBeNull();
});

it('removes errors field when permissive header is not set (backward compatibility)', async () => {
mockSuccessResponse(
{
data: [
{ id: 'success-email-1' },
{ id: 'success-email-2' },
{ id: 'success-email-3' },
],
},
{
headers: {
Authorization: 'Bearer re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop',
},
},
);

const payload: CreateBatchOptions = [
{
from: '[email protected]',
to: '[email protected]',
subject: 'Test 1',
html: '<h1>Test 1</h1>',
},
{
from: '[email protected]',
to: '[email protected]',
subject: 'Test 2',
html: '<h1>Test 2</h1>',
},
{
from: '[email protected]',
to: 'invalid-email',
subject: 'Test 3',
html: '<h1>Test 3</h1>',
},
];

const response = await resend.batch.create(payload);
// Should not have errors field for backward compatibility
expect(response.data).not.toHaveProperty('errors');
expect(response.data?.data).toEqual([
{ id: 'success-email-1' },
{ id: 'success-email-2' },
{ id: 'success-email-3' },
]);
});
});
});
23 changes: 14 additions & 9 deletions src/batch/batch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type * as React from 'react';
import type { EmailApiOptions } from '../common/interfaces/email-api-options.interface';
import { parseEmailToApiOptions } from '../common/utils/parse-email-to-api-options';
import type { Resend } from '../resend';
Expand All @@ -13,17 +12,17 @@ export class Batch {
private renderAsync?: (component: React.ReactElement) => Promise<string>;
constructor(private readonly resend: Resend) {}

async send(
async send<Options extends CreateBatchRequestOptions>(
payload: CreateBatchOptions,
options: CreateBatchRequestOptions = {},
): Promise<CreateBatchResponse> {
options?: Options,
): Promise<CreateBatchResponse<Options>> {
return this.create(payload, options);
}

async create(
async create<Options extends CreateBatchRequestOptions>(
payload: CreateBatchOptions,
options: CreateBatchRequestOptions = {},
): Promise<CreateBatchResponse> {
options?: Options,
): Promise<CreateBatchResponse<Options>> {
const emails: EmailApiOptions[] = [];

for (const email of payload) {
Expand All @@ -46,10 +45,16 @@ export class Batch {
emails.push(parseEmailToApiOptions(email));
}

const data = await this.resend.post<CreateBatchSuccessResponse>(
const data = await this.resend.post<CreateBatchSuccessResponse<Options>>(
'/emails/batch',
emails,
options,
{
...options,
headers: {
'x-batch-validation': options?.batchValidation ?? 'strict',
...options?.headers,
},
},
);

return data;
Expand Down
Loading