Skip to content

Commit

Permalink
fix(context): set headers values correctly (#1808)
Browse files Browse the repository at this point in the history
* fix(context): set headers values correctly

* denoify
  • Loading branch information
yusukebe authored Dec 12, 2023
1 parent 75dbd4d commit 0f33cf8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
11 changes: 3 additions & 8 deletions deno_dist/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 ??= {}
Expand Down
10 changes: 10 additions & 0 deletions src/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
11 changes: 3 additions & 8 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 ??= {}
Expand Down

0 comments on commit 0f33cf8

Please sign in to comment.