UNPKG

phecda-server

Version:

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

176 lines (162 loc) 8.84 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 _chunk5ZZAOOKEjs = require('../../chunk-5ZZAOOKE.js'); var _chunk76UDKZOJjs = require('../../chunk-76UDKZOJ.js'); var _chunkLLF55NZPjs = require('../../chunk-LLF55NZP.js'); // src/http/fastify/bind.ts var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var debug = _debug2.default.call(void 0, "phecda-server/fastify"); function bind(fastify, data, opts = {}) { const { globalGuards, parallelRoute, globalAddons = [], parallelAddons = [], globalFilter, globalPipe, fastifyOpts, dynamic = false } = opts; const { moduleMap, meta } = data; const metaMap = _chunk76UDKZOJjs.createControllerMetaMap.call(void 0, meta, (meta2) => { const { controller, http, method, tag } = meta2.data; if (controller === "http" && _optionalChain([http, 'optionalAccess', _2 => _2.method])) { debug(`register method "${method}" in module "${tag}"`); return true; } }); _chunk76UDKZOJjs.detectAopDep.call(void 0, meta, { addons: [ ...globalAddons, ...parallelAddons ], guards: globalGuards }); fastify.register(async (fastify2, _, done) => { _chunk76UDKZOJjs.Context.applyAddons(globalAddons, fastify2, "fastify"); if (parallelRoute) { fastify2.register(async (fastify3, _opts, done2) => { _chunk76UDKZOJjs.Context.applyAddons(parallelAddons, fastify3, "fastify"); fastify3.post(parallelRoute, async (req, res) => { const { body } = req; async function errorHandler(e) { const error = await _chunk76UDKZOJjs.Context.filterRecord.default(e); return res.status(error.status).send(error); } _chunkLLF55NZPjs.__name.call(void 0, errorHandler, "errorHandler"); if (!Array.isArray(body)) return errorHandler(new (0, _chunk76UDKZOJjs.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 _chunk76UDKZOJjs.Context.filterRecord.default(new (0, _chunk76UDKZOJjs.BadRequestException)(`module "${tag}" doesn't exist`))); const meta2 = metaMap.get(tag)[method]; if (!meta2) return resolve(await _chunk76UDKZOJjs.Context.filterRecord.default(new (0, _chunk76UDKZOJjs.BadRequestException)(`"${method}" in "${tag}" doesn't exist`))); const aop = _chunk76UDKZOJjs.Context.getAop(meta2, { globalFilter, globalGuards, globalPipe }); const contextData = { type: "fastify", category: "http", parallel: true, request: req, index: i, meta: meta2, response: res, moduleMap, app: fastify3, ...item, // @ts-expect-error need @fastify/cookie getCookie: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, (key) => req.cookies[key], "getCookie"), // @ts-expect-error need @fastify/cookie setCookie: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, (key, value, opts2) => res.setCookie(key, value, opts2), "setCookie"), // @ts-expect-error need @fastify/cookie delCookie: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, (key) => res.clearCookie(key), "delCookie"), redirect: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, (url, status) => res.redirect(url, status), "redirect"), setResHeaders: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, (headers) => res.headers(headers), "setResHeaders"), setResStatus: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, (code) => res.status(code), "setResStatus"), getRequest: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, () => req.raw, "getRequest"), getResponse: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, () => res.raw, "getResponse") }; const context = new (0, _chunk76UDKZOJjs.Context)(contextData); context.run(aop, resolve, resolve); }); })).then((ret) => { res.send(ret); }); } catch (e) { return errorHandler(e); } }); done2(); }); } for (const [tag, record] of metaMap) { for (const method in record) { const meta2 = metaMap.get(tag)[method]; const { data: { addons, define, http } } = meta2; if (!_optionalChain([http, 'optionalAccess', _3 => _3.method])) continue; fastify2.register(async (fastify3, _opts, done2) => { _chunk76UDKZOJjs.Context.applyAddons(addons, fastify3, "fastify"); let aop; if (!dynamic) { aop = _chunk76UDKZOJjs.Context.getAop(meta2, { globalFilter, globalGuards, globalPipe }); } fastify3[http.method](_chunk76UDKZOJjs.joinUrl.call(void 0, http.prefix, http.route), _optionalChain([define, 'optionalAccess', _4 => _4.fastify]) || {}, async (req, res) => { debug(`invoke method "${method}" in module "${tag}"`); const contextData = { type: "fastify", category: "http", request: req, meta: meta2, response: res, moduleMap, tag, method, query: req.query, body: req.body, params: req.params, headers: req.headers, app: fastify3, // @ts-expect-error need @fastify/cookie getCookie: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, (key) => req.cookies[key], "getCookie"), // @ts-expect-error need @fastify/cookie setCookie: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, (key, value, opts2) => res.setCookie(key, value, opts2), "setCookie"), // @ts-expect-error need @fastify/cookie delCookie: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, (key) => res.clearCookie(key, { url: "" }), "delCookie"), redirect: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, (url, status) => res.redirect(url, status), "redirect"), setResHeaders: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, (headers) => res.headers(headers), "setResHeaders"), setResStatus: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, (code) => res.status(code), "setResStatus"), getRequest: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, () => req.raw, "getRequest"), getResponse: /* @__PURE__ */ _chunkLLF55NZPjs.__name.call(void 0, () => res.raw, "getResponse") }; const context = new (0, _chunk76UDKZOJjs.Context)(contextData); if (http.headers) res.headers(http.headers); if (dynamic) { aop = _chunk76UDKZOJjs.Context.getAop(meta2, { globalFilter, globalGuards, globalPipe }); } return context.run(aop, (returnData) => { if (res.sent) return; return returnData; }, (err) => { if (res.sent) return; res.status(err.status).send(err); }); }); done2(); }); } } done(); }, fastifyOpts); } _chunkLLF55NZPjs.__name.call(void 0, bind, "bind"); function Fastify(opts) { return _chunk5ZZAOOKEjs.Define.call(void 0, "fastify", opts); } _chunkLLF55NZPjs.__name.call(void 0, Fastify, "Fastify"); exports.Fastify = Fastify; exports.bind = bind;