Refactor (#35)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* export Handler * Fixed example * Update readme * Rename * Param value as string * Update jsx ssr example * Hono is Ultrafast web framework
authored by Yusuke Wada · committed by GitHub · parent 7920f9a · Browse files · 20 files·+150 added−92 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
20 files+150 added−92 removed
- tests:+20 added−20 removed
- other:+130 added−72 removed
Provenance
- Commit
f9fa200- Tree
578d184- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
7920f9a
20 files changed+150 added−92 removed
modified:example/basic/index.js+4 added-3 removed
112334567832 unmodified lines4142434444454647484926 unmodified linesconst { Hono, Middleware } = require('../../dist/index')import { Hono, Middleware } from '../../dist'// or install from npm:// const { Hono, Middleware } = require('hono')// import { Hono, Middleware } from 'hono'
const app = new Hono()
// Mount Builtin Middlewareapp.use('*', Middleware.poweredBy())32 unmodified linesapp.get('/', (c) => c.text('Hono!!'))app.get('/hello', () => new Response('This is /hello'))app.get('/entry/:id', (c) => { const id = c.req.params('id') return new Response(`Your ID is ${id}`) return c.text(`Your ID is ${id}`)})app.get('/auth/*', async (ctx) => ctx.text('You are authorized'))
// Async26 unmodified linesmodified:example/compute-at-edge/index.js+4 added-5 removed
1123456789910111011121413141516const { Hono } = require('hono')import { Hono } from 'hono'
const app = new Hono()
app.use('*', async (c, next) => { console.log(`[${c.req.method}] ${c.req.url}`) next()})
app.get('/', () => { return new Response('Hono!! Compute@Edge!!')})app.get('/', (c) => c.text('Hono!! Compute@Edge!!'))
app.get('/hello/:name', (c) => { return new Response(`Hello ${c.req.params('name')}!!!!!`) return c.text(`Hello ${c.req.params('name')}!!!!!`)})
app.fire()deleted:example/jsx-ssr/src/app.jsx-20 removed
1234567891011121314151617181920import { Component } from 'nano-jsx'import { Link } from './Link'
export class App extends Component { render() { return ( <div> <header> <h1>Hello! Hono!</h1> </header> <main> <ul> <Link title='Hono GitHub Repositry' url='https://github.com/yusukebe/hono'></Link> <Link title='Hono npm Registry' url='https://www.npmjs.com/package/hono'></Link> </ul> </main> </div> ) }}added:example/jsx-ssr/src/Footer.jsx+7 added
1234567export const Footer = () => { return ( <footer> <address><a href="https://github.com/yusukebe/hono">Hono</a></address> </footer> )}No newline at end of fileadded:example/jsx-ssr/src/Header.jsx+7 added
1234567export const Header = () => { return ( <header> <h1><a href="/">Hono Example</a></h1> </header> )}modified:example/jsx-ssr/src/index.js+39 added-3 removed
122434567891011123 unmodified lines1617181920212223242526272829303132333435363738394041424320444546474849505152535455565758596061import { Hono } from '../../../dist/index'
import Nano from 'nano-jsx'import { App } from './App'import { Post } from './Post'import { Top } from './Top'
const app = new Hono()
// Viewconst makeHTML = (body) => { return `<!DOCTYPE html> <html>3 unmodified lines</html>`}
// Modelconst posts = [ { id: '1', title: 'Good Morning', body: 'Let us eat breakfast' }, { id: '2', title: 'Good Afternoon', body: 'Let us eat Lunch' }, { id: '3', title: 'Good Evening', body: 'Let us eat Dinner' }, { id: '4', title: 'Good Night', body: 'Let us drink Beer' }, { id: '5', title: 'こんにちは', body: '昼からビールを飲みます' },]
// Logicconst getPosts = () => { return posts}
const getPost = (id) => { for (const post of posts) { if (post.id === id) { return post } }}
// Controllerapp.get('/', (c) => { const body = Nano.renderSSR(<App />) const posts = getPosts() const body = Nano.renderSSR(<Top posts={posts} />) const html = makeHTML(body) return c.html(html)})
app.get('/post/:id{[0-9]+}', (c) => { const id = c.req.params('id') const post = getPost(id) if (!post) { return c.text('Not Found', 404) } const body = Nano.renderSSR(<Post post={post} />) const html = makeHTML(body) return c.html(html)})
app.fire()deleted:example/jsx-ssr/src/link.jsx-7 removed
1234567export const Link = (props) => { return ( <li> <a href={props.url}>{props.title}</a> </li> )}added:example/jsx-ssr/src/Post.jsx+15 added
123456789101112131415import { Header } from './Header'import { Footer } from './Footer'
export const Post = (props) => { return ( <div> <Header /> <main> <h2>{props.post.title}</h2> <p>{props.post.body}</p> </main> <Footer /> </div> )}added:example/jsx-ssr/src/Top.jsx+20 added
1+import { Header } from './Header'2+import { Footer } from './Footer'3+4+export const Top = (props) => {5+ return (6+ <div>7+ <Header />8+ <main>9+ <h2>Posts</h2>10+ {props.posts}11+ <ul>12+ {props.posts.map((post) => {13+ return <li><a href={`/post/${post.id}`}>{post.title}</a></li>14+ })}15+ </ul>16+ </main>17+ <Footer />18+ </div>19+ )20+}added:src/middleware/powered-by/powered-by.test.ts+20 added
1+import makeServiceWorkerEnv from 'service-worker-mock'2+import { Hono, Middleware } from '../../hono'3+4+declare let global: any5+Object.assign(global, makeServiceWorkerEnv())6+7+describe('Powered by Middleware', () => {8+ const app = new Hono()9+10+ app.use('*', Middleware.poweredBy())11+ app.get('/', () => new Response('root'))12+13+ it('Response headers include X-Powered-By', async () => {14+ const req = new Request('/')15+ const res = await app.dispatch(req)16+ expect(res).not.toBeNull()17+ expect(res.status).toBe(200)18+ expect(res.headers.get('X-Powered-By')).toBe('Hono')19+ })20+})added:src/middleware/powered-by/powered-by.ts+9 added
1+import type { Context } from '../../context'2+3+export const poweredBy = () => {4+ return async (c: Context, next: Function) => {5+ await next()6+ // await c.res.headers.append('X-Powered-By', 'Hono')7+ c.res.headers.append('X-Powered-By', 'Hono')8+ }9+}deleted:src/middleware/poweredBy/poweredBy.test.ts-20 removed
1-import makeServiceWorkerEnv from 'service-worker-mock'2-import { Hono, Middleware } from '../../hono'3-4-declare let global: any5-Object.assign(global, makeServiceWorkerEnv())6-7-describe('Powered by Middleware', () => {8- const app = new Hono()9-10- app.use('*', Middleware.poweredBy())11- app.get('/', () => new Response('root'))12-13- it('Response headers include X-Powered-By', async () => {14- const req = new Request('/')15- const res = await app.dispatch(req)16- expect(res).not.toBeNull()17- expect(res.status).toBe(200)18- expect(res.headers.get('X-Powered-By')).toBe('Hono')19- })20-})deleted:src/middleware/poweredBy/poweredBy.ts-9 removed
1-import type { Context } from '../../context'2-3-export const poweredBy = () => {4- return async (c: Context, next: Function) => {5- await next()6- // await c.res.headers.append('X-Powered-By', 'Hono')7- c.res.headers.append('X-Powered-By', 'Hono')8- }9-}added:src/middleware/default.ts+11 added
1+import type { Context } from '../context'2+3+export const defaultMiddleware = async (c: Context, next: Function) => {4+ c.req.query = (key: string) => {5+ // eslint-disable-next-line6+ const url = new URL(c.req.url)7+ return url.searchParams.get(key)8+ }9+10+ await next()11+}deleted:src/middleware/defaultFilter.ts-11 removed
1-import type { Context } from '../context'2-3-export const defaultFilter = async (c: Context, next: Function) => {4- c.req.query = (key: string) => {5- // eslint-disable-next-line6- const url = new URL(c.req.url)7- return url.searchParams.get(key)8- }9-10- await next()11-}modified:src/hono.ts+4 added-4 removed
9 unmodified lines10 const METHOD_NAME_OF_ALL = 'ALL'11 12 declare global {13 interface Request {14- params: (key: string) => any14+ params: (key: string) => string15 query: (key: string) => string | null16 }17 }18 19-type Handler = (c: Context, next?: Function) => Response | Promise<Response>20-type MiddlwareHandler = (c: Context, next: Function) => Promise<void>19+export type Handler = (c: Context, next?: Function) => Response | Promise<Response>20+export type MiddlwareHandler = (c: Context, next: Function) => Promise<void>21 22 export class Router<T> {23 node: Node<T>24 123 unmodified lines148 context.res = await handler(context)149 await next()150 }151 152- middleware.push(Middleware.defaultFilter)152+ middleware.push(Middleware.default)153 middleware.push(wrappedHandler)154 155 const composed = compose(middleware)156 const c = new Context(request, response)20 unmodified linesmodified:src/index.ts+1 added-1 removed
1-export { Hono, Middleware } from './hono'1+export { Hono, Middleware, Handler, MiddlwareHandler } from './hono'2 export { Context } from './context'modified:src/middleware.ts+3 added-3 removed
1-import { defaultFilter } from './middleware/defaultFilter'2-import { poweredBy } from './middleware/poweredBy/poweredBy'1+import { defaultMiddleware } from './middleware/default'2+import { poweredBy } from './middleware/powered-by/powered-by'3 import { logger } from './middleware/logger/logger'4 import { basicAuth } from './middleware/basic-auth/basic-auth'5 6 export class Middleware {7- static defaultFilter = defaultFilter7+ static default = defaultMiddleware8 static poweredBy = poweredBy9 static logger = logger10 static basicAuth = basicAuth11 }modified:package.json+1 added-1 removed
1 {2 "name": "hono",3 "version": "0.0.12",4- "description": "Tiny web framework for Cloudflare Workers and others.",4+ "description": "Ultrafast web framework for Cloudflare Workers.",5 "main": "dist/index.js",6 "types": "dist/index.d.ts",7 "files": [8 "dist"53 unmodified linesmodified:README.md+5 added-5 removed
1 # Hono2 3-Hono [炎] - Tiny web framework for Cloudflare Workers and others.3+Hono [炎] - Ultrafast web framework for Cloudflare Workers.4 5 ```js6-const { Hono } = require('hono')6+import { Hono } from 'hono'7 const app = new Hono()8 9 app.get('/', (c) => c.text('Hono!!'))10 11 app.fire()12 ```13 14-Hono[炎] - _**means flame🔥 in Japanese**_ - is small, simple, and ultrafast web flamework for a Service Workers API based serverless such as **Cloudflare Workers** and **Fastly Compute@Edge**. Hono does not depend on any npm packages. However, Hono has a router, context object, and middleware including the builtins. It's easy to make a web application.14+Hono[炎] - _**means flame🔥 in Japanese**_ - is small, simple, and ultrafast web flamework for a Service Workers API based serverless such as **Cloudflare Workers** and **Fastly Compute@Edge**. Hono does not depend on any npm packages. But, Hono has a router, context object, and middleware including the builtins. Easy to make a web application.15 16 ## Features17 18 - **Fast** - the router is implemented with Trie-Tree structure.110 unmodified lines129 130 ### Builtin Middleware131 132 ```js133-const { Hono, Middleware } = require('hono')133+import { Hono, Middleware } from 'hono'134 135 ...136 137 app.use('*', Middleware.poweredBy())184 unmodified lines322 323 Only 4 lines!!324 325 ```js326-const { Hono } = require('hono')326+import { Hono } from 'hono'327 const app = new Hono()328 329 app.get('/', (c) => c.text('Hello! Hono!'))330 49 unmodified lines