Skip to content
Merged
Changes from 1 commit
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
Loading