|
|
| import { HTTPException } from "./http-exception.js";
|
| import { GET_MATCH_RESULT } from "./request/constants.js";
|
| import { parseBody } from "./utils/body.js";
|
| import { decodeURIComponent_, getQueryParam, getQueryParams, tryDecode } from "./utils/url.js";
|
| var tryDecodeURIComponent = (str) => tryDecode(str, decodeURIComponent_);
|
| 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) ? tryDecodeURIComponent(param) : param;
|
| }
|
| #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 !== void 0) {
|
| decoded[key] = /\%/.test(value) ? tryDecodeURIComponent(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) ?? void 0;
|
| }
|
| const headerData = {};
|
| this.raw.headers.forEach((value, key) => {
|
| headerData[key] = value;
|
| });
|
| return headerData;
|
| }
|
| async parseBody(options) {
|
| return 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");
|
| }
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| bytes() {
|
| return this.#cachedBody("arrayBuffer").then((buffer) => new Uint8Array(buffer));
|
| }
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| 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 [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;
|
| }
|
| };
|
| var cloneRawRequest = async (req) => {
|
| if (!req.raw.bodyUsed) {
|
| return req.raw.clone();
|
| }
|
| const cacheKey = Object.keys(req.bodyCache)[0];
|
| if (!cacheKey) {
|
| throw new HTTPException(500, {
|
| message: "Cannot clone request: body was already consumed and not cached. Please use HonoRequest methods (e.g., req.json(), req.text()) instead of consuming req.raw directly."
|
| });
|
| }
|
| const requestInit = {
|
| body: await req[cacheKey](),
|
| cache: req.raw.cache,
|
| credentials: req.raw.credentials,
|
| headers: req.header(),
|
| integrity: req.raw.integrity,
|
| keepalive: req.raw.keepalive,
|
| method: req.method,
|
| mode: req.raw.mode,
|
| redirect: req.raw.redirect,
|
| referrer: req.raw.referrer,
|
| referrerPolicy: req.raw.referrerPolicy,
|
| signal: req.raw.signal
|
| };
|
| return new Request(req.url, requestInit);
|
| };
|
| export {
|
| HonoRequest,
|
| cloneRawRequest
|
| };
|
|
|