Skip to content
Myr

fix(routers): RegExpRouter and LinearRouter support multiple optional params (#1951)

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

* test(router): add optional params tests * fix(routers): RegExpRouter and LinearRouter support multiple optional params * fixed RegExpRouter * denoify

authored by Yusuke Wada · committed by GitHub · parent e8753d7 · Browse files · 8 files+98 added−22 removed
This commit

State

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

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

8 files+98 added−22 removed

  • tests:+88 added−0 removed
  • other:+10 added−22 removed

8 files+98 added−22 removed

modified:deno_dist/router/linear-router/router.ts+4 added-7 removed
modified:deno_dist/router/reg-exp-router/router.ts+1 added-4 removed
196 unmodified lines
197 ...(findMiddleware(middleware[m], path) ||198 findMiddleware(middleware[METHOD_NAME_ALL], path) ||199 []),200 ]201 routes[m][path].push([202 handler,203 paths.length === 2 && i === 0 ? paramCount - 1 : paramCount,204 ])201 routes[m][path].push([handler, paramCount - len + i + 1])202 }203 })204 }205 }
68 unmodified lines
modified:src/router/linear-router/router.test.ts+22 added
170 unmodified lines
171 const [res] = router.match('GET', '/api/animals')172 expect(res[0][0]).toEqual('animals')173 expect(res[0][1]['type']).toBeUndefined()174 })175 router.add('GET', '/v1/:version?/:platform?', 'result')176 it('GET /v1/123/abc', () => {177 const [res] = router.match('GET', '/v1/123/abc')178 expect(res.length).toBe(1)179 expect(res[0][0]).toEqual('result')180 expect(res[0][1]['version']).toBe('123')181 expect(res[0][1]['platform']).toBe('abc')182 })183 it('GET /v1/123', () => {184 const [res] = router.match('GET', '/v1/123')185 expect(res.length).toBe(1)186 expect(res[0][0]).toEqual('result')187 expect(res[0][1]['version']).toBe('123')188 expect(res[0][1]['platform']).toBeUndefined()189 })190 it('GET /v1', () => {191 const [res] = router.match('GET', '/v1')192 expect(res.length).toBe(1)193 expect(res[0][0]).toEqual('result')194 expect(res[0][1]['version']).toBeUndefined()195 expect(res[0][1]['platform']).toBeUndefined()196 })197})198199describe('Trailing slash', () => {200 const router = new LinearRouter<string>()
152 unmodified lines
modified:src/router/linear-router/router.ts+4 added-7 removed
1import type { Router, Result, Params } from '../../router'2import { METHOD_NAME_ALL, UnsupportedPathError } from '../../router'3import { checkOptionalParameter } from '../../utils/url'45type RegExpMatchArrayWithIndices = RegExpMatchArray & { indices: [number, number][] }67const emptyParams = {}
4 unmodified lines
12 name: string = 'LinearRouter'13 routes: [string, string, T][] = []1415 add(method: string, path: string, handler: T) {15 if (path.charCodeAt(path.length - 1) === 63) {16 // /path/to/:label? means /path/to/:label or /path/to17 this.routes.push([method, path.slice(0, -1), handler])18 this.routes.push([method, path.replace(/\/[^/]+$/, ''), handler])19 } else {20 this.routes.push([method, path, handler])21 }16 ;(checkOptionalParameter(path) || [path]).forEach((p) => {17 this.routes.push([method, p, handler])18 })19 }2021 match(method: string, path: string): Result<T> {22 const handlers: [T, Params][] = []
110 unmodified lines
modified:src/router/pattern-router/router.test.ts+22 added
169 unmodified lines
170 const [res] = router.match('GET', '/api/animals')171 expect(res[0][0]).toEqual('animals')172 expect(res[0][1]['type']).toBeUndefined()173 })174 router.add('GET', '/v1/:version?/:platform?', 'result')175 it('GET /v1/123/abc', () => {176 const [res] = router.match('GET', '/v1/123/abc')177 expect(res.length).toBe(1)178 expect(res[0][0]).toEqual('result')179 expect(res[0][1]['version']).toBe('123')180 expect(res[0][1]['platform']).toBe('abc')181 })182 it('GET /v1/123', () => {183 const [res] = router.match('GET', '/v1/123')184 expect(res.length).toBe(1)185 expect(res[0][0]).toEqual('result')186 expect(res[0][1]['version']).toBe('123')187 expect(res[0][1]['platform']).toBeUndefined()188 })189 it('GET /v1', () => {190 const [res] = router.match('GET', '/v1')191 expect(res.length).toBe(1)192 expect(res[0][0]).toEqual('result')193 expect(res[0][1]['version']).toBeUndefined()194 expect(res[0][1]['platform']).toBeUndefined()195 })196})197198describe('routing order with named parameters', () => {199 const router = new PatternRouter<string>()
371 unmodified lines
modified:src/router/reg-exp-router/router.test.ts+22 added
342 unmodified lines
343 expect(res.length).toBe(1)344 expect(res[0][0]).toEqual('animals')345 expect(stash?.[res[0][1]['type'] as number]).toBeUndefined()346 })347 router.add('GET', '/v1/:version?/:platform?', 'result')348 it('GET /v1/123/abc', () => {349 const [res, stash] = router.match('GET', '/v1/123/abc')350 expect(res.length).toBe(1)351 expect(res[0][0]).toEqual('result')352 expect(stash?.[res[0][1]['version'] as number]).toBe('123')353 expect(stash?.[res[0][1]['platform'] as number]).toBe('abc')354 })355 it('GET /v1/123', () => {356 const [res, stash] = router.match('GET', '/v1/123')357 expect(res.length).toBe(1)358 expect(res[0][0]).toEqual('result')359 expect(stash?.[res[0][1]['version'] as number]).toBe('123')360 expect(stash?.[res[0][1]['platform'] as number]).toBeUndefined()361 })362 it('GET /v1', () => {363 const [res, stash] = router.match('GET', '/v1')364 expect(res.length).toBe(1)365 expect(res[0][0]).toEqual('result')366 expect(stash?.[res[0][1]['version'] as number]).toBeUndefined()367 expect(stash?.[res[0][1]['platform'] as number]).toBeUndefined()368 })369})370371describe('All', () => {372 const router = new RegExpRouter<string>()
308 unmodified lines
modified:src/router/reg-exp-router/router.ts+1 added-4 removed
196 unmodified lines
197 ...(findMiddleware(middleware[m], path) ||198 findMiddleware(middleware[METHOD_NAME_ALL], path) ||199 []),200 ]201 routes[m][path].push([202 handler,203 paths.length === 2 && i === 0 ? paramCount - 1 : paramCount,204 ])201 routes[m][path].push([handler, paramCount - len + i + 1])202 }203 })204 }205 }
68 unmodified lines
modified:src/router/trie-router/router.test.ts+22 added
170 unmodified lines
171 const [res] = router.match('GET', '/api/animals')172 expect(res[0][0]).toEqual('animals')173 expect(res[0][1]['type']).toBeUndefined()174 })175 router.add('GET', '/v1/:version?/:platform?', 'result')176 it('GET /v1/123/abc', () => {177 const [res] = router.match('GET', '/v1/123/abc')178 expect(res.length).toBe(1)179 expect(res[0][0]).toEqual('result')180 expect(res[0][1]['version']).toBe('123')181 expect(res[0][1]['platform']).toBe('abc')182 })183 it('GET /v1/123', () => {184 const [res] = router.match('GET', '/v1/123')185 expect(res.length).toBe(1)186 expect(res[0][0]).toEqual('result')187 expect(res[0][1]['version']).toBe('123')188 expect(res[0][1]['platform']).toBeUndefined()189 })190 it('GET /v1', () => {191 const [res] = router.match('GET', '/v1')192 expect(res.length).toBe(1)193 expect(res[0][0]).toEqual('result')194 expect(res[0][1]['version']).toBeUndefined()195 expect(res[0][1]['platform']).toBeUndefined()196 })197})198199describe('routing order with named parameters', () => {200 const router = new TrieRouter<string>()
43 unmodified lines