Skip to content
Myr

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

7 files+84 added−2 removed

modified:deno_dist/router/trie-router/router.ts+9 added
modified:deno_dist/utils/encode.ts+1 added-1 removed
modified:deno_dist/utils/url.ts+14 added
modified:src/router/trie-router/router.test.ts+15 added
143 unmodified lines
144 expect(res?.handlers).toEqual(['slug'])145 expect(res?.params['slug']).toBe('foo')146 })147})148149describe('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
1import type { Result, Router } from '../../router'2import { checkOptionalParameter } from '../../utils/url'3import { Node } from './node'45export class TrieRouter<T> implements Router<T> {6 node: Node<T>
2 unmodified lines
9 this.node = new Node()10 }1112 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 }2021 this.node.insert(method, path, handler)22 }2324 match(method: string, path: string): Result<T> | null {
3 unmodified lines
modified:src/utils/url.test.ts+22 added-1 removed
1import { splitPath, getPattern, getPathFromURL, mergePath, getQueryStringFromURL } from './url'1import {2 splitPath,3 getPattern,4 getPathFromURL,5 mergePath,6 getQueryStringFromURL,7 checkOptionalParameter,8} from './url'910describe('url', () => {11 it('splitPath', () => {12 let ps = splitPath('/')
99 unmodified lines
112 it('Should be `/`', () => {113 expect(mergePath('/', '/')).toBe('/')114 })115 })116117 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 lines
84 }8586 return p87}8889export 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 null9798 const base = match[1]99 const optional = base + match[2]100 return [base, optional]101}