diff --git a/deno_dist/context.ts b/deno_dist/context.ts index 40cd7a3bd..39fd4c573 100644 --- a/deno_dist/context.ts +++ b/deno_dist/context.ts @@ -230,17 +230,11 @@ export class Context< }) } - // Return Response immediately if arg is ResponseInit. if (arg && typeof arg !== 'number') { - const res = new Response(data, arg) - const contentType = this.#preparedHeaders?.['content-type'] - if (contentType) { - res.headers.set('content-type', contentType) - } - return res + this.res = new Response(data, arg) } - const status = arg ?? this.#status + let status = typeof arg === 'number' ? arg : this.#status this.#preparedHeaders ??= {} this.#headers ??= new Headers() @@ -255,6 +249,7 @@ export class Context< for (const [k, v] of Object.entries(this.#preparedHeaders)) { this.#headers.set(k, v) } + status = this.#res.status } headers ??= {} diff --git a/src/context.test.ts b/src/context.test.ts index 660a335c5..19c5bd1a7 100644 --- a/src/context.test.ts +++ b/src/context.test.ts @@ -205,6 +205,16 @@ describe('Context header', () => { const res = c.text('foo') expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/) }) + + it('Should set header values if the #this.headers is set and the arg is ResponseInit', async () => { + c.header('foo', 'bar') + const res = c.body('foo', { + headers: { + 'Content-Type': 'text/plain', + }, + }) + expect(res.headers.get('foo')).toBe('bar') + }) }) describe('Pass a ResponseInit to respond methods', () => { diff --git a/src/context.ts b/src/context.ts index bfb987d51..895eecc23 100644 --- a/src/context.ts +++ b/src/context.ts @@ -230,17 +230,11 @@ export class Context< }) } - // Return Response immediately if arg is ResponseInit. if (arg && typeof arg !== 'number') { - const res = new Response(data, arg) - const contentType = this.#preparedHeaders?.['content-type'] - if (contentType) { - res.headers.set('content-type', contentType) - } - return res + this.res = new Response(data, arg) } - const status = arg ?? this.#status + let status = typeof arg === 'number' ? arg : this.#status this.#preparedHeaders ??= {} this.#headers ??= new Headers() @@ -255,6 +249,7 @@ export class Context< for (const [k, v] of Object.entries(this.#preparedHeaders)) { this.#headers.set(k, v) } + status = this.#res.status } headers ??= {}