Repository: honojs/hono
File: src/context.ts (TypeScript)

Code before:
  }

  #newResponse(
    data: Data | null,
    arg?: StatusCode | ResponseOrInit,
    headers?: HeaderRecord
  ): Response {
    const responseHeaders = this.#res
      ? new Headers(this.#res.headers)
      : this.#preparedHeaders ?? new Headers()

    if (typeof arg === 'object' && 'headers' in arg) {
      const argHeaders = arg.headers instanceof Headers ? arg.headers : new Headers(arg.headers)
      for (const [key, value] of argHeaders) {
        if (key.toLowerCase() === 'set-cookie') {
          responseHeaders.append(key, value)
        } else {
          responseHeaders.set(key, value)
        }
      }
    }

    if (headers) {
      for (const [key, value] of Object.entries(headers)) {
        if (Array.isArray(value)) {
          for (const v of value) responseHeaders.append(key, v)
        } else {
          responseHeaders.set(key, value)
        }
      }
    }

    const status = typeof arg === 'number' ? arg : arg?.status ?? this.#status
    return new Response(data, { status, headers: responseHeaders })
  }

  newResponse: NewResponse = (...args) => this.#newResponse(...(args as Parameters<NewResponse>))

  /**
   * `.body()` can return the HTTP response.
   * You can set headers with `.header()` and set HTTP status code with `.status`.
   * This can also be set in `.text()`, `.json()` and so on.
   *
   * @see {@link https://hono.dev/docs/api/context#body}
   *
   * @example
   * ```ts
   * app.get('/welcome', (c) => {
   *   // Set headers
   *   c.header('X-Message', 'Hello!')
   *   c.header('Content-Type', 'text/plain')

Review comment (suggestion):
I think this is necessary to ensure compatibility.

```suggestion
          responseHeaders.delete(key)
          for (const v of value) {
            responseHeaders.append(key, v)
          }
```

Code after:
  }

  #newResponse(
    data: Data | null,
    arg?: StatusCode | ResponseOrInit,
    headers?: HeaderRecord
  ): Response {
    const responseHeaders = this.#res
      ? new Headers(this.#res.headers)
      : this.#preparedHeaders ?? new Headers()

    if (typeof arg === 'object' && 'headers' in arg) {
      const argHeaders = arg.headers instanceof Headers ? arg.headers : new Headers(arg.headers)
      for (const [key, value] of argHeaders) {
        if (key.toLowerCase() === 'set-cookie') {
          responseHeaders.append(key, value)
        } else {
          responseHeaders.set(key, value)
        }
      }
    }

    if (headers) {
      for (const [k, v] of Object.entries(headers)) {
        if (typeof v === 'string') {
          responseHeaders.set(k, v)
        } else {
          responseHeaders.delete(k)
          for (const v2 of v) {
            responseHeaders.append(k, v2)
          }
        }
      }
    }

    const status = typeof arg === 'number' ? arg : arg?.status ?? this.#status
    return new Response(data, { status, headers: responseHeaders })
  }

  newResponse: NewResponse = (...args) => this.#newResponse(...(args as Parameters<NewResponse>))

  /**
   * `.body()` can return the HTTP response.
   * You can set headers with `.header()` and set HTTP status code with `.status`.
   * This can also be set in `.text()`, `.json()` and so on.
   *
   * @see {@link https://hono.dev/docs/api/context#body}
   *
   * @example
   * ```ts
   * app.get('/welcome', (c) => {