Use named import, add types by .d.ts file
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 71aac69 · Browse files · 11 files·+74 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+74 added−23 removed
- tests:+9 added−8 removed
- other:+65 added−15 removed
Provenance
- Commit
3198fa0- Tree
98087dd- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
71aac69
11 files changed+74 added−23 removed
modified:benchmark/handleEvent/index.js+2 added-2 removed
1233456789101011121314106 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'
makeEdgeEnv()
const hono = Hono()const hono = new Hono()hono.get('/user', () => new Response('User'))hono.get('/user/comments', () => new Response('User Comments'))hono.get('/user/avatar', () => new Response('User Avatar'))hono.get('/user/lookup/username/:username', () => new Response('User Lookup Username'))106 unmodified linesExpand allmodified:benchmark/webapp/hono/index.js+2 added-2 removed
11233456717 unmodified linesconst Hono = require('../../../src/hono')const { Hono } = require('../../../src/hono')
const hono = Hono()const hono = new Hono()hono.get('/user', () => new Response('User'))hono.get('/user/comments', () => new Response('User Comments'))hono.get('/user/avatar', () => new Response('User Avatar'))hono.get('/user/lookup/username/:username', () => new Response('User Lookup Username'))17 unmodified linesmodified:example/basic/index.js+2 added-2 removed
1212345622 unmodified linesconst Hono = require('../../src/hono')const app = Hono()const { Hono } = require('../../src/hono')const app = new Hono()
// Middlewareconst logger = (c, next) => { console.log(`[${c.req.method}] ${c.req.url}`)22 unmodified linesmodified:example/basic/README.md+2 added-2 removed
112345678891011# Hono example for cloudflare worker# Hono example for Cloudflare Workers
Before you can start publishing your service to cloudflare worker, you must sign up for a Cloudflare Workers account first, you can check out this [document](https://developers.cloudflare.com/workers/get-started/guide)
You can update the information (`name`, `zoon_id`, etc) in wrangler file, then you can test and deploy your service by simply doing,
```sh$ npm install $ npm install$ npm run dev # Start a local server for developing your worker$ npm run publish # Publish your worker to the orange cloud```modified:example/compute-at-edge/index.js+2 added-2 removed
1-const Hono = require('hono')2-const app = Hono()1+const { Hono } = require('hono')2+const app = new Hono()3 4 app.use('*', (c, next) => {5 console.log(`[${c.req.method}] ${c.req.url}`)6 next()11 unmodified linesadded:src/hono.d.ts+42 added
1+declare class FetchEvent {}2+declare class Request {}3+declare class Response {}4+declare class Context {}5+6+declare class Node {}7+8+declare class Router {9+ tempPath: string10+ node: Node11+12+ add(method: string, path: string, handlers: any[]): Node13+ match(method: string, path: string): Node14+}15+16+export class Hono {17+ router: Router18+ middlewareRouters: Router[]19+20+ 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(): void27+28+ notFound(): Response29+30+ route(path: string): Hono31+32+ use(path: string, middleware: any): void33+34+ 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+}41+42+export class Middleware {}modified:src/hono.js+8 added-3 removed
2 unmodified lines3 const Node = require('./node')4 const compose = require('./compose')5 const methods = require('./methods')6 const defaultFilter = require('./middleware/defaultFilter')7+const Middleware = require('./middleware')8 9 const METHOD_NAME_OF_ALL = 'ALL'10 11 class Router {120 unmodified lines132 return new Response('Not Found', { status: 404 })133 }134 }135 135-module.exports = () => {136- return new Hono()137-}136+// Default Export137+module.exports = Hono138+exports = module.exports139+140+// Named Export141+exports.Hono = Hono142+exports.Middleware = Middlewaremodified:src/hono.test.js+5 added-4 removed
1-const Hono = require('./hono')1 const fetch = require('node-fetch')2+const { Hono } = require('./hono')3 4 describe('GET Request', () => {5- const app = Hono()5+ const app = new Hono()6+7 app.get('/hello', () => {8 return new fetch.Response('hello', {9 status: 200,10 })18 unmodified lines29 })30 })31 32 describe('params and query', () => {32- const app = Hono()33+ const app = new Hono()34 35 app.get('/entry/:id', async (c) => {36 const id = await c.req.params('id')37 return new fetch.Response(`id is ${id}`, {22 unmodified lines60 })61 })62 63 describe('Middleware', () => {63- const app = Hono()64+ const app = new Hono()65 66 const logger = async (c, next) => {67 console.log(`${c.req.method} : ${c.req.url}`)68 next()47 unmodified linesadded:src/middleware.js+3 added
1+class Middleware {}2+3+module.exports = Middlewaremodified:src/router.test.js+4 added-4 removed
1-const App = require('./hono')1+const { Hono } = require('./hono')2 3 describe('Basic Usage', () => {4- const router = App()4+ const router = new Hono()5 6 it('get, post hello', async () => {7 router.get('/hello', 'get hello')8 router.post('/hello', 'post hello')14 unmodified lines23 })24 })25 26 describe('Complex', () => {27- let router = App()27+ let router = new Hono()28 29 it('Named Param', async () => {30 router.get('/entry/:id', 'get entry')31 res = await router.matchRoute('GET', '/entry/123')23 unmodified lines55 })56 })57 58 describe('Chained Route', () => {59- let router = App()59+ let router = new Hono()60 61 it('Return rooter object', async () => {62 router = router.patch('/hello', 'patch hello')63 expect(router).not.toBeNull()25 unmodified linesmodified:README.md+2 added-2 removed
1 unmodified line2 3 Hono [炎] - Tiny web framework for Cloudflare Workers and others.4 5 ```js6-const Hono = require('hono')7-const app = Hono()6+const { Hono } = require('hono')7+const app = new Hono()8 9 app.get('/', () => new Response('Hono!!'))10 11 app.fire()166 unmodified lines