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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "resend",
"version": "6.18.1",
"version": "6.19.0",
"description": "Node.js library for the Resend API",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
Expand Down
71 changes: 71 additions & 0 deletions src/broadcasts/broadcasts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import createFetchMock from 'vitest-fetch-mock';
import type { ErrorResponse } from '../interfaces';
import { Resend } from '../resend';
import { mockSuccessResponse } from '../test-utils/mock-fetch';
import type { CancelBroadcastResponseSuccess } from './interfaces/cancel-broadcast.interface';
import type {
CreateBroadcastOptions,
CreateBroadcastResponseSuccess,
Expand Down Expand Up @@ -629,6 +630,76 @@ describe('Broadcasts', () => {
});
});

describe('cancel', () => {
it('cancels a broadcast', async () => {
const id = 'b01e0de9-7c27-4a53-bf38-2e3f98389a65';
const response: CancelBroadcastResponseSuccess = {
object: 'broadcast',
id,
};
fetchMock.mockOnce(JSON.stringify(response), {
status: 200,
headers: {
'content-type': 'application/json',
},
});

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');

await expect(
resend.broadcasts.cancel(id),
).resolves.toMatchInlineSnapshot(`
{
"data": {
"id": "b01e0de9-7c27-4a53-bf38-2e3f98389a65",
"object": "broadcast",
},
"error": null,
"headers": {
"content-type": "application/json",
},
}
`);
});

describe('when broadcast is not cancelable', () => {
it('returns error', async () => {
const response: ErrorResponse = {
name: 'validation_error',
statusCode: 403,
message: 'Only queued or scheduled broadcasts can be canceled',
};

fetchMock.mockOnce(JSON.stringify(response), {
status: 403,
headers: {
'content-type': 'application/json',
},
});

const resend = new Resend('re_zKa4RCko_Lhm9ost2YjNCctnPjbLw8Nop');

const result = resend.broadcasts.cancel(
'559ac32e-9ef5-46fb-82a1-b76b840c0f7b',
);

await expect(result).resolves.toMatchInlineSnapshot(`
{
"data": null,
"error": {
"message": "Only queued or scheduled broadcasts can be canceled",
"name": "validation_error",
"statusCode": 403,
},
"headers": {
"content-type": "application/json",
},
}
`);
});
});
});

describe('update', () => {
it('updates a broadcast', async () => {
const id = 'b01e0de9-7c27-4a53-bf38-2e3f98389a65';
Expand Down
11 changes: 11 additions & 0 deletions src/broadcasts/broadcasts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { buildPaginationQuery } from '../common/utils/build-pagination-query';
import { render } from '../render';
import type { Resend } from '../resend';
import type {
CancelBroadcastResponse,
CancelBroadcastResponseSuccess,
} from './interfaces/cancel-broadcast.interface';
import type {
CreateBroadcastOptions,
CreateBroadcastRequestOptions,
Expand Down Expand Up @@ -96,6 +100,13 @@ export class Broadcasts {
return data;
}

async cancel(id: string): Promise<CancelBroadcastResponse> {
const data = await this.resend.post<CancelBroadcastResponseSuccess>(
`/broadcasts/${id}/cancel`,
);
return data;
}

async update(
id: string,
payload: UpdateBroadcastOptions,
Expand Down
8 changes: 8 additions & 0 deletions src/broadcasts/interfaces/cancel-broadcast.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Response } from '../../interfaces';
import type { Broadcast } from './broadcast';

export interface CancelBroadcastResponseSuccess extends Pick<Broadcast, 'id'> {
object: 'broadcast';
}

export type CancelBroadcastResponse = Response<CancelBroadcastResponseSuccess>;
1 change: 1 addition & 0 deletions src/broadcasts/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './broadcast';
export * from './cancel-broadcast.interface';
export * from './create-broadcast-options.interface';
export * from './get-broadcast.interface';
export * from './list-broadcasts.interface';
Expand Down
Loading