Refactored
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 ceedeed · Browse files · 1 file·+13 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+13 added−20 removed
- other:+13 added−20 removed
Provenance
- Commit
bdfb9d7- Tree
c5f9461- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
ceedeed
1 file changed+13 added−20 removed
modified:src/hono.js+13 added-20 removed
6 unmodified lines7891011111213147 unmodified lines2223242527262728293023 unmodified lines545556575960615859606162676364656667686970717672737479807576777883798085818283848514 unmodified lines10010110210310810910410511111211310610710811611710911011111211311411 unmodified lines1261271281291371301311321331411346 unmodified lines }
add(method, path, ...handlers) { this.node.insert(method, path, handlers) return WrappedRouter(this) }
match(method, path) { method = method.toLowerCase()7 unmodified lines (...args) => { if (target.constructor.prototype.hasOwnProperty(prop)) { return target[prop](...args) } else { if (args.length === 1) { if (args.length == 1) { return target.addRoute(prop, target.router.tempPath, ...args) } return target.addRoute(prop, ...args) }23 unmodified lines
class App { constructor() { this.router = new Router() this.routerBefore = new Router() this.routerAfter = new Router() this.middleware = [] this.middlewareRouter = new Router() }
addRoute(method, path, ...args) { this.router.add(method, path, ...args) return WrappedRouter(this) return WrappedApp(this) }
matchRoute(method, path) { return this.router.match(method, path) }
route(path) { this.router.tempPath = path return WrappedRouter(this) return WrappedApp(this) }
use(path, fn) { fn = [fn] use(path, middleware) { middleware = [middleware] const result = this.middlewareRouter.match('all', path) if (result) { fn.push(...result.handler) middleware.push(...result.handler) } this.middlewareRouter.add('all', path, ...fn) this.middlewareRouter.add('all', path, ...middleware) }
async dispatch(request, response) { const url = new URL(request.url)14 unmodified lines if (mwResult) { middleware.push(...mwResult.handler) }
const handlers = result.handler for (const handler of handlers) { for (const handler of result.handler) { const handleReponse = handler(request) if (handleReponse) { response = handleReponse } if (handleReponse) response = handleReponse }
const fn = compose(middleware) fn(request, response) const composed = compose(middleware) composed(request, response)
return response }
11 unmodified lines return new Response('Not Found', { status: 404 }) }}
const WrappedRouter = (router = new App()) => {const WrappedApp = (router = new App()) => { return new Proxy(router, proxyHandler)}
module.exports = WrappedRoutermodule.exports = WrappedApp