@busy-hour/blaze
Version:
<h1 align='center'>🔥 Blaze</h1> <div align='center'> An event driven framework for 🔥 Hono.js </div>
47 lines (46 loc) • 1.29 kB
JavaScript
// src/extractor/rest/index.ts
import qs from "node:querystring";
import {
FORM_CONTENT_TYPE,
REST_CONTENT_TYPE
} from "../../utils/constant/rest/index.js";
async function getReqBody(honoCtx) {
const contentType = honoCtx.req.header("Content-Type");
if (!contentType)
return null;
if (FORM_CONTENT_TYPE.some((type) => contentType.startsWith(type))) {
return honoCtx.req.parseBody({ all: true });
}
if (contentType.startsWith(REST_CONTENT_TYPE.JSON)) {
return honoCtx.req.json();
}
if (contentType.startsWith(REST_CONTENT_TYPE.TEXT)) {
return honoCtx.req.text();
}
if (contentType.startsWith(REST_CONTENT_TYPE.BODY)) {
return honoCtx.req.blob();
}
return null;
}
function getReqQuery(honoCtx) {
const searchParams = new URLSearchParams(honoCtx.req.url.split("?")[1] || "");
return qs.parse(searchParams.toString());
}
function extractRestPath(restRoute) {
const restPath = restRoute.split(" ");
if (restPath.length === 1) {
return [null, restPath[0]];
}
return [restPath[0], restPath[1]];
}
function extractRestParams(params) {
if (typeof params === "string")
return extractRestPath(params);
return [params.method ?? null, params.path];
}
export {
extractRestParams,
extractRestPath,
getReqBody,
getReqQuery
};