From 3198fa05d82b76ad8d697b8fd3828d5460d4d9cf Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Sat, 1 Jan 2022 15:29:41 +0900 Subject: [PATCH] Use named import, add types by .d.ts file --- README.md | 4 +-- benchmark/handleEvent/index.js | 4 +-- benchmark/webapp/hono/index.js | 4 +-- example/basic/README.md | 4 +-- example/basic/index.js | 4 +-- example/compute-at-edge/index.js | 4 +-- src/hono.d.ts | 42 ++++++++++++++++++++++++++++++++ src/hono.js | 11 ++++++--- src/hono.test.js | 9 ++++--- src/middleware.js | 3 +++ src/router.test.js | 8 +++--- 11 files changed, 74 insertions(+), 23 deletions(-) create mode 100644 src/hono.d.ts create mode 100644 src/middleware.js diff --git a/README.md b/README.md index 2b04db00..7ddd5edb 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ Hono [炎] - Tiny web framework for Cloudflare Workers and others. ```js -const Hono = require('hono') -const app = Hono() +const { Hono } = require('hono') +const app = new Hono() app.get('/', () => new Response('Hono!!')) diff --git a/benchmark/handleEvent/index.js b/benchmark/handleEvent/index.js index 96a52717..194dc0f0 100644 --- a/benchmark/handleEvent/index.js +++ b/benchmark/handleEvent/index.js @@ -1,13 +1,13 @@ import 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 } = itty import { 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')) diff --git a/benchmark/webapp/hono/index.js b/benchmark/webapp/hono/index.js index 48467f39..5f370ce5 100644 --- a/benchmark/webapp/hono/index.js +++ b/benchmark/webapp/hono/index.js @@ -1,6 +1,6 @@ -const 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')) diff --git a/example/basic/README.md b/example/basic/README.md index 22cb56f8..371fda9a 100644 --- a/example/basic/README.md +++ b/example/basic/README.md @@ -1,11 +1,11 @@ -# 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 ``` diff --git a/example/basic/index.js b/example/basic/index.js index abccfec8..1ecff8cc 100644 --- a/example/basic/index.js +++ b/example/basic/index.js @@ -1,5 +1,5 @@ -const Hono = require('../../src/hono') -const app = Hono() +const { Hono } = require('../../src/hono') +const app = new Hono() // Middleware const logger = (c, next) => { diff --git a/example/compute-at-edge/index.js b/example/compute-at-edge/index.js index 75fa3b3b..71e95deb 100644 --- a/example/compute-at-edge/index.js +++ b/example/compute-at-edge/index.js @@ -1,5 +1,5 @@ -const Hono = require('hono') -const app = Hono() +const { Hono } = require('hono') +const app = new Hono() app.use('*', (c, next) => { console.log(`[${c.req.method}] ${c.req.url}`) diff --git a/src/hono.d.ts b/src/hono.d.ts new file mode 100644 index 00000000..d47faa76 --- /dev/null +++ b/src/hono.d.ts @@ -0,0 +1,42 @@ +declare class FetchEvent {} +declare class Request {} +declare class Response {} +declare class Context {} + +declare class Node {} + +declare class Router { + tempPath: string + node: Node + + add(method: string, path: string, handlers: any[]): Node + match(method: string, path: string): Node +} + +export class Hono { + router: Router + middlewareRouters: Router[] + + getRouter(): Router + addRoute(method: string, args: any[]): Hono + matchRoute(method: string, path: string): Node + createContext(req: Request, res: Response): Context + dispatch(req: Request, res: Response): Response + handleEvent(event: FetchEvent): Response + fire(): void + + notFound(): Response + + route(path: string): Hono + + use(path: string, middleware: any): void + + all(path: string, handler: any): Hono + get(path: string, handler: any): Hono + post(path: string, handler: any): Hono + put(path: string, handler: any): Hono + head(path: string, handler: any): Hono + delete(path: string, handler: any): Hono +} + +export class Middleware {} diff --git a/src/hono.js b/src/hono.js index b24e1b8b..5d09557e 100644 --- a/src/hono.js +++ b/src/hono.js @@ -4,6 +4,7 @@ const Node = require('./node') const compose = require('./compose') const methods = require('./methods') const defaultFilter = require('./middleware/defaultFilter') +const Middleware = require('./middleware') const METHOD_NAME_OF_ALL = 'ALL' @@ -132,6 +133,10 @@ class Hono { } } -module.exports = () => { - return new Hono() -} +// Default Export +module.exports = Hono +exports = module.exports + +// Named Export +exports.Hono = Hono +exports.Middleware = Middleware diff --git a/src/hono.test.js b/src/hono.test.js index a0474e81..6494285c 100644 --- a/src/hono.test.js +++ b/src/hono.test.js @@ -1,8 +1,9 @@ -const Hono = require('./hono') const fetch = require('node-fetch') +const { Hono } = require('./hono') describe('GET Request', () => { - const app = Hono() + const app = new Hono() + app.get('/hello', () => { return new fetch.Response('hello', { status: 200, @@ -29,7 +30,7 @@ describe('GET Request', () => { }) describe('params and query', () => { - const app = Hono() + const app = new Hono() app.get('/entry/:id', async (c) => { const id = await c.req.params('id') @@ -60,7 +61,7 @@ describe('params and query', () => { }) describe('Middleware', () => { - const app = Hono() + const app = new Hono() const logger = async (c, next) => { console.log(`${c.req.method} : ${c.req.url}`) diff --git a/src/middleware.js b/src/middleware.js new file mode 100644 index 00000000..c49064bf --- /dev/null +++ b/src/middleware.js @@ -0,0 +1,3 @@ +class Middleware {} + +module.exports = Middleware diff --git a/src/router.test.js b/src/router.test.js index ac6b5f25..f541de4e 100644 --- a/src/router.test.js +++ b/src/router.test.js @@ -1,7 +1,7 @@ -const App = require('./hono') +const { Hono } = require('./hono') describe('Basic Usage', () => { - const router = App() + const router = new Hono() it('get, post hello', async () => { router.get('/hello', 'get hello') @@ -24,7 +24,7 @@ describe('Basic Usage', () => { }) describe('Complex', () => { - let router = App() + let router = new Hono() it('Named Param', async () => { router.get('/entry/:id', 'get entry') @@ -56,7 +56,7 @@ describe('Complex', () => { }) describe('Chained Route', () => { - let router = App() + let router = new Hono() it('Return rooter object', async () => { router = router.patch('/hello', 'patch hello')