From 39fd306470387e71bef09cf7b8a172f74869e45a Mon Sep 17 00:00:00 2001 From: Meshan Khosla Date: Fri, 5 Apr 2024 10:54:17 -0700 Subject: [PATCH 1/5] Event filtering --- src/client/client.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/client/client.ts b/src/client/client.ts index 43abaf00..d851caa5 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -3,7 +3,7 @@ import { HttpClient, Requester, RetryConfig } from "./http"; import { Messages } from "./messages"; import { Schedules } from "./schedules"; import { Topics } from "./topics"; -import { Event } from "./types"; +import { Event, State } from "./types"; import { prefixHeaders } from "./utils"; type ClientConfig = { /** @@ -157,6 +157,15 @@ export type PublishJsonRequest = Omit & { export type EventsRequest = { cursor?: number; + messageId?: string; + state?: State; + url?: string; + topicName?: string; + scheduleId?: string; + queueName?: string; + fromDate?: number; // unix timestamp (ms) + toDate?: number; // unix timestamp (ms) + count?: number; }; export type GetEventsResponse = { @@ -354,10 +363,14 @@ export class Client { * ``` */ public async events(req?: EventsRequest): Promise { - const query: Record = {}; - if (req?.cursor && req.cursor > 0) { - query.cursor = req.cursor; - } + const query: Record = {}; + Object.keys(req ?? {}).forEach(key => { + const value = (req as Record)[key]; + if (typeof value !== "undefined") { + query[key] = value.toString(); + } + }); + const res = await this.http.request({ path: ["v2", "events"], method: "GET", From fec87d2178a50dff125325badc2cb2b2e6f07232 Mon Sep 17 00:00:00 2001 From: Meshan Khosla Date: Fri, 5 Apr 2024 11:04:46 -0700 Subject: [PATCH 2/5] Add number check --- src/client/client.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/client/client.ts b/src/client/client.ts index d851caa5..dfab6233 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -366,6 +366,9 @@ export class Client { const query: Record = {}; Object.keys(req ?? {}).forEach(key => { const value = (req as Record)[key]; + if (typeof value === "number" && value < 0) { + return; + } if (typeof value !== "undefined") { query[key] = value.toString(); } From 7c248604e582daae6e53713907e3e7f771b571e9 Mon Sep 17 00:00:00 2001 From: Meshan Khosla Date: Mon, 8 Apr 2024 12:07:29 -0700 Subject: [PATCH 3/5] Improve types --- src/client/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/client.ts b/src/client/client.ts index dfab6233..26053f1b 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -365,7 +365,7 @@ export class Client { public async events(req?: EventsRequest): Promise { const query: Record = {}; Object.keys(req ?? {}).forEach(key => { - const value = (req as Record)[key]; + const value = (req as Record)[key]; if (typeof value === "number" && value < 0) { return; } From 4bca9bf7ccceda53972104807093552c15245f49 Mon Sep 17 00:00:00 2001 From: Meshan Khosla Date: Thu, 18 Apr 2024 15:02:29 -0700 Subject: [PATCH 4/5] Move filters to their own attribute --- src/client/client.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/client/client.ts b/src/client/client.ts index 26053f1b..e5f0ba7e 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -157,6 +157,10 @@ export type PublishJsonRequest = Omit & { export type EventsRequest = { cursor?: number; + filter?: EventsRequestFilter +}; + +type EventsRequestFilter = { messageId?: string; state?: State; url?: string; @@ -166,7 +170,7 @@ export type EventsRequest = { fromDate?: number; // unix timestamp (ms) toDate?: number; // unix timestamp (ms) count?: number; -}; +} export type GetEventsResponse = { cursor?: number; @@ -364,8 +368,11 @@ export class Client { */ public async events(req?: EventsRequest): Promise { const query: Record = {}; - Object.keys(req ?? {}).forEach(key => { - const value = (req as Record)[key]; + if (req?.cursor && req.cursor > 0) { + query.cursor = req.cursor.toString(); + } + + Object.entries(req?.filter ?? {}).forEach(([key, value]) => { if (typeof value === "number" && value < 0) { return; } From 7a3aac57dc4b53beefee83da8a458df4018b22ea Mon Sep 17 00:00:00 2001 From: Meshan Khosla Date: Fri, 19 Apr 2024 10:15:59 -0700 Subject: [PATCH 5/5] Move to for loop from foreach --- src/client/client.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/client/client.ts b/src/client/client.ts index e5f0ba7e..284fab37 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -372,14 +372,14 @@ export class Client { query.cursor = req.cursor.toString(); } - Object.entries(req?.filter ?? {}).forEach(([key, value]) => { + for (const [key, value] of Object.entries(req?.filter ?? {})) { if (typeof value === "number" && value < 0) { - return; + continue; } if (typeof value !== "undefined") { query[key] = value.toString(); } - }); + } const res = await this.http.request({ path: ["v2", "events"],