Skip to content
Myr

fix(context): fixed middleware + `c.json({}, requestInit)` problem (#2025)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(context): fixed middleware + `c.json({}, requestInit)` problem * denoify

authored by Yusuke Wada · committed by GitHub · parent a5d9ca8 · Browse files · 3 files+46 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+46 added−16 removed

  • tests:+20 added−0 removed
  • other:+26 added−16 removed

3 files+46 added−16 removed

modified:deno_dist/context.ts+13 added-8 removed
83 unmodified lines
84}8586export const TEXT_PLAIN = 'text/plain; charset=UTF-8'8788const setHeaders = (headers: Headers, map: Record<string, string> = {}) => {89 Object.entries(map).forEach(([key, value]) => headers.set(key, value))90 return headers91}9293export class Context<94 // eslint-disable-next-line @typescript-eslint/no-explicit-any95 E extends Env = any,96 // eslint-disable-next-line @typescript-eslint/no-explicit-any
146 unmodified lines
243 })244 }245246 if (arg && typeof arg !== 'number') {242 this.res = new Response(data, arg)247 const headers = setHeaders(new Headers(arg.headers), this.#preparedHeaders)248 return new Response(data, {249 headers,250 status: arg.status,251 })252 }253245 const status = typeof arg === 'number' ? arg : arg ? arg.status : this.#status254 const status = typeof arg === 'number' ? arg : this.#status255 this.#preparedHeaders ??= {}256257 this.#headers ??= new Headers()249 for (const [k, v] of Object.entries(this.#preparedHeaders)) {250 this.#headers.set(k, v)251 }258 setHeaders(this.#headers, this.#preparedHeaders)259260 if (this.#res) {261 this.#res.headers.forEach((v, k) => {262 this.#headers?.set(k, v)263 })257 for (const [k, v] of Object.entries(this.#preparedHeaders)) {258 this.#headers.set(k, v)259 }264 setHeaders(this.#headers, this.#preparedHeaders)265 }266267 headers ??= {}268 for (const [k, v] of Object.entries(headers)) {
224 unmodified lines
modified:src/context.ts+13 added-8 removed
83 unmodified lines
84}8586export const TEXT_PLAIN = 'text/plain; charset=UTF-8'8788const setHeaders = (headers: Headers, map: Record<string, string> = {}) => {89 Object.entries(map).forEach(([key, value]) => headers.set(key, value))90 return headers91}9293export class Context<94 // eslint-disable-next-line @typescript-eslint/no-explicit-any95 E extends Env = any,96 // eslint-disable-next-line @typescript-eslint/no-explicit-any
146 unmodified lines
243 })244 }245246 if (arg && typeof arg !== 'number') {242 this.res = new Response(data, arg)247 const headers = setHeaders(new Headers(arg.headers), this.#preparedHeaders)248 return new Response(data, {249 headers,250 status: arg.status,251 })252 }253245 const status = typeof arg === 'number' ? arg : arg ? arg.status : this.#status254 const status = typeof arg === 'number' ? arg : this.#status255 this.#preparedHeaders ??= {}256257 this.#headers ??= new Headers()249 for (const [k, v] of Object.entries(this.#preparedHeaders)) {250 this.#headers.set(k, v)251 }258 setHeaders(this.#headers, this.#preparedHeaders)259260 if (this.#res) {261 this.#res.headers.forEach((v, k) => {262 this.#headers?.set(k, v)263 })257 for (const [k, v] of Object.entries(this.#preparedHeaders)) {258 this.#headers.set(k, v)259 }264 setHeaders(this.#headers, this.#preparedHeaders)265 }266267 headers ??= {}268 for (const [k, v] of Object.entries(headers)) {
224 unmodified lines
modified:src/hono.test.ts+20 added
1275 unmodified lines
1276 expect(res.headers.get('X-Header-2')).toBe('bar')1277 })1278})12791280describe('Middleware + c.json(0, requestInit)', () => {1281 const app = new Hono()1282 app.use('/', async (c, next) => {1283 await next()1284 })1285 app.get('/', (c) => {1286 return c.json(0, {1287 status: 200,1288 headers: {1289 foo: 'bar',1290 },1291 })1292 })1293 it('Should return a correct headers', async () => {1294 const res = await app.request('/')1295 expect(res.headers.get('content-type')).toMatch(/^application\/json/)1296 expect(res.headers.get('foo')).toBe('bar')1297 })1298})12991300describe('Hono with `app.route`', () => {1301 describe('Basic', () => {1302 const app = new Hono()1303 const api = new Hono()
1940 unmodified lines