Skip to content
Myr

fix: avoid circular dependency between body.ts and request.ts (#5071)

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

* fix: avoid circular dependency between body.ts and request.ts * docs: add comments to the criteria for condition evaluation

authored by Taku Amano · committed by GitHub · parent b20d422 · Browse files · 1 file+6 added−4 removed
This commit

State

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

Diff stats

1 file+6 added−4 removed

  • other:+6 added−4 removed

1 file+6 added−4 removed

modified:src/utils/body.ts+6 added-4 removed
1 unmodified line
2 * @module3 * Body utility.4 */56import { HonoRequest } from '../request'6import type { HonoRequest } from '../request'7import { bufferToFormData } from './buffer'89type BodyDataValueDot = { [x: string]: string | File | BodyDataValueDot }10type BodyDataValueDotAll = {
62 unmodified lines
73 */74 dot: boolean75}7677const isRawRequest = (request: HonoRequest | Request): request is Request => 'headers' in request // 'headers' method exists only on Request, not on HonoRequest.7879/**80 * Parses the body of a request based on the provided options.81 *82 * @template T - The type of the parsed body data.
16 unmodified lines
99 options = Object.create(null)100) => {101 const { all = false, dot = false } = options102101 const headers = request instanceof HonoRequest ? request.raw.headers : request.headers103 const headers = isRawRequest(request) ? request.headers : request.raw.headers104 const contentType = headers.get('Content-Type')105106 const mediaType = contentType?.split(';')[0].trim().toLowerCase()107
15 unmodified lines
123async function parseFormData<T extends BodyData>(124 request: HonoRequest | Request,125 options: ParseBodyOptions126): Promise<T> {125 const headers = request instanceof HonoRequest ? request.raw.headers : request.headers127 const headers = isRawRequest(request) ? request.headers : request.raw.headers128 const arrayBuffer = await (request as Request).arrayBuffer()129 const formDataPromise = bufferToFormData(arrayBuffer, headers.get('Content-Type') || '')128 if (request instanceof HonoRequest) {130 if (!isRawRequest(request)) {131 // Cache so that a later `c.req.formData()` reuses the already-consumed body132 request.bodyCache.formData = formDataPromise as unknown as FormData133 }134 const formData = await formDataPromise
108 unmodified lines