Skip to content

Commit

Permalink
Merge pull request #184 from upstash/DX-1300
Browse files Browse the repository at this point in the history
DX-1300: fix cursor type
  • Loading branch information
CahidArda authored Sep 20, 2024
2 parents bd3d298 + cf9f326 commit 8cc4659
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export type PublishJsonRequest = Omit<PublishRequest, "body"> & {
};

export type EventsRequest = {
cursor?: number;
cursor?: string | number;
filter?: EventsRequestFilter;
};

Expand All @@ -246,7 +246,7 @@ type EventsRequestFilter = {
};

export type GetEventsResponse = {
cursor?: number;
cursor?: string;
events: Event[];
};

Expand Down Expand Up @@ -464,8 +464,11 @@ export class Client {
*/
public async events(request?: EventsRequest): Promise<GetEventsResponse> {
const query: Record<string, string> = {};
if (request?.cursor && request.cursor > 0) {

if (typeof request?.cursor === "number" && request.cursor > 0) {
query.cursor = request.cursor.toString();
}else if (typeof request?.cursor === "string" && request.cursor !== "") {
query.cursor = request.cursor;
}

for (const [key, value] of Object.entries(request?.filter ?? {})) {
Expand Down
20 changes: 20 additions & 0 deletions src/client/events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { describe, expect, test } from "bun:test";
import { Client } from "./client";
import { parseCursor } from "./utils";

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

test("should use cursor", async () => {
const result1 = await client.events()

const result2 = await client.events({ cursor: result1.cursor })


if (result1.cursor && result2.cursor) {
expect(parseCursor(result1.cursor).timestamp).toBeGreaterThan(parseCursor(result2.cursor).timestamp);
}

})
});
2 changes: 1 addition & 1 deletion src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type Event = {
export type EventPayload = Omit<Event, "urlGroup"> & { topicName: string };

export type GetEventsPayload = {
cursor?: number;
cursor?: string ;
events: EventPayload[];
};

Expand Down
10 changes: 10 additions & 0 deletions src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,13 @@ export function decodeBase64(base64: string) {
return atob(base64);
}
}

export function parseCursor(cursor: string) {
const [timestamp, sequence] = cursor.split("-");

return {
timestamp: Number.parseInt(timestamp, 10),
sequence: Number.parseInt(sequence, 10),
};

}

0 comments on commit 8cc4659

Please sign in to comment.