Skip to content

feat(openapi-react-query): Add the ability to bring queryClient as argument #1814

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 9 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-jobs-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-react-query": minor
---

feat: Allow passing a queryClient as an argument to the `useQuery`, `useMutation`, and `useSuspenseQuery` methods
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Lives at [https://openapi-ts.dev](https://openapi-ts.dev).
Setup requires the latest version of [Node.js](https://nodejs.org/en) and [pnpm](https://pnpm.io/). With both installed, run:

```
pnpm 1
pnpm i
pnpm run dev
```

Expand Down
2 changes: 1 addition & 1 deletion docs/data/contributors.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions docs/openapi-react-query/use-mutation.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const $api = createClient(fetchClient);
## Api

```tsx
const query = $api.useQuery(method, path, options, queryOptions);
const query = $api.useQuery(method, path, options, queryOptions, queryClient);
```

**Arguments**
Expand All @@ -62,4 +62,6 @@ const query = $api.useQuery(method, path, options, queryOptions);
- `queryOptions`
- The original `useMutation` options.
- [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useMutation)

- `queryClient`
- The original `queryClient` option.
- [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useMutation)
6 changes: 4 additions & 2 deletions docs/openapi-react-query/use-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const $api = createClient(fetchClient);
## Api

```tsx
const query = $api.useQuery(method, path, options, queryOptions);
const query = $api.useQuery(method, path, options, queryOptions, queryClient);
```

**Arguments**
Expand All @@ -70,4 +70,6 @@ const query = $api.useQuery(method, path, options, queryOptions);
- `queryOptions`
- The original `useQuery` options.
- [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery)

- `queryClient`
- The original `queryClient` option.
- [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery)
6 changes: 4 additions & 2 deletions docs/openapi-react-query/use-suspense-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const $api = createClient(fetchClient);
## Api

```tsx
const query = $api.useSuspenseQuery(method, path, options, queryOptions);
const query = $api.useSuspenseQuery(method, path, options, queryOptions, queryClient);
```

**Arguments**
Expand All @@ -76,4 +76,6 @@ const query = $api.useSuspenseQuery(method, path, options, queryOptions);
- `queryOptions`
- The original `useSuspenseQuery` options.
- [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useSuspenseQuery)

- `queryClient`
- The original `queryClient` option.
- [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useSuspenseQuery)
2 changes: 1 addition & 1 deletion docs/scripts/update-contributors.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const CONTRIBUTORS = {
"armandabric",
"illright",
]),
"openapi-react-query": new Set(["drwpow", "kerwanp"]),
"openapi-react-query": new Set(["drwpow", "kerwanp", "yoshi2no"]),
};

async function main() {
Expand Down
103 changes: 57 additions & 46 deletions packages/openapi-react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type UseQueryResult,
type UseSuspenseQueryOptions,
type UseSuspenseQueryResult,
type QueryClient,
useMutation,
useQuery,
useSuspenseQuery,
Expand All @@ -21,9 +22,9 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
>(
method: Method,
url: Path,
...[init, options]: HasRequiredKeys<Init> extends never
? [(Init & { [key: string]: unknown })?, Options?]
: [Init & { [key: string]: unknown }, Options?]
...[init, options, queryClient]: HasRequiredKeys<Init> extends never
? [(Init & { [key: string]: unknown })?, Options?, QueryClient?]
: [Init & { [key: string]: unknown }, Options?, QueryClient?]
) => UseQueryResult<Response["data"], Response["error"]>;

export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
Expand All @@ -35,9 +36,9 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
>(
method: Method,
url: Path,
...[init, options]: HasRequiredKeys<Init> extends never
? [(Init & { [key: string]: unknown })?, Options?]
: [Init & { [key: string]: unknown }, Options?]
...[init, options, queryClient]: HasRequiredKeys<Init> extends never
? [(Init & { [key: string]: unknown })?, Options?, QueryClient?]
: [Init & { [key: string]: unknown }, Options?, QueryClient?]
) => UseSuspenseQueryResult<Response["data"], Response["error"]>;

export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
Expand All @@ -50,6 +51,7 @@ export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}
method: Method,
url: Path,
options?: Options,
queryClient?: QueryClient,
) => UseMutationResult<Response["data"], Response["error"], Init>;

export interface OpenapiQueryClient<Paths extends {}, Media extends MediaType = MediaType> {
Expand All @@ -64,51 +66,60 @@ export default function createClient<Paths extends {}, Media extends MediaType =
client: FetchClient<Paths, Media>,
): OpenapiQueryClient<Paths, Media> {
return {
useQuery: (method, path, ...[init, options]) => {
return useQuery({
queryKey: [method, path, init],
queryFn: async () => {
const mth = method.toUpperCase() as keyof typeof client;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
const { data, error } = await fn(path, init as any); // TODO: find a way to avoid as any
if (error || !data) {
throw error;
}
return data;
useQuery: (method, path, ...[init, options, queryClient]) => {
return useQuery(
{
queryKey: [method, path, init],
queryFn: async () => {
const mth = method.toUpperCase() as keyof typeof client;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
const { data, error } = await fn(path, init as any); // TODO: find a way to avoid as any
if (error || !data) {
throw error;
}
return data;
},
...options,
},
...options,
});
queryClient,
);
},
useSuspenseQuery: (method, path, ...[init, options]) => {
return useSuspenseQuery({
queryKey: [method, path, init],
queryFn: async () => {
const mth = method.toUpperCase() as keyof typeof client;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
const { data, error } = await fn(path, init as any); // TODO: find a way to avoid as any
if (error || !data) {
throw error;
}
return data;
useSuspenseQuery: (method, path, ...[init, options, queryClient]) => {
return useSuspenseQuery(
{
queryKey: [method, path, init],
queryFn: async () => {
const mth = method.toUpperCase() as keyof typeof client;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
const { data, error } = await fn(path, init as any); // TODO: find a way to avoid as any
if (error || !data) {
throw error;
}
return data;
},
...options,
},
...options,
});
queryClient,
);
},
useMutation: (method, path, options) => {
return useMutation({
mutationKey: [method, path],
mutationFn: async (init) => {
// TODO: Put in external fn for reusability
const mth = method.toUpperCase() as keyof typeof client;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
const { data, error } = await fn(path, init as any); // TODO: find a way to avoid as any
if (error || !data) {
throw error;
}
return data;
useMutation: (method, path, options, queryClient) => {
return useMutation(
{
mutationKey: [method, path],
mutationFn: async (init) => {
// TODO: Put in external fn for reusability
const mth = method.toUpperCase() as keyof typeof client;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
const { data, error } = await fn(path, init as any); // TODO: find a way to avoid as any
if (error || !data) {
throw error;
}
return data;
},
...options,
},
...options,
});
queryClient,
);
},
};
}
Loading