UNPKG

phecda-server

Version:

server framework that provide IOC/type-reuse/http&rpc-adaptor

161 lines (150 loc) 8.87 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _chunkHKN3AAB2js = require('../../chunk-HKN3AAB2.js'); var _chunk4LLLQOMFjs = require('../../chunk-4LLLQOMF.js'); // src/http/h3/bind.ts var _h3 = require('h3'); var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var debug = _debug2.default.call(void 0, "phecda-server/h3"); function bind(router, data, opts = {}) { const { globalGuards, parallelRoute = "/__PHECDA_SERVER__", globalAddons = [], parallelAddons = [], globalFilter, globalPipe, dynamic = false } = opts; const { moduleMap, meta } = data; const metaMap = _chunkHKN3AAB2js.createControllerMetaMap.call(void 0, meta, (meta2) => { const { controller, http, method, tag } = meta2.data; if (controller === "http" && _optionalChain([http, 'optionalAccess', _ => _.method])) { debug(`register method "${method}" in module "${tag}"`); return true; } }); _chunkHKN3AAB2js.detectAopDep.call(void 0, meta, { addons: [ ...globalAddons, ...parallelAddons ], guards: globalGuards }); registerRoute(); function registerRoute() { _chunkHKN3AAB2js.Context.applyAddons(globalAddons, router, "h3"); if (parallelRoute) { const subRouter = _h3.createRouter.call(void 0, ); _chunkHKN3AAB2js.Context.applyAddons(parallelAddons, subRouter, "h3"); subRouter.post(parallelRoute, _h3.eventHandler.call(void 0, { handler: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, async (event) => { const body = await _h3.readBody.call(void 0, event, { strict: true }); async function errorHandler(e) { const error = await _chunkHKN3AAB2js.Context.filterRecord.default(e); _h3.setResponseStatus.call(void 0, event, error.status); return error; } _chunk4LLLQOMFjs.__name.call(void 0, errorHandler, "errorHandler"); if (!Array.isArray(body)) return errorHandler(new (0, _chunkHKN3AAB2js.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 _chunkHKN3AAB2js.Context.filterRecord.default(new (0, _chunkHKN3AAB2js.BadRequestException)(`module "${tag}" doesn't exist`))); const meta2 = metaMap.get(tag)[method]; if (!meta2) return resolve(await _chunkHKN3AAB2js.Context.filterRecord.default(new (0, _chunkHKN3AAB2js.BadRequestException)(`"${method}" in "${tag}" doesn't exist`))); const aop = _chunkHKN3AAB2js.Context.getAop(meta2, { globalFilter, globalGuards, globalPipe }); const contextData = { type: "h3", category: "http", index: i, event, meta: meta2, moduleMap, parallel: true, app: router, ...item, getCookie: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (key) => _h3.getCookie.call(void 0, event, key), "getCookie"), setCookie: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (key, value, opts2) => _h3.setCookie.call(void 0, event, key, value, opts2), "setCookie"), delCookie: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (key) => _h3.deleteCookie.call(void 0, event, key), "delCookie"), redirect: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (url, status) => _h3.sendRedirect.call(void 0, event, url, status), "redirect"), setResHeaders: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (headers) => _h3.setResponseHeaders.call(void 0, event, headers), "setResHeaders"), setResStatus: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (code) => _h3.setResponseStatus.call(void 0, event, code), "setResStatus"), getRequest: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, () => event.node.req, "getRequest"), getResponse: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, () => event.node.res, "getResponse") }; const context = new (0, _chunkHKN3AAB2js.Context)(contextData); context.run(aop, resolve, resolve); }); })); } catch (e) { return errorHandler(e); } }, "handler") })); router.use(parallelRoute, _h3.useBase.call(void 0, "", subRouter.handler)); } for (const [tag, record] of metaMap) { for (const method in record) { const meta2 = metaMap.get(tag)[method]; const { data: { http, params, addons } } = meta2; if (!_optionalChain([http, 'optionalAccess', _2 => _2.method])) continue; const needBody = params.some((item) => item.type === "body"); let aop; if (!dynamic) { aop = _chunkHKN3AAB2js.Context.getAop(meta2, { globalFilter, globalGuards, globalPipe }); } const subRouter = _h3.createRouter.call(void 0, ); _chunkHKN3AAB2js.Context.applyAddons(addons, subRouter, "h3"); subRouter[http.method](_chunkHKN3AAB2js.joinUrl.call(void 0, http.prefix, http.route), _h3.defineEventHandler.call(void 0, async (event) => { debug(`invoke method "${method}" in module "${tag}"`); const contextData = { type: "h3", category: "http", meta: meta2, event, moduleMap, tag, method, headers: _h3.getRequestHeaders.call(void 0, event), params: _h3.getRouterParams.call(void 0, event), query: _h3.getQuery.call(void 0, event), app: router, body: needBody ? await _h3.readBody.call(void 0, event, { strict: true }) : void 0, getCookie: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (key) => _h3.getCookie.call(void 0, event, key), "getCookie"), setCookie: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (key, value, opts2) => _h3.setCookie.call(void 0, event, key, value, opts2), "setCookie"), redirect: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (url) => _h3.sendRedirect.call(void 0, event, url), "redirect"), setResHeaders: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (headers) => _h3.setResponseHeaders.call(void 0, event, headers), "setResHeaders"), setResStatus: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (code) => _h3.setResponseStatus.call(void 0, event, code), "setResStatus"), delCookie: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (key) => _h3.deleteCookie.call(void 0, event, key), "delCookie"), getRequest: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, () => event.node.req, "getRequest"), getResponse: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, () => event.node.res, "getResponse") }; const context = new (0, _chunkHKN3AAB2js.Context)(contextData); _h3.setHeaders.call(void 0, event, http.headers || {}); if (dynamic) { aop = _chunkHKN3AAB2js.Context.getAop(meta2, { globalFilter, globalGuards, globalPipe }); } return context.run(aop, (returnData) => returnData, (err) => { _h3.setResponseStatus.call(void 0, event, err.status); return err; }); })); router.use(_chunkHKN3AAB2js.joinUrl.call(void 0, http.prefix, http.route), _h3.useBase.call(void 0, "", subRouter.handler)); } } } _chunk4LLLQOMFjs.__name.call(void 0, registerRoute, "registerRoute"); } _chunk4LLLQOMFjs.__name.call(void 0, bind, "bind"); exports.bind = bind;