feat(helper/route): enable to get route path at specific index (#4423)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Taku Amano · committed by GitHub · parent 393ded9 · Browse files · 2 files·+36 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+36 added−9 removed
- tests:+16 added−0 removed
- other:+20 added−9 removed
Provenance
- Commit
81bda2e- Tree
1fd9cce- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
393ded9
2 files changed+36 added−9 removed
modified:src/helper/route/index.test.ts+16 added
61 unmodified lines62 })63 64 expect(routePath(c)).toBe('/:id')65 66+ expect(routePath(c, 0)).toBe('/:id')67+ expect(routePath(c, 1)).toBe('/:id/:name')68+ expect(routePath(c, -1)).toBe('/:id/:name')69+ expect(routePath(c, 2)).toBe('')70+71 c.req.routeIndex = 172 expect(routePath(c)).toBe('/:id/:name')73 })74 })25 unmodified lines100 })101 102 expect(baseRoutePath(c)).toBe('/')103 104+ expect(baseRoutePath(c, 0)).toBe('/')105+ expect(baseRoutePath(c, 1)).toBe('/sub')106+ expect(baseRoutePath(c, 2)).toBe('/:sub')107+ expect(baseRoutePath(c, -1)).toBe('/:sub')108+ expect(baseRoutePath(c, 3)).toBe('')109+110 c.req.routeIndex = 1111 expect(baseRoutePath(c)).toBe('/sub')112 113 c.req.routeIndex = 225 unmodified lines139 140 expect(basePath(c)).toBe('/')141 expect(basePath(c)).toBe('/') // cached value142 143+ expect(basePath(c, 0)).toBe('/')144+ expect(basePath(c, 1)).toBe('/sub')145+ expect(basePath(c, -1)).toBe('/sub')146+ expect(basePath(c, 2)).toBe('')147+148 c.req.routeIndex = 1149 expect(basePath(c)).toBe('/sub')150 })151 68 unmodified linesmodified:src/helper/route/index.ts+20 added-9 removed
35 unmodified lines36373839404142434445464748495051525354555657515859606162636465666768698 unmodified lines78798081738283848586878889909192938 unmodified lines10210310410595969710610710810911010010111111211311411510511611711811912012 unmodified lines13313413513612613713813914014135 unmodified lines/** * Get the route path registered within the handler * * @param {Context} c - The context object * @param {number} index - The index of the root from which to retrieve the path, similar to Array.prototype.at(), where a negative number is the index counted from the end of the matching root. Defaults to the current root index. * @returns The route path registered within the handler * * @example * ```ts * import { routePath } from 'hono/route' * * app.use('*', (c, next) => { * console.log(routePath(c)) // '*' * console.log(routePath(c, -1)) // '/posts/:id' * return next() * }) * * app.get('/posts/:id', (c) => { * return c.text(routePath(c)) // '/posts/:id' * }) * ``` */export const routePath = (c: Context): string => matchedRoutes(c)[c.req.routeIndex].pathexport const routePath = (c: Context, index?: number): string => matchedRoutes(c).at(index ?? c.req.routeIndex)?.path ?? ''
/** * Get the basePath of the as-is route specified by routing. * * @param {Context} c - The context object * @param {number} index - The index of the root from which to retrieve the path, similar to Array.prototype.at(), where a negative number is the index counted from the end of the matching root. Defaults to the current root index. * @returns The basePath of the as-is route specified by routing. * * @example * ```ts8 unmodified lines * * app.route('/:sub', subApp) * ``` */export const baseRoutePath = (c: Context): string => matchedRoutes(c)[c.req.routeIndex].basePathexport const baseRoutePath = (c: Context, index?: number): string => matchedRoutes(c).at(index ?? c.req.routeIndex)?.basePath ?? ''
/** * Get the basePath with embedded parameters * * @param {Context} c - The context object * @param {number} index - The index of the root from which to retrieve the path, similar to Array.prototype.at(), where a negative number is the index counted from the end of the matching root. Defaults to the current root index. * @returns The basePath with embedded parameters. * * @example * ```ts8 unmodified lines * * app.route('/:sub', subApp) * ``` */const basePathCacheMap: WeakMap<Context, string[]> = new WeakMap()export const basePath = (c: Context): string => { const routeIndex = c.req.routeIndexconst basePathCacheMap: WeakMap<Context, Record<number, string>> = new WeakMap()export const basePath = (c: Context, index?: number): string => { index ??= c.req.routeIndex
const cache = basePathCacheMap.get(c) || [] if (typeof cache[routeIndex] === 'string') { return cache[routeIndex] if (typeof cache[index] === 'string') { return cache[index] }
let result: string const rp = baseRoutePath(c) const rp = baseRoutePath(c, index) if (!/[:*]/.test(rp)) { result = rp } else { const paths = splitRoutingPath(rp)12 unmodified lines } result = reqPath.substring(0, basePathLength) }
cache[routeIndex] = result cache[index] = result basePathCacheMap.set(c, cache)
return result}