Repository: honojs/hono
File: src/helper/factory/index.ts (TypeScript)

Code before:
    handler4: H<E5, P, I4, R>,
    handler5: H<E6, P, I5, R>,
    handler6: H<E7, P, I6, R>,
    handler7: H<E8, P, I7, R>,
    handler8: H<E9, P, I8, R>,
    handler9: H<E10, P, I9, R>,
    handler10: H<E11, P, I10, R>
  ): [
    H<E2, P, I, R>,
    H<E3, P, I2, R>,
    H<E4, P, I3, R>,
    H<E5, P, I4, R>,
    H<E6, P, I5, R>,
    H<E7, P, I6, R>,
    H<E8, P, I7, R>,
    H<E9, P, I8, R>,
    H<E10, P, I9, R>,
    H<E11, P, I10, R>
  ]
}

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

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

  createApp = (options?: HonoOptions<E>): Hono<E> => {
    const app = new Hono<E>(
      options && this.#defaultOptions
        ? { ...this.#defaultOptions, ...options }
        : options ?? this.#defaultOptions
    )
    if (this.initApp) {
      this.initApp(app)
    }
    return app
  }

  createMiddleware = <I extends Input = {}>(middleware: MiddlewareHandler<E, P, I>) => middleware

  createHandlers: CreateHandlersInterface<E, P> = (...handlers: any) => {
    // @ts-expect-error this should not be typed
    return handlers.filter((handler) => handler !== undefined)
  }
}

export const createFactory = <E extends Env = any, P extends string = any>(init?: {

Review comment (suggestion):
`Factory` does not provide only `createApp()` but it provides `createMiddleware()` and `createHandlers()`. So the name `defaultOptions` may introduce confusion because the user may understand the option is not for `createApp()`. So I think it should have a different name, such as `defaultAppOptions`.

Code after:
    handler4: H<E5, P, I4, R>,
    handler5: H<E6, P, I5, R>,
    handler6: H<E7, P, I6, R>,
    handler7: H<E8, P, I7, R>,
    handler8: H<E9, P, I8, R>,
    handler9: H<E10, P, I9, R>,
    handler10: H<E11, P, I10, R>
  ): [
    H<E2, P, I, R>,
    H<E3, P, I2, R>,
    H<E4, P, I3, R>,
    H<E5, P, I4, R>,
    H<E6, P, I5, R>,
    H<E7, P, I6, R>,
    H<E8, P, I7, R>,
    H<E9, P, I8, R>,
    H<E10, P, I9, R>,
    H<E11, P, I10, R>
  ]
}

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

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

  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)
    }
    return app
  }

  createMiddleware = <I extends Input = {}>(middleware: MiddlewareHandler<E, P, I>) => middleware

  createHandlers: CreateHandlersInterface<E, P> = (...handlers: any) => {
    // @ts-expect-error this should not be typed
    return handlers.filter((handler) => handler !== undefined)
  }
}

export const createFactory = <E extends Env = any, P extends string = any>(init?: {