|
| 1 | +--- |
| 2 | +title: openapi-react-query |
| 3 | +--- |
| 4 | +# Introduction |
| 5 | + |
| 6 | +openapi-react-query is a type-safe tiny wrapper (1 kb) around [@tanstack/react-query](https://tanstack.com/query/latest/docs/framework/react/overview) to work with OpenAPI schema. |
| 7 | + |
| 8 | +It works by using [openapi-fetch](../openapi-fetch/) and [openapi-typescript](../introduction) so you get all the following features: |
| 9 | + |
| 10 | +- ✅ No typos in URLs or params. |
| 11 | +- ✅ All parameters, request bodies, and responses are type-checked and 100% match your schema |
| 12 | +- ✅ No manual typing of your API |
| 13 | +- ✅ Eliminates `any` types that hide bugs |
| 14 | +- ✅ Also eliminates `as` type overrides that can also hide bugs |
| 15 | + |
| 16 | +::: code-group |
| 17 | + |
| 18 | +```tsx [src/my-component.ts] |
| 19 | +import createFetchClient from "openapi-fetch"; |
| 20 | +import createClient from "openapi-react-query"; |
| 21 | +import type { paths } from "./my-openapi-3-schema"; // generated by openapi-typescript |
| 22 | + |
| 23 | +const fetchClient = createFetchClient<paths>({ |
| 24 | + baseUrl: "https://myapi.dev/v1/", |
| 25 | +}); |
| 26 | +const $api = createClient(fetchClient); |
| 27 | + |
| 28 | +const MyComponent = () => { |
| 29 | + const { data, error, isLoading } = $api.useQuery( |
| 30 | + "get", |
| 31 | + "/blogposts/{post_id}", |
| 32 | + { |
| 33 | + params: { |
| 34 | + path: { post_id: 5 }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + ); |
| 38 | + |
| 39 | + if (isLoading || !data) return "Loading..."; |
| 40 | + |
| 41 | + if (error) return `An error occured: ${error.message}`; |
| 42 | + |
| 43 | + return <div>{data.title}</div>; |
| 44 | +}; |
| 45 | +``` |
| 46 | + |
| 47 | +::: |
| 48 | + |
| 49 | +## Setup |
| 50 | + |
| 51 | +Install this library along with [openapi-fetch](../openapi-fetch/) and [openapi-typescript](../introduction): |
| 52 | + |
| 53 | +```bash |
| 54 | +npm i openapi-react-query openapi-fetch |
| 55 | +npm i -D openapi-typescript typescript |
| 56 | +``` |
| 57 | + |
| 58 | +::: tip Highly recommended |
| 59 | + |
| 60 | +Enable [noUncheckedIndexedAccess](https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess) in your `tsconfig.json` ([docs](/advanced#enable-nouncheckedindexaccess-in-your-tsconfigjson)) |
| 61 | + |
| 62 | +::: |
| 63 | + |
| 64 | +Next, generate TypeScript types from your OpenAPI schema using openapi-typescript: |
| 65 | + |
| 66 | +```bash |
| 67 | +npx openapi-typescript ./path/to/api/v1.yaml -o ./src/lib/api/v1.d.ts |
| 68 | +``` |
| 69 | + |
| 70 | +## Basic usage |
| 71 | + |
| 72 | +Once your types has been generated from your schema, you can create a [fetch client](../introduction.md), a react-query client and start querying your API. |
| 73 | + |
| 74 | +::: code-group |
| 75 | + |
| 76 | +```tsx [src/my-component.ts] |
| 77 | +import createFetchClient from "openapi-fetch"; |
| 78 | +import createClient from "openapi-react-query"; |
| 79 | +import type { paths } from "./my-openapi-3-schema"; // generated by openapi-typescript |
| 80 | + |
| 81 | +const fetchClient = createFetchClient<paths>({ |
| 82 | + baseUrl: "https://myapi.dev/v1/", |
| 83 | +}); |
| 84 | +const $api = createClient(fetchClient); |
| 85 | + |
| 86 | +const MyComponent = () => { |
| 87 | + const { data, error, isLoading } = $api.useQuery( |
| 88 | + "get", |
| 89 | + "/blogposts/{post_id}", |
| 90 | + { |
| 91 | + params: { |
| 92 | + path: { post_id: 5 }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + ); |
| 96 | + |
| 97 | + if (isLoading || !data) return "Loading..."; |
| 98 | + |
| 99 | + if (error) return `An error occured: ${error.message}`; |
| 100 | + |
| 101 | + return <div>{data.title}</div>; |
| 102 | +}; |
| 103 | +``` |
| 104 | + |
| 105 | +::: |
| 106 | + |
| 107 | +::: tip |
| 108 | +You can find more information about `createFetchClient` on the [openapi-fetch documentation](../openapi-fetch/index.md). |
| 109 | +::: |
| 110 | + |
0 commit comments