feat(trie-router): support optional parameter (#540)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by Yusuke Wada · parent 730453a · Browse files · 7 files·+84 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+84 added−2 removed
- tests:+37 added−1 removed
- other:+47 added−1 removed
Provenance
- Commit
24c17e3- Tree
43edeaa- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
730453a
7 files changed+84 added−2 removed
modified:deno_dist/router/trie-router/router.ts+9 added
1234562 unmodified lines91011121314151617181920212223243 unmodified linesimport type { Result, Router } from '../../router.ts'import { checkOptionalParameter } from '../../utils/url.ts'import { Node } from './node.ts'
export class TrieRouter<T> implements Router<T> { node: Node<T>2 unmodified lines this.node = new Node() }
add(method: string, path: string, handler: T) { const results = checkOptionalParameter(path) if (results) { for (const p of results) { this.node.insert(method, p, handler) } return }
this.node.insert(method, path, handler) }
match(method: string, path: string): Result<T> | null {3 unmodified linesmodified:deno_dist/utils/encode.ts+1 added-1 removed
11234572 unmodified linesimport { Buffer } from "https://deno.land/std@0.156.0/node/buffer.ts";import { Buffer } from "https://deno.land/std@0.155.0/node/buffer.ts";export const encodeBase64 = (str: string): string => { if (str === null) { throw new TypeError('1st argument of "encodeBase64" should not be null.') }72 unmodified linesmodified:deno_dist/utils/url.ts+14 added
83 unmodified lines8485868788899091929394959697989910010183 unmodified lines }
return p}
export const checkOptionalParameter = (path: string): string[] | null => { /* If path is `/api/animals/:type?` it will return: [`/api/animals`, `/api/animals/:type`] in other cases it will return null */ const match = path.match(/(^.+)(\/\:[^\/]+)\?$/) if (!match) return null
const base = match[1] const optional = base + match[2] return [base, optional]}modified:src/router/trie-router/router.test.ts+15 added
143 unmodified lines144 expect(res?.handlers).toEqual(['slug'])145 expect(res?.params['slug']).toBe('foo')146 })147 })148+149+describe('Optional route', () => {150+ const router = new TrieRouter<string>()151+ router.add('GET', '/api/animals/:type?', 'animals')152+ it('GET /api/animals/dog', async () => {153+ const res = router.match('GET', '/api/animals/dog')154+ expect(res?.handlers).toEqual(['animals'])155+ expect(res?.params['type']).toBe('dog')156+ })157+ it('GET /api/animals', async () => {158+ const res = router.match('GET', '/api/animals')159+ expect(res?.handlers).toEqual(['animals'])160+ expect(res?.params['type']).toBeUndefined()161+ })162+})modified:src/router/trie-router/router.ts+9 added
1 import type { Result, Router } from '../../router'2+import { checkOptionalParameter } from '../../utils/url'3 import { Node } from './node'4 5 export class TrieRouter<T> implements Router<T> {6 node: Node<T>2 unmodified lines9 this.node = new Node()10 }11 12 add(method: string, path: string, handler: T) {13+ const results = checkOptionalParameter(path)14+ if (results) {15+ for (const p of results) {16+ this.node.insert(method, p, handler)17+ }18+ return19+ }20+21 this.node.insert(method, path, handler)22 }23 24 match(method: string, path: string): Result<T> | null {3 unmodified linesmodified:src/utils/url.test.ts+22 added-1 removed
1-import { splitPath, getPattern, getPathFromURL, mergePath, getQueryStringFromURL } from './url'1+import {2+ splitPath,3+ getPattern,4+ getPathFromURL,5+ mergePath,6+ getQueryStringFromURL,7+ checkOptionalParameter,8+} from './url'9 10 describe('url', () => {11 it('splitPath', () => {12 let ps = splitPath('/')99 unmodified lines112 it('Should be `/`', () => {113 expect(mergePath('/', '/')).toBe('/')114 })115 })116+117+ describe('checkOptionalParameter', () => {118+ it('checkOptionalParameter', () => {119+ expect(checkOptionalParameter('/api/animals/:type?')).toEqual([120+ '/api/animals',121+ '/api/animals/:type',122+ ])123+ expect(checkOptionalParameter('/api/animals/type?')).toBeNull()124+ expect(checkOptionalParameter('/api/animals/:type')).toBeNull()125+ expect(checkOptionalParameter('/api/animals')).toBeNull()126+ expect(checkOptionalParameter('/api/:animals?/type')).toBeNull()127+ expect(checkOptionalParameter('/api/animals/:type?/')).toBeNull()128+ })129+ })130 })modified:src/utils/url.ts+14 added
83 unmodified lines84 }85 86 return p87 }88+89+export const checkOptionalParameter = (path: string): string[] | null => {90+ /*91+ If path is `/api/animals/:type?` it will return:92+ [`/api/animals`, `/api/animals/:type`]93+ in other cases it will return null94+ */95+ const match = path.match(/(^.+)(\/\:[^\/]+)\?$/)96+ if (!match) return null97+98+ const base = match[1]99+ const optional = base + match[2]100+ return [base, optional]101+}