Skip to content

Commit

Permalink
refactor: use react query 5
Browse files Browse the repository at this point in the history
  • Loading branch information
ArFnds committed Dec 13, 2023
1 parent 98552e3 commit ccc54c0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 35 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"name": "ecyrbe",
"email": "[email protected]"
},
"homepage": "https://github.com/ecyrbe/zodios-react",
"homepage": "https://github.com/TechMagister/zodios-react",
"repository": {
"type": "git",
"url": "https://github.com/ecyrbe/zodios-react.git"
"url": "https://github.com/TechMagister/zodios-react.git"
},
"license": "MIT",
"keywords": [
Expand All @@ -48,20 +48,20 @@
"react": ">=16.8.0"
},
"devDependencies": {
"@tanstack/react-query": "4.36.1",
"@tanstack/react-query": "^5.13.4",
"@testing-library/dom": "9.3.3",
"@testing-library/jest-dom": "6.1.4",
"@testing-library/react": "14.0.0",
"@testing-library/react-hooks": "8.0.1",
"@testing-library/user-event": "14.5.1",
"@types/axios": "0.14.0",
"@types/axios": "^0.14.0",
"@types/cors": "2.8.14",
"@types/express": "4.17.19",
"@types/jest": "29.5.5",
"@types/node": "20.8.9",
"@types/react": "18.2.30",
"@zodios/core": "10.9.6",
"axios": "1.5.1",
"axios": "1.6.2",
"cors": "2.8.5",
"express": "4.18.2",
"jest": "29.7.0",
Expand Down
4 changes: 4 additions & 0 deletions src/hooks.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ describe("zodios hooks", () => {
},
{
getPageParamList: () => ["page"],
initialPageParam: {},
getNextPageParam: (lastPage, pages) => {
return lastPage.nextPage
? {
Expand Down Expand Up @@ -733,6 +734,7 @@ describe("zodios hooks", () => {
undefined,
{
getPageParamList: () => ["page"],
initialPageParam: {},
getNextPageParam: (lastPage, pages) => {
return lastPage.nextPage
? {
Expand Down Expand Up @@ -1194,6 +1196,7 @@ describe("zodios hooks", () => {
},
{
getPageParamList: () => ["page"],
initialPageParam: {},
getNextPageParam: (lastPage, pages) => {
return lastPage.nextPage
? {
Expand Down Expand Up @@ -1282,6 +1285,7 @@ describe("zodios hooks", () => {
undefined,
{
getPageParamList: () => ["page"],
initialPageParam: {},
getNextPageParam: (lastPage, pages) => {
return lastPage.nextPage
? {
Expand Down
96 changes: 66 additions & 30 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
QueryFunctionContext,
QueryKey,
UseInfiniteQueryResult,
InfiniteData,
} from "@tanstack/react-query";
import { ZodiosError, ZodiosInstance } from "@zodios/core";
import type {
Expand Down Expand Up @@ -236,11 +237,17 @@ export class ZodiosHooksClass<Api extends ZodiosEndpointDefinitions> {
})
: () => this.zodios.get(path, config as any);
const queryClient = useQueryClient();
const invalidate = () => queryClient.invalidateQueries(key);
const queryResult = useQuery(key, query, queryOptions) as any;
queryResult.invalidate = invalidate;
queryResult.key = key;
return queryResult;
const invalidate = () => queryClient.invalidateQueries({ queryKey: key });
const queryResult = useQuery({
queryKey: key,
queryFn: query,
...queryOptions,
});
return {
...queryResult,
invalidate,
key,
};
}

useImmutableQuery<
Expand Down Expand Up @@ -280,11 +287,16 @@ export class ZodiosHooksClass<Api extends ZodiosEndpointDefinitions> {
})
: () => this.zodios.post(path, body, config as any);
const queryClient = useQueryClient();
const invalidate = () => queryClient.invalidateQueries(key);
const queryResult = useQuery(key, query, queryOptions) as any;
queryResult.invalidate = invalidate;
queryResult.key = key;
return queryResult;
const invalidate = () => queryClient.invalidateQueries({ queryKey: key });
return {
...useQuery({
queryKey: key,
queryFn: query,
...queryOptions,
}),
invalidate,
key,
};
}

useInfiniteQuery<
Expand Down Expand Up @@ -318,7 +330,7 @@ export class ZodiosHooksClass<Api extends ZodiosEndpointDefinitions> {
}
]
): UseInfiniteQueryResult<
TData,
InfiniteData<TData>,
Errors<UnknownIfNever<ZodiosErrorByPath<Api, "get", Path, number>>>
> & {
invalidate: () => Promise<void>;
Expand All @@ -343,7 +355,10 @@ export class ZodiosHooksClass<Api extends ZodiosEndpointDefinitions> {
}
const key = [{ api: this.apiName, path }, params];
const query = this.options.shouldAbortOnUnmount
? ({ pageParam = undefined, signal }: QueryFunctionContext) =>
? ({
pageParam,
signal,
}: QueryFunctionContext<typeof key, AnyZodiosMethodOptions>) =>
this.zodios.get(path, {
...config,
queries: {
Expand All @@ -356,7 +371,9 @@ export class ZodiosHooksClass<Api extends ZodiosEndpointDefinitions> {
},
signal: combineSignals(signal, (config as any)?.signal),
} as unknown as ReadonlyDeep<TConfig>)
: ({ pageParam = undefined }: QueryFunctionContext) =>
: ({
pageParam,
}: QueryFunctionContext<typeof key, AnyZodiosMethodOptions>) =>
this.zodios.get(path, {
...config,
queries: {
Expand All @@ -369,16 +386,19 @@ export class ZodiosHooksClass<Api extends ZodiosEndpointDefinitions> {
},
} as unknown as ReadonlyDeep<TConfig>);
const queryClient = useQueryClient();
const invalidate = () => queryClient.invalidateQueries(key);
const invalidate = () => queryClient.invalidateQueries({ queryKey: key });

return {
...useInfiniteQuery({
queryKey: key,
queryFn: query,
initialPageParam: {},
getNextPageParam: () => ({}),
...(queryOptions as Omit<typeof queryOptions, "getPageParamList">),
}),
invalidate,
key,
...useInfiniteQuery(
key,
query,
queryOptions as Omit<typeof queryOptions, "getPageParamList">
),
} as any;
};
}

useImmutableInfiniteQuery<
Expand Down Expand Up @@ -415,7 +435,7 @@ export class ZodiosHooksClass<Api extends ZodiosEndpointDefinitions> {
}
]
): UseInfiniteQueryResult<
TData,
InfiniteData<TData>,
Errors<UnknownIfNever<ZodiosErrorByPath<Api, "post", Path, number>>>
> & {
invalidate: () => Promise<void>;
Expand Down Expand Up @@ -448,7 +468,13 @@ export class ZodiosHooksClass<Api extends ZodiosEndpointDefinitions> {
}
const key = [{ api: this.apiName, path }, params, bodyKey];
const query = this.options.shouldAbortOnUnmount
? ({ pageParam = undefined, signal }: QueryFunctionContext) =>
? ({
pageParam,
signal,
}: QueryFunctionContext<
typeof key,
AnyZodiosMethodOptions | undefined
>) =>
this.zodios.post(
path,
{
Expand All @@ -468,7 +494,12 @@ export class ZodiosHooksClass<Api extends ZodiosEndpointDefinitions> {
signal: combineSignals(signal, (config as any)?.signal),
} as unknown as ReadonlyDeep<TConfig>
)
: ({ pageParam = undefined }: QueryFunctionContext) =>
: ({
pageParam,
}: QueryFunctionContext<
typeof key,
AnyZodiosMethodOptions | undefined
>) =>
this.zodios.post(
path,
{
Expand All @@ -488,15 +519,17 @@ export class ZodiosHooksClass<Api extends ZodiosEndpointDefinitions> {
} as unknown as ReadonlyDeep<TConfig>
);
const queryClient = useQueryClient();
const invalidate = () => queryClient.invalidateQueries(key);
const invalidate = () => queryClient.invalidateQueries({ queryKey: key });
return {
invalidate,
key,
...useInfiniteQuery(
key,
query,
queryOptions as Omit<typeof queryOptions, "getPageParamList">
),
...useInfiniteQuery({
queryKey: key,
queryFn: query,
initialPageParam: {},
getNextPageParam: () => ({}),
...(queryOptions as Omit<typeof queryOptions, "getPageParamList">),
}),
} as any;
}

Expand Down Expand Up @@ -530,7 +563,10 @@ export class ZodiosHooksClass<Api extends ZodiosEndpointDefinitions> {
data: variables,
} as any);
};
return useMutation(mutation, mutationOptions);
return useMutation({
mutationFn: mutation,
...mutationOptions,
});
}

useGet<
Expand Down

0 comments on commit ccc54c0

Please sign in to comment.