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
Provenance
- Commit
b78ca3d- Tree
bc91252- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
a5d9ca8
3 files changed+46 added−16 removed
modified:deno_dist/context.ts+13 added-8 removed
83 unmodified lines84 }85 86 export const TEXT_PLAIN = 'text/plain; charset=UTF-8'87 88+const setHeaders = (headers: Headers, map: Record<string, string> = {}) => {89+ Object.entries(map).forEach(([key, value]) => headers.set(key, value))90+ return headers91+}92+93 export 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-any146 unmodified lines243 })244 }245 246 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 }253 245- const status = typeof arg === 'number' ? arg : arg ? arg.status : this.#status254+ const status = typeof arg === 'number' ? arg : this.#status255 this.#preparedHeaders ??= {}256 257 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)259 260 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 }266 267 headers ??= {}268 for (const [k, v] of Object.entries(headers)) {224 unmodified linesmodified:src/context.ts+13 added-8 removed
83 unmodified lines84 }85 86 export const TEXT_PLAIN = 'text/plain; charset=UTF-8'87 88+const setHeaders = (headers: Headers, map: Record<string, string> = {}) => {89+ Object.entries(map).forEach(([key, value]) => headers.set(key, value))90+ return headers91+}92+93 export 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-any146 unmodified lines243 })244 }245 246 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 }253 245- const status = typeof arg === 'number' ? arg : arg ? arg.status : this.#status254+ const status = typeof arg === 'number' ? arg : this.#status255 this.#preparedHeaders ??= {}256 257 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)259 260 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 }266 267 headers ??= {}268 for (const [k, v] of Object.entries(headers)) {224 unmodified linesmodified:src/hono.test.ts+20 added
1275 unmodified lines1276 expect(res.headers.get('X-Header-2')).toBe('bar')1277 })1278 })1279 1280+describe('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+})1299+1300 describe('Hono with `app.route`', () => {1301 describe('Basic', () => {1302 const app = new Hono()1303 const api = new Hono()1940 unmodified lines