From 943389b645ea7ae7b9aa4dcf0dce4fe222c831f7 Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Mon, 22 Jul 2024 15:28:30 +0200 Subject: [PATCH 01/11] add repro test --- .../tests/index.test.tsx | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/packages/react-router-with-query/tests/index.test.tsx b/packages/react-router-with-query/tests/index.test.tsx index 38cae5d8dc..8d15cebd73 100644 --- a/packages/react-router-with-query/tests/index.test.tsx +++ b/packages/react-router-with-query/tests/index.test.tsx @@ -1,5 +1,66 @@ -import { expect, test } from 'vitest' +import { QueryClient } from '@tanstack/react-query' +import { + createRootRouteWithContext, + createRouter, +} from '@tanstack/react-router' + +import { expect, expectTypeOf, test } from 'vitest' + +import { routerWithQueryClient } from '../src' test('it has a test', async () => { expect(true).toBe(true) }) + +test('types', () => { + { + // basic + const root = createRootRouteWithContext<{ + queryClient: QueryClient + }>()({}) + + const queryClient = new QueryClient() + const router = createRouter({ + context: { queryClient }, + routeTree: root, + }) + + const routerWithQuery = routerWithQueryClient(router, queryClient) + expectTypeOf(routerWithQuery).toMatchTypeOf(router) + } + + { + // without query client + const root = createRootRouteWithContext()({}) + + const queryClient = new QueryClient() + const router = createRouter({ + routeTree: root, + }) + + const routerWithQuery = routerWithQueryClient( + // @ts-expect-error - QueryClient must be in context type + router, + queryClient, + ) + } + + { + // with extra props + const root = createRootRouteWithContext<{ + queryClient: QueryClient + extra: string + }>()({}) + + const queryClient = new QueryClient() + const router = createRouter({ + routeTree: root, + }) + + const routerWithQuery = routerWithQueryClient( + // @ts-expect-error - QueryClient must be in context type + router, + queryClient, + ) + } +}) From 6e1b89a0f4e24489ed6dad955891d8084b28f1d8 Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Mon, 22 Jul 2024 15:29:55 +0200 Subject: [PATCH 02/11] actually test the thing that's broken --- packages/react-router-with-query/tests/index.test.tsx | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/react-router-with-query/tests/index.test.tsx b/packages/react-router-with-query/tests/index.test.tsx index 8d15cebd73..0672506e79 100644 --- a/packages/react-router-with-query/tests/index.test.tsx +++ b/packages/react-router-with-query/tests/index.test.tsx @@ -54,13 +54,10 @@ test('types', () => { const queryClient = new QueryClient() const router = createRouter({ + context: { queryClient, extra: 'extra' }, routeTree: root, }) - const routerWithQuery = routerWithQueryClient( - // @ts-expect-error - QueryClient must be in context type - router, - queryClient, - ) + const routerWithQuery = routerWithQueryClient(router, queryClient) } }) From 7214a6ed961efde625a9a7f730da619000ff5b59 Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Mon, 22 Jul 2024 15:30:05 +0200 Subject: [PATCH 03/11] ok --- packages/react-router-with-query/tests/index.test.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/react-router-with-query/tests/index.test.tsx b/packages/react-router-with-query/tests/index.test.tsx index 0672506e79..308371f06c 100644 --- a/packages/react-router-with-query/tests/index.test.tsx +++ b/packages/react-router-with-query/tests/index.test.tsx @@ -59,5 +59,6 @@ test('types', () => { }) const routerWithQuery = routerWithQueryClient(router, queryClient) + expectTypeOf(routerWithQuery).toMatchTypeOf(router) } }) From 71fb4c447c5d96bb35bcbd34eb34b7d4b87368b6 Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Mon, 22 Jul 2024 15:31:20 +0200 Subject: [PATCH 04/11] toEqualTypeOf --- packages/react-router-with-query/tests/index.test.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react-router-with-query/tests/index.test.tsx b/packages/react-router-with-query/tests/index.test.tsx index 308371f06c..d89275ffb5 100644 --- a/packages/react-router-with-query/tests/index.test.tsx +++ b/packages/react-router-with-query/tests/index.test.tsx @@ -26,7 +26,7 @@ test('types', () => { }) const routerWithQuery = routerWithQueryClient(router, queryClient) - expectTypeOf(routerWithQuery).toMatchTypeOf(router) + expectTypeOf(routerWithQuery).toEqualTypeOf(router) } { @@ -59,6 +59,6 @@ test('types', () => { }) const routerWithQuery = routerWithQueryClient(router, queryClient) - expectTypeOf(routerWithQuery).toMatchTypeOf(router) + expectTypeOf(routerWithQuery).toEqualTypeOf(router) } }) From 2c7285000b68ddae5cb4d7894949574f29b9ab4b Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Mon, 22 Jul 2024 15:42:35 +0200 Subject: [PATCH 05/11] fix it --- packages/react-router-with-query/src/index.tsx | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/react-router-with-query/src/index.tsx b/packages/react-router-with-query/src/index.tsx index 50ea13bcb4..a65d2f0481 100644 --- a/packages/react-router-with-query/src/index.tsx +++ b/packages/react-router-with-query/src/index.tsx @@ -4,19 +4,22 @@ import { hashKey, hydrate, } from '@tanstack/react-query' -import { type AnyRouterWithContext } from '@tanstack/react-router' import { Fragment } from 'react' +import type { AnyRouter, AnyRouterWithContext } from '@tanstack/react-router' import type { QueryClient, QueryObserverResult, UseQueryOptions, } from '@tanstack/react-query' -export function routerWithQueryClient< - T extends AnyRouterWithContext<{ - queryClient: QueryClient - }>, ->(router: T, queryClient: QueryClient): T { +export function routerWithQueryClient( + router: TRouter extends AnyRouterWithContext + ? Ctx extends { queryClient: QueryClient } + ? TRouter + : never + : never, + queryClient: QueryClient, +): TRouter { const seenQueryKeys = new Set() const streamedQueryKeys = new Set() From e46accc842d5324699a0f6ccb937a7be79cb57bb Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Mon, 22 Jul 2024 15:46:53 +0200 Subject: [PATCH 06/11] fix type tests --- packages/react-router-with-query/src/index.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/react-router-with-query/src/index.tsx b/packages/react-router-with-query/src/index.tsx index a65d2f0481..57e824a716 100644 --- a/packages/react-router-with-query/src/index.tsx +++ b/packages/react-router-with-query/src/index.tsx @@ -13,13 +13,14 @@ import type { } from '@tanstack/react-query' export function routerWithQueryClient( - router: TRouter extends AnyRouterWithContext + _router: TRouter extends AnyRouterWithContext ? Ctx extends { queryClient: QueryClient } ? TRouter - : never - : never, + : 'Context type mismatch. Expected queryClient to be in context type.' + : 'Context type mismatch. Expected queryClient to be in context type.', queryClient: QueryClient, ): TRouter { + const router = _router as unknown as AnyRouter const seenQueryKeys = new Set() const streamedQueryKeys = new Set() @@ -126,5 +127,5 @@ export function routerWithQueryClient( }, } - return router + return router as TRouter } From 7d48cacbcaa620a62fc792db78ed0ffcf80a3d37 Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Tue, 23 Jul 2024 10:06:21 +0200 Subject: [PATCH 07/11] do some refactors --- .../react-router-with-query/src/index.tsx | 9 ++- .../tests/index.test-d.ts | 55 ++++++++++++++++ .../tests/index.test.tsx | 64 ------------------- 3 files changed, 59 insertions(+), 69 deletions(-) create mode 100644 packages/react-router-with-query/tests/index.test-d.ts delete mode 100644 packages/react-router-with-query/tests/index.test.tsx diff --git a/packages/react-router-with-query/src/index.tsx b/packages/react-router-with-query/src/index.tsx index 57e824a716..a65d2f0481 100644 --- a/packages/react-router-with-query/src/index.tsx +++ b/packages/react-router-with-query/src/index.tsx @@ -13,14 +13,13 @@ import type { } from '@tanstack/react-query' export function routerWithQueryClient( - _router: TRouter extends AnyRouterWithContext + router: TRouter extends AnyRouterWithContext ? Ctx extends { queryClient: QueryClient } ? TRouter - : 'Context type mismatch. Expected queryClient to be in context type.' - : 'Context type mismatch. Expected queryClient to be in context type.', + : never + : never, queryClient: QueryClient, ): TRouter { - const router = _router as unknown as AnyRouter const seenQueryKeys = new Set() const streamedQueryKeys = new Set() @@ -127,5 +126,5 @@ export function routerWithQueryClient( }, } - return router as TRouter + return router } diff --git a/packages/react-router-with-query/tests/index.test-d.ts b/packages/react-router-with-query/tests/index.test-d.ts new file mode 100644 index 0000000000..77a004ce07 --- /dev/null +++ b/packages/react-router-with-query/tests/index.test-d.ts @@ -0,0 +1,55 @@ +import { QueryClient } from '@tanstack/react-query' +import { + createRootRouteWithContext, + createRouter, +} from '@tanstack/react-router' + +import { expectTypeOf, test } from 'vitest' + +import { routerWithQueryClient } from '../src' + +test('basic { queryClient } context', () => { + const root = createRootRouteWithContext<{ + queryClient: QueryClient + }>()({}) + + const queryClient = new QueryClient() + const router = createRouter({ + context: { queryClient }, + routeTree: root, + }) + + const routerWithQuery = routerWithQueryClient(router, queryClient) + expectTypeOf(routerWithQuery).toEqualTypeOf(router) +}) + +test('no context fails', () => { + const root = createRootRouteWithContext()({}) + + const queryClient = new QueryClient() + const router = createRouter({ + routeTree: root, + }) + + routerWithQueryClient( + // @ts-expect-error - QueryClient must be in context type + router, + queryClient, + ) +}) + +test('allows additional props on context', () => { + const root = createRootRouteWithContext<{ + queryClient: QueryClient + extra: string + }>()({}) + + const queryClient = new QueryClient() + const router = createRouter({ + context: { queryClient, extra: 'extra' }, + routeTree: root, + }) + + const routerWithQuery = routerWithQueryClient(router, queryClient) + expectTypeOf(routerWithQuery).toEqualTypeOf(router) +}) diff --git a/packages/react-router-with-query/tests/index.test.tsx b/packages/react-router-with-query/tests/index.test.tsx deleted file mode 100644 index d89275ffb5..0000000000 --- a/packages/react-router-with-query/tests/index.test.tsx +++ /dev/null @@ -1,64 +0,0 @@ -import { QueryClient } from '@tanstack/react-query' -import { - createRootRouteWithContext, - createRouter, -} from '@tanstack/react-router' - -import { expect, expectTypeOf, test } from 'vitest' - -import { routerWithQueryClient } from '../src' - -test('it has a test', async () => { - expect(true).toBe(true) -}) - -test('types', () => { - { - // basic - const root = createRootRouteWithContext<{ - queryClient: QueryClient - }>()({}) - - const queryClient = new QueryClient() - const router = createRouter({ - context: { queryClient }, - routeTree: root, - }) - - const routerWithQuery = routerWithQueryClient(router, queryClient) - expectTypeOf(routerWithQuery).toEqualTypeOf(router) - } - - { - // without query client - const root = createRootRouteWithContext()({}) - - const queryClient = new QueryClient() - const router = createRouter({ - routeTree: root, - }) - - const routerWithQuery = routerWithQueryClient( - // @ts-expect-error - QueryClient must be in context type - router, - queryClient, - ) - } - - { - // with extra props - const root = createRootRouteWithContext<{ - queryClient: QueryClient - extra: string - }>()({}) - - const queryClient = new QueryClient() - const router = createRouter({ - context: { queryClient, extra: 'extra' }, - routeTree: root, - }) - - const routerWithQuery = routerWithQueryClient(router, queryClient) - expectTypeOf(routerWithQuery).toEqualTypeOf(router) - } -}) From 2e0e9b20a93f7b51a0b97d63f5113baf37dcf7db Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Tue, 23 Jul 2024 10:11:12 +0200 Subject: [PATCH 08/11] add context in example --- examples/react/start-basic-react-query/app/router.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/react/start-basic-react-query/app/router.tsx b/examples/react/start-basic-react-query/app/router.tsx index 24604116c1..ee35b01cbf 100644 --- a/examples/react/start-basic-react-query/app/router.tsx +++ b/examples/react/start-basic-react-query/app/router.tsx @@ -15,6 +15,7 @@ export function createRouter() { return routerWithQueryClient( createTanStackRouter({ routeTree, + context: { queryClient }, defaultPreload: 'intent', defaultErrorComponent: DefaultCatchBoundary, defaultNotFoundComponent: () => , From 01b35b5f207029a8e03b27204b9dcb77fa73d6ba Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Fri, 26 Jul 2024 23:45:05 +0200 Subject: [PATCH 09/11] this seems to do it --- packages/react-router-with-query/src/index.tsx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/react-router-with-query/src/index.tsx b/packages/react-router-with-query/src/index.tsx index a65d2f0481..d2aa9efba1 100644 --- a/packages/react-router-with-query/src/index.tsx +++ b/packages/react-router-with-query/src/index.tsx @@ -13,10 +13,8 @@ import type { } from '@tanstack/react-query' export function routerWithQueryClient( - router: TRouter extends AnyRouterWithContext - ? Ctx extends { queryClient: QueryClient } - ? TRouter - : never + router: TRouter['options']['context'] extends { queryClient: QueryClient } + ? TRouter : never, queryClient: QueryClient, ): TRouter { From 89f6153d8965ddc977230b052757903d7ab6fedd Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Fri, 26 Jul 2024 23:47:33 +0200 Subject: [PATCH 10/11] revert import changes --- packages/react-router-with-query/src/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-router-with-query/src/index.tsx b/packages/react-router-with-query/src/index.tsx index d2aa9efba1..616f0b2a4e 100644 --- a/packages/react-router-with-query/src/index.tsx +++ b/packages/react-router-with-query/src/index.tsx @@ -4,8 +4,8 @@ import { hashKey, hydrate, } from '@tanstack/react-query' +import type { AnyRouter } from '@tanstack/react-router' import { Fragment } from 'react' -import type { AnyRouter, AnyRouterWithContext } from '@tanstack/react-router' import type { QueryClient, QueryObserverResult, From 159f0a08e1f5b759455387015f4f7a23f01d3471 Mon Sep 17 00:00:00 2001 From: Manuel Schiller Date: Sat, 27 Jul 2024 00:42:38 +0200 Subject: [PATCH 11/11] fix eslint --- packages/react-router-with-query/src/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-router-with-query/src/index.tsx b/packages/react-router-with-query/src/index.tsx index 616f0b2a4e..3af3673069 100644 --- a/packages/react-router-with-query/src/index.tsx +++ b/packages/react-router-with-query/src/index.tsx @@ -1,3 +1,4 @@ +import { Fragment } from 'react' import { QueryClientProvider, dehydrate, @@ -5,7 +6,6 @@ import { hydrate, } from '@tanstack/react-query' import type { AnyRouter } from '@tanstack/react-router' -import { Fragment } from 'react' import type { QueryClient, QueryObserverResult,