0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 18:18:57 +01:00

Use named import, add types by .d.ts file

This commit is contained in:
Yusuke Wada 2022-01-01 15:29:41 +09:00
parent 71aac69116
commit 3198fa05d8
11 changed files with 74 additions and 23 deletions

View File

@ -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!!'))

View File

@ -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'))

View File

@ -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'))

View File

@ -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
```

View File

@ -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) => {

View File

@ -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}`)

42
src/hono.d.ts vendored Normal file
View File

@ -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 {}

View File

@ -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

View File

@ -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}`)

3
src/middleware.js Normal file
View File

@ -0,0 +1,3 @@
class Middleware {}
module.exports = Middleware

View File

@ -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')