Skip to content

Commit

Permalink
fix: fix routeUrl and remove breadcrumbs
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `request.breadcrumbs` are removed.

The routeUrl handler now sets the original request `pathname` before
passing it to the next handler.
  • Loading branch information
davidbonnet committed Aug 21, 2020
1 parent b3b7e28 commit ae6099c
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/routeUrl.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import { toPairs, orderBy } from 'lodash'
import { URL } from 'url'

const { URL } = global
import { toPairs, orderBy } from 'lodash'

export function routeUrl(routes) {
const orderedRoutes = orderBy(toPairs(routes), ['0.length'], ['desc'])
return async function (request, next) {
if (!request.href) {
const href = new URL(`http://${request.headers['host']}${request.url}`)
const href = new URL(
`http://${request.headers['host'] || 'localhost'}${request.url}`,
)
request.href = href
request.pathname = href.pathname
request.breadcrumbs = []
}
const { pathname } = request
const { length } = orderedRoutes
const { length: pathnameLength } = pathname
for (let i = 0; i < length; i++) {
const route = orderedRoutes[i]
const pattern = route[0]
const middleware = route[1]
const handler = route[1]
if (pattern.length > pathnameLength) {
continue
}
if (pattern === pathname || pathname.startsWith(pattern)) {
request.pathname = pathname.slice(pattern.length)
request.breadcrumbs.push(pattern)
return await middleware(request, next)
return await handler(request, function (request) {
request.pathname = pathname
return next(request)
})
}
}
return await next(request)
Expand Down

0 comments on commit ae6099c

Please sign in to comment.