Skip to content

Commit

Permalink
refactor(factory): rename defaultOptions to defaultAppOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
miyaji255 committed Jan 1, 2025
1 parent 89e8add commit 24337a9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/helper/factory/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,16 @@ describe('createApp', () => {
expect(await res.text()).toBe('bar')
})

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

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

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

createApp = (options?: HonoOptions<E>): Hono<E> => {
const app = new Hono<E>(
options && this.#defaultOptions
? { ...this.#defaultOptions, ...options }
: options ?? this.#defaultOptions
options && this.#defaultAppOptions
? { ...this.#defaultAppOptions, ...options }
: options ?? this.#defaultAppOptions
)
if (this.initApp) {
this.initApp(app)
Expand All @@ -315,7 +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>
defaultOptions?: HonoOptions<E>
defaultAppOptions?: HonoOptions<E>
}): Factory<E, P> => new Factory<E, P>(init)

export const createMiddleware = <
Expand Down

0 comments on commit 24337a9

Please sign in to comment.