From ff5419c0e9a57a8f50e8163cefef01c13ca53b65 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 12 Dec 2023 11:58:48 +0900 Subject: [PATCH 1/2] fix(context): set headers values correctly --- src/context.test.ts | 10 ++++++++++ src/context.ts | 11 +++-------- 2 files changed, 13 insertions(+), 8 deletions(-) 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 ??= {} From 09b29658f96688123095919a91497417778875e9 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 12 Dec 2023 13:00:17 +0900 Subject: [PATCH 2/2] denoify --- deno_dist/context.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) 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 ??= {}