Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-ideas-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@knocklabs/client": patch
---

Improve `FetchFeedOptionsForRequest` type
19 changes: 15 additions & 4 deletions packages/client/src/clients/feed/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,25 @@ export type FetchFeedOptions = {
__fetchSource?: "socket" | "http";
} & Omit<FeedClientOptions, "__experimentalCrossBrowserUpdates">;

// The final data shape that is sent to the API
// Should match types here: https://docs.knock.app/reference#get-feed
/**
* The final data shape that is sent to the the list feed items endpoint of the Knock API.
*
* @see https://docs.knock.app/api-reference/users/feeds/list_items
*/
export type FetchFeedOptionsForRequest = Omit<
FeedClientOptions,
"trigger_data"
"trigger_data" | "inserted_at_date_range"
> & {
// Formatted trigger data into a string
/** The trigger data of the feed items (as a JSON string). */
trigger_data?: string;
/** Limits the results to items inserted after or on the given date. */
"inserted_at.gte"?: string;
/** Limits the results to items inserted before or on the given date. */
"inserted_at.lte"?: string;
/** Limits the results to items inserted after the given date. */
"inserted_at.gt"?: string;
/** Limits the results to items inserted before the given date. */
"inserted_at.lt"?: string;
// Unset options that should not be sent to the API
__loadingType: undefined;
__fetchSource: undefined;
Expand Down
13 changes: 11 additions & 2 deletions packages/client/src/clients/feed/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { FeedClientOptions, FeedItem } from "./interfaces";
import type {
FeedClientOptions,
FeedItem,
FetchFeedOptionsForRequest,
} from "./interfaces";

export function deduplicateItems(items: FeedItem[]): FeedItem[] {
const seen: Record<string, boolean> = {};
Expand All @@ -22,14 +26,19 @@ export function sortItems(items: FeedItem[]) {
});
}

type DateRangeParams = Pick<
FetchFeedOptionsForRequest,
"inserted_at.gte" | "inserted_at.lte" | "inserted_at.gt" | "inserted_at.lt"
>;

export function mergeDateRangeParams(options: FeedClientOptions) {
const { inserted_at_date_range, ...rest } = options;

if (!inserted_at_date_range) {
return rest;
}

const dateRangeParams: Record<string, string> = {};
const dateRangeParams: DateRangeParams = {};

// Determine which operators to use based on the inclusive flag
const isInclusive = inserted_at_date_range.inclusive ?? false;
Expand Down
Loading