From fc9cc6da28de5d86187234a4085defe9408a44ee Mon Sep 17 00:00:00 2001 From: PatrickJS Date: Mon, 14 Oct 2024 22:50:39 -0400 Subject: [PATCH] feat(powered-by): optional server name (#3492) * refactor(powered-by): optional server name * style(powered-by): format * test(powered-by): test new config * Update index.test.ts --- src/middleware/powered-by/index.test.ts | 10 ++++++++++ src/middleware/powered-by/index.ts | 9 ++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/middleware/powered-by/index.test.ts b/src/middleware/powered-by/index.test.ts index 89a650a1..d0a1ada3 100644 --- a/src/middleware/powered-by/index.test.ts +++ b/src/middleware/powered-by/index.test.ts @@ -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() @@ -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') + }) }) diff --git a/src/middleware/powered-by/index.ts b/src/middleware/powered-by/index.ts index 605065a0..66004cb2 100644 --- a/src/middleware/powered-by/index.ts +++ b/src/middleware/powered-by/index.ts @@ -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') } }