-
-
Notifications
You must be signed in to change notification settings - Fork 540
openapi-react-query #1717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
openapi-react-query #1717
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
92dd0ef
Create base packages openapi-tanstack-query
kerwanp e3781fd
Update openapi-react-query library naming, tags and dependencies
kerwanp 94a03e9
Migrate current react-query impl to typescript
kerwanp 19e9cc0
Migrate current react-query impl to typescript
kerwanp eb39931
Fix openapi-react-query builds scripts
kerwanp 2b69f32
Add useSuspenseQuery impl
kerwanp d6a1198
Write useQuery tests
kerwanp 7ce75e0
Write useSuspenseQuery and useMutation tests
kerwanp 281d82f
Write useSuspenseQuery and useMutation tests
kerwanp fa6c3c4
Write documentation for openapi-react-query
kerwanp 36d7b2c
Update openapi-react-query dependencies
kerwanp 349193b
Fix openapi-react-query devDependencies for types gen
kerwanp 9cb791c
Add openapi-react-query CONTRIBUTING.md and README.md
kerwanp 81f3442
Add openapi-react-query LICENSE
kerwanp d91b4db
Fix typo in fixtures mock-server
kerwanp 7e8e417
Update tsconfigs moduleResolution to Bundler and jsx to react-jsx
kerwanp 13e9d36
Add config files to npmignores
kerwanp 46911de
Revert openapi-typescript moduleResolution
kerwanp 966d72a
Fix exports cjs path
kerwanp 335530c
write changesets
kerwanp 28f687f
Fix documentation mistakes
kerwanp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"openapi-fetch": patch | ||
"openapi-typescript": patch | ||
"openapi-typescript-helpers": patch | ||
--- | ||
|
||
Ignore configuration files in published package |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"openapi-fetch": patch | ||
--- | ||
|
||
Create own client type for easier reusability |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"openapi-react-query": patch | ||
--- | ||
|
||
Initial release |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
title: About openapi-react-query | ||
description: openapi-react-query Project Goals and contributors | ||
--- | ||
<script setup> | ||
import { VPTeamMembers } from 'vitepress/theme'; | ||
import contributors from '../data/contributors.json'; | ||
</script> | ||
|
||
# About | ||
|
||
## Project Goals | ||
|
||
1. Types should be strict and inferred automatically from OpenAPI schemas with the absolute minimum number of generics needed. | ||
2. Respect the original `@tanstack/react-query` APIs while reducing boilerplate. | ||
3. Be as light and performant as possible. | ||
|
||
## Contributors | ||
|
||
This library wouldn’t be possible without all these amazing contributors: | ||
|
||
<VPTeamMembers size="small" :members="contributors['openapi-react-query']" /> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
--- | ||
title: openapi-react-query | ||
--- | ||
# Introduction | ||
|
||
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. | ||
|
||
It works by using [openapi-fetch](../openapi-fetch/) and [openapi-typescript](../introduction) 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 | ||
- ✅ Also eliminates `as` type overrides that can also hide bugs | ||
|
||
::: code-group | ||
|
||
```tsx [src/my-component.ts] | ||
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, isLoading } = $api.useQuery( | ||
"get", | ||
"/blogposts/{post_id}", | ||
{ | ||
params: { | ||
path: { post_id: 5 }, | ||
}, | ||
}, | ||
); | ||
|
||
if (isLoading || !data) return "Loading..."; | ||
|
||
if (error) return `An error occured: ${error.message}`; | ||
|
||
return <div>{data.title}</div>; | ||
}; | ||
``` | ||
|
||
::: | ||
|
||
## Setup | ||
|
||
Install this library along with [openapi-fetch](../openapi-fetch/) and [openapi-typescript](../introduction): | ||
|
||
```bash | ||
npm i openapi-react-query openapi-fetch | ||
npm i -D openapi-typescript typescript | ||
``` | ||
|
||
::: tip Highly recommended | ||
|
||
Enable [noUncheckedIndexedAccess](https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess) in your `tsconfig.json` ([docs](/advanced#enable-nouncheckedindexaccess-in-your-tsconfigjson)) | ||
|
||
::: | ||
|
||
Next, generate TypeScript types from your OpenAPI schema using openapi-typescript: | ||
|
||
```bash | ||
npx openapi-typescript ./path/to/api/v1.yaml -o ./src/lib/api/v1.d.ts | ||
``` | ||
|
||
## Basic usage | ||
|
||
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. | ||
|
||
::: code-group | ||
|
||
```tsx [src/my-component.ts] | ||
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, isLoading } = $api.useQuery( | ||
"get", | ||
"/blogposts/{post_id}", | ||
{ | ||
params: { | ||
path: { post_id: 5 }, | ||
}, | ||
}, | ||
); | ||
|
||
if (isLoading || !data) return "Loading..."; | ||
|
||
if (error) return `An error occured: ${error.message}`; | ||
|
||
return <div>{data.title}</div>; | ||
}; | ||
``` | ||
|
||
::: | ||
|
||
::: tip | ||
You can find more information about `createFetchClient` on the [openapi-fetch documentation](../openapi-fetch/index.md). | ||
::: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
title: useQuery | ||
kerwanp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--- | ||
# {{ $frontmatter.title }} | ||
|
||
The `useMutation` method allows you to use the original [useMutation](https://tanstack.com/query/latest/docs/framework/react/guides/queries). | ||
kerwanp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- The result is the same as the original function. | ||
- The `mutationKey` is `[method, path]`. | ||
- `data` and `error` are fully typed. | ||
|
||
::: tip | ||
You can find more information about `useMutation` on the [@tanstack/react-query documentation](https://tanstack.com/query/latest/docs/framework/react/guides/mutations). | ||
::: | ||
|
||
## Example | ||
|
||
::: code-group | ||
|
||
```tsx [src/app.tsx] | ||
import { $api } from "./api"; | ||
|
||
export const App = () => { | ||
const { mutate } = $api.useMutation("patch", "/users"); | ||
|
||
return ( | ||
<button onClick={() => mutate({ body: { firstname: "John" } })}> | ||
Update | ||
</button> | ||
); | ||
}; | ||
``` | ||
|
||
```ts [src/api.ts] | ||
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/", | ||
}); | ||
export const $api = createClient(fetchClient); | ||
``` | ||
|
||
::: | ||
|
||
## Api | ||
|
||
```tsx | ||
const query = $api.useQuery(method, path, options, queryOptions); | ||
``` | ||
|
||
**Arguments** | ||
|
||
- `method` **(required)** | ||
- The HTTP method to use for the request. | ||
- The method is used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. | ||
- `path` **(required)** | ||
- The pathname to use for the request. | ||
- Must be an available path for the given method in your schema. | ||
- The pathname is used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. | ||
- `queryOptions` | ||
- The original `useMutation` options. | ||
- [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useMutation) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
title: useQuery | ||
--- | ||
# {{ $frontmatter.title }} | ||
|
||
The `useQuery` method allows you to use the original [useQuery](https://tanstack.com/query/latest/docs/framework/react/guides/queries). | ||
|
||
- The result is the same as the original function. | ||
- The `functionKey` is `[method, path, params]`. | ||
- `data` and `error` are fully typed. | ||
- You can pass queries options as fourth parameter. | ||
|
||
::: tip | ||
You can find more information about `useQuery` on the [@tanstack/react-query documentation](https://tanstack.com/query/latest/docs/framework/react/guides/queries). | ||
::: | ||
|
||
## Example | ||
|
||
::: code-group | ||
|
||
```tsx [src/app.tsx] | ||
import { $api } from "./api"; | ||
|
||
export const App = () => { | ||
const { data, error, isLoading } = $api.useQuery("get", "/users/{user_id}", { | ||
params: { | ||
path: { user_id: 5 }, | ||
}, | ||
}); | ||
|
||
if (!data || isLoading) return "Loading..."; | ||
if (error) return `An error occured: ${error.message}`; | ||
|
||
return <div>{data.firstname}</div>; | ||
}; | ||
``` | ||
|
||
```ts [src/api.ts] | ||
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/", | ||
}); | ||
export const $api = createClient(fetchClient); | ||
``` | ||
|
||
::: | ||
|
||
## Api | ||
|
||
```tsx | ||
const query = $api.useQuery(method, path, options, queryOptions); | ||
``` | ||
|
||
**Arguments** | ||
|
||
- `method` **(required)** | ||
- The HTTP method to use for the request. | ||
- The method is used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. | ||
- `path` **(required)** | ||
- The pathname to use for the request. | ||
- Must be an available path for the given method in your schema. | ||
- The pathname is used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. | ||
- `options` | ||
- The fetch options to use for the request. | ||
- Only required if the OpenApi schema requires parameters. | ||
- The options `params` are used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. | ||
- `queryOptions` | ||
- The original `useQuery` options. | ||
- [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
title: useSuspenseQuery | ||
--- | ||
# {{ $frontmatter.title }} | ||
|
||
The `useSuspenseQuery` method allows you to use the original [useSuspenseQuery](https://tanstack.com/query/latest/docs/framework/react/guides/suspense). | ||
|
||
- The result is the same as the original function. | ||
- The `functionKey` is `[method, path, params]`. | ||
- `data` and `error` are fully typed. | ||
- You can pass queries options as fourth parameter. | ||
|
||
::: tip | ||
You can find more information about `useSuspenseQuery` on the [@tanstack/react-query documentation](https://tanstack.com/query/latest/docs/framework/react/guides/suspense). | ||
::: | ||
|
||
## Example | ||
|
||
::: code-group | ||
|
||
```tsx [src/app.tsx] | ||
import { ErrorBoundary } from "react-error-boundary"; | ||
import { $api } from "./api"; | ||
|
||
const MyComponent = () => { | ||
const { data } = $api.useSuspenseQuery("get", "/users/{user_id}", { | ||
params: { | ||
path: { user_id: 5 }, | ||
}, | ||
}); | ||
|
||
return <div>{data.firstname}</div>; | ||
}; | ||
|
||
export const App = () => { | ||
return ( | ||
<ErrorBoundary fallbackRender={({ error }) => `Error: ${error.message}`}> | ||
<MyComponent /> | ||
</ErrorBoundary> | ||
); | ||
}; | ||
``` | ||
|
||
```ts [src/api.ts] | ||
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/", | ||
}); | ||
export const $api = createClient(fetchClient); | ||
``` | ||
|
||
::: | ||
|
||
## Api | ||
|
||
```tsx | ||
const query = $api.useSuspenseQuery(method, path, options, queryOptions); | ||
``` | ||
|
||
**Arguments** | ||
|
||
- `method` **(required)** | ||
- The HTTP method to use for the request. | ||
- The method is used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. | ||
- `path` **(required)** | ||
- The pathname to use for the request. | ||
- Must be an available path for the given method in your schema. | ||
- The pathname is used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. | ||
- `options` | ||
- The fetch options to use for the request. | ||
- Only required if the OpenApi schema requires parameters. | ||
- The options `params` are used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. | ||
- `queryOptions` | ||
- The original `useSuspenseQuery` options. | ||
- [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useSuspenseQuery) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.