Skip to content
Myr

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

22 files+100 added−66 removed

modified:examples/basic/index.js+9 added-7 removed
modified:examples/blog/src/index.ts+6 added-3 removed
modified:examples/mustache/index.js+3 added-2 removed
modified:examples/serve-static/src/index.js+3 added-2 removed
modified:src/middleware/basic-auth/basic-auth.test.ts+2 added-2 removed
modified:src/middleware/body-parse/body-parse.test.ts+2 added-2 removed
1import { Hono } from '../../hono'2import { Middleware } from '../../middleware'2import { bodyParse } from './body-parse'34describe('Parse Body Middleware', () => {5 const app = new Hono()67 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 lines
modified:src/middleware/cookie/cookie.test.ts+4 added-4 removed
1import { Hono } from '../../hono'2import { Middleware } from '../../middleware'2import { cookie } from './cookie'34describe('Cookie Middleware', () => {5 const app = new Hono()6 app.use('/cookie', Middleware.cookie())6 app.use('/cookie', cookie())78 app.get('/cookie', (c) => {9 const yummyCookie = c.req.cookie('yummy_cookie')10 const tastyCookie = c.req.cookie('tasty_cookie')
12 unmodified lines
23 expect(res.headers.get('Yummy-Cookie')).toBe('choco')24 expect(res.headers.get('Tasty-Cookie')).toBe('strawberry')25 })2627 app.use('/set-cookie', Middleware.cookie())27 app.use('/set-cookie', cookie())2829 app.get('/set-cookie', (c) => {30 c.cookie('delicious_cookie', 'macha')31 return c.text('Give cookie')
6 unmodified lines
38 const header = res.headers.get('Set-Cookie')39 expect(header).toBe('delicious_cookie=macha')40 })4142 app.use('/set-cookie-complex', Middleware.cookie())42 app.use('/set-cookie-complex', cookie())4344 app.get('/set-cookie-complex', (c) => {45 c.cookie('greate_cookie', 'banana', {46 path: '/',
20 unmodified lines
modified:src/middleware/cors/cors.test.ts+3 added-3 removed
1import { Hono } from '../../hono'2import { Middleware } from '../../middleware'2import { cors } from './cors'34describe('CORS by Middleware', () => {5 const app = new Hono()67 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 lines
modified:src/middleware/logger/logger.test.ts+2 added-2 removed
1import { Hono } from '../../hono'2import { Middleware } from '../../middleware'2import { logger } from './logger'34describe('Logger by Middleware', () => {5 const app = new Hono()6
4 unmodified lines
1112 const shortRandomString = 'hono'13 const longRandomString = 'hono'.repeat(1000)1415 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 lines
modified:src/middleware/mustache/mustache.test.ts+2 added-2 removed
1import { Hono } from '../../hono'2import { Middleware } from '../../middleware'2import { mustache } from './mustache'34// Mock5const store: { [key: string]: string } = {6 'index.abcdef.mustache': '{{> header}}Title: {{ title }}{{> footer}}',
17 unmodified lines
2425describe('Mustache by Middleware', () => {26 const app = new Hono()2728 app.use('*', Middleware.mustache())28 app.use('*', mustache())29 app.get('/', (c) => {30 return c.render(31 'index',32 { title: 'Hono!' },
14 unmodified lines
modified:src/middleware/powered-by/powered-by.test.ts+2 added-2 removed
1import { Hono } from '../../hono'2import { Middleware } from '../../middleware'2import { poweredBy } from './powered-by'34describe('Powered by Middleware', () => {5 const app = new Hono()67 app.use('*', Middleware.poweredBy())7 app.use('*', poweredBy())8 app.get('/', () => new Response('root'))910 it('Response headers include X-Powered-By', async () => {11 const req = new Request('http://localhost/')
6 unmodified lines
modified:src/middleware/serve-static/serve-static.test.ts+3 added-3 removed
1import { Hono } from '../../hono'2import { Middleware } from '../../middleware'2import { serveStatic } from './serve-static'34// Mock5const store: { [key: string]: string } = {6 'assets/static/plain.abcdef.txt': 'This is plain.txt',
19 unmodified lines
2627describe('ServeStatic Middleware', () => {28 const app = new Hono()2930 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())3233 it('Serve static files', async () => {34 let req = new Request('http://localhost/static/plain.txt')35 let res = await app.dispatch(req)
33 unmodified lines
modified:src/index.ts-1 removed
1export { Hono } from './hono'2export type { Handler, MiddlewareHandler } from './hono'3export { Middleware } from './middleware'3export { Context } from './context'4export type { Env } from './context'
modified:src/middleware.ts-16 removed
1import { defaultMiddleware } from './middleware/default'2import { poweredBy } from './middleware/powered-by/powered-by'3import { logger } from './middleware/logger/logger'4import { basicAuth } from './middleware/basic-auth/basic-auth'5import { bodyParse } from './middleware/body-parse/body-parse'6import { cors } from './middleware/cors/cors'7import { cookie } from './middleware/cookie/cookie'8import { mustache } from './middleware/mustache/mustache'9import { serveStatic } from './middleware/serve-static/serve-static'23export 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
1import { Hono } from '../src/hono'1import { Hono } from '../src/index'23describe('GET Request', () => {4 const app = new Hono()5
270 unmodified lines
modified:test/middleware.test.ts+2 added-2 removed
1import { Hono } from '../src/hono'2import { Middleware } from '../src/middleware'2import { poweredBy } from '../src/middleware/powered-by/powered-by'34describe('Builtin Middleware', () => {5 const app = new Hono()67 app.use('*', Middleware.poweredBy())7 app.use('*', poweredBy())8 app.get('/', () => new Response('root'))910 it('Builtin Powered By Middleware', async () => {11 const req = new Request('http://localhost/')
6 unmodified lines
modified:.npmignore+1 added-1 removed
1example1examples2benchmark3.github4tsconfig.json5src
modified:jest.config.js+1 added-1 removed
1 unmodified line
2 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 lines
modified: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 lines
93 "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 line
modified:README.md+11 added-6 removed
33 unmodified lines
34## Hono in 1 minute3536Below is a demonstration to create an application of Cloudflare Workers with Hono.3738![Demo](https://user-images.githubusercontent.com/10682/151102477-be0f950e-8d23-49c5-b6d8-d8ecb6b7484e.gif)38![Demo](https://user-images.githubusercontent.com/10682/151973526-342644f9-71c5-4fee-81f4-64a7558bb192.gif)3940## Install4142You can install from npm registry:
94 unmodified lines
137138### Builtin Middleware139140```js141import { Hono, Middleware } from 'hono'141import { Hono } from 'hono'142import { poweredBy } from 'hono/powered-by'143import { logger } from 'hono/logger'144import { basicAuth } from 'hono/basicAuth'145143...146const app = new Hono()147145app.use('*', Middleware.poweredBy())146app.use('*', Middleware.logger())148app.use('*', poweredBy())149app.use('*', logger())150app.use(151 '/auth/*',149 Middleware.basicAuth({152 basicAuth({153 username: 'hono',154 password: 'acoolproject',155 })156)157158...159```160161Available builtin middleware are listed on [src/middleware](https://github.com/yusukebe/hono/tree/master/src/middleware).162
309 unmodified lines
modified:tsconfig.json+1 added-1 removed
20 unmodified lines
21 ],22 "exclude": [23 "src/**/*.test.ts"24 ]25}25}
modified:yarn.lock+1 added-1 removed
3684 unmodified lines
3685 integrity sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==3686 dependencies:3687 is-typedarray "^1.0.0"36883689typescript@^4.5.4:3689typescript@^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