diff --git a/src/helper/factory/index.test.ts b/src/helper/factory/index.test.ts index a6eeefca6..aef251472 100644 --- a/src/helper/factory/index.test.ts +++ b/src/helper/factory/index.test.ts @@ -272,6 +272,22 @@ describe('createApp', () => { expect(res.status).toBe(200) expect(await res.text()).toBe('bar') }) + + it('Should use the default options', async () => { + const app = createFactory({ defaultOptions: { strict: false } }).createApp() + app.get('/hello', (c) => c.text('hello')) + const res = await app.request('/hello/') + expect(res.status).toBe(200) + expect(await res.text()).toBe('hello') + }) + + it('Should override the default options when creating', async () => { + const app = createFactory({ defaultOptions: { strict: true } }).createApp({ strict: false }) + app.get('/hello', (c) => c.text('hello')) + const res = await app.request('/hello/') + expect(res.status).toBe(200) + expect(await res.text()).toBe('hello') + }) }) describe('Lint rules', () => { diff --git a/src/helper/factory/index.ts b/src/helper/factory/index.ts index 49a3cb1c9..4cf6d1448 100644 --- a/src/helper/factory/index.ts +++ b/src/helper/factory/index.ts @@ -5,6 +5,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { Hono } from '../../hono' +import type { HonoOptions } from '../../hono-base' import type { Env, H, @@ -285,13 +286,19 @@ export interface CreateHandlersInterface { export class Factory { private initApp?: InitApp + #defaultOptions?: HonoOptions - constructor(init?: { initApp?: InitApp }) { + constructor(init?: { initApp?: InitApp; defaultOptions?: HonoOptions }) { this.initApp = init?.initApp + this.#defaultOptions = init?.defaultOptions } - createApp = (): Hono => { - const app = new Hono() + createApp = (options?: HonoOptions): Hono => { + const app = new Hono( + options && this.#defaultOptions + ? { ...this.#defaultOptions, ...options } + : options ?? this.#defaultOptions + ) if (this.initApp) { this.initApp(app) } @@ -308,6 +315,7 @@ export class Factory { export const createFactory = (init?: { initApp?: InitApp + defaultOptions?: HonoOptions }): Factory => new Factory(init) export const createMiddleware = <