From c95c76cc19154a476102b975495785494188da86 Mon Sep 17 00:00:00 2001 From: Sokratis Vidros Date: Mon, 14 Oct 2024 17:14:02 +0300 Subject: [PATCH] fix(js): Bypass cache during novu.notifications.list() Introduce novu.notifications.list({ useCache: false }) option to always fetch the latest notifications and bypass the cache. --- packages/js/src/notifications/notifications.ts | 11 ++++++++--- packages/js/src/notifications/types.ts | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/js/src/notifications/notifications.ts b/packages/js/src/notifications/notifications.ts index 37caab090e9..52ac080bbc1 100644 --- a/packages/js/src/notifications/notifications.ts +++ b/packages/js/src/notifications/notifications.ts @@ -59,11 +59,16 @@ export class Notifications extends BaseModule { this.#useCache = useCache; } - async list({ limit = 10, ...restOptions }: ListNotificationsArgs = {}): Result { + async list({ + limit = 10, + useCache = true, + ...restOptions + }: ListNotificationsArgs = {}): Result { return this.callWithSession(async () => { const args = { limit, ...restOptions }; try { - let data: ListNotificationsResponse | undefined = this.#useCache ? this.cache.getAll(args) : undefined; + let data: ListNotificationsResponse | undefined = + this.#useCache || useCache ? this.cache.getAll(args) : undefined; this._emitter.emit('notifications.list.pending', { args, data }); if (!data) { @@ -78,7 +83,7 @@ export class Notifications extends BaseModule { notifications: response.data.map((el) => new Notification(el, this._emitter, this._inboxService)), }; - if (this.#useCache) { + if (this.#useCache || useCache) { this.cache.set(args, data); data = this.cache.getAll(args); } diff --git a/packages/js/src/notifications/types.ts b/packages/js/src/notifications/types.ts index e44e5b38630..ec28a44eaaf 100644 --- a/packages/js/src/notifications/types.ts +++ b/packages/js/src/notifications/types.ts @@ -8,6 +8,7 @@ export type ListNotificationsArgs = { limit?: number; after?: string; offset?: number; + useCache?: boolean; }; export type ListNotificationsResponse = { notifications: Notification[]; hasMore: boolean; filter: NotificationFilter };