UNPKG

lemon-engine

Version:

Lemon Engine Module to Synchronize Node over DynamoDB + ElastiCache + Elasticsearch by [lemoncloud](https://lemoncloud.io)

138 lines 6.25 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var http_proxy_1 = __importDefault(require("./http-proxy")); var maker = function (_$, name, options) { name = name || 'WS'; var $U = _$.U; // re-use global instance (utils). var $_ = _$._; // re-use global instance (_ lodash). if (!$U) throw new Error('$U is required!'); if (!$_) throw new Error('$_ is required!'); var NS = $U.NS(name, 'yellow'); // NAMESPACE TO BE PRINTED. //! load common functions var _log = _$.log; var _inf = _$.inf; var _err = _$.err; /** **************************************************************************************************************** * Internal Proxy Function ** ****************************************************************************************************************/ var ENDPOINT = $U.env('WS_ENDPOINT', typeof options == 'string' ? options : ''); var HEADERS = options && (typeof options === 'object' ? options.headers : null) || null; // headers to pass. var $proxy = function () { if (!ENDPOINT) throw new Error('env:WS_ENDPOINT is required!'); //! re-use proxy by name var SVC = 'X' + name; var $SVC = _$(SVC, null); if ($SVC) return $SVC; //! prepare options. var options = { endpoint: ENDPOINT, }; //! relay HEADERS to `WEB-API` if (HEADERS) { var RELAY_KEY_1 = 'x-lemon-'; options.headers = Object.keys(HEADERS).reduce(function (H, key, i) { var val = HEADERS[key]; var name = RELAY_KEY_1 + key; var text = "" + val; H[name] = text; return H; }, {}); } //! will register service as <SVC>. return http_proxy_1.default(_$, SVC, options); }; /** **************************************************************************************************************** * Main Implementation. ** ****************************************************************************************************************/ var thiz = new (/** @class */ (function () { function class_1() { this.name = function () { return "web-proxy:" + name; }; this.endpoint = function () { return ENDPOINT; }; } /** * GET HOST/PATH?$param * * * @param host hostname like (127.0.0.1, https://127.0.0.1, 127.0.0.1:8080) * @param path full path * @param $opt optional settings * @param $param query parameter (if string, then direct use w/o encoding) * @param $body body payload (if string, then use as payload) */ class_1.prototype.do_get = function (host, path, $opt, $param, $body) { if ($body) return Promise.reject(new Error(NS + ':$body is invalid!')); if (host === undefined) return Promise.reject(new Error(NS + ':host is required!')); // if (path === undefined) return Promise.reject(new Error(NS + ':path is required!')); // if ($opt === undefined) return Promise.reject(new Error(NS + ':$opt is required!')); return $proxy() .do_get(host, path, undefined, $param, $body) .then(function (_) { return _.result; }); }; /** * PUT HOST/PATH?$param * */ class_1.prototype.do_put = function (host, path, $opt, $param, $body) { if (host === undefined) return Promise.reject(new Error(NS + ':host is required!')); // if (path === undefined) return Promise.reject(new Error(NS + ':path is required!')); // if ($opt === undefined) return Promise.reject(new Error(NS + ':$opt is required!')); return $proxy() .do_put(host, path, undefined, $param, $body) .then(function (_) { return _.result; }); }; /** * POST HOST/PATH?$param * */ class_1.prototype.do_post = function (host, path, $opt, $param, $body) { if (host === undefined) return Promise.reject(new Error(NS + ':host is required!')); // if (path === undefined) return Promise.reject(new Error(NS + ':path is required!')); // if ($opt === undefined) return Promise.reject(new Error(NS + ':$opt is required!')); return $proxy() .do_post(host, path, undefined, $param, $body) .then(function (_) { return _.result; }); }; /** * PATCH HOST/PATH?$param * */ class_1.prototype.do_patch = function (host, path, $opt, $param, $body) { if (host === undefined) return Promise.reject(new Error(NS + ':host is required!')); // if (path === undefined) return Promise.reject(new Error(NS + ':path is required!')); // if ($opt === undefined) return Promise.reject(new Error(NS + ':$opt is required!')); return $proxy() .do_patch(host, path, undefined, $param, $body) .then(function (_) { return _.result; }); }; /** * DELETE HOST/PATH?$param * */ class_1.prototype.do_delete = function (host, path, $opt, $param, $body) { if (host === undefined) return Promise.reject(new Error(NS + ':host is required!')); // if (path === undefined) return Promise.reject(new Error(NS + ':path is required!')); // if ($opt === undefined) return Promise.reject(new Error(NS + ':$opt is required!')); return $proxy() .do_delete(host, path, undefined, $param, $body) .then(function (_) { return _.result; }); }; return class_1; }()))(); //! create & register service. return _$(name, thiz); }; exports.default = maker; //# sourceMappingURL=web-proxy.js.map