Skip to content
Myr

Merge pull request #10 from yusukebe/feature/dont-use-proxy

Before adoption: this commit came in with the history the project adopted, so no landing records it.

New API

authored by Yusuke Wada · committed by GitHub · parents 16350cb, 3198fa0 · Browse files · 12 files+116 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

12 files+116 added−38 removed

  • tests:+9 added−8 removed
  • other:+107 added−30 removed

12 files+116 added−38 removed

modified:benchmark/handleEvent/index.js+2 added-2 removed
modified:benchmark/webapp/hono/index.js+2 added-2 removed
modified:example/basic/index.js+2 added-2 removed
modified:example/basic/README.md+2 added-2 removed
modified:example/compute-at-edge/index.js+2 added-2 removed
1const Hono = require('hono')2const app = Hono()1const { Hono } = require('hono')2const app = new Hono()34app.use('*', (c, next) => {5 console.log(`[${c.req.method}] ${c.req.url}`)6 next()
11 unmodified lines
added:src/hono.d.ts+42 added
1declare class FetchEvent {}2declare class Request {}3declare class Response {}4declare class Context {}56declare class Node {}78declare class Router {9 tempPath: string10 node: Node1112 add(method: string, path: string, handlers: any[]): Node13 match(method: string, path: string): Node14}1516export class Hono {17 router: Router18 middlewareRouters: Router[]1920 getRouter(): Router21 addRoute(method: string, args: any[]): Hono22 matchRoute(method: string, path: string): Node23 createContext(req: Request, res: Response): Context24 dispatch(req: Request, res: Response): Response25 handleEvent(event: FetchEvent): Response26 fire(): void2728 notFound(): Response2930 route(path: string): Hono3132 use(path: string, middleware: any): void3334 all(path: string, handler: any): Hono35 get(path: string, handler: any): Hono36 post(path: string, handler: any): Hono37 put(path: string, handler: any): Hono38 head(path: string, handler: any): Hono39 delete(path: string, handler: any): Hono40}4142export class Middleware {}
modified:src/hono.js+20 added-18 removed
1'use strict'23const Node = require('./node')4const compose = require('./compose')5const methods = require('./methods')6const defaultFilter = require('./middleware/defaultFilter')7const Middleware = require('./middleware')89const METHOD_NAME_OF_ALL = 'ALL'1011class Router {
16 unmodified lines
28 const match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/)29 return match[5]30}3130const proxyHandler = {31 get:32 (target, prop) =>33 (...args) => {34 if (target.constructor.prototype.hasOwnProperty(prop)) {35 return target[prop](...args)36 } else {37 return target.addRoute(prop, ...args)38 }39 },40}4132class Hono {33 constructor() {34 this.router = new Router()35 this.middlewareRouters = []3637 for (const method of methods) {38 this[method] = (...args) => {39 return this.addRoute(method, ...args)40 }41 }42 }4344 all(...args) {45 this.addRoute('ALL', ...args)46 }4748 getRouter() {49 return this.router50 }51
3 unmodified lines
55 this.router.add(method, this.router.tempPath, ...args)56 } else {57 this.router.add(method, ...args)58 }59 return WrappedApp(this)59 return this60 }6162 route(path) {63 this.router.tempPath = path64 return WrappedApp(this)64 return this65 }6667 use(path, middleware) {68 const router = new Router()
63 unmodified lines
132 return new Response('Not Found', { status: 404 })133 }134}135136const WrappedApp = (hono = new Hono()) => {137 return new Proxy(hono, proxyHandler)138}136// Default Export137module.exports = Hono138exports = module.exports139140module.exports = WrappedApp140// Named Export141exports.Hono = Hono142exports.Middleware = Middleware
modified:src/hono.test.js+5 added-4 removed
1const Hono = require('./hono')1const fetch = require('node-fetch')2const { Hono } = require('./hono')34describe('GET Request', () => {5 const app = Hono()5 const app = new Hono()67 app.get('/hello', () => {8 return new fetch.Response('hello', {9 status: 200,10 })
18 unmodified lines
29 })30})3132describe('params and query', () => {32 const app = Hono()33 const app = new Hono()3435 app.get('/entry/:id', async (c) => {36 const id = await c.req.params('id')37 return new fetch.Response(`id is ${id}`, {
22 unmodified lines
60 })61})6263describe('Middleware', () => {63 const app = Hono()64 const app = new Hono()6566 const logger = async (c, next) => {67 console.log(`${c.req.method} : ${c.req.url}`)68 next()
47 unmodified lines
added:src/methods.js+30 added
1const methods = [2 'get',3 'post',4 'put',5 'head',6 'delete',7 'options',8 'trace',9 'copy',10 'lock',11 'mkcol',12 'move',13 'patch',14 'purge',15 'propfind',16 'proppatch',17 'unlock',18 'report',19 'mkactivity',20 'checkout',21 'merge',22 'm-search',23 'notify',24 'subscribe',25 'unsubscribe',26 'search',27 'connect',28]2930module.exports = methods
added:src/middleware.js+3 added
1class Middleware {}23module.exports = Middleware
modified:src/router.test.js+4 added-4 removed
1const App = require('./hono')1const { Hono } = require('./hono')23describe('Basic Usage', () => {4 const router = App()4 const router = new Hono()56 it('get, post hello', async () => {7 router.get('/hello', 'get hello')8 router.post('/hello', 'post hello')
14 unmodified lines
23 })24})2526describe('Complex', () => {27 let router = App()27 let router = new Hono()2829 it('Named Param', async () => {30 router.get('/entry/:id', 'get entry')31 res = await router.matchRoute('GET', '/entry/123')
23 unmodified lines
55 })56})5758describe('Chained Route', () => {59 let router = App()59 let router = new Hono()6061 it('Return rooter object', async () => {62 router = router.patch('/hello', 'patch hello')63 expect(router).not.toBeNull()
25 unmodified lines
modified:README.md+2 added-2 removed
1 unmodified line
23Hono [炎] - Tiny web framework for Cloudflare Workers and others.45```js6const Hono = require('hono')7const app = Hono()6const { Hono } = require('hono')7const app = new Hono()89app.get('/', () => new Response('Hono!!'))1011app.fire()
166 unmodified lines