Skip to content

Commit 987fd07

Browse files
committed
✨ Add normal query and mutation to react-query
1 parent cdf8c72 commit 987fd07

File tree

5 files changed

+248
-239
lines changed

5 files changed

+248
-239
lines changed

.changeset/sixty-ghosts-admire.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@ts-rest/core': minor
3+
'@ts-rest/react-query': minor
4+
---
5+
6+
Add query and mutation support to react-query, instead of requring useMutation and useQuery

libs/ts-rest/core/src/lib/client.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,28 @@ type DataReturnArgs<TRoute extends AppRoute> = {
2424
: never;
2525
};
2626

27-
type DataReturn<TRoute extends AppRoute> = (
27+
/**
28+
* Returned from a mutation or query call
29+
*/
30+
export type DataReturn<TRoute extends AppRoute> = (
2831
args: Without<DataReturnArgs<TRoute>, never>
2932
) => Promise<
3033
| { data: TRoute['response']; error: null; status: number }
3134
| { data: null; error: string; status: number }
3235
>;
3336

34-
type ClientArgs = {
37+
export type ClientArgs = {
3538
baseUrl: string;
3639
baseHeaders: Record<string, string>;
3740
api: ApiFetcher;
3841
};
3942

40-
const defaultApi: ApiFetcher = async ({ path, method, headers, body }) => {
43+
export const defaultApi: ApiFetcher = async ({
44+
path,
45+
method,
46+
headers,
47+
body,
48+
}) => {
4149
const result = await fetch(path, { method, headers, body });
4250

4351
if (result.ok) {
@@ -67,7 +75,7 @@ export type ApiFetcher = (args: {
6775
}
6876
>;
6977

70-
const getRouteQuery = <TAppRoute extends AppRoute>(
78+
export const getRouteQuery = <TAppRoute extends AppRoute>(
7179
route: TAppRoute,
7280
clientArgs: ClientArgs
7381
) => {

libs/ts-rest/react-query/src/lib/ts-rest-client.ts

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { z } from 'zod';
22
import {
3+
ApiFetcher,
34
AppRoute,
45
AppRouteMutation,
56
AppRouteQuery,
67
AppRouter,
8+
ClientArgs,
9+
DataReturn,
10+
defaultApi,
11+
getRouteQuery,
712
isAppRoute,
813
Without,
914
} from '@ts-rest/core';
@@ -54,11 +59,6 @@ type DataReturnArgs<TRoute extends AppRoute> = {
5459
: never;
5560
};
5661

57-
// Used on X.query and X.mutation
58-
type DataReturn<TRoute extends AppRoute> = (
59-
args: Without<DataReturnArgs<TRoute>, never>
60-
) => Promise<{ data: TRoute['response']; status: number }>;
61-
6262
// Used on X.useQuery
6363
type DataReturnQuery<TAppRoute extends AppRoute> = (
6464
queryKey: QueryKey,
@@ -81,30 +81,6 @@ type DataReturnMutation<TAppRoute extends AppRoute> = (
8181
unknown
8282
>;
8383

84-
type ClientArgs = {
85-
baseUrl: string;
86-
baseHeaders: Record<string, string>;
87-
api: ApiFetcher;
88-
};
89-
90-
const defaultApi: ApiFetcher = async ({ path, method, headers, body }) => {
91-
const result = await fetch(path, { method, headers, body }).then((res) =>
92-
res.json()
93-
);
94-
95-
return { status: 200, data: result };
96-
};
97-
98-
export type ApiFetcher = (args: {
99-
path: string;
100-
method: string;
101-
headers: Record<string, string>;
102-
body: string | undefined;
103-
}) => Promise<{
104-
status: number;
105-
data: unknown;
106-
}>;
107-
10884
const getCompleteUrl = (query: any, baseUrl: string, path: string) => {
10985
const queryString =
11086
typeof query === 'object'
@@ -194,9 +170,9 @@ const createNewProxy = (router: AppRouter | AppRoute, args: ClientArgs) => {
194170
if (isAppRoute(router)) {
195171
switch (propKey) {
196172
case 'query':
197-
throw new Error('Not implemented');
173+
throw getRouteQuery(router, args);
198174
case 'mutation':
199-
throw new Error('Not implemented');
175+
throw getRouteQuery(router, args);
200176
case 'useQuery':
201177
return getRouteUseQuery(router, args);
202178
case 'useMutation':

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
],
1313
"dependencies": {
1414
"@changesets/cli": "^2.24.1",
15-
"@docusaurus/core": "2.0.0-rc.1",
16-
"@docusaurus/preset-classic": "2.0.0-rc.1",
17-
"@mdx-js/react": "1.6.21",
15+
"@docusaurus/core": "2.0.1",
16+
"@docusaurus/preset-classic": "2.0.1",
17+
"@mdx-js/react": "2.1.2",
1818
"@nestjs/common": "^9.0.7",
1919
"@nestjs/core": "^9.0.7",
2020
"@nestjs/platform-express": "^9.0.7",
2121
"@prisma/client": "^4.1.1",
2222
"@stackblitz/sdk": "^1.8.0",
23-
"@swc/helpers": "~0.3.3",
23+
"@swc/helpers": "~0.4.3",
2424
"@tailwindcss/typography": "^0.5.4",
2525
"@tanstack/react-query": "^4.0.10",
2626
"@tanstack/react-query-devtools": "^4.0.10",
@@ -45,7 +45,7 @@
4545
"zod": "^3.17.10"
4646
},
4747
"devDependencies": {
48-
"@docusaurus/module-type-aliases": "2.0.0-rc.1",
48+
"@docusaurus/module-type-aliases": "2.0.1",
4949
"@nestjs/schematics": "^9.0.1",
5050
"@nestjs/testing": "^9.0.7",
5151
"@nrwl/cli": "14.5.1",
@@ -77,7 +77,7 @@
7777
"@typescript-eslint/parser": "^5.31.0",
7878
"babel-jest": "28.1.3",
7979
"cypress": "^10.3.1",
80-
"eslint": "~8.20.0",
80+
"eslint": "~8.21.0",
8181
"eslint-config-next": "12.2.3",
8282
"eslint-config-prettier": "8.5.0",
8383
"eslint-plugin-cypress": "^2.10.3",

0 commit comments

Comments
 (0)