Skip to content

TanStack Query: need last-modified from response header #2070

@topical

Description

@topical

Description

Hi,

I use the TanStack Query code generator and need access to response header.

In my scenario, REST-API requires 'If-Unmodified-Since' header to be passed to properly handle concurrent updates.

For this, I need to extract 'last-modified' from response headers. Actually, this looks like being almost impossible with code generated by HeyAPI.

E.g. I have an endpoint to retrieve information about a user. HeyAPI generates the following code for this:

export const getUserQueryQueryKey = (options?: OptionsLegacyParser<GetUserQueryData>) => [
  createQueryKey('getUserQuery', options),
]

export const getUserQueryOptions = (options?: OptionsLegacyParser<GetUserQueryData>) => {
  return queryOptions({
    queryFn: async ({ queryKey, signal }) => {
      // watch out: response header is discarded here!
      const { data } = await getUserQuery({
        ...options,
        ...queryKey[0],
        signal,
        throwOnError: true,
      })
      return data
    },
    queryKey: getUserQueryQueryKey(options),
  })
}

You see that the response header is discarded deeply inside.

Effectively, I have to rewrite getUserQueryQueryKey() and getUserQueryOptions(). That's kind of OK for a single API-endpoint, but not suitable for my application.

So: is there any idea to handle this more generic?

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions