UNPKG

lemon-engine

Version:

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

168 lines 8.12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var request_1 = __importDefault(require("request")); var query_string_1 = __importDefault(require("query-string")); var maker = function (_$, name, options) { name = name || 'HS'; 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, 'magenta'); // NAMESPACE TO BE PRINTED. var ENDPOINT = options && (typeof options === 'string' ? options : options.endpoint || '') || ''; // service endpoint. var HEADERS = options && (typeof options === 'object' ? options.headers : {}) || {}; // custom headers. if (!ENDPOINT) throw new Error('endpoint is required!'); //! load common functions var _log = _$.log; var _inf = _$.inf; var _err = _$.err; /** **************************************************************************************************************** * Main Implementation. ** ****************************************************************************************************************/ //! request in local server. var my_request_http = function (METHOD, TYPE, ID, CMD, $param, $body) { if (!METHOD) return Promise.reject(new Error(NS + ':METHOD is required!')); if (!request_1.default) throw new Error('request is required!'); //! prepare request parameters var query_string = $param ? query_string_1.default.stringify($param) : ''; var url = ENDPOINT + (TYPE === undefined ? '' : '/' + encodeURIComponent(TYPE)) + (TYPE === undefined || ID === undefined ? '' : '/' + encodeURIComponent(ID)) + (TYPE === undefined || ID === undefined || CMD === undefined ? '' : '/' + encodeURI(CMD)) + //NOTE - cmd could have additional '/' char. (!query_string ? '' : '?' + query_string); var request = request_1.default; var options = { method: METHOD || 'GET', uri: url, body: $body === null ? undefined : $body, json: (typeof $body === 'string') ? false : true, }; //! attache headers. if (HEADERS && Object.keys(HEADERS).length > 0) options.headers = HEADERS; // _log(NS, ' url :=', options.method, url); _log(NS, '*', options.method, url, options.json ? 'json' : 'plain'); // _inf(NS, '> options =', options); // options.headers && _log(NS, '> headers =', options.headers); //! returns promise return new Promise(function (resolve, reject) { //! start request.. request(options, function (error, response, body) { error && _err(NS, '>>>>> requested! err=', error); if (error) return reject(error); //! detecte trouble. var statusCode = response.statusCode; var statusMessage = response.statusMessage; if (statusCode !== 200) { //! handle for not-found. if (statusCode === 400 || statusCode === 404) { var msg = '' + body; return reject(new Error(msg.indexOf('404 NOT FOUND') >= 0 ? msg : '404 NOT FOUND')); } _log(NS, '> code=' + statusCode + ', msg=' + statusMessage + ', body=', body); // if (statusCode === 400 || statusCode === 404) // return reject(new Error(body||statusMessage)); // else // return reject(new Error(body||statusMessage)); body = body || statusMessage; return reject(typeof body === 'string' ? new Error(body) : body); } //! try to parse body. try { if (body && typeof body == 'string' && body.startsWith('{') && body.endsWith('}')) { body = JSON.parse(body); } else if (body && typeof body == 'string' && body.startsWith('[') && body.endsWith(']')) { body = JSON.parse(body); } } catch (e) { _err(NS, '!WARN! parse =', e); } //! ok! successed. resolve(body); }); }); }; /** * class: HttpProxyBody */ var HttpProxyBody = /** @class */ (function () { function HttpProxyBody(endpoint) { var _this = this; this.name = function () { return "http-proxy:" + _this._endpoint; }; this.endpoint = function () { return _this._endpoint; }; this._endpoint = endpoint; } /** * GET /:type/:id/:cmd?$param */ HttpProxyBody.prototype.do_get = function (TYPE, ID, CMD, $param, $body) { if ($body) return Promise.reject(new Error(NS + ':$body is invalid!')); if (TYPE === undefined) return Promise.reject(new Error(NS + ':TYPE is required!')); // if (ID === undefined) return Promise.reject(new Error(NS + ':ID is required!')); // if (CMD === undefined) return Promise.reject(new Error(NS + ':CMD is required!')); return my_request_http('GET', TYPE, ID, CMD, $param, $body); }; /** * PUT /:type/:id/:cmd?$param */ HttpProxyBody.prototype.do_put = function (TYPE, ID, CMD, $param, $body) { if (TYPE === undefined) return Promise.reject(new Error(NS + ':TYPE is required!')); if (ID === undefined) return Promise.reject(new Error(NS + ':ID is required!')); // if (CMD === undefined) return Promise.reject(new Error(NS + ':CMD is required!')); return my_request_http('PUT', TYPE, ID, CMD, $param, $body); }; /** * POST /:type/:id/:cmd?$param */ HttpProxyBody.prototype.do_post = function (TYPE, ID, CMD, $param, $body) { if (TYPE === undefined) return Promise.reject(new Error(NS + ':TYPE is required!')); if (ID === undefined) return Promise.reject(new Error(NS + ':ID is required!')); // if (CMD === undefined) return Promise.reject(new Error(NS + ':CMD is required!')); return my_request_http('POST', TYPE, ID, CMD, $param, $body); }; /** * PATCH /:type/:id/:cmd?$param */ HttpProxyBody.prototype.do_patch = function (TYPE, ID, CMD, $param, $body) { if (TYPE === undefined) return Promise.reject(new Error(NS + ':TYPE is required!')); if (ID === undefined) return Promise.reject(new Error(NS + ':ID is required!')); // if (CMD === undefined) return Promise.reject(new Error(NS + ':CMD is required!')); return my_request_http('PATCH', TYPE, ID, CMD, $param, $body); }; /** * DELETE /:type/:id/:cmd?$param */ HttpProxyBody.prototype.do_delete = function (TYPE, ID, CMD, $param, $body) { if (TYPE === undefined) return Promise.reject(new Error(NS + ':TYPE is required!')); if (ID === undefined) return Promise.reject(new Error(NS + ':ID is required!')); // if (CMD === undefined) return Promise.reject(new Error(NS + ':CMD is required!')); return my_request_http('DELETE', TYPE, ID, CMD, $param, $body); }; return HttpProxyBody; }()); //! create & register service. return _$(name, new HttpProxyBody(ENDPOINT)); }; exports.default = maker; //# sourceMappingURL=http-proxy.js.map