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
Provenance
- Commit
2a80b58- Tree
bcab27f- Author
Taku Amano <taku@taaas.jp>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
24c17e3
3 files changed+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 */2 import type { Router, Result } from '../../router.ts'3 import { METHOD_NAME_ALL, METHODS, UnsupportedPathError } from '../../router.ts'4+import { checkOptionalParameter } from '../../utils/url.ts'5 import { PATH_ERROR } from './node.ts'6 import type { ParamMap } from './trie.ts'7 import { Trie } from './trie.ts'8 104 unmodified lines113 114 return115 }116 116- 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]120 121+ 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+ ]128+124- 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 }136 137 match(method: string, path: string): Result<T> | null {138 const matchers = this.buildAllMatchers()67 unmodified linesmodified:src/router/reg-exp-router/router.test.ts+15 added
247 unmodified lines248 expect(res).not.toBeNull()249 expect(res?.handlers).toEqual(['/*', '*', '/x', '/x/*'])250 })251 })252+253+describe('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 */2 import type { Router, Result } from '../../router'3 import { METHOD_NAME_ALL, METHODS, UnsupportedPathError } from '../../router'4+import { checkOptionalParameter } from '../../utils/url'5 import { PATH_ERROR } from './node'6 import type { ParamMap } from './trie'7 import { Trie } from './trie'8 104 unmodified lines113 114 return115 }116 116- 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]120 121+ 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+ ]128+124- 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 }136 137 match(method: string, path: string): Result<T> | null {138 const matchers = this.buildAllMatchers()67 unmodified lines