Skip to content
Myr

fix(reg-exp-router): Copy from METHOD_NAME_ALL for new methods (#691)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(reg-exp-router): Copy from METHOD_NAME_ALL for new methods * chore: denoify * fix(reg-exp-router): Initialize both middleware and routes. * chore: denoify * fix(reg-exp-router):Fix middleware search routine. * chore: denoify

authored by Taku Amano · committed by GitHub · parent 07efdb5 · Browse files · 3 files+58 added−24 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+58 added−24 removed

  • tests:+40 added−0 removed
  • other:+18 added−24 removed

3 files+58 added−24 removed

modified:deno_dist/router/reg-exp-router/router.ts+9 added-12 removed
89 unmodified lines
90 throw new Error('Can not add a route since the matcher is already built.')91 }9293 if (!methodNames.includes(method)) methodNames.push(method)94 if (!middleware[method]) {95 ;[middleware, routes].forEach((handlerMap) => {96 handlerMap[method] = {}97 Object.keys(handlerMap[METHOD_NAME_ALL]).forEach((p) => {98 handlerMap[method][p] = [...handlerMap[METHOD_NAME_ALL][p]]99 })100 })101 }102103 if (path === '/*') {104 path = '*'105 }106107 if (/\*$/.test(path)) {100 middleware[method] ||= {}108 const re = buildWildcardRegExp(path)109 middleware[method][path] ||=110 findMiddleware(middleware[method], path) ||111 findMiddleware(middleware[METHOD_NAME_ALL], path) ||
4 unmodified lines
116 ;(path === '*' || re.test(p)) && middleware[m][p].push(handler)117 })118 }119 })113 if (method !== METHOD_NAME_ALL) {114 Object.keys(middleware[METHOD_NAME_ALL]).forEach((p) => {115 if (!middleware[method][p] && (path === '*' || re.test(p))) {116 middleware[method][p] = [...middleware[METHOD_NAME_ALL][p], handler]117 }118 })119 }120121 Object.keys(routes).forEach((m) => {122 if (method === METHOD_NAME_ALL || method === m) {123 Object.keys(routes[m]).forEach(
8 unmodified lines
132 const paths = checkOptionalParameter(path) || [path]133 for (let i = 0, len = paths.length; i < len; i++) {134 const path = paths[i]135136 routes[method] ||= {}137136 Object.keys(routes).forEach((m) => {137 if (method === METHOD_NAME_ALL || method === m) {138 routes[m][path] ||= [141 ...(routes[METHOD_NAME_ALL][path] ||142 findMiddleware(middleware[method], path) ||139 ...(findMiddleware(middleware[m], path) ||140 findMiddleware(middleware[METHOD_NAME_ALL], path) ||141 []),142 ]143 routes[m][path].push(handler)
74 unmodified lines
modified:src/router/reg-exp-router/router.test.ts+40 added
329 unmodified lines
330 expect(res?.handlers).toEqual(['long-prefix', 'long', 'star1', 'star2'])331 })332 })333})334335describe('static routes of ALL and GET', () => {336 const router = new RegExpRouter<string>()337338 router.add('ALL', '/foo', 'foo')339 router.add('GET', '/bar', 'bar')340341 it('get /foo', () => {342 const res = router.match('GET', '/foo')343 expect(res?.handlers).toEqual(['foo'])344 })345})346347describe('ALL and Star', () => {348 const router = new RegExpRouter<string>()349350 router.add('ALL', '/x', '/x')351 router.add('GET', '*', 'star')352353 it('Should return /x and star', async () => {354 const res = router.match('GET', '/x')355 expect(res).not.toBeNull()356 expect(res?.handlers).toEqual(['/x', 'star'])357 })358})359360describe('GET star, ALL static, GET star...', () => {361 const router = new RegExpRouter<string>()362363 router.add('GET', '*', 'star1')364 router.add('ALL', '/x', '/x')365 router.add('GET', '*', 'star2')366 router.add('GET', '*', 'star3')367368 it('Should return /x and star', async () => {369 const res = router.match('GET', '/x')370 expect(res).not.toBeNull()371 expect(res?.handlers).toEqual(['star1', '/x', 'star2', 'star3'])372 })373})
modified:src/router/reg-exp-router/router.ts+9 added-12 removed
89 unmodified lines
90 throw new Error('Can not add a route since the matcher is already built.')91 }9293 if (!methodNames.includes(method)) methodNames.push(method)94 if (!middleware[method]) {95 ;[middleware, routes].forEach((handlerMap) => {96 handlerMap[method] = {}97 Object.keys(handlerMap[METHOD_NAME_ALL]).forEach((p) => {98 handlerMap[method][p] = [...handlerMap[METHOD_NAME_ALL][p]]99 })100 })101 }102103 if (path === '/*') {104 path = '*'105 }106107 if (/\*$/.test(path)) {100 middleware[method] ||= {}108 const re = buildWildcardRegExp(path)109 middleware[method][path] ||=110 findMiddleware(middleware[method], path) ||111 findMiddleware(middleware[METHOD_NAME_ALL], path) ||
4 unmodified lines
116 ;(path === '*' || re.test(p)) && middleware[m][p].push(handler)117 })118 }119 })113 if (method !== METHOD_NAME_ALL) {114 Object.keys(middleware[METHOD_NAME_ALL]).forEach((p) => {115 if (!middleware[method][p] && (path === '*' || re.test(p))) {116 middleware[method][p] = [...middleware[METHOD_NAME_ALL][p], handler]117 }118 })119 }120121 Object.keys(routes).forEach((m) => {122 if (method === METHOD_NAME_ALL || method === m) {123 Object.keys(routes[m]).forEach(
8 unmodified lines
132 const paths = checkOptionalParameter(path) || [path]133 for (let i = 0, len = paths.length; i < len; i++) {134 const path = paths[i]135136 routes[method] ||= {}137136 Object.keys(routes).forEach((m) => {137 if (method === METHOD_NAME_ALL || method === m) {138 routes[m][path] ||= [141 ...(routes[METHOD_NAME_ALL][path] ||142 findMiddleware(middleware[method], path) ||139 ...(findMiddleware(middleware[m], path) ||140 findMiddleware(middleware[METHOD_NAME_ALL], path) ||141 []),142 ]143 routes[m][path].push(handler)
74 unmodified lines