Skip to content

Commit cb9b84c

Browse files
henribruDamianOsipiuk
authored andcommitted
feat(hooks): add config param for queryProviders (#40)
* Add config param for useQueryProvider * Add config param for useNuxtQueryProvider
1 parent 7388851 commit cb9b84c

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

src/ssr/useNuxtQueryProvider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { useContext } from "@nuxtjs/composition-api";
22
import { useQueryClient, useQueryProvider } from "../index";
3+
import type { QueryClientConfig } from "../useQueryProvider";
34
import { hydrate } from "./hydration";
45

5-
export function useNuxtQueryProvider(): void {
6-
useQueryProvider();
6+
export function useNuxtQueryProvider(config: QueryClientConfig = {}): void {
7+
useQueryProvider(config);
78

89
// @ts-expect-error Nuxt.js injected client property
910
if (process.client) {

src/useQueryProvider.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import { QueryClient } from "react-query/core";
33

44
import { VUE_QUERY_CLIENT } from "./useQueryClient";
55

6-
export function useQueryProvider(): void {
7-
const queryClient = new QueryClient();
6+
// QueryClientConfig isn't exported so we need to extract it
7+
export type QueryClientConfig = NonNullable<
8+
ConstructorParameters<typeof QueryClient>[0]
9+
>;
10+
11+
export function useQueryProvider(config: QueryClientConfig = {}): void {
12+
const queryClient = new QueryClient(config);
813
queryClient.mount();
914

1015
provide(VUE_QUERY_CLIENT, queryClient);

tests/ssr/useNuxtQueryProvider.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ describe("useNuxtQueryProvider", () => {
4040
expect(useQueryProviderSpy).toHaveBeenCalledTimes(1);
4141
});
4242

43+
test("should call queryProvider with passed config", () => {
44+
const config = {
45+
defaultOptions: {
46+
queries: {
47+
enabled: false,
48+
},
49+
},
50+
};
51+
// @ts-expect-error Nuxt.js injected client property
52+
process.client = false;
53+
useNuxtQueryProvider(config);
54+
55+
expect(useQueryProviderSpy).toHaveBeenCalledWith(config);
56+
});
57+
4358
test("should call useQueryClient when vueQueryState is present", () => {
4459
useContextSpy.mockReturnValueOnce(withVueQueryState);
4560
useNuxtQueryProvider();

tests/useQueryProvider.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ describe("useQueryProvider", () => {
3232
expect(queryClientSpy).toHaveBeenCalledTimes(1);
3333
});
3434

35+
test("should create queryClient with passed config", () => {
36+
const config = {
37+
defaultOptions: {
38+
queries: {
39+
enabled: false,
40+
},
41+
},
42+
};
43+
44+
useQueryProvider(config);
45+
46+
expect(queryClientSpy).toBeCalledWith(config);
47+
});
48+
3549
test("should call mount on QueryClient", () => {
3650
useQueryProvider();
3751

0 commit comments

Comments
 (0)