Skip to content

Commit 4cd1b1e

Browse files
authored
feat(client): add feed support for date range filters (#457)
1 parent 30156e5 commit 4cd1b1e

File tree

6 files changed

+49
-7
lines changed

6 files changed

+49
-7
lines changed

.changeset/better-bugs-sneeze.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@knocklabs/client": minor
3+
"client-example": patch
4+
---
5+
6+
add support for filtering by date in the feed client

examples/client-example/vite.config.js

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ export default defineConfig({
1111
include: [/node_modules/],
1212
},
1313
},
14-
optimizeDeps: {
15-
include: ['@knocklabs/client'],
16-
},
1714
resolve: {
1815
extensions: ['.js', '.jsx', '.json'],
1916
},

examples/nextjs-example/next-env.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/// <reference types="next/image-types/global" />
33

44
// NOTE: This file should not be edited
5-
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
5+
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.

examples/slack-connect-example/next-env.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/// <reference types="next/image-types/global" />
33

44
// NOTE: This file should not be edited
5-
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
5+
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.

packages/client/src/clients/feed/feed.ts

+32-2
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,42 @@ class Feed {
497497
__experimentalCrossBrowserUpdates: undefined,
498498
auto_manage_socket_connection: undefined,
499499
auto_manage_socket_connection_delay: undefined,
500+
inserted_at_date_range: undefined,
500501
};
501502

503+
// If the user has set a date range, transform it to the backend's expected format
504+
const dateRange = options.inserted_at_date_range;
505+
let finalQueryParams = { ...queryParams };
506+
507+
if (dateRange) {
508+
// Create a properly typed object for our date filter parameters
509+
const dateRangeParams: Record<string, string> = {};
510+
511+
// Determine which operators to use based on the inclusive flag
512+
const isInclusive = dateRange.inclusive ?? false;
513+
514+
// For start date: use gte if inclusive, gt if not
515+
if (dateRange.start) {
516+
const startOperator = isInclusive
517+
? "inserted_at.gte"
518+
: "inserted_at.gt";
519+
dateRangeParams[startOperator] = dateRange.start;
520+
}
521+
522+
// For end date: use lte if inclusive, lt if not
523+
if (dateRange.end) {
524+
const endOperator = isInclusive ? "inserted_at.lte" : "inserted_at.lt";
525+
dateRangeParams[endOperator] = dateRange.end;
526+
}
527+
528+
// Create a new object combining queryParams and dateRangeParams
529+
finalQueryParams = { ...queryParams, ...dateRangeParams };
530+
}
531+
502532
const result = await this.knock.client().makeRequest({
503533
method: "GET",
504534
url: `/v1/users/${this.knock.userId}/feeds/${this.feedId}`,
505-
params: queryParams,
535+
params: finalQueryParams,
506536
});
507537

508538
if (result.statusCode === "error" || !result.body) {
@@ -623,7 +653,7 @@ class Feed {
623653
}
624654
});
625655

626-
// Tnis is a hack to determine the direction of whether we're
656+
// This is a hack to determine the direction of whether we're
627657
// adding or removing from the badge count
628658
const direction = type.startsWith("un")
629659
? itemsToUpdate.length

packages/client/src/clients/feed/interfaces.ts

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ export interface FeedClientOptions {
3030
// Optionally set the delay amount in milliseconds when automatically disconnecting sockets from inactive tabs (defaults to `2000`)
3131
// Requires `auto_manage_socket_connection` to be `true`
3232
auto_manage_socket_connection_delay?: number;
33+
// Optionally scope notifications to a given date range
34+
inserted_at_date_range?: {
35+
// Optionally set the start date with a string in ISO 8601 format
36+
start?: string;
37+
// Optionally set the end date with a string in ISO 8601 format
38+
end?: string;
39+
// Optionally set whether to be inclusive of the start and end dates
40+
inclusive?: boolean;
41+
};
3342
}
3443

3544
export type FetchFeedOptions = {

0 commit comments

Comments
 (0)