// src/request.ts import { parseBody } from "./utils/body.js"; import { getQueryParam, getQueryParams, decodeURIComponent_ } from "./utils/url.js"; var HonoRequest = class { raw; #validatedData; #matchResult; routeIndex = 0; path; bodyCache = {}; constructor(request, path = "/", matchResult = [[]]) { this.raw = request; this.path = path; this.#matchResult = matchResult; this.#validatedData = {}; } param(key) { return key ? this.getDecodedParam(key) : this.getAllDecodedParams(); } getDecodedParam(key) { const paramKey = this.#matchResult[0][this.routeIndex][1][key]; const param = this.getParamValue(paramKey); return param ? /\%/.test(param) ? decodeURIComponent_(param) : param : void 0; } getAllDecodedParams() { const decoded = {}; const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]); for (const key of keys) { const value = this.getParamValue(this.#matchResult[0][this.routeIndex][1][key]); if (value && typeof value === "string") { decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value; } } return decoded; } getParamValue(paramKey) { return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey; } query(key) { return getQueryParam(this.url, key); } queries(key) { return getQueryParams(this.url, key); } header(name) { if (name) { return this.raw.headers.get(name.toLowerCase()) ?? void 0; } const headerData = {}; this.raw.headers.forEach((value, key) => { headerData[key] = value; }); return headerData; } async parseBody(options) { if (this.bodyCache.parsedBody) { return this.bodyCache.parsedBody; } const parsedBody = await parseBody(this, options); this.bodyCache.parsedBody = parsedBody; return parsedBody; } cachedBody = (key) => { const { bodyCache, raw } = this; const cachedBody = bodyCache[key]; if (cachedBody) { return cachedBody; } if (!bodyCache[key]) { for (const keyOfBodyCache of Object.keys(bodyCache)) { if (keyOfBodyCache === "parsedBody") { continue; } return (async () => { let body = await bodyCache[keyOfBodyCache]; if (keyOfBodyCache === "json") { body = JSON.stringify(body); } return await new Response(body)[key](); })(); } } return bodyCache[key] = raw[key](); }; json() { return this.cachedBody("json"); } text() { return this.cachedBody("text"); } arrayBuffer() { return this.cachedBody("arrayBuffer"); } blob() { return this.cachedBody("blob"); } formData() { return this.cachedBody("formData"); } addValidatedData(target, data) { this.#validatedData[target] = data; } valid(target) { return this.#validatedData[target]; } get url() { return this.raw.url; } get method() { return this.raw.method; } get matchedRoutes() { return this.#matchResult[0].map(([[, route]]) => route); } get routePath() { return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path; } }; export { HonoRequest };