diff --git a/deno_dist/router/trie-router/node.ts b/deno_dist/router/trie-router/node.ts index f9de7d582..c55a23af4 100644 --- a/deno_dist/router/trie-router/node.ts +++ b/deno_dist/router/trie-router/node.ts @@ -5,12 +5,15 @@ import { splitPath, splitRoutingPath, getPattern } from '../../utils/url.ts' type HandlerSet = { handler: T - params: Record possibleKeys: string[] score: number name: string // For debug } +type HandlerParamsSet = HandlerSet & { + params: Record +} + export class Node { methods: Record>[] @@ -26,7 +29,7 @@ export class Node { this.name = '' if (method && handler) { const m: Record> = {} - m[method] = { handler, params: {}, possibleKeys: [], score: 0, name: this.name } + m[method] = { handler, possibleKeys: [], score: 0, name: this.name } this.methods = [m] } this.patterns = [] @@ -74,7 +77,6 @@ export class Node { const handlerSet: HandlerSet = { handler, - params: {}, possibleKeys, name: this.name, score: this.order, @@ -87,12 +89,17 @@ export class Node { } // getHandlerSets - private gHSets(node: Node, method: string, params: Record): HandlerSet[] { - const handlerSets: HandlerSet[] = [] + private gHSets( + node: Node, + method: string, + params: Record + ): HandlerParamsSet[] { + const handlerSets: HandlerParamsSet[] = [] for (let i = 0, len = node.methods.length; i < len; i++) { const m = node.methods[i] - const handlerSet = m[method] || m[METHOD_NAME_ALL] + const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet if (handlerSet !== undefined) { + handlerSet.params = {} handlerSet.possibleKeys.map((key) => { handlerSet.params[key] = params[key] }) @@ -103,7 +110,7 @@ export class Node { } search(method: string, path: string): [[T, Params][]] { - const handlerSets: HandlerSet[] = [] + const handlerSets: HandlerParamsSet[] = [] const params: Record = {} this.params = {} @@ -126,11 +133,9 @@ export class Node { if (isLast === true) { // '/hello/*' => match '/hello' if (nextNode.children['*']) { - handlerSets.push( - ...this.gHSets(nextNode.children['*'], method, { ...params, ...node.params }) - ) + handlerSets.push(...this.gHSets(nextNode.children['*'], method, node.params)) } - handlerSets.push(...this.gHSets(nextNode, method, { ...params, ...node.params })) + handlerSets.push(...this.gHSets(nextNode, method, node.params)) } else { tempNodes.push(nextNode) } @@ -144,7 +149,7 @@ export class Node { if (pattern === '*') { const astNode = node.children['*'] if (astNode) { - handlerSets.push(...this.gHSets(astNode, method, { ...params, ...node.params })) + handlerSets.push(...this.gHSets(astNode, method, node.params)) tempNodes.push(astNode) } continue diff --git a/src/router/trie-router/node.test.ts b/src/router/trie-router/node.test.ts index 1978eeffe..794109359 100644 --- a/src/router/trie-router/node.test.ts +++ b/src/router/trie-router/node.test.ts @@ -89,6 +89,21 @@ describe('Name path', () => { expect(res[0][0]).toEqual('get events') expect(res[0][1]['location']).toBe('yokohama') }) + + it('Should not return a previous param value', () => { + const node = new Node() + node.insert('delete', '/resource/:id', 'resource') + const [resA] = node.search('delete', '/resource/a') + const [resB] = node.search('delete', '/resource/b') + expect(resA).not.toBeNull() + expect(resA.length).toBe(1) + expect(resA[0][0]).toEqual('resource') + expect(resA[0][1]).toEqual({ id: 'a' }) + expect(resB).not.toBeNull() + expect(resB.length).toBe(1) + expect(resB[0][0]).toEqual('resource') + expect(resB[0][1]).toEqual({ id: 'b' }) + }) }) describe('Name path - Multiple route', () => { diff --git a/src/router/trie-router/node.ts b/src/router/trie-router/node.ts index 2a34266f7..7b120d4de 100644 --- a/src/router/trie-router/node.ts +++ b/src/router/trie-router/node.ts @@ -5,12 +5,15 @@ import { splitPath, splitRoutingPath, getPattern } from '../../utils/url' type HandlerSet = { handler: T - params: Record possibleKeys: string[] score: number name: string // For debug } +type HandlerParamsSet = HandlerSet & { + params: Record +} + export class Node { methods: Record>[] @@ -26,7 +29,7 @@ export class Node { this.name = '' if (method && handler) { const m: Record> = {} - m[method] = { handler, params: {}, possibleKeys: [], score: 0, name: this.name } + m[method] = { handler, possibleKeys: [], score: 0, name: this.name } this.methods = [m] } this.patterns = [] @@ -74,7 +77,6 @@ export class Node { const handlerSet: HandlerSet = { handler, - params: {}, possibleKeys, name: this.name, score: this.order, @@ -87,12 +89,17 @@ export class Node { } // getHandlerSets - private gHSets(node: Node, method: string, params: Record): HandlerSet[] { - const handlerSets: HandlerSet[] = [] + private gHSets( + node: Node, + method: string, + params: Record + ): HandlerParamsSet[] { + const handlerSets: HandlerParamsSet[] = [] for (let i = 0, len = node.methods.length; i < len; i++) { const m = node.methods[i] - const handlerSet = m[method] || m[METHOD_NAME_ALL] + const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet if (handlerSet !== undefined) { + handlerSet.params = {} handlerSet.possibleKeys.map((key) => { handlerSet.params[key] = params[key] }) @@ -103,7 +110,7 @@ export class Node { } search(method: string, path: string): [[T, Params][]] { - const handlerSets: HandlerSet[] = [] + const handlerSets: HandlerParamsSet[] = [] const params: Record = {} this.params = {} @@ -126,11 +133,9 @@ export class Node { if (isLast === true) { // '/hello/*' => match '/hello' if (nextNode.children['*']) { - handlerSets.push( - ...this.gHSets(nextNode.children['*'], method, { ...params, ...node.params }) - ) + handlerSets.push(...this.gHSets(nextNode.children['*'], method, node.params)) } - handlerSets.push(...this.gHSets(nextNode, method, { ...params, ...node.params })) + handlerSets.push(...this.gHSets(nextNode, method, node.params)) } else { tempNodes.push(nextNode) } @@ -144,7 +149,7 @@ export class Node { if (pattern === '*') { const astNode = node.children['*'] if (astNode) { - handlerSets.push(...this.gHSets(astNode, method, { ...params, ...node.params })) + handlerSets.push(...this.gHSets(astNode, method, node.params)) tempNodes.push(astNode) } continue