phecda-server
Version:
server framework that provide IOC/type-reuse/http&rpc-adaptor
166 lines (164 loc) • 6.54 kB
JavaScript
import {
BadRequestException,
Context,
createControllerMetaMap,
detectAopDep
} from "../../chunk-WST6E6MQ.mjs";
import {
__name
} from "../../chunk-URKOYTBU.mjs";
// src/http/hono/bind.ts
import Debug from "debug";
import { Hono } from "hono";
import { deleteCookie, getCookie, setCookie } from "hono/cookie";
var debug = Debug("phecda-server/hono");
function bind(router, data, opts = {}) {
const { globalGuards, parallelRoute, globalAddons = [], parallelAddons = [], globalFilter, globalPipe, dynamic = false } = opts;
const { moduleMap, meta } = data;
const metaMap = createControllerMetaMap(meta, (meta2) => {
const { controller, http, method, tag } = meta2.data;
if (controller === "http" && http?.method) {
debug(`register method "${method}" in module "${tag}"`);
return true;
}
});
detectAopDep(meta, {
addons: [
...globalAddons,
...parallelAddons
],
guards: globalGuards
});
registerRoute();
function registerRoute() {
if (parallelRoute) {
const subApp = new Hono();
Context.applyAddons(parallelAddons, subApp, "hono");
subApp.post("", async (c) => {
const body = await c.req.json();
async function errorHandler(e) {
const error = await Context.filterRecord.default(e);
c.status(error.status);
return c.json(error);
}
__name(errorHandler, "errorHandler");
if (!Array.isArray(body)) return errorHandler(new BadRequestException("data format should be an array"));
try {
return Promise.all(body.map((item, i) => {
return new Promise(async (resolve) => {
if (!item) return resolve(null);
const { tag, method } = item;
debug(`(parallel)invoke method "${method}" in module "${tag}"`);
if (!metaMap.has(tag)) return resolve(await Context.filterRecord.default(new BadRequestException(`module "${tag}" doesn't exist`)));
const meta2 = metaMap.get(tag)[method];
if (!meta2) return resolve(await Context.filterRecord.default(new BadRequestException(`"${method}" in "${tag}" doesn't exist`)));
const aop = Context.getAop(meta2, {
globalFilter,
globalGuards,
globalPipe
});
const contextData = {
type: "hono",
parallel: true,
category: "http",
context: c,
index: i,
meta: meta2,
moduleMap,
app: router,
...item,
getCookie: /* @__PURE__ */ __name((key) => getCookie(c, key), "getCookie"),
delCookie: /* @__PURE__ */ __name((key) => deleteCookie(c, key), "delCookie"),
setCookie: /* @__PURE__ */ __name((key, value, opts2) => setCookie(c, key, value, opts2), "setCookie"),
redirect: /* @__PURE__ */ __name((url) => c.redirect(url), "redirect"),
setResHeaders: /* @__PURE__ */ __name((headers) => {
for (const name in headers) c.header(name, headers[name]);
}, "setResHeaders"),
setResStatus: /* @__PURE__ */ __name((status) => c.status(status), "setResStatus"),
getRequest: /* @__PURE__ */ __name(() => c.req.raw, "getRequest"),
getResponse: /* @__PURE__ */ __name(() => c.res, "getResponse")
};
const context = new Context(contextData);
context.run(aop, resolve, resolve);
});
})).then((ret) => {
return c.json(ret);
});
} catch (e) {
return errorHandler(e);
}
});
router.route(parallelRoute, subApp);
}
for (const [tag, record] of metaMap) {
for (const method in record) {
const meta2 = metaMap.get(tag)[method];
const { data: { params, addons, http } } = meta2;
if (!http?.method) continue;
const needBody = params.some((item) => item.type === "body");
let aop;
if (!dynamic) {
aop = Context.getAop(meta2, {
globalFilter,
globalGuards,
globalPipe
});
}
const subApp = new Hono();
Context.applyAddons(addons, subApp, "hono");
subApp[http.method](http.route, async (c) => {
debug(`invoke method "${method}" in module "${tag}"`);
const contextData = {
type: "hono",
category: "http",
context: c,
meta: meta2,
moduleMap,
tag,
method,
query: c.req.query(),
body: needBody ? await c.req.json() : void 0,
params: c.req.param(),
headers: c.req.header(),
app: router,
getCookie: /* @__PURE__ */ __name((key) => getCookie(c, key), "getCookie"),
delCookie: /* @__PURE__ */ __name((key) => deleteCookie(c, key), "delCookie"),
setCookie: /* @__PURE__ */ __name((key, value, opts2) => setCookie(c, key, value, opts2), "setCookie"),
redirect: /* @__PURE__ */ __name((url) => c.redirect(url), "redirect"),
setResHeaders: /* @__PURE__ */ __name((headers) => {
for (const name in headers) c.header(name, headers[name]);
}, "setResHeaders"),
setResStatus: /* @__PURE__ */ __name((status) => c.status(status), "setResStatus"),
getRequest: /* @__PURE__ */ __name(() => c.req.raw, "getRequest"),
getResponse: /* @__PURE__ */ __name(() => c.res, "getResponse")
};
const context = new Context(contextData);
if (http.headers) {
for (const name in http.headers) c.header(name, http.headers[name]);
}
if (dynamic) {
aop = Context.getAop(meta2, {
globalFilter,
globalGuards,
globalPipe
});
}
return context.run(aop, (returnData) => {
if (returnData instanceof Response) return returnData;
if (typeof returnData === "string") return c.text(returnData);
else return c.json(returnData);
}, (err) => {
c.status(err.status);
return c.json(err);
});
});
router.route(http.prefix, subApp);
}
}
}
__name(registerRoute, "registerRoute");
}
__name(bind, "bind");
export {
bind
};