Skip to content

Commit

Permalink
feat: add deleteMany and deleteAll to messages
Browse files Browse the repository at this point in the history
  • Loading branch information
CahidArda committed Jun 26, 2024
1 parent 5c01b1c commit dd93920
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/client/messages.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { describe, expect, test } from "bun:test";
import { beforeAll, describe, expect, test } from "bun:test";
import { Client } from "./client";

describe("Messages", () => {
const client = new Client({ token: process.env.QSTASH_TOKEN! });

beforeAll(async () => {
await client.messages.deleteAll();
});

test(
"should send message, cancel it then verify cancel",
async () => {
Expand Down Expand Up @@ -44,4 +48,45 @@ describe("Messages", () => {
},
{ timeout: 20_000 }
);

test(
"should delete many and all",
async () => {
const messages = await client.batchJSON([
{
url: `https://example.com`,
body: { hello: "world" },
timeout: 90,
delay: 10,
},
{
url: `https://example.com`,
body: { hello: "world" },
timeout: 90,
delay: 10,
},
{
url: `https://example.com`,
body: { hello: "world" },
timeout: 90,
delay: 10,
},
]);

// eslint-disable-next-line @typescript-eslint/no-magic-numbers
expect(messages.length).toBe(3);

const deleted = await client.messages.deleteMany([
messages[0].messageId,
messages[1].messageId,
]);

// eslint-disable-next-line @typescript-eslint/no-magic-numbers
expect(deleted).toBe(2);

const deletedAll = await client.messages.deleteAll();
expect(deletedAll).toBe(1);
},
{ timeout: 20_000 }
);
});
18 changes: 18 additions & 0 deletions src/client/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,22 @@ export class Messages {
parseResponseAsJson: false,
});
}

public async deleteMany(messageIds: string[]): Promise<number> {
const result = (await this.http.request({
method: "DELETE",
path: ["v2", "messages"],
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ messageIds }),
})) as { cancelled: number };
return result.cancelled;
}

public async deleteAll(): Promise<number> {
const result = (await this.http.request({
method: "DELETE",
path: ["v2", "messages"],
})) as { cancelled: number };
return result.cancelled;
}
}

0 comments on commit dd93920

Please sign in to comment.