-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create a react query hook plugin (#63)
- Loading branch information
1 parent
41d6e62
commit 764bd70
Showing
99 changed files
with
3,382 additions
and
372 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
examples/src/ | ||
lib/ | ||
|
||
packages/typoas-react-query/src/__tests__/sample-client.ts |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-9.07 KB
.yarn/cache/@babel-helper-validator-identifier-npm-7.16.7-8599fb00fc-42b9b56c35.zip
Binary file not shown.
Binary file added
BIN
+17.6 KB
.yarn/cache/@babel-helper-validator-identifier-npm-7.24.5-d1938535fe-38aaf6a64a.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+319 KB
.yarn/cache/@tanstack-react-query-npm-5.35.5-0fb0467ba0-b3c5c5528c.zip
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+757 KB
.yarn/cache/@testing-library-react-npm-15.0.7-fc23780217-a9342ad09b.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+89.3 KB
.yarn/cache/dom-accessibility-api-npm-0.5.16-d3e2310666-377b4a7f9e.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+4.85 KB
.yarn/cache/html-encoding-sniffer-npm-3.0.0-daac3dfe41-707a812ec2.zip
Binary file not shown.
Binary file added
BIN
+2.88 KB
.yarn/cache/is-potential-custom-element-name-npm-1.0.1-f352f606f8-ced7bbbb64.zip
Binary file not shown.
Binary file added
BIN
+4.55 KB
.yarn/cache/jest-environment-jsdom-npm-29.7.0-0b72dd0e0b-23bbfc9bca.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file renamed
BIN
+12.4 KB
...ycode-npm-2.3.0-df4bdce06b-d4e7fbb96f.zip → ...ycode-npm-2.3.1-97543c420d-febdc4362b.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
595 changes: 298 additions & 297 deletions
595
.yarn/releases/yarn-4.1.0.cjs → .yarn/releases/yarn-4.2.2.cjs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,3 @@ | ||
releases: | ||
"@typoas/react-query": major | ||
"@typoas/runtime": patch |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -20,5 +20,5 @@ | |
"prettier": "^3.2.5", | ||
"typescript": "^5.3.3" | ||
}, | ||
"packageManager": "yarn@4.1.0" | ||
"packageManager": "yarn@4.2.2" | ||
} |
This file contains 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,169 @@ | ||
# `@typoas/react-query` | ||
|
||
> This package is related to the [_Typoas_](https://github.com/Embraser01/typoas) project. | ||
This package provides a set of hook factories to interact with a generated _Typoas_ client in a React application with [`react-query`](https://tanstack.com/query/latest). | ||
|
||
## Install | ||
|
||
This dependency should be added into your dependencies. | ||
|
||
```shell | ||
# @typoas/react-query depends on @tanstack/react-query and @typoas/runtime (peer dependencies) | ||
yarn add @typoas/react-query @tanstack/react-query @typoas/runtime | ||
``` | ||
|
||
## Usage | ||
|
||
### Configuration | ||
|
||
First, you will need to add the `ApiContextProvider` to your react application: | ||
|
||
```tsx | ||
import { ApiContextProvider } from '@typoas/react-query'; | ||
import { createClient } from './generated/client'; | ||
|
||
const client = createClient(); | ||
|
||
export function App() { | ||
return ( | ||
<ApiContextProvider client={client}> | ||
<MyComponent /> | ||
</ApiContextProvider> | ||
); | ||
} | ||
``` | ||
|
||
Once the `ApiContextProvider` is added, you can use one of the hook factories to interact with your generated client: | ||
|
||
- [`createQueryHook`](#createqueryhook) | ||
- [`createInfiniteQueryHook`](#createinfinitequeryhook) | ||
- [`createMutationHook`](#createmutationhook) | ||
|
||
### `createQueryHook` | ||
|
||
```tsx | ||
import { createQueryHook } from '@typoas/react-query'; | ||
import { findPetsByStatus } from './generated/client'; | ||
|
||
const useFindPetsByStatus = createQueryHook(findPetsByStatus, { | ||
// Base options used for all queries created with this hook | ||
}); | ||
|
||
export function MyComponent() { | ||
const { data, isLoading } = useFindPetsByStatus({ | ||
// These options are typed based on the operation's parameters | ||
// All react-query options are supported except for 'queryFn' | ||
queryKey: [{ status: 'available' }], | ||
}); | ||
|
||
if (isLoading) { | ||
return <div>Loading...</div>; | ||
} | ||
|
||
return ( | ||
<ul> | ||
{data.map((pet) => ( | ||
<li key={pet.id}>{pet.name}</li> | ||
))} | ||
</ul> | ||
); | ||
} | ||
``` | ||
|
||
### `createInfiniteQueryHook` | ||
|
||
Similar as the `createQueryHook`, supporting infinite queries. | ||
|
||
```tsx | ||
import { createQueryHook } from '@typoas/react-query'; | ||
import { findPetsByStatus } from './generated/client'; | ||
|
||
const useFindPetsByStatus = createInfiniteQueryHook(findPetsByStatus, { | ||
initialPageParam: { page: 1 }, | ||
getNextPageParam: (lastPage, allPages) => { | ||
if (allPages.length === 1) { | ||
return { page: 2 }; | ||
} | ||
return undefined; | ||
}, | ||
}); | ||
|
||
export function MyComponent() { | ||
const { data, isLoading } = useFindPetsByStatus({ | ||
// These options are typed based on the operation's parameters | ||
// All react-query options are supported except for 'queryFn' | ||
queryKey: [{ status: 'available' }], | ||
}); | ||
|
||
if (isLoading) { | ||
return <div>Loading...</div>; | ||
} | ||
|
||
return ( | ||
<ul> | ||
{data.pages.map((page) => | ||
page.map((pet) => <li key={pet.id}>{pet.name}</li>), | ||
)} | ||
</ul> | ||
); | ||
} | ||
``` | ||
|
||
### `createMutationHook` | ||
|
||
Mutation are also supported: | ||
|
||
```tsx | ||
import { useCallback } from 'react'; | ||
import { createQueryHook } from '@typoas/react-query'; | ||
import { addPet } from './generated/client'; | ||
|
||
const useAddPetMutation = createMutationHook(addPet, {}); | ||
|
||
export function MyComponent() { | ||
const mutation = useAddPetMutation({}); | ||
|
||
const onClick = useCallback(() => { | ||
mutation.mutate([ | ||
{}, // Path params | ||
{ id: 1, name: 'Rufus', status: 'available', photoUrls: [] }, // Body | ||
]); | ||
}, [mutation.mutate]); | ||
|
||
return <button onClick={onClick}></button>; | ||
} | ||
``` | ||
|
||
## Notes | ||
|
||
Each hook factory accepts a second argument which is the base options for the query/mutation. This also allows 2 additional options: | ||
|
||
- `fetcherData`: Object to be passed to the _Typoas_ fetcher | ||
- `successStatus`: Status code to consider the request as successful (default: 2XX, same as the `ok` function in `@typoas/runtime`) | ||
|
||
Note that `fetcherData` can also be passed in the hook options (will override the base one if provided). | ||
|
||
If you need, you can also use the `useApiContext` hook to access the client directly: | ||
|
||
```tsx | ||
import { useEffect } from 'react'; | ||
import { useApiContext } from '@typoas/react-query'; | ||
import { addPet } from './generated/client'; | ||
|
||
// Must be used as a child of the ApiContextProvider | ||
export function MyComponent() { | ||
const ctx = useApiContext(); | ||
|
||
// Use the client | ||
useEffect(() => { | ||
addPet( | ||
ctx, | ||
{}, | ||
{ id: 1, name: 'Rufus', status: 'available', photoUrls: [] }, | ||
); | ||
}, []); | ||
|
||
return <div></div>; | ||
} | ||
``` |
This file contains 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,6 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'jsdom', | ||
testMatch: ['**/__tests__/**/*.spec.[jt]s?(x)'], | ||
testPathIgnorePatterns: ['/node_modules/', '/lib/'], | ||
}; |
This file contains 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,45 @@ | ||
{ | ||
"name": "@typoas/react-query", | ||
"version": "3.0.0", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/embraser01/typoas", | ||
"directory": "packages/typoas-react-query" | ||
}, | ||
"main": "./src/index.ts", | ||
"scripts": { | ||
"prepack": "tsc", | ||
"start": "ts-node ./src/bin.ts", | ||
"test:types": "tsc --noEmit", | ||
"test": "jest" | ||
}, | ||
"devDependencies": { | ||
"@jest/globals": "^29.7.0", | ||
"@tanstack/react-query": "^5.35.5", | ||
"@testing-library/react": "^15.0.7", | ||
"@types/node": "^20.11.24", | ||
"@types/react": "^18.3.2", | ||
"@types/react-dom": "^18.3.0", | ||
"@typoas/runtime": "workspace:^", | ||
"jest": "^29.7.0", | ||
"jest-environment-jsdom": "^29.7.0", | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1", | ||
"ts-jest": "^29.1.2", | ||
"typescript": "^5.3.3" | ||
}, | ||
"peerDependencies": { | ||
"@tanstack/react-query": "^5.35.5", | ||
"@typoas/runtime": "workspace:^" | ||
}, | ||
"files": [ | ||
"/lib/**/*.js", | ||
"/lib/**/*.d.ts", | ||
"!/lib/**/__tests__/*" | ||
], | ||
"publishConfig": { | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts" | ||
} | ||
} |
Oops, something went wrong.