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
Provenance
- Commit
0574740- Tree
1080760- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
489a241
6 files changed+61 added−48 removed
deleted:src/middleware/default.test.ts-26 removed
1234567891011121314151617181920212223242526import { Hono } from '../hono'
describe('default middleware', () => { const app = new Hono() app.get('/:foo', (c) => { const query = c.req.query('foo') const param = c.req.param('foo') const header = c.req.header('User-Agent') c.header('X-Query', query) c.header('X-Param', param) c.header('X-Header', header) return c.body('Hono') })
it('query', async () => { const url = new URL('http://localhost/bar') url.searchParams.append('foo', 'bar') const req = new Request(url.toString()) req.headers.append('User-Agent', 'bar') const res = await app.dispatch(req) expect(res.status).toBe(200) expect(res.headers.get('X-Query')).toBe('bar') expect(res.headers.get('X-Param')).toBe('bar') expect(res.headers.get('X-Header')).toBe('bar') })})deleted:src/middleware/default.ts-14 removed
1234567891011121314import type { Context } from '../context'
export const defaultMiddleware = async (c: Context, next: Function) => { c.req.query = (key: string) => { // eslint-disable-next-line const url = new URL(c.req.url) return url.searchParams.get(key) } c.req.header = (name: string): string => { return c.req.headers.get(name) }
await next()}modified:src/context.ts+10 added
23 unmodified lines24252627282930313233343536373839404181 unmodified lines23 unmodified lines this._headers = {} }
header(name: string, value: string): void { /* XXX: app.use('*', (c, next) => { next() c.header('foo', 'bar') // => c.res.headers.set(...) }) */ if (this.res) { this.res.headers.set(name, value) } this._headers[name] = value }
status(number: number): void {81 unmodified linesmodified:src/hono.ts+9 added-3 removed
9 unmodified lines10 11 declare 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 lines127 const [method, path] = [request.method, getPathFromURL(request.url)]128 129 const result = await this.matchRoute(method, path)130 131+ // 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+ }144 145 const handler = result ? result.handler[0] : this.notFound // XXX146 147 const middleware = []9 unmodified lines157 context.res = await handler(context)158 await next()159 }160 154- middleware.push(Middleware.default)161 middleware.push(wrappedHandler)162 163 const composed = compose(middleware)164 const c = new Context(request, { env: env, event: event, res: null })44 unmodified linesmodified:src/middleware.ts+1 added-5 removed
123145import { defaultMiddleware } from './middleware/default'
export class Middleware {export class Middleware {} static default = defaultMiddleware}modified:test/hono.test.ts+41 added
271 unmodified lines272 expect(await res.text()).toBe('Custom Error Message')273 expect(res.headers.get('debug')).toBe('This is Error')274 })275 })276+277+describe('Request methods with custom middleware', () => {278+ const app = new Hono()279+280+ 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+ })289+290+ 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+ })299+300+ 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)306+307+ 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')311+312+ 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+})