Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(factory): Allow HonoOptions<E> with factory #3786

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/helper/factory/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,22 @@ describe('createApp', () => {
expect(res.status).toBe(200)
expect(await res.text()).toBe('bar')
})

it('Should use the default app options', async () => {
const app = createFactory({ defaultAppOptions: { 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 app options when creating', async () => {
const app = createFactory({ defaultAppOptions: { 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', () => {
Expand Down
14 changes: 11 additions & 3 deletions src/helper/factory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -285,13 +286,19 @@ export interface CreateHandlersInterface<E extends Env, P extends string> {

export class Factory<E extends Env = any, P extends string = any> {
private initApp?: InitApp<E>
#defaultAppOptions?: HonoOptions<E>

constructor(init?: { initApp?: InitApp<E> }) {
constructor(init?: { initApp?: InitApp<E>; defaultAppOptions?: HonoOptions<E> }) {
this.initApp = init?.initApp
this.#defaultAppOptions = init?.defaultAppOptions
}

createApp = (): Hono<E> => {
const app = new Hono<E>()
createApp = (options?: HonoOptions<E>): Hono<E> => {
const app = new Hono<E>(
options && this.#defaultAppOptions
? { ...this.#defaultAppOptions, ...options }
: options ?? this.#defaultAppOptions
)
if (this.initApp) {
this.initApp(app)
}
Expand All @@ -308,6 +315,7 @@ export class Factory<E extends Env = any, P extends string = any> {

export const createFactory = <E extends Env = any, P extends string = any>(init?: {
initApp?: InitApp<E>
defaultAppOptions?: HonoOptions<E>
}): Factory<E, P> => new Factory<E, P>(init)

export const createMiddleware = <
Expand Down
Loading