Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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/quiet-plums-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/router-core': patch
---

fix(router-core): use search validator output type for search middleware context
4 changes: 1 addition & 3 deletions packages/router-core/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1274,9 +1274,7 @@ export interface UpdatableRouteOptions<
preloadGcTime?: number
search?: {
middlewares?: Array<
SearchMiddleware<
ResolveFullSearchSchemaInput<TParentRoute, TSearchValidator>
>
SearchMiddleware<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>
>
}
/**
Expand Down
39 changes: 39 additions & 0 deletions packages/router-core/tests/searchMiddleware.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expectTypeOf, test } from 'vitest'
import { BaseRootRoute, BaseRoute, type ValidatorAdapter } from '../src'

describe('search middlewares', () => {
test('should use search validator output for middleware context', () => {
type SearchInput = {
page?: string
}

type SearchOutput = {
page: number
}

const searchValidator: ValidatorAdapter<SearchInput, SearchOutput> = {
types: undefined as never,
parse: () => ({ page: 1 }),
}

const rootRoute = new BaseRootRoute({})

new BaseRoute({
getParentRoute: () => rootRoute,
path: '/posts',
validateSearch: searchValidator,
search: {
middlewares: [
({ search, next }) => {
expectTypeOf(search).toEqualTypeOf<SearchOutput>()
expectTypeOf(next).toBeCallableWith({ page: 2 })
expectTypeOf(next).parameter(0).toEqualTypeOf<SearchOutput>()
expectTypeOf(next({ page: 2 })).toEqualTypeOf<SearchOutput>()

return search
},
],
},
})
})
})
Loading