From 88df68327fa684927d1dda78d9247adaac5075bb Mon Sep 17 00:00:00 2001 From: Marcel Overdijk Date: Tue, 3 Sep 2024 17:52:26 +0200 Subject: [PATCH] feat(context): added (optional) message argument to notFound() handler --- src/context.ts | 4 ++-- src/hono-base.ts | 11 +++++++++-- src/hono.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/types.ts | 5 ++++- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/context.ts b/src/context.ts index e3dbc3c5..59f35a98 100644 --- a/src/context.ts +++ b/src/context.ts @@ -899,8 +899,8 @@ export class Context< * }) * ``` */ - notFound = (): Response | Promise => { + notFound = (message?: string | object): Response | Promise => { this.#notFoundHandler ??= () => new Response() - return this.#notFoundHandler(this) + return this.#notFoundHandler(this, message) } } diff --git a/src/hono-base.ts b/src/hono-base.ts index 812a1d6c..75de79eb 100644 --- a/src/hono-base.ts +++ b/src/hono-base.ts @@ -33,8 +33,15 @@ import { getPath, getPathNoStrict, mergePath } from './utils/url' */ export const COMPOSED_HANDLER = Symbol('composedHandler') -const notFoundHandler = (c: Context) => { - return c.text('404 Not Found', 404) +const notFoundHandler = (c: Context, message?: string | object) => { + if (!message) { + message = '404 Not Found' + } + const status = 404 + if (typeof message === 'string') { + return c.text(message, status) + } + return c.json(message, 404) } const errorHandler = (err: Error | HTTPResponseError, c: Context) => { diff --git a/src/hono.test.ts b/src/hono.test.ts index 91d37f29..9bda1631 100644 --- a/src/hono.test.ts +++ b/src/hono.test.ts @@ -1365,6 +1365,46 @@ describe('Not Found', () => { expect(await res.text()).toBe('Custom NotFound') }) }) + + describe('Not Found message as string', () => { + const app = new Hono() + + app.get('/not-found', (c) => c.notFound('Custom not found message string')) + + it('Custom 404 Not Found message as string', async () => { + const res = await app.request('http://localhost/not-found') + expect(res.status).toBe(404) + expect(await res.text()).toBe('Custom not found message string') + }) + }) + + describe('Not Found message as object', () => { + const app = new Hono() + + app.get('/not-found', (c) => c.notFound({ message: 'Custom not found message object' })) + + it('Custom 404 Not Found message as object', async () => { + const res = await app.request('http://localhost/not-found') + expect(res.status).toBe(404) + expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8') + expect(await res.text()).toBe('{"message":"Custom not found message object"}') + }) + }) + + describe('Custom 404 Not Found handler with message', () => { + const app = new Hono() + app.notFound((c, message) => { + return c.text(message as string, 404) + }) + + app.get('/not-found', (c) => c.notFound('Custom not found handler and message')) + + it('Custom 404 Not Found handler and message', async () => { + const res = await app.request('http://localhost/not-found') + expect(res.status).toBe(404) + expect(await res.text()).toBe('Custom not found handler and message') + }) + }) }) describe('Redirect', () => { diff --git a/src/types.ts b/src/types.ts index fe4a3025..13d81f2c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -89,7 +89,10 @@ export type H< R extends HandlerResponse = any > = Handler | MiddlewareHandler -export type NotFoundHandler = (c: Context) => Response | Promise +export type NotFoundHandler = ( + c: Context, + message?: string | object +) => Response | Promise export interface HTTPResponseError extends Error { getResponse: () => Response