phecda-server
Version:
server framework that provide IOC/type-reuse/http&rpc-adaptor
176 lines (162 loc) • 8.86 kB
JavaScript
;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 _chunkUYZSUBX4js = require('../../chunk-UYZSUBX4.js');
var _chunkHKN3AAB2js = require('../../chunk-HKN3AAB2.js');
var _chunk4LLLQOMFjs = require('../../chunk-4LLLQOMF.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 = "/__PHECDA_SERVER__", globalAddons = [], parallelAddons = [], globalFilter, globalPipe, fastifyOpts, 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', _2 => _2.method])) {
debug(`register method "${method}" in module "${tag}"`);
return true;
}
});
_chunkHKN3AAB2js.detectAopDep.call(void 0, meta, {
addons: [
...globalAddons,
...parallelAddons
],
guards: globalGuards
});
fastify.register(async (fastify2, _, done) => {
_chunkHKN3AAB2js.Context.applyAddons(globalAddons, fastify2, "fastify");
if (parallelRoute) {
fastify2.register(async (fastify3, _opts, done2) => {
_chunkHKN3AAB2js.Context.applyAddons(parallelAddons, fastify3, "fastify");
fastify3.post(parallelRoute, async (req, res) => {
const { body } = req;
async function errorHandler(e) {
const error = await _chunkHKN3AAB2js.Context.filterRecord.default(e);
return res.status(error.status).send(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: "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__ */ _chunk4LLLQOMFjs.__name.call(void 0, (key) => req.cookies[key], "getCookie"),
// @ts-expect-error need @fastify/cookie
setCookie: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (key, value, opts2) => res.setCookie(key, value, opts2), "setCookie"),
// @ts-expect-error need @fastify/cookie
delCookie: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (key) => res.clearCookie(key), "delCookie"),
redirect: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (url, status) => res.redirect(url, status), "redirect"),
setResHeaders: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (headers) => res.headers(headers), "setResHeaders"),
setResStatus: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (code) => res.status(code), "setResStatus"),
getRequest: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, () => req.raw, "getRequest"),
getResponse: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, () => res.raw, "getResponse")
};
const context = new (0, _chunkHKN3AAB2js.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) => {
_chunkHKN3AAB2js.Context.applyAddons(addons, fastify3, "fastify");
let aop;
if (!dynamic) {
aop = _chunkHKN3AAB2js.Context.getAop(meta2, {
globalFilter,
globalGuards,
globalPipe
});
}
fastify3[http.method](_chunkHKN3AAB2js.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__ */ _chunk4LLLQOMFjs.__name.call(void 0, (key) => req.cookies[key], "getCookie"),
// @ts-expect-error need @fastify/cookie
setCookie: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (key, value, opts2) => res.setCookie(key, value, opts2), "setCookie"),
// @ts-expect-error need @fastify/cookie
delCookie: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (key) => res.clearCookie(key, {
url: ""
}), "delCookie"),
redirect: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (url, status) => res.redirect(url, status), "redirect"),
setResHeaders: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (headers) => res.headers(headers), "setResHeaders"),
setResStatus: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, (code) => res.status(code), "setResStatus"),
getRequest: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, () => req.raw, "getRequest"),
getResponse: /* @__PURE__ */ _chunk4LLLQOMFjs.__name.call(void 0, () => res.raw, "getResponse")
};
const context = new (0, _chunkHKN3AAB2js.Context)(contextData);
if (http.headers) res.headers(http.headers);
if (dynamic) {
aop = _chunkHKN3AAB2js.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);
}
_chunk4LLLQOMFjs.__name.call(void 0, bind, "bind");
function Fastify(opts) {
return _chunkUYZSUBX4js.Define.call(void 0, "fastify", opts);
}
_chunk4LLLQOMFjs.__name.call(void 0, Fastify, "Fastify");
exports.Fastify = Fastify; exports.bind = bind;