Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

openapi-react-query

openapi-ts168.3kMIT0.5.0TypeScript support: included

Fast, type-safe @tanstack/react-query client to work with your OpenAPI schema.

openapi, swagger, rest, api, oapi_3, oapi_3_1, typescript, fetch, react, react-query, tanstack

readme

openapi-react-query

openapi-react-query is a type-safe tiny wrapper (1 kb) around @tanstack/react-query to work with OpenAPI schema.

It works by using openapi-fetch and openapi-typescript so you get all the following features:

  • ✅ No typos in URLs or params.
  • ✅ All parameters, request bodies, and responses are type-checked and 100% match your schema
  • ✅ No manual typing of your API
  • ✅ Eliminates any types that hide bugs
  • ✅ Eliminates as type overrides that can also hide bugs

Setup

Install this library along with openapi-fetch and openapi-typescript:

npm i openapi-react-query openapi-fetch
npm i -D openapi-typescript typescript

Next, generate TypeScript types from your OpenAPI schema using openapi-typescript:

npx openapi-typescript ./path/to/api/v1.yaml -o ./src/lib/api/v1.d.ts

Usage

Once your types have been generated from your schema, you can create a fetch client, a react-query client and start querying your API.

import createFetchClient from "openapi-fetch";
import createClient from "openapi-react-query";
import type { paths } from "./my-openapi-3-schema"; // generated by openapi-typescript

const fetchClient = createFetchClient<paths>({
  baseUrl: "https://myapi.dev/v1/",
});
const $api = createClient(fetchClient);

const MyComponent = () => {
  const { data, error, isPending } = $api.useQuery(
    "get",
    "/blogposts/{post_id}",
    {
      params: {
        path: { post_id: 5 },
      },
    }
  );

  if (isPending || !data) return "Loading...";

  if (error) return `An error occurred: ${error.message}`;

  return <div>{data.title}</div>;
};

You can find more information about createFetchClient in the openapi-fetch documentation.

📓 Docs

View Docs

changelog

openapi-react-query

0.5.0

Minor Changes

Patch Changes

0.4.2

Patch Changes

0.4.1

Patch Changes

0.4.0

Minor Changes

  • #2169 a76801c Thanks @awmichel! - #2169: Infer returned data type from select option when used with the useInfiniteQuery method.

0.3.2

Patch Changes

0.3.1

Patch Changes

0.3.0

Minor Changes

Patch Changes

0.2.10

Patch Changes

0.2.9

Patch Changes

0.2.8

Patch Changes

Refresh of 0.2.7; corrupted package

0.2.7

Patch Changes

0.2.6

Patch Changes

0.2.5

Patch Changes

  • #1975 621792f Thanks @HugeLetters! - - Fixed empty value check in queryFn: only throws error for undefined, other falsy values are allowed
    • Fixed empty value check in mutationFn: allow falsy values

0.2.4

Patch Changes

0.2.3

Patch Changes

0.2.2

Patch Changes

0.2.1

Patch Changes

0.2.0

Minor Changes

  • #1858 29bd162 Thanks @zsugabubus! - Introduce queryOptions that can be used as a building block to integrate with useQueries/fetchQueries/prefetchQueries… etc.

0.1.7

Patch Changes

0.1.6

Patch Changes

0.1.5

Patch Changes

0.1.4

Patch Changes

0.1.3

Patch Changes

0.1.2

Patch Changes

0.1.1

Patch Changes

0.1.0

Minor Changes

  • #1814 2a044d0 Thanks @yoshi2no! - feat: Allow passing a queryClient as an argument to the useQuery, useMutation, and useSuspenseQuery methods

Patch Changes

0.0.3

Patch Changes

0.0.2

Patch Changes

0.0.1

Patch Changes