Skip to content

Commit

Permalink
rename events to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ytkimirti committed Feb 17, 2025
1 parent 91dcd03 commit e0e7ddd
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/solidjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ curl -X POST "https://qstash.upstash.io/v2/publish/<NGROK_URL>/api" \
-d '{"message":"Hello, World!"}'
```

Check the Events from QStash dashboard at Upstash Console to see that the request was completed successfully.
Check the logs from QStash dashboard at Upstash Console to see that the request was completed successfully.
2 changes: 1 addition & 1 deletion examples/svelte/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ curl -X POST "https://qstash.upstash.io/v2/publish/<NGROK_URL>/api" \
-d '{"message":"Hello, World!"}'
```

Check the Events from QStash dashboard at Upstash Console to see that the request was completed successfully.
Check the logs from QStash dashboard at Upstash Console to see that the request was completed successfully.
80 changes: 65 additions & 15 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { Queue } from "./queue";
import { Schedules } from "./schedules";
import type {
BodyInit,
Event,
Log,
FlowControl,
GetEventsPayload,
GetLogsPayload,
HeadersInit,
HTTPMethods,
State,
Expand Down Expand Up @@ -260,12 +260,19 @@ export type PublishJsonRequest = Omit<PublishRequest, "body"> & {
body: unknown;
};

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

type EventsRequestFilter = {
/**
* Deprecated, use `LogsRequest` type instead.
*
* @deprecated
*/
export type EventsRequest = LogsRequest;

type LogsRequestFilter = {
messageId?: string;
state?: State;
url?: string;
Expand All @@ -279,11 +286,25 @@ type EventsRequestFilter = {
count?: number;
};

export type GetEventsResponse = {
export type GetLogsResponse = {
cursor?: string;
events: Event[];
/**
* Deprecated, use the `logs` field instead.
*
* @deprecated
*/
events: Log[];

logs: Log[];
};

/**
* Deprecated, use `GetLogsResponse` instead.
*
* @deprecated
*/
export type GetEventsResponse = GetLogsResponse;

export type QueueRequest = {
queueName?: string;
};
Expand Down Expand Up @@ -500,7 +521,7 @@ export class Client {
* }
* ```
*/
public async events(request?: EventsRequest): Promise<GetEventsResponse> {
public async logs(request?: LogsRequest): Promise<GetLogsResponse> {
const query: Record<string, string> = {};

if (typeof request?.cursor === "number" && request.cursor > 0) {
Expand All @@ -521,21 +542,50 @@ export class Client {
}
}

const responsePayload = await this.http.request<GetEventsPayload>({
const responsePayload = await this.http.request<GetLogsPayload>({
path: ["v2", "events"],
method: "GET",
query,
});

const finalLogs = responsePayload.events.map((event) => {
return {
...event,
urlGroup: event.topicName,
};
});

return {
cursor: responsePayload.cursor,
events: responsePayload.events.map((event) => {
return {
...event,
urlGroup: event.topicName,
};
}),
logs: finalLogs,
events: finalLogs,
};
}

/**
* Deprecated, use the `logs` function instead.
*
* The logs endpoint is paginated and returns only 100 logs at a time.
* If you want to receive more logs, you can use the cursor to paginate.
*
* The cursor is a unix timestamp with millisecond precision
*
* @example
* ```ts
* let cursor = Date.now()
* const logs: Log[] = []
* while (cursor > 0) {
* const res = await qstash.logs({ cursor })
* logs.push(...res.logs)
* cursor = res.cursor ?? 0
* }
* ```
*
* @returns
*/
public async events(request?: LogsRequest): Promise<GetLogsResponse> {
return this.logs(request);
}
}

export type PublishToApiResponse = {
Expand Down
20 changes: 0 additions & 20 deletions src/client/events.test.ts

This file was deleted.

46 changes: 46 additions & 0 deletions src/client/logs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { describe, expect, test } from "bun:test";
import { Client } from "./client";
import { parseCursor } from "./utils";

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

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

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

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

test("should have both events and logs fields", async () => {
const result = await client.logs();

expect(result).toHaveProperty("events");
expect(result).toHaveProperty("logs");
// eslint-disable-next-line @typescript-eslint/no-deprecated
expect(result.events).toEqual(result.logs);
});

test("the old events field should still work", async () => {
const result = await client.events();

expect(result).toHaveProperty("events");
expect(result).toHaveProperty("logs");
// eslint-disable-next-line @typescript-eslint/no-deprecated
expect(result.events).toEqual(result.logs);

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

if (result.cursor && result2.cursor) {
expect(parseCursor(result.cursor).timestamp).toBeGreaterThan(
parseCursor(result2.cursor).timestamp
);
}
});
});
30 changes: 26 additions & 4 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export type State = "CREATED" | "ACTIVE" | "DELIVERED" | "ERROR" | "RETRY" | "FA

export type HTTPMethods = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";

export type Event = {
export type Log = {
time: number;
state: State;
messageId: string;
Expand All @@ -16,13 +16,35 @@ export type Event = {
body?: string; // base64 encoded
};

export type EventPayload = Omit<Event, "urlGroup"> & { topicName: string };
/**
* Deprecated, use the `Log` type instead
*
* @deprecated
*/
export type Event = Log;

export type GetEventsPayload = {
export type LogPayload = Omit<Log, "urlGroup"> & { topicName: string };

/**
* Deprecated, use `LogPayload` instead
*
* @deprecated
*/
export type EventPayload = LogPayload;

// The response returned from the QStash API
export type GetLogsPayload = {
cursor?: string;
events: EventPayload[];
events: LogPayload[];
};

/**
* Deprecated, use `GetLogsPayload` instead
*
* @deprecated
*/
export type GetEventsPayload = GetLogsPayload;

export type WithCursor<T> = T & { cursor?: number };

export type BodyInit = Blob | FormData | URLSearchParams | ReadableStream<Uint8Array> | string;
Expand Down

0 comments on commit e0e7ddd

Please sign in to comment.