fix(trie-router-router): For "Named param with trailing wildcard" (#1246)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(trie-router-router): For "Named param with trailing wildcard" * denoify
authored by Yusuke Wada · committed by GitHub · parent 3e663af · Browse files · 4 files·+56 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+56 added−6 removed
- tests:+38 added−0 removed
- other:+18 added−6 removed
Provenance
- Commit
47dbeaa- Tree
09429bf- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
3e663af
4 files changed+56 added−6 removed
modified:deno_dist/router/trie-router/node.ts+9 added-3 removed
169 unmodified lines170 // Named match171 // `/posts/:id` => match /posts/123172 const [key, name, matcher] = pattern173 174+ const child = node.children[key]175+176 // `/js/:filename{[a-z]+.js}` => match /js/chunk/123.js177 const restPathString = parts.slice(i).join('/')178 if (matcher instanceof RegExp && matcher.test(restPathString)) {177- handlerSets.push(...this.gHSets(node.children[key], method))179+ handlerSets.push(...this.gHSets(child, method))180 params[name] = restPathString181 continue182 }183 184 if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {185 if (typeof key === 'string') {186 if (isLast === true) {185- handlerSets.push(...this.gHSets(node.children[key], method))187+ handlerSets.push(...this.gHSets(child, method))188+ // `/:id/*` => match `/foo`189+ if (child.children['*']) {190+ handlerSets.push(...this.gHSets(child.children['*'], method))191+ }192 } else {187- tempNodes.push(node.children[key])193+ tempNodes.push(child)194 }195 }196 197 // `/book/a` => no-slug34 unmodified linesmodified:src/router/trie-router/node.test.ts+25 added
125 unmodified lines126 expect(res?.params['baz']).toBe('world')127 })128 })129 130+describe('Named params and a wildcard', () => {131+ const node = new Node()132+133+ node.insert('get', '/:id/*', 'onepart')134+135+ it('get /', () => {136+ const res = node.search('get', '/')137+ expect(res).toBeNull()138+ })139+140+ it('get /foo', () => {141+ const res = node.search('get', '/foo')142+ expect(res).not.toBeNull()143+ expect(res?.handlers).toEqual(['onepart'])144+ expect(res?.params['id']).toEqual('foo')145+ })146+147+ it('get /foo/bar', () => {148+ const res = node.search('get', '/foo/bar')149+ expect(res).not.toBeNull()150+ expect(res?.handlers).toEqual(['onepart'])151+ expect(res?.params['id']).toEqual('foo')152+ })153+})154+155 describe('Wildcard', () => {156 const node = new Node()157 node.insert('get', '/wildcard-abc/*/wildcard-efg', 'wildcard')158 it('/wildcard-abc/xxxxxx/wildcard-efg', () => {488 unmodified linesmodified:src/router/trie-router/node.ts+9 added-3 removed
169 unmodified lines170 // Named match171 // `/posts/:id` => match /posts/123172 const [key, name, matcher] = pattern173 174+ const child = node.children[key]175+176 // `/js/:filename{[a-z]+.js}` => match /js/chunk/123.js177 const restPathString = parts.slice(i).join('/')178 if (matcher instanceof RegExp && matcher.test(restPathString)) {177- handlerSets.push(...this.gHSets(node.children[key], method))179+ handlerSets.push(...this.gHSets(child, method))180 params[name] = restPathString181 continue182 }183 184 if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {185 if (typeof key === 'string') {186 if (isLast === true) {185- handlerSets.push(...this.gHSets(node.children[key], method))187+ handlerSets.push(...this.gHSets(child, method))188+ // `/:id/*` => match `/foo`189+ if (child.children['*']) {190+ handlerSets.push(...this.gHSets(child.children['*'], method))191+ }192 } else {187- tempNodes.push(node.children[key])193+ tempNodes.push(child)194 }195 }196 197 // `/book/a` => no-slug34 unmodified linesmodified:src/router/trie-router/router.test.ts+13 added
37 unmodified lines38 expect(res?.handlers).toEqual(['get entry'])39 expect(res?.params['id']).toBe('123')40 })41 42+ it('Named param with trailing wildcard', async () => {43+ router.add('GET', '/article/:id/*', 'get article with wildcard')44+ let res = router.match('GET', '/article/123')45+ expect(res).not.toBeNull()46+ expect(res?.handlers).toEqual(['get article with wildcard'])47+ expect(res?.params['id']).toBe('123')48+49+ res = router.match('GET', '/article/123/action')50+ expect(res).not.toBeNull()51+ expect(res?.handlers).toEqual(['get article with wildcard'])52+ expect(res?.params['id']).toBe('123')53+ })54+55 it('Wildcard', async () => {56 router.add('GET', '/wild/*/card', 'get wildcard')57 const res = router.match('GET', '/wild/xxx/card')58 expect(res).not.toBeNull()148 unmodified lines