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
Provenance
- Commit
34a7ada- Tree
84e716f- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
e8753d7
8 files changed+98 added−22 removed
modified:deno_dist/router/linear-router/router.ts+4 added-7 removed
12345674 unmodified lines121314151516171819202116171819202122110 unmodified linesExpand allimport type { Router, Result, Params } from '../../router.ts'import { METHOD_NAME_ALL, UnsupportedPathError } from '../../router.ts'import { checkOptionalParameter } from '../../utils/url.ts'
type RegExpMatchArrayWithIndices = RegExpMatchArray & { indices: [number, number][] }
const emptyParams = {}4 unmodified lines name: string = 'LinearRouter' routes: [string, string, T][] = []
add(method: string, path: string, handler: T) { if (path.charCodeAt(path.length - 1) === 63) { // /path/to/:label? means /path/to/:label or /path/to this.routes.push([method, path.slice(0, -1), handler]) this.routes.push([method, path.replace(/\/[^/]+$/, ''), handler]) } else { this.routes.push([method, path, handler]) } ;(checkOptionalParameter(path) || [path]).forEach((p) => { this.routes.push([method, p, handler]) }) }
match(method: string, path: string): Result<T> { const handlers: [T, Params][] = []110 unmodified linesExpand allmodified:deno_dist/router/reg-exp-router/router.ts+1 added-4 removed
196 unmodified lines197 ...(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 linesmodified:src/router/linear-router/router.test.ts+22 added
170 unmodified lines171 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 })198 199 describe('Trailing slash', () => {200 const router = new LinearRouter<string>()152 unmodified linesmodified:src/router/linear-router/router.ts+4 added-7 removed
1 import type { Router, Result, Params } from '../../router'2 import { METHOD_NAME_ALL, UnsupportedPathError } from '../../router'3+import { checkOptionalParameter } from '../../utils/url'4 5 type RegExpMatchArrayWithIndices = RegExpMatchArray & { indices: [number, number][] }6 7 const emptyParams = {}4 unmodified lines12 name: string = 'LinearRouter'13 routes: [string, string, T][] = []14 15 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 }20 21 match(method: string, path: string): Result<T> {22 const handlers: [T, Params][] = []110 unmodified linesmodified:src/router/pattern-router/router.test.ts+22 added
169 unmodified lines170 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 })197 198 describe('routing order with named parameters', () => {199 const router = new PatternRouter<string>()371 unmodified linesmodified:src/router/reg-exp-router/router.test.ts+22 added
342 unmodified lines343 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 })370 371 describe('All', () => {372 const router = new RegExpRouter<string>()308 unmodified linesmodified:src/router/reg-exp-router/router.ts+1 added-4 removed
196 unmodified lines197 ...(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 linesmodified:src/router/trie-router/router.test.ts+22 added
170 unmodified lines171 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 })198 199 describe('routing order with named parameters', () => {200 const router = new TrieRouter<string>()43 unmodified lines