feat: builtin middleware as sub module (#81)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
# BREAKING CHANGES!! Now, builtin middleware are not in main package. You can't use `Middleware.logger()` method. Call sub modules if needed: ```js import { Hono } from 'hono' import { poweredBy } from 'hono/powered-by' import { logger } from 'hono/logger' const app = new Hono() app.use('*', poweredBy()) app.use('*', logger()) ``` Closes: #80 --- * feat: builtin middleware as sub module * fixed middleware * Update readme * Add types * New gif
authored by Yusuke Wada · committed by GitHub · parent 1841a17 · Browse files · 22 files·+100 added−66 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Closes: #80
Diff stats
22 files+100 added−66 removed
- tests:+23 added−23 removed
- other:+77 added−43 removed
Provenance
- Commit
7dd486a- Tree
1a89597- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
1841a17
22 files changed+100 added−66 removed
modified:examples/basic/index.js+9 added-7 removed
1231234567898910101112131413151617181987 unmodified linesimport { Hono, Middleware } from '../../dist'// or install from npm:// import { Hono, Middleware } from 'hono'import { Hono } from '../../dist'import { poweredBy } from '../../src/middleware/powered-by/powered-by'import { logger } from '../../src/middleware/logger/logger'import { bodyParse } from '../../src/middleware/body-parse/body-parse'import { basicAuth } from '../../src/middleware/basic-auth/basic-auth'
const app = new Hono()
// Mount Builtin Middlewareapp.use('*', Middleware.poweredBy())app.use('*', Middleware.logger())app.use('/form', Middleware.bodyParse())app.use('*', poweredBy())app.use('*', logger())app.use('/form', bodyParse())app.use( '/auth/*', Middleware.basicAuth({ basicAuth({ username: 'hono', password: 'acoolproject', }))87 unmodified linesmodified:examples/blog/src/index.ts+6 added-3 removed
11234567867910111213146 unmodified linesimport { Hono, Middleware } from '../../../dist'import { Hono } from '../../../dist'import { bodyParse } from '../../../dist/middleware/body-parse/body-parse'import { cors } from '../../../dist/middleware/cors/cors'
import * as Controller from './controller'
export const app = new Hono()
app.use('/posts/*', Middleware.bodyParse())app.use('/posts/*', Middleware.cors())app.use('/posts/*', bodyParse())app.use('/posts/*', cors())
app.get('/', Controller.root)
app.get('/posts', Controller.list)6 unmodified linesmodified:examples/mustache/index.js+3 added-2 removed
11234556789106 unmodified linesimport { Hono, Middleware } from '../../dist'import { Hono } from '../../dist'import { mustache } from '../../src/middleware/mustache/mustache'
const app = new Hono()
app.use('*', Middleware.mustache())app.use('*', mustache())
app.get('/', (c) => { return c.render( 'index',6 unmodified linesmodified:examples/serve-static/src/index.js+3 added-2 removed
11234556789import { Hono, Middleware } from '../../../dist'import { Hono } from '../../../dist'import { serveStatic } from '../../../src/middleware/serve-static/serve-static'
const hono = new Hono()
hono.use('/static/*', Middleware.serveStatic({ root: './assets' }))hono.use('/static/*', serveStatic({ root: './assets' }))hono.get('/', (c) => c.text('This is Home! You can access: /static/hello.txt'))
hono.fire()modified:src/middleware/basic-auth/basic-auth.test.ts+2 added-2 removed
12234568 unmodified lines1516171819192021222321 unmodified linesimport { Hono } from '../../hono'import { Middleware } from '../../middleware'import { basicAuth } from './basic-auth'
describe('Basic Auth by Middleware', () => { const crypto = global.crypto beforeAll(() => {8 unmodified lines const password = 'hono-password-a'
app.use( '*', Middleware.basicAuth({ basicAuth({ username, password, }) )21 unmodified linesmodified:src/middleware/body-parse/body-parse.test.ts+2 added-2 removed
1 import { Hono } from '../../hono'2-import { Middleware } from '../../middleware'2+import { bodyParse } from './body-parse'3 4 describe('Parse Body Middleware', () => {5 const app = new Hono()6 7- app.use('*', Middleware.bodyParse())7+ app.use('*', bodyParse())8 app.post('/json', async (ctx) => {9 return ctx.json(ctx.req.parsedBody, 200)10 })11 app.post('/text', async (ctx) => {51 unmodified linesmodified:src/middleware/cookie/cookie.test.ts+4 added-4 removed
1 import { Hono } from '../../hono'2-import { Middleware } from '../../middleware'2+import { cookie } from './cookie'3 4 describe('Cookie Middleware', () => {5 const app = new Hono()6- app.use('/cookie', Middleware.cookie())6+ app.use('/cookie', cookie())7 8 app.get('/cookie', (c) => {9 const yummyCookie = c.req.cookie('yummy_cookie')10 const tastyCookie = c.req.cookie('tasty_cookie')12 unmodified lines23 expect(res.headers.get('Yummy-Cookie')).toBe('choco')24 expect(res.headers.get('Tasty-Cookie')).toBe('strawberry')25 })26 27- app.use('/set-cookie', Middleware.cookie())27+ app.use('/set-cookie', cookie())28 29 app.get('/set-cookie', (c) => {30 c.cookie('delicious_cookie', 'macha')31 return c.text('Give cookie')6 unmodified lines38 const header = res.headers.get('Set-Cookie')39 expect(header).toBe('delicious_cookie=macha')40 })41 42- app.use('/set-cookie-complex', Middleware.cookie())42+ app.use('/set-cookie-complex', cookie())43 44 app.get('/set-cookie-complex', (c) => {45 c.cookie('greate_cookie', 'banana', {46 path: '/',20 unmodified linesmodified:src/middleware/cors/cors.test.ts+3 added-3 removed
1 import { Hono } from '../../hono'2-import { Middleware } from '../../middleware'2+import { cors } from './cors'3 4 describe('CORS by Middleware', () => {5 const app = new Hono()6 7- app.use('/api/*', Middleware.cors())7+ app.use('/api/*', cors())8 app.use(9 '/api2/*',10- Middleware.cors({10+ cors({11 origin: 'http://example.com',12 allowHeaders: ['X-Custom-Header', 'Upgrade-Insecure-Requests'],13 allowMethods: ['POST', 'GET', 'OPTIONS'],14 exposeHeaders: ['Content-Length', 'X-Kuma-Revision'],49 unmodified linesmodified:src/middleware/logger/logger.test.ts+2 added-2 removed
1 import { Hono } from '../../hono'2-import { Middleware } from '../../middleware'2+import { logger } from './logger'3 4 describe('Logger by Middleware', () => {5 const app = new Hono()6 4 unmodified lines11 12 const shortRandomString = 'hono'13 const longRandomString = 'hono'.repeat(1000)14 15- app.use('*', Middleware.logger(logFn))15+ app.use('*', logger(logFn))16 app.get('/short', (c) => c.text(shortRandomString))17 app.get('/long', (c) => c.text(longRandomString))18 app.get('/empty', (c) => c.text(''))19 41 unmodified linesmodified:src/middleware/mustache/mustache.test.ts+2 added-2 removed
1 import { Hono } from '../../hono'2-import { Middleware } from '../../middleware'2+import { mustache } from './mustache'3 4 // Mock5 const store: { [key: string]: string } = {6 'index.abcdef.mustache': '{{> header}}Title: {{ title }}{{> footer}}',17 unmodified lines24 25 describe('Mustache by Middleware', () => {26 const app = new Hono()27 28- app.use('*', Middleware.mustache())28+ app.use('*', mustache())29 app.get('/', (c) => {30 return c.render(31 'index',32 { title: 'Hono!' },14 unmodified linesmodified:src/middleware/powered-by/powered-by.test.ts+2 added-2 removed
1 import { Hono } from '../../hono'2-import { Middleware } from '../../middleware'2+import { poweredBy } from './powered-by'3 4 describe('Powered by Middleware', () => {5 const app = new Hono()6 7- app.use('*', Middleware.poweredBy())7+ app.use('*', poweredBy())8 app.get('/', () => new Response('root'))9 10 it('Response headers include X-Powered-By', async () => {11 const req = new Request('http://localhost/')6 unmodified linesmodified:src/middleware/serve-static/serve-static.test.ts+3 added-3 removed
1 import { Hono } from '../../hono'2-import { Middleware } from '../../middleware'2+import { serveStatic } from './serve-static'3 4 // Mock5 const store: { [key: string]: string } = {6 'assets/static/plain.abcdef.txt': 'This is plain.txt',19 unmodified lines26 27 describe('ServeStatic Middleware', () => {28 const app = new Hono()29 30- app.use('/static/*', Middleware.serveStatic({ root: './assets' }))31- app.use('/static-no-root/*', Middleware.serveStatic())30+ app.use('/static/*', serveStatic({ root: './assets' }))31+ app.use('/static-no-root/*', serveStatic())32 33 it('Serve static files', async () => {34 let req = new Request('http://localhost/static/plain.txt')35 let res = await app.dispatch(req)33 unmodified linesmodified:src/index.ts-1 removed
1 export { Hono } from './hono'2 export type { Handler, MiddlewareHandler } from './hono'3-export { Middleware } from './middleware'3 export { Context } from './context'4 export type { Env } from './context'modified:src/middleware.ts-16 removed
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-import { bodyParse } from './middleware/body-parse/body-parse'6-import { cors } from './middleware/cors/cors'7-import { cookie } from './middleware/cookie/cookie'8-import { mustache } from './middleware/mustache/mustache'9-import { serveStatic } from './middleware/serve-static/serve-static'2 3 export class Middleware {4 static default = defaultMiddleware13- static poweredBy = poweredBy14- static logger = logger15- static basicAuth = basicAuth16- static bodyParse = bodyParse17- static cors = cors18- static cookie = cookie19- static mustache = mustache20- static serveStatic = serveStatic5 }modified:test/hono.test.ts+1 added-1 removed
1-import { Hono } from '../src/hono'1+import { Hono } from '../src/index'2 3 describe('GET Request', () => {4 const app = new Hono()5 270 unmodified linesmodified:test/middleware.test.ts+2 added-2 removed
1 import { Hono } from '../src/hono'2-import { Middleware } from '../src/middleware'2+import { poweredBy } from '../src/middleware/powered-by/powered-by'3 4 describe('Builtin Middleware', () => {5 const app = new Hono()6 7- app.use('*', Middleware.poweredBy())7+ app.use('*', poweredBy())8 app.get('/', () => new Response('root'))9 10 it('Builtin Powered By Middleware', async () => {11 const req = new Request('http://localhost/')6 unmodified linesmodified:.npmignore+1 added-1 removed
1-example1+examples2 benchmark3 .github4 tsconfig.json5 srcmodified:jest.config.js+1 added-1 removed
1 unmodified line2 testMatch: ['**/test/**/*.+(ts|tsx|js)', '**/src/**/(*.)+(spec|test).+(ts|tsx|js)'],3 transform: {4 '^.+\\.(ts|tsx)$': 'ts-jest',5 },6- testPathIgnorePatterns: ['./example'],6+ testPathIgnorePatterns: ['./examples'],7 testEnvironment: 'miniflare',8 testEnvironmentOptions: {9 /*10 bindings: {14 unmodified linesmodified:package.json+41 added-2 removed
1 {2 "name": "hono",3- "version": "0.2.4",3+ "version": "0.2.5-alpha",4 "description": "[炎] Ultrafast web framework for Cloudflare Workers.",5 "main": "dist/index.js",6 "types": "dist/index.d.ts",7 "files": [8 "dist"9 ],10+ "exports": {11+ ".": "./dist/index.js",12+ "./basic-auth": "./dist/middleware/basic-auth/basic-auth.js",13+ "./body-parse": "./dist/middleware/body-parse/body-parse.js",14+ "./cookie": "./dist/middleware/cookie/cookie.js",15+ "./cors": "./dist/middleware/cors/cors.js",16+ "./logger": "./dist/middleware/logger/logger.js",17+ "./mustache": "./dist/middleware/mustache/mustache.js",18+ "./powered-by": "./dist/middleware/powered-by/powered-by.js",19+ "./serve-static": "./dist/middleware/serve-static/serve-static.js"20+ },21+ "typesVersions": {22+ "*": {23+ "basic-auth": [24+ "./dist/middleware/basic-auth/basic-auth.d.ts"25+ ],26+ "body-parse": [27+ "./dist/middleware/body-parse/body-parse.d.ts"28+ ],29+ "cookie": [30+ "./dist/middleware/cookie/cookie.d.ts"31+ ],32+ "cors": [33+ "./dist/middleware/cors/cors.d.ts"34+ ],35+ "logger": [36+ "./dist/middleware/logger/logger.d.ts"37+ ],38+ "mustache": [39+ "./dist/middleware/mustache/mustache.d.ts"40+ ],41+ "powered-by": [42+ "./dist/middleware/powered-by/powered-by.d.ts"43+ ],44+ "serve-static": [45+ "./dist/middleware/serve-static/serve-static.d.ts"46+ ]47+ }48+ },49 "scripts": {50 "test": "jest",51 "lint": "eslint --ext js,ts src .eslintrc.js test",52 "build": "rimraf dist && tsc",40 unmodified lines93 "jest-environment-miniflare": "^2.0.0",94 "mustache": "^4.2.0",95 "rimraf": "^3.0.2",96 "ts-jest": "^27.1.2",58- "typescript": "^4.5.4"97+ "typescript": "^4.5.5"98 },99 "engines": {100 "node": ">=11.0.0"101 }1 unmodified linemodified:README.md+11 added-6 removed
33 unmodified lines34 ## Hono in 1 minute35 36 Below is a demonstration to create an application of Cloudflare Workers with Hono.37 38-38+39 40 ## Install41 42 You can install from npm registry:94 unmodified lines137 138 ### Builtin Middleware139 140 ```js141-import { Hono, Middleware } from 'hono'141+import { Hono } from 'hono'142+import { poweredBy } from 'hono/powered-by'143+import { logger } from 'hono/logger'144+import { basicAuth } from 'hono/basicAuth'145 143-...146+const app = new Hono()147 145-app.use('*', Middleware.poweredBy())146-app.use('*', Middleware.logger())148+app.use('*', poweredBy())149+app.use('*', logger())150 app.use(151 '/auth/*',149- Middleware.basicAuth({152+ basicAuth({153 username: 'hono',154 password: 'acoolproject',155 })156 )157+158+...159 ```160 161 Available builtin middleware are listed on [src/middleware](https://github.com/yusukebe/hono/tree/master/src/middleware).162 309 unmodified linesmodified:tsconfig.json+1 added-1 removed
20 unmodified lines21 ],22 "exclude": [23 "src/**/*.test.ts"24 ]25-}25+}modified:yarn.lock+1 added-1 removed
3684 unmodified lines3685 integrity sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==3686 dependencies:3687 is-typedarray "^1.0.0"3688 3689-typescript@^4.5.4:3689+typescript@^4.5.5:3690 version "4.5.5"3691 resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.5.tgz#d8c953832d28924a9e3d37c73d729c846c5896f3"3692 integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==3693 197 unmodified lines