Skip to content

Get response headers #2438

@moecasts

Description

@moecasts

Description

Sometimes, the pagination info is given in resoponse headers such as wordpress rest api.

Proposal

We can customize the fetcher function to access response headers directly:

const { data, error, isLoading, isValidating, mutate } = useQuery(
  path,
  init,
  {
    fetcher: async (path, init) => {
      const res = await wpClient.GET(path, init);
      
      if (res.error) {
        throw res.error;
      }
      
      // Access response headers here
      const totalCount = res.response.headers.get('x-wp-total');
      
      return res.data;
    },
  },
);

Alternatively, we could enhance the hook to return response headers directly:

const { data, headers, error, isLoading, isValidating, mutate } = useQuery(
  path,
  init,
  config,
);

The useInfinite hook should also support response header access for consistency.

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestswr-openapiRelevant to swr-openapi library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions