-
Notifications
You must be signed in to change notification settings - Fork 77
feat: extend sort with field type #1673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2168,32 +2168,37 @@ export type MemberFilters = QueryFilters< | |||||
|
|
||||||
| export type BannedUsersSort = BannedUsersSortBase | Array<BannedUsersSortBase>; | ||||||
|
|
||||||
| export type BannedUsersSortBase = { created_at?: AscDesc }; | ||||||
| export type BannedUsersSortBase = Sort<'created_at'>; | ||||||
|
|
||||||
| export type ReactionSort = ReactionSortBase | Array<ReactionSortBase>; | ||||||
|
|
||||||
| export type ReactionSortBase = Sort<CustomReactionData> & { | ||||||
| created_at?: AscDesc; | ||||||
| }; | ||||||
| export type ReactionSortBase = Sort<CustomReactionData> & Sort<'created_at'>; | ||||||
|
|
||||||
| export type ChannelSort = ChannelSortBase | Array<ChannelSortBase>; | ||||||
|
|
||||||
| export type ChannelSortBase = Sort<CustomChannelData> & { | ||||||
| created_at?: AscDesc; | ||||||
| has_unread?: AscDesc; | ||||||
| last_message_at?: AscDesc; | ||||||
| last_updated?: AscDesc; | ||||||
| member_count?: AscDesc; | ||||||
| pinned_at?: AscDesc; | ||||||
| unread_count?: AscDesc; | ||||||
| updated_at?: AscDesc; | ||||||
| }; | ||||||
| export type ChannelSortBase = Sort<CustomChannelData> & | ||||||
| Sort< | ||||||
| | 'created_at' | ||||||
| | 'has_unread' | ||||||
| | 'last_message_at' | ||||||
| | 'last_updated' | ||||||
| | 'member_count' | ||||||
| | 'pinned_at' | ||||||
| | 'unread_count' | ||||||
| | 'updated_at' | ||||||
| >; | ||||||
|
|
||||||
| export type PinnedMessagesSort = PinnedMessagesSortBase | Array<PinnedMessagesSortBase>; | ||||||
| export type PinnedMessagesSortBase = { pinned_at?: AscDesc }; | ||||||
|
|
||||||
| export type Sort<T> = { | ||||||
| [P in keyof T]?: AscDesc; | ||||||
| export type PinnedMessagesSortBase = Sort<'pinned_at'>; | ||||||
|
|
||||||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||||||
| export type Sort<T extends string | Record<string, any>> = { | ||||||
| [P in T extends string ? T : keyof T]?: | ||||||
| | AscDesc | ||||||
| | { | ||||||
| direction: AscDesc; | ||||||
| type?: 'string' | 'number'; | ||||||
| }; | ||||||
| }; | ||||||
|
|
||||||
| export type UserSort = Sort<UserResponse> | Array<Sort<UserResponse>>; | ||||||
|
|
@@ -2212,51 +2217,40 @@ export type MemberSort = | |||||
| > | ||||||
| >; | ||||||
|
|
||||||
| export type SearchMessageSortBase = Sort<CustomMessageData> & { | ||||||
| attachments?: AscDesc; | ||||||
| 'attachments.type'?: AscDesc; | ||||||
| created_at?: AscDesc; | ||||||
| id?: AscDesc; | ||||||
| 'mentioned_users.id'?: AscDesc; | ||||||
| parent_id?: AscDesc; | ||||||
| pinned?: AscDesc; | ||||||
| relevance?: AscDesc; | ||||||
| reply_count?: AscDesc; | ||||||
| text?: AscDesc; | ||||||
| type?: AscDesc; | ||||||
| updated_at?: AscDesc; | ||||||
| 'user.id'?: AscDesc; | ||||||
| }; | ||||||
| export type SearchMessageSortBase = Sort<CustomMessageData> & | ||||||
| Sort< | ||||||
| | 'attachments' | ||||||
| | 'attachments.type' | ||||||
| | 'created_at' | ||||||
| | 'id' | ||||||
| | 'mentioned_users.id' | ||||||
| | 'parent_id' | ||||||
| | 'pinned' | ||||||
| | 'relevance' | ||||||
| | 'reply_count' | ||||||
| | 'text' | ||||||
| | 'type' | ||||||
| | 'updated_at' | ||||||
| | 'user.id' | ||||||
| >; | ||||||
|
|
||||||
| export type SearchMessageSort = SearchMessageSortBase | Array<SearchMessageSortBase>; | ||||||
|
|
||||||
| export type QuerySort = BannedUsersSort | ChannelSort | SearchMessageSort | UserSort; | ||||||
|
|
||||||
| export type DraftSortBase = { | ||||||
| created_at?: AscDesc; | ||||||
| }; | ||||||
| export type DraftSortBase = Sort<'created_ats'>; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| export type DraftSort = DraftSortBase | Array<DraftSortBase>; | ||||||
|
|
||||||
| export type PollSort = PollSortBase | Array<PollSortBase>; | ||||||
|
|
||||||
| export type PollSortBase = { | ||||||
| created_at?: AscDesc; | ||||||
| id?: AscDesc; | ||||||
| is_closed?: AscDesc; | ||||||
| name?: AscDesc; | ||||||
| updated_at?: AscDesc; | ||||||
| }; | ||||||
| export type PollSortBase = Sort< | ||||||
| 'created_at' | 'id' | 'is_closed' | 'name' | 'updated_at' | ||||||
| >; | ||||||
|
|
||||||
| export type VoteSort = VoteSortBase | Array<VoteSortBase>; | ||||||
|
|
||||||
| export type VoteSortBase = { | ||||||
| created_at?: AscDesc; | ||||||
| id?: AscDesc; | ||||||
| is_closed?: AscDesc; | ||||||
| name?: AscDesc; | ||||||
| updated_at?: AscDesc; | ||||||
| }; | ||||||
| export type VoteSortBase = PollSortBase; | ||||||
|
|
||||||
| /** | ||||||
| * Base Types | ||||||
|
|
@@ -3569,10 +3563,9 @@ export type QueryMessageHistorySort = | |||||
| | QueryMessageHistorySortBase | ||||||
| | Array<QueryMessageHistorySortBase>; | ||||||
|
|
||||||
| export type QueryMessageHistorySortBase = { | ||||||
| message_updated_at?: AscDesc; | ||||||
| message_updated_by_id?: AscDesc; | ||||||
| }; | ||||||
| export type QueryMessageHistorySortBase = Sort< | ||||||
| 'message_updated_at' | 'message_updated_by_id' | ||||||
| >; | ||||||
|
|
||||||
| export type QueryMessageHistoryOptions = Pager; | ||||||
|
|
||||||
|
|
@@ -4321,15 +4314,15 @@ export type LiveLocationPayload = { | |||||
|
|
||||||
| export type ThreadSort = ThreadSortBase | Array<ThreadSortBase>; | ||||||
|
|
||||||
| export type ThreadSortBase = { | ||||||
| active_participant_count?: AscDesc; | ||||||
| created_at?: AscDesc; | ||||||
| last_message_at?: AscDesc; | ||||||
| parent_message_id?: AscDesc; | ||||||
| participant_count?: AscDesc; | ||||||
| reply_count?: AscDesc; | ||||||
| updated_at?: AscDesc; | ||||||
| }; | ||||||
| export type ThreadSortBase = Sort< | ||||||
| | 'active_participant_count' | ||||||
| | 'created_at' | ||||||
| | 'last_message_at' | ||||||
| | 'parent_message_id' | ||||||
| | 'participant_count' | ||||||
| | 'reply_count' | ||||||
| | 'updated_at' | ||||||
| >; | ||||||
|
|
||||||
| export type ThreadFilters = QueryFilters< | ||||||
| { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,7 @@ import type { | |||||
| PromoteChannelParams, | ||||||
| QueryChannelAPIResponse, | ||||||
| ReactionGroupResponse, | ||||||
| Sort, | ||||||
| UpdatedMessage, | ||||||
| UserResponse, | ||||||
| } from './types'; | ||||||
|
|
@@ -131,20 +132,26 @@ export function addFileToFormData( | |||||
|
|
||||||
| return data; | ||||||
| } | ||||||
| export function normalizeQuerySort<T extends Record<string, AscDesc | undefined>>( | ||||||
| sort: T | T[], | ||||||
| ) { | ||||||
| const sortFields: Array<{ direction: AscDesc; field: keyof T }> = []; | ||||||
| const sortArr = Array.isArray(sort) ? sort : [sort]; | ||||||
| for (const item of sortArr) { | ||||||
| const entries = Object.entries(item) as [keyof T, AscDesc][]; | ||||||
| if (entries.length > 1) { | ||||||
| console.warn( | ||||||
| "client._buildSort() - multiple fields in a single sort object detected. Object's field order is not guaranteed", | ||||||
| ); | ||||||
| } | ||||||
| for (const [field, direction] of entries) { | ||||||
| sortFields.push({ field, direction }); | ||||||
|
|
||||||
| export function normalizeQuerySort<T extends Sort<string>>(sort: T | T[]) { | ||||||
| const sortFields: Array<{ direction: AscDesc; field: keyof T; type: string | null }> = | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Not sure, but basing it on this: https://github.com/GetStream/stream-chat-js/pull/1673/changes#r2664806559
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type is just a string in this case as it's supposed to be sent to the BE, TS should not remove this during compile time. In this case it's just generalized to |
||||||
| []; | ||||||
| const sortArray = Array.isArray(sort) ? sort : [sort]; | ||||||
| for (const item of sortArray) { | ||||||
| const entries = Object.entries(item); | ||||||
|
|
||||||
| for (const [field, directionOrObject] of entries) { | ||||||
| if (!directionOrObject) continue; | ||||||
|
|
||||||
| if (typeof directionOrObject === 'number') { | ||||||
| sortFields.push({ field, direction: directionOrObject, type: null }); | ||||||
| } else { | ||||||
| sortFields.push({ | ||||||
| field, | ||||||
| direction: directionOrObject.direction, | ||||||
| type: directionOrObject.type ?? null, | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| return sortFields; | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kanat what types will be supported?