UNPKG

hono

Version:

Web framework built on Web Standards

140 lines (139 loc) 4.22 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var request_exports = {}; __export(request_exports, { HonoRequest: () => HonoRequest }); module.exports = __toCommonJS(request_exports); var import_constants = require("./request/constants"); var import_body = require("./utils/body"); var import_url = require("./utils/url"); const tryDecodeURIComponent = (str) => (0, import_url.tryDecode)(str, import_url.decodeURIComponent_); class HonoRequest { 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) ? tryDecodeURIComponent(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) ? tryDecodeURIComponent(value) : value; } } return decoded; } #getParamValue(paramKey) { return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey; } query(key) { return (0, import_url.getQueryParam)(this.url, key); } queries(key) { return (0, import_url.getQueryParams)(this.url, key); } header(name) { if (name) { return this.raw.headers.get(name) ?? void 0; } const headerData = {}; this.raw.headers.forEach((value, key) => { headerData[key] = value; }); return headerData; } async parseBody(options) { return this.bodyCache.parsedBody ??= await (0, import_body.parseBody)(this, options); } #cachedBody = (key) => { const { bodyCache, raw } = this; const cachedBody = bodyCache[key]; if (cachedBody) { return cachedBody; } const anyCachedKey = Object.keys(bodyCache)[0]; if (anyCachedKey) { return bodyCache[anyCachedKey].then((body) => { if (anyCachedKey === "json") { body = JSON.stringify(body); } return new Response(body)[key](); }); } return bodyCache[key] = raw[key](); }; json() { return this.#cachedBody("text").then((text) => JSON.parse(text)); } 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 [import_constants.GET_MATCH_RESULT]() { return this.#matchResult; } get matchedRoutes() { return this.#matchResult[0].map(([[, route]]) => route); } get routePath() { return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { HonoRequest });