|
| 1 | +--- |
| 2 | +title: swr-openapi |
| 3 | +--- |
| 4 | + |
| 5 | +# Introduction |
| 6 | + |
| 7 | +swr-openapi is a type-safe wrapper around [`swr`](https://swr.vercel.app). |
| 8 | + |
| 9 | +It works by using [openapi-fetch](../openapi-fetch/) and [openapi-typescript](../introduction) so you get all the following features: |
| 10 | + |
| 11 | +- ✅ No typos in URLs or params. |
| 12 | +- ✅ All parameters, request bodies, and responses are type-checked and 100% match your schema |
| 13 | +- ✅ No manual typing of your API |
| 14 | +- ✅ Eliminates `any` types that hide bugs |
| 15 | +- ✅ Also eliminates `as` type overrides that can also hide bugs |
| 16 | + |
| 17 | +::: code-group |
| 18 | + |
| 19 | +```tsx [src/my-component.ts] |
| 20 | +import createClient from "openapi-fetch"; |
| 21 | +import { createQueryHook } from "swr-openapi"; |
| 22 | +import type { paths } from "./my-openapi-3-schema"; // generated by openapi-typescript |
| 23 | + |
| 24 | +const client = createClient<paths>({ |
| 25 | + baseUrl: "https://myapi.dev/v1/", |
| 26 | +}); |
| 27 | + |
| 28 | +const useQuery = createQueryHook(client, "my-api"); |
| 29 | + |
| 30 | +function MyComponent() { |
| 31 | + const { data, error, isLoading, isValidating, mutate } = useQuery( |
| 32 | + "/blogposts/{post_id}", |
| 33 | + { |
| 34 | + params: { |
| 35 | + path: { post_id: "123" }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + ); |
| 39 | + |
| 40 | + if (isLoading || !data) return "Loading..."; |
| 41 | + |
| 42 | + if (error) return `An error occured: ${error.message}`; |
| 43 | + |
| 44 | + return <div>{data.title}</div>; |
| 45 | +} |
| 46 | + |
| 47 | +``` |
| 48 | + |
| 49 | +::: |
| 50 | + |
| 51 | +## Setup |
| 52 | + |
| 53 | +Install this library along with [openapi-fetch](../openapi-fetch/) and [openapi-typescript](../introduction): |
| 54 | + |
| 55 | +```bash |
| 56 | +npm i swr-openapi openapi-fetch |
| 57 | +npm i -D openapi-typescript typescript |
| 58 | +``` |
| 59 | + |
| 60 | +::: tip Highly recommended |
| 61 | + |
| 62 | +Enable [noUncheckedIndexedAccess](https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess) in your `tsconfig.json` ([docs](../advanced#enable-nouncheckedindexedaccess-in-tsconfig)) |
| 63 | + |
| 64 | +::: |
| 65 | + |
| 66 | +Next, generate TypeScript types from your OpenAPI schema using openapi-typescript: |
| 67 | + |
| 68 | +```bash |
| 69 | +npx openapi-typescript ./path/to/api/v1.yaml -o ./src/lib/api/v1.d.ts |
| 70 | +``` |
| 71 | + |
| 72 | +## Basic usage |
| 73 | + |
| 74 | +Once types have been generated from your schema, you can create a [fetch client](../introduction.md) and export wrapped `swr` hooks. |
| 75 | + |
| 76 | + |
| 77 | +Wrapper hooks are provided 1:1 for each hook exported by SWR. Check out the other sections of this documentation to learn more about each one. |
| 78 | + |
| 79 | +::: code-group |
| 80 | + |
| 81 | +```ts [src/my-api.ts] |
| 82 | +import createClient from "openapi-fetch"; |
| 83 | +import { |
| 84 | + createQueryHook, |
| 85 | + createImmutableHook, |
| 86 | + createInfiniteHook, |
| 87 | + createMutateHook, |
| 88 | +} from "swr-openapi"; |
| 89 | +import { isMatch } from "lodash-es"; |
| 90 | +import type { paths } from "./my-openapi-3-schema"; // generated by openapi-typescript |
| 91 | + |
| 92 | +const client = createClient<paths>({ |
| 93 | + baseUrl: "https://myapi.dev/v1/", |
| 94 | +}); |
| 95 | +const prefix = "my-api"; |
| 96 | + |
| 97 | +export const useQuery = createQueryHook(client, prefix); |
| 98 | +export const useImmutable = createImmutableHook(client, prefix); |
| 99 | +export const useInfinite = createInfiniteHook(client, prefix); |
| 100 | +export const useMutate = createMutateHook( |
| 101 | + client, |
| 102 | + prefix, |
| 103 | + isMatch, // Or any comparision function |
| 104 | +); |
| 105 | +``` |
| 106 | +::: |
| 107 | + |
| 108 | +::: tip |
| 109 | +You can find more information about `createClient` on the [openapi-fetch documentation](../openapi-fetch/index.md). |
| 110 | +::: |
| 111 | + |
| 112 | + |
| 113 | +Then, import these hooks in your components: |
| 114 | + |
| 115 | +::: code-group |
| 116 | +```tsx [src/my-component.tsx] |
| 117 | +import { useQuery } from "./my-api"; |
| 118 | + |
| 119 | +function MyComponent() { |
| 120 | + const { data, error, isLoading, isValidating, mutate } = useQuery( |
| 121 | + "/blogposts/{post_id}", |
| 122 | + { |
| 123 | + params: { |
| 124 | + path: { post_id: "123" }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + ); |
| 128 | + |
| 129 | + if (isLoading || !data) return "Loading..."; |
| 130 | + |
| 131 | + if (error) return `An error occured: ${error.message}`; |
| 132 | + |
| 133 | + return <div>{data.title}</div>; |
| 134 | +} |
| 135 | +``` |
| 136 | +::: |
0 commit comments