Skip to content
Myr

fix(request): remove `parseBody` from bodyCache to prevent TypeError (#4807)

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

authored by Yusuke Wada · committed by GitHub · parent fe689ec · Browse files · 2 files+44 added−2 removed
This commit

State

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

Diff stats

2 files+44 added−2 removed

  • tests:+42 added−0 removed
  • other:+2 added−2 removed

2 files+44 added−2 removed

modified:src/request.test.ts+42 added
352 unmodified lines
353 expect(async () => await req.arrayBuffer()).not.toThrow()354 expect(async () => await req.blob()).not.toThrow()355 })356357 describe('should not break body methods after parseBody() with non-form content-type', () => {358 const createReq = () =>359 new HonoRequest(360 new Request('http://localhost', {361 method: 'POST',362 body: JSON.stringify({ event: 'push' }),363 headers: { 'Content-Type': 'application/json' },364 })365 )366367 it('text()', async () => {368 const req = createReq()369 await req.parseBody()370 expect(await req.text()).toBe(JSON.stringify({ event: 'push' }))371 })372373 it('json()', async () => {374 const req = createReq()375 await req.parseBody()376 expect(await req.json()).toEqual({ event: 'push' })377 })378379 it('arrayBuffer()', async () => {380 const req = createReq()381 await req.parseBody()382 expect(await req.arrayBuffer()).toBeInstanceOf(ArrayBuffer)383 })384385 it('blob()', async () => {386 const req = createReq()387 await req.parseBody()388 expect(await req.blob()).toBeInstanceOf(Blob)389 })390391 it('formData()', async () => {392 const req = createReq()393 await req.parseBody()394 // application/json is not a valid formData content-type, so this should throw395 expect(req.formData()).rejects.toThrow()396 })397 })398399 describe('Return type', () => {400 let req: HonoRequest401 beforeEach(() => {402 const data = new FormData()
165 unmodified lines
modified:src/request.ts+2 added-2 removed
22 unmodified lines
23 arrayBuffer: ArrayBuffer24 blob: Blob25 formData: FormData26}27type BodyCache = Partial<Body & { parsedBody: BodyData }>27type BodyCache = Partial<Body>2829type OptionalRequestInitProperties = 'window' | 'priority'30type RequiredRequestInit = Required<Omit<RequestInit, OptionalRequestInitProperties>> & {31 [Key in OptionalRequestInitProperties]?: RequestInit[Key]
181 unmodified lines
213 options?: Options214 ): Promise<T>215 async parseBody<T extends BodyData>(options?: Partial<ParseBodyOptions>): Promise<T>216 async parseBody(options?: Partial<ParseBodyOptions>) {217 return (this.bodyCache.parsedBody ??= await parseBody(this, options))217 return parseBody(this, options)218 }219220 #cachedBody = (key: keyof Body) => {221 const { bodyCache, raw } = this
268 unmodified lines