Skip to content

Commit

Permalink
feat(powered-by): optional server name (#3492)
Browse files Browse the repository at this point in the history
* refactor(powered-by): optional server name

* style(powered-by): format

* test(powered-by): test new config

* Update index.test.ts
  • Loading branch information
PatrickJS authored Oct 15, 2024
1 parent cebf4e8 commit fc9cc6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/middleware/powered-by/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ describe('Powered by Middleware', () => {
app.use('/poweredBy2/*', poweredBy())
app.get('/poweredBy2', (c) => c.text('root'))

app.use('/poweredBy3/*', poweredBy({ serverName: 'Foo' }))
app.get('/poweredBy3', (c) => c.text('root'))

it('Should return with X-Powered-By header', async () => {
const res = await app.request('http://localhost/poweredBy')
expect(res).not.toBeNull()
Expand All @@ -24,4 +27,11 @@ describe('Powered by Middleware', () => {
expect(res.status).toBe(200)
expect(res.headers.get('X-Powered-By')).toBe('Hono')
})

it('Should return custom serverName', async () => {
const res = await app.request('http://localhost/poweredBy3')
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Powered-By')).toBe('Foo')
})
})
9 changes: 6 additions & 3 deletions src/middleware/powered-by/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
* @module
* Powered By Middleware for Hono.
*/

import type { MiddlewareHandler } from '../../types'

export const poweredBy = (): MiddlewareHandler => {
type PoweredByOptions = {
serverName?: string
}

export const poweredBy = (options?: PoweredByOptions): MiddlewareHandler => {
return async function poweredBy(c, next) {
await next()
c.res.headers.set('X-Powered-By', 'Hono')
c.res.headers.set('X-Powered-By', options?.serverName ?? 'Hono')
}
}

0 comments on commit fc9cc6d

Please sign in to comment.