Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
49 changes: 44 additions & 5 deletions packages/react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,52 @@ export type QueryClient<TApiRouter extends FlatApiRouter> =
}
: never

function isEmptyObject(obj: any): boolean {
return (
obj != null && typeof obj === 'object' && !Array.isArray(obj) && Object.keys(obj).length === 0
)
}

function sortObjectDeep(obj: any): any {
if (obj == null || typeof obj !== 'object' || Array.isArray(obj)) {
return obj
}

return Object.keys(obj)
.sort()
.reduce((result, key) => {
result[key] = sortObjectDeep(obj[key])
return result
}, {} as any)
}

function normalizePayload(payload?: any): any {
if (!payload) return undefined

const normalized = {
pathParams: payload.pathParams,
query: payload.query,
headers: payload.headers,
}

const filtered = Object.entries(normalized).reduce((acc, [key, value]) => {
if (value != null && !isEmptyObject(value)) {
acc[key] = sortObjectDeep(value)
}
return acc
}, {} as any)

return Object.keys(filtered).length > 0 ? filtered : undefined
}

export function queryKey(method: string, path: string | number | symbol, payload?: any) {
const payloadKey = {
pathParams: payload?.pathParams ?? {},
query: payload?.query ?? {},
headers: payload?.headers ?? {},
const normalizedPayload = normalizePayload(payload)

if (normalizedPayload) {
return [method, path, normalizedPayload] as const
}
return [method, path, payloadKey] as const

return [method, path] as const
}

export function createQueryClient<TApp extends GensekiAppCompiled>(
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/react/views/collections/list/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ function _CollectionListProvider<T extends BaseData>(props: CollectionListProvid
const queryClient = useQueryClient()
const query = useCollectionListQuery({ slug: context.slug })

const invalidateList = async (page?: number) => {
const additionalKeys = page ? [{ query: { page } }] : []
const invalidateList = async () => {
await queryClient.invalidateQueries({
queryKey: ['GET', `/${context.slug}`, ...additionalKeys],
queryKey: query.queryKey,
exact: false,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ export function useCollectionListQuery(
search: args.search ?? search,
sort: sort,
}
const fullQueryKey = ['GET', `/${args.slug}`, { query: queryKey }] as const

const query: UseQueryResult<CollectionListResponse> = useQuery({
queryKey: ['GET', `/${args.slug}`, { query: queryKey }] as const,
queryKey: fullQueryKey,
queryFn: async (context) => {
const [, , payload] = context.queryKey
const params = new URLSearchParams([
Expand Down Expand Up @@ -50,5 +51,5 @@ export function useCollectionListQuery(
placeholderData: keepPreviousData,
})

return query
return { ...query, queryKey: fullQueryKey }
}
Loading