Use proxy handler
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 21f682d · Browse files · 3 files·+22 added−27 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+22 added−27 removed
- tests:+1 added−1 removed
- other:+21 added−26 removed
Provenance
- Commit
de6b5bd- Tree
367ac80- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
21f682d
3 files changed+22 added−27 removed
modified:benchmark/handleEvent/index.js+1 added-1 removed
12334567113 unmodified linesExpand allimport Benchmark from 'benchmark'import { makeEdgeEnv } from 'edge-mock'import Hono from '../src/hono.js'import Hono from '../../src/hono.js'import itty from 'itty-router'const { Router: IttyRouter } = ittyimport { Router as SunderRouter, Sunder } from 'sunder'
113 unmodified linesExpand allmodified:src/hono.js+20 added-25 removed
23 unmodified lines24 const match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/)25 return match[5]26 }27 28+const proxyHandler = {29+ get:30+ (target, prop) =>31+ (...args) => {32+ if (target.constructor.prototype.hasOwnProperty(prop)) {33+ return target[prop](...args)34+ } else {35+ return target.addRoute(prop, ...args)36+ }37+ },38+}39+40 class Hono {41 constructor() {42 this.router = new Router()43 this.middlewareRouter = new Router()44 this.middlewareRouters = []45 }46 35- get(...args) {36- return this.addRoute('GET', ...args)47+ getRouter() {48+ return this.router49 }38- post(...args) {39- return this.addRoute('POST', ...args)40- }41- put(...args) {42- return this.addRoute('PUT', ...args)43- }44- delete(...args) {45- return this.addRoute('DELETE', ...args)46- }47- patch(...args) {48- return this.addRoute('PATCH', ...args)49- }50 51 addRoute(method, ...args) {52 method = method.toUpperCase()53 if (args.length === 1) {54 this.router.add(method, this.router.tempPath, ...args)55 } else {56 this.router.add(method, ...args)57 }58- return this58+ return WrappedApp(this)59 }60 61- getRouter() {62- return this.router63- }64-61 route(path) {62 this.router.tempPath = path67- return this63+ return WrappedApp(this)64 }65 66 use(path, middleware) {67 const router = new Router()1 unmodified line69 this.middlewareRouters.push(router)70 }71 72 async matchRoute(method, path) {77- const res = this.router.match(method, path)78- return res73+ return this.router.match(method, path)74 }75 76 // XXX77 async createContext(req, res) {53 unmodified lines131 return new Response('Not Found', { status: 404 })132 }133 }134 140-const CreateApp = () => {141- return new Hono()135+const WrappedApp = (hono = new Hono()) => {136+ return new Proxy(hono, proxyHandler)137 }138 144-module.exports = CreateApp139+module.exports = WrappedAppmodified:src/util.test.js+1 added-1 removed
11234524 unmodified linesconst { splitPath, getPattern, getParamName } = require('./util')const { splitPath, getPattern } = require('./util')
describe('Utility methods', () => { it('splitPath', () => { let ps = splitPath('/')24 unmodified lines