MCPcopy
hub / github.com/kriasoft/universal-router / resolve

Method resolve

src/universal-router.ts:291–355  ·  view source on GitHub ↗

* Traverses the list of routes in the order they are defined until it finds * the first route that matches provided URL path string and whose action function * returns anything other than `null` or `undefined`.

(pathnameOrContext: string | ResolveContext)

Source from the content-addressed store, hash-verified

289 * returns anything other than `null` or `undefined`.
290 */
291 resolve(pathnameOrContext: string | ResolveContext): Promise<RouteResult<R>> {
292 const context: ResolveContext = {
293 router: this,
294 ...this.options.context,
295 ...(typeof pathnameOrContext === 'string'
296 ? { pathname: pathnameOrContext }
297 : pathnameOrContext),
298 }
299 const matchResult = matchRoute(
300 this.root,
301 this.baseUrl,
302 this.options,
303 context.pathname.substr(this.baseUrl.length),
304 )
305 const resolve = this.options.resolveRoute || resolveRoute
306 let matches: IteratorResult<RouteMatch<R, C>, false>
307 let nextMatches: IteratorResult<RouteMatch<R, C>, false> | null
308 let currentContext = context
309
310 function next(
311 resume: boolean,
312 parent: Route<R, C> | false = !matches.done && matches.value.route,
313 prevResult?: RouteResult<R>,
314 ): Promise<RouteResult<R>> {
315 const routeToSkip =
316 prevResult === null && !matches.done && matches.value.route
317 matches = nextMatches || matchResult.next(routeToSkip)
318 nextMatches = null
319
320 if (!resume) {
321 if (matches.done || !isChildRoute(parent, matches.value.route)) {
322 nextMatches = matches
323 return Promise.resolve(null)
324 }
325 }
326
327 if (matches.done) {
328 const error: RouteError = new Error('Route not found')
329 error.status = 404
330 return Promise.reject(error)
331 }
332
333 currentContext = { ...context, ...matches.value }
334
335 return Promise.resolve(
336 resolve(currentContext as RouteContext<R, C>, matches.value.params),
337 ).then((result) => {
338 if (result !== null && result !== undefined) {
339 return result
340 }
341 return next(resume, parent, result)
342 })
343 }
344
345 context['next'] = next
346
347 return Promise.resolve()
348 .then(() => next(true, this.root))

Callers 3

nextMethod · 0.45

Calls 2

matchRouteFunction · 0.70
nextFunction · 0.70

Tested by

no test coverage detected