Skip to content
Myr

Implemented "all"

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 53edcda · Browse files · 4 files+100 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

4 files+100 added−24 removed

  • tests:+31 added−8 removed
  • other:+69 added−16 removed

4 files+100 added−24 removed

modified:example/basic/index.js+18 added
modified:src/hono.js+9 added-3 removed
modified:src/router.js+42 added-13 removed
2 unmodified lines
3class Router {4 constructor() {5 this.node = new Node({ label: "/" })6 }7 add(path, stuff) {8 this.node.insert(path, stuff);7 add(method, path, handler) {8 return this.node.insert(method, path, handler);9 }10 match(path) {11 return this.node.search(path);10 match(method, path) {11 return this.node.search(method, path);12 }13}1415class Node {16 constructor({ label, stuff, children } = {}) {16 constructor({ label, method, handler, children } = {}) {17 this.label = label || "";18 this.stuff = stuff || {};18 this.children = children || [];19 this.method = {};20 if (method) {21 this['method'][method] = handler || {}22 }23 }2422 insert(path, stuff) {25 insert(method, path, handler) {26 let curNode = this27 if (path == '/') {28 curNode.label = path26 curNode.stuff = stuff29 curNode['method'][method] = handler30 }31 const ps = this.splitPath(path)32 for (const p of ps) {33 let nextNode = curNode.children[p]34 if (nextNode) {35 curNode = nextNode36 } else {34 curNode.children[p] = new Node({ label: p, stuff: stuff, children: [] })37 curNode.children[p] = new Node({38 method: method, label: p, handler: handler, children: []39 })40 curNode = curNode.children[p]41 }42 }43 return this44 }4546 splitPath(path) {47 const ps = []
25 unmodified lines
73 noRoute() {74 return null75 }7671 search(path) {7277 search(method, path) {78 let curNode = this79 const params = {}8081 for (const p of this.splitPath(path)) {
31 unmodified lines
113 if (isParamMatch == false) {114 return this.noRoute()115 }116 }117118 let handler = curNode['method']['all']119 if (!handler) {120 handler = curNode['method'][method];121 }122 if (!handler) {123 return this.noRoute()124 }112 return [curNode.stuff, params]125 return [handler, params]126 }127}128129const proxyHandler = {130 get: (target, prop) => (...args) => {131 if (target.constructor.prototype.hasOwnProperty(prop)) {132 return target[prop](...args)133 } else {134 return target.add(prop, ...args)135 }136 }137}138139const WrappedRouter = () => {140 return new Proxy(141 new Router(), proxyHandler142 )143}144116module.exports = Router145module.exports = WrappedRouter
modified:src/router.test.js+31 added-8 removed
1const Router = require('./router')23let router = new Router()43describe('root match', () => {4 let router = Router()5 let route = router.get('/', 'get handler')6 route = router.post('/', 'post handler')78 let match = router.match('get', '/')9 expect(match[0]).toBe('get handler')10 match = router.match('post', '/')11 expect(match[0]).toBe('post handler')12})1314describe.skip('all', () => {15 let router = Router()16 router.all('/hello', 'all hello')1718 let match = router.match('get', '/hello')19 expect(match[0]).toBe('all hello')20 match = router.match('post', '/hello')21 expect(match[0]).toBe('all hello')22})2324describe.skip('', () => {25 let router = Router()26 it('/ match', () => {7 router.add('/', 'root')8 let match = router.match('/')27 router.add('get', '/', 'root')28 let match = router.match('get', '/')29 expect(match).not.toBeNull()30 expect(match[0]).toBe('root')11 match = router.match('/foo')31 match = router.match('get', '/foo')32 expect(match).toBeNull()33 })3435 it('/hello match', () => {
4 unmodified lines
40 expect(match[0]).toBe('hello')41 })42})434424describe('path match', () => {45describe.skip('path match', () => {46 let router = Router()47 router.add('/entry/:id', 'entry-id')48 router.add('/entry/:id/:comment', 'entry-id-comment')49 router.add('/year/:year{[0-9]{4}}/:month{[0-9]{2}}', 'date-regex')50
24 unmodified lines
75 expect(match).toBeNull()76 })77})7857describe('wildcard', () => {79describe.skip('wildcard', () => {80 let router = Router()81 it('match', () => {59 router = new Router()82 router = Router()83 router.add('/abc/*/def')84 let match = router.match('/abc/xxx/def')85 expect(match).not.toBeNull()86 match = router.match('/abc/xxx/abc')
3 unmodified lines