UNPKG

will-api

Version:

Web API gateway adapter for moleculer

130 lines (129 loc) 6.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.KnGateWay = void 0; const moleculer_web_1 = __importDefault(require("moleculer-web")); const JSONReply_1 = require("./JSONReply"); const will_util_1 = __importDefault(require("will-util")); const REPLY_HEADER_INFO = will_util_1.default.env("REPLY_HEADER_INFO", "false") === "true"; class KnGateWay { constructor(name) { this.name = "api"; this.mixins = [moleculer_web_1.default]; this.settings = { port: will_util_1.default.env("GATEWAY_PORT", "8080") || 8080, path: "/api", mappingPolicy: "all", cors: true, routes: [{ mappingPolicy: "all", bodyParsers: { json: true, urlencoded: { extended: true } }, onBeforeCall: (ctx, route, req, res, alias) => { ctx.meta.userAgent = req.headers["user-agent"]; ctx.meta.session = req.session; ctx.meta.headers = req.headers; ctx.meta.$headers = {}; let logger = req.$service.logger; logger.debug("headers", req.headers); }, onAfterCall: (ctx, route, req, res, data) => { let responseRaw = false; let responseType = null; if (ctx.meta.$headers) { for (let p in ctx.meta.$headers) { res.setHeader(p, ctx.meta.$headers[p]); } } if (req.$action) { if (req.$action.responseType) { responseType = req.$action.responseType; } if (typeof req.$action.responseRaw !== "undefined") { responseRaw = req.$action.responseRaw; } } if (ctx.meta.$responseType) { responseType = ctx.meta.$responseType; } if (typeof ctx.meta.$responseRaw !== "undefined") { responseRaw = ctx.meta.$responseRaw; } if (responseType) { responseType = responseType.toLowerCase(); let idx = responseType.indexOf("text/plain"); if (idx >= 0) return data; idx = responseType.indexOf("text/html"); if (idx >= 0) return data; } if (data instanceof JSONReply_1.JSONReply) { return data; } else { if (responseRaw) { if (data instanceof Map) { return Object.fromEntries(data); } else if (data instanceof Set) { return Array.from(data); } return data; } let reply = new JSONReply_1.JSONReply(); reply.head.modeling(req.$service.name, req.$action.name); reply.head.composeNoError(); if (REPLY_HEADER_INFO) { reply.head.info = { id: ctx.id, nodeID: ctx.nodeID, requestID: ctx.requestID }; } if (typeof data === "string") { reply.body = { data: data }; } else { if (data instanceof Map) { reply.body = Object.fromEntries(data); } else if (data instanceof Set) { reply.body = Array.from(data); } else { reply.body = data; } } return reply; } } }], onError(req, res, err) { let metname = req.$action?.name; if (!metname) metname = err.data?.name; let errcode = err.code ? err.code : 500; let errno = err.errno ? err.errno : errcode; if (errcode <= 0 || errcode >= 1024) errcode = 406; //not acceptable let reply = new JSONReply_1.JSONReply(); reply.head.modeling(req.$service?.name, metname); reply.head.composeError(String(errno), err.message); reply.head.details = err; if (REPLY_HEADER_INFO) { let ctx = req.$ctx; if (ctx) { reply.head.info = { id: ctx.id, nodeID: ctx.nodeID, requestID: ctx.requestID }; } } res.setHeader("Content-Type", "application/json; charset=utf-8"); res.writeHead(Number(errcode)); res.end(JSON.stringify(reply)); }, }; if (name) this.name = name; } } exports.KnGateWay = KnGateWay;