diff --git a/.changeset/shaggy-ideas-matter.md b/.changeset/shaggy-ideas-matter.md new file mode 100644 index 000000000..c8f786108 --- /dev/null +++ b/.changeset/shaggy-ideas-matter.md @@ -0,0 +1,5 @@ +--- +"@knocklabs/client": patch +--- + +Improve `FetchFeedOptionsForRequest` type diff --git a/packages/client/src/clients/feed/interfaces.ts b/packages/client/src/clients/feed/interfaces.ts index eeb9a3f41..37221775f 100644 --- a/packages/client/src/clients/feed/interfaces.ts +++ b/packages/client/src/clients/feed/interfaces.ts @@ -65,14 +65,25 @@ export type FetchFeedOptions = { __fetchSource?: "socket" | "http"; } & Omit; -// 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; diff --git a/packages/client/src/clients/feed/utils.ts b/packages/client/src/clients/feed/utils.ts index 9d764c513..c108a58ed 100644 --- a/packages/client/src/clients/feed/utils.ts +++ b/packages/client/src/clients/feed/utils.ts @@ -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 = {}; @@ -22,6 +26,11 @@ 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; @@ -29,7 +38,7 @@ export function mergeDateRangeParams(options: FeedClientOptions) { return rest; } - const dateRangeParams: Record = {}; + const dateRangeParams: DateRangeParams = {}; // Determine which operators to use based on the inclusive flag const isInclusive = inserted_at_date_range.inclusive ?? false;