MCPcopy
hub / github.com/TanStack/router / buildMiddlewareChain

Function buildMiddlewareChain

packages/router-core/src/router.ts:3062–3173  ·  view source on GitHub ↗
(destRoutes: ReadonlyArray<AnyRoute>)

Source from the content-addressed store, hash-verified

3060}
3061
3062function buildMiddlewareChain(destRoutes: ReadonlyArray<AnyRoute>) {
3063 const context = {
3064 dest: null as unknown as BuildNextOptions,
3065 _includeValidateSearch: false,
3066 middlewares: [] as Array<SearchMiddleware<any>>,
3067 }
3068
3069 for (const route of destRoutes) {
3070 if ('search' in route.options) {
3071 if (route.options.search?.middlewares) {
3072 context.middlewares.push(...route.options.search.middlewares)
3073 }
3074 }
3075 // TODO remove preSearchFilters and postSearchFilters in v2
3076 else if (
3077 route.options.preSearchFilters ||
3078 route.options.postSearchFilters
3079 ) {
3080 const legacyMiddleware: SearchMiddleware<any> = ({ search, next }) => {
3081 let nextSearch = search
3082
3083 if (
3084 'preSearchFilters' in route.options &&
3085 route.options.preSearchFilters
3086 ) {
3087 nextSearch = route.options.preSearchFilters.reduce(
3088 (prev, next) => next(prev),
3089 search,
3090 )
3091 }
3092
3093 const result = next(nextSearch)
3094
3095 if (
3096 'postSearchFilters' in route.options &&
3097 route.options.postSearchFilters
3098 ) {
3099 return route.options.postSearchFilters.reduce(
3100 (prev, next) => next(prev),
3101 result,
3102 )
3103 }
3104
3105 return result
3106 }
3107 context.middlewares.push(legacyMiddleware)
3108 }
3109
3110 if (route.options.validateSearch) {
3111 const validate: SearchMiddleware<any> = ({ search, next }) => {
3112 const result = next(search)
3113 if (!context._includeValidateSearch) return result
3114 try {
3115 const validatedSearch = {
3116 ...result,
3117 ...(validateSearch(route.options.validateSearch, result) ??
3118 undefined),
3119 }

Callers 1

applySearchMiddlewareFunction · 0.85

Calls 1

applyNextFunction · 0.85

Tested by

no test coverage detected