Skip to content
Myr

feat(reg-exp-router): support optional parameter (#542)

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

* feat(reg-exp-router): support optional parameter * chore: denoify

authored by Taku Amano · committed by Yusuke Wada · parent 24c17e3 · Browse files · 3 files+51 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+51 added−24 removed

  • tests:+15 added−0 removed
  • other:+36 added−24 removed

3 files+51 added−24 removed

modified:deno_dist/router/reg-exp-router/router.ts+18 added-12 removed
1/* eslint-disable @typescript-eslint/ban-ts-comment */2import type { Router, Result } from '../../router.ts'3import { METHOD_NAME_ALL, METHODS, UnsupportedPathError } from '../../router.ts'4import { checkOptionalParameter } from '../../utils/url.ts'5import { PATH_ERROR } from './node.ts'6import type { ParamMap } from './trie.ts'7import { Trie } from './trie.ts'8
104 unmodified lines
113114 return115 }116116 routes[method] ||= {}117 routes[method][path] ||= [118 ...(routes[METHOD_NAME_ALL][path] ||119 findMiddleware(middleware[method], path) ||120 findMiddleware(middleware[METHOD_NAME_ALL], path) ||121 []),122 ]117 const paths = checkOptionalParameter(path) || [path]118 for (let i = 0, len = paths.length; i < len; i++) {119 const path = paths[i]120121 routes[method] ||= {}122 routes[method][path] ||= [123 ...(routes[METHOD_NAME_ALL][path] ||124 findMiddleware(middleware[method], path) ||125 findMiddleware(middleware[METHOD_NAME_ALL], path) ||126 []),127 ]128124 Object.keys(routes).forEach((m) => {125 ;(method === METHOD_NAME_ALL || method === m) &&126 routes[m][path] &&127 routes[m][path].push(handler)128 })129 Object.keys(routes).forEach((m) => {130 ;(method === METHOD_NAME_ALL || method === m) &&131 routes[m][path] &&132 routes[m][path].push(handler)133 })134 }135 }136137 match(method: string, path: string): Result<T> | null {138 const matchers = this.buildAllMatchers()
67 unmodified lines
modified:src/router/reg-exp-router/router.test.ts+15 added
247 unmodified lines
248 expect(res).not.toBeNull()249 expect(res?.handlers).toEqual(['/*', '*', '/x', '/x/*'])250 })251})252253describe('Optional route', () => {254 const router = new RegExpRouter<string>()255 router.add('GET', '/api/animals/:type?', 'animals')256 it('GET /api/animals/dog', async () => {257 const res = router.match('GET', '/api/animals/dog')258 expect(res?.handlers).toEqual(['animals'])259 expect(res?.params['type']).toBe('dog')260 })261 it('GET /api/animals', async () => {262 const res = router.match('GET', '/api/animals')263 expect(res?.handlers).toEqual(['animals'])264 expect(res?.params['type']).toBeUndefined()265 })266})
modified:src/router/reg-exp-router/router.ts+18 added-12 removed
1/* eslint-disable @typescript-eslint/ban-ts-comment */2import type { Router, Result } from '../../router'3import { METHOD_NAME_ALL, METHODS, UnsupportedPathError } from '../../router'4import { checkOptionalParameter } from '../../utils/url'5import { PATH_ERROR } from './node'6import type { ParamMap } from './trie'7import { Trie } from './trie'8
104 unmodified lines
113114 return115 }116116 routes[method] ||= {}117 routes[method][path] ||= [118 ...(routes[METHOD_NAME_ALL][path] ||119 findMiddleware(middleware[method], path) ||120 findMiddleware(middleware[METHOD_NAME_ALL], path) ||121 []),122 ]117 const paths = checkOptionalParameter(path) || [path]118 for (let i = 0, len = paths.length; i < len; i++) {119 const path = paths[i]120121 routes[method] ||= {}122 routes[method][path] ||= [123 ...(routes[METHOD_NAME_ALL][path] ||124 findMiddleware(middleware[method], path) ||125 findMiddleware(middleware[METHOD_NAME_ALL], path) ||126 []),127 ]128124 Object.keys(routes).forEach((m) => {125 ;(method === METHOD_NAME_ALL || method === m) &&126 routes[m][path] &&127 routes[m][path].push(handler)128 })129 Object.keys(routes).forEach((m) => {130 ;(method === METHOD_NAME_ALL || method === m) &&131 routes[m][path] &&132 routes[m][path].push(handler)133 })134 }135 }136137 match(method: string, path: string): Result<T> | null {138 const matchers = this.buildAllMatchers()
67 unmodified lines