Skip to content
Myr

refactor: remove default middleware (#91)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixed some bugs about handling req/res object on middleware.

authored by Yusuke Wada · committed by GitHub · parent 489a241 · Browse files · 6 files+61 added−48 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+61 added−48 removed

  • tests:+41 added−26 removed
  • other:+20 added−22 removed

6 files+61 added−48 removed

deleted:src/middleware/default.test.ts-26 removed
deleted:src/middleware/default.ts-14 removed
modified:src/context.ts+10 added
modified:src/hono.ts+9 added-3 removed
9 unmodified lines
1011declare global {12 interface Request {13 param: (key: string) => string14 query: (key: string) => string | null14 query: (key: string) => string15 header: (name: string) => string16 parsedBody: any17 }18}
108 unmodified lines
127 const [method, path] = [request.method, getPathFromURL(request.url)]128129 const result = await this.matchRoute(method, path)130131 // Methods for Request object132 request.param = (key: string): string => {133 if (result) {134 return result.params[key]135 }135 return ''136 }137 request.header = (name: string): string => {138 return request.headers.get(name)139 }140 request.query = (key: string): string => {141 const url = new URL(c.req.url)142 return url.searchParams.get(key)143 }144145 const handler = result ? result.handler[0] : this.notFound // XXX146147 const middleware = []
9 unmodified lines
157 context.res = await handler(context)158 await next()159 }160154 middleware.push(Middleware.default)161 middleware.push(wrappedHandler)162163 const composed = compose(middleware)164 const c = new Context(request, { env: env, event: event, res: null })
44 unmodified lines
modified:src/middleware.ts+1 added-5 removed
modified:test/hono.test.ts+41 added
271 unmodified lines
272 expect(await res.text()).toBe('Custom Error Message')273 expect(res.headers.get('debug')).toBe('This is Error')274 })275})276277describe('Request methods with custom middleware', () => {278 const app = new Hono()279280 app.use('*', async (c, next) => {281 const query = c.req.query('foo')282 const param = c.req.param('foo')283 const header = c.req.header('User-Agent')284 await next()285 c.header('X-Query-2', query)286 c.header('X-Param-2', param)287 c.header('X-Header-2', header)288 })289290 app.get('/:foo', (c) => {291 const query = c.req.query('foo')292 const param = c.req.param('foo')293 const header = c.req.header('User-Agent')294 c.header('X-Query', query)295 c.header('X-Param', param)296 c.header('X-Header', header)297 return c.body('Hono')298 })299300 it('query', async () => {301 const url = new URL('http://localhost/bar')302 url.searchParams.append('foo', 'bar')303 const req = new Request(url.toString())304 req.headers.append('User-Agent', 'bar')305 const res = await app.dispatch(req)306307 expect(res.status).toBe(200)308 expect(res.headers.get('X-Query')).toBe('bar')309 expect(res.headers.get('X-Param')).toBe('bar')310 expect(res.headers.get('X-Header')).toBe('bar')311312 expect(res.headers.get('X-Query-2')).toBe('bar')313 expect(res.headers.get('X-Param-2')).toBe('bar')314 expect(res.headers.get('X-Header-2')).toBe('bar')315 })316})