UNPKG

@nodeswork/applet

Version:
84 lines (82 loc) 3.82 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("underscore"); const request = require("request-promise"); const url = require("url"); const logger = require("@nodeswork/logger"); const kiws = require("@nodeswork/kiws"); const sbase = require("@nodeswork/sbase"); const utils_1 = require("@nodeswork/utils"); const applet_info_service_1 = require("./applet.info.service"); const constants_1 = require("../constants"); const PROXY_HOST = 'http://nodeswork-container-proxy:28320'; const NAM = 'nam'; const GET_METHOD = { method: 'GET' }; const POST_METHOD = { method: 'POST' }; const LOG = logger.getLogger(); let RequestService = class RequestService { constructor(appletInfo) { this.appletInfo = appletInfo; const info = this.appletInfo.getAppletInfo(); if (info.appletId == null) { LOG.error('Applet id is missing, try set NW_APPLET_ID from env'); throw new utils_1.NodesworkError('Applet id is missing, try set NW_APPLET_ID from env'); } if (info.appletToken == null) { LOG.error('Applet token is missing, try set NW_APPLET_TOKEN from env'); throw new utils_1.NodesworkError('Applet token is missing, try set NW_APPLET_TOKEN from env'); } const headers = {}; headers[constants_1.constants.headers.request.APPLET_ID] = info.appletId; headers[constants_1.constants.headers.request.APPLET_TOKEN] = info.appletToken; headers[constants_1.constants.headers.request.NA_TYPE] = info.naType; headers[constants_1.constants.headers.request.NA_VERSION] = info.naVersion; headers[constants_1.constants.headers.request.PACKAGE_NAME] = info.packageName; headers[constants_1.constants.headers.request.PACKAGE_VERSION] = info.packageVersion; headers[sbase.constants.headers.request.NODESWORK_FORWARDED_TO] = NAM; const defaults = { headers, json: true, }; if (info.env === 'production') { defaults.proxy = PROXY_HOST; } this.defaultRequest = request.defaults(defaults); LOG.debug('Set default request', defaults); } async request(options) { const uri = new url.URL(options.uri, 'http://localhost:28310'); const requestOptions = _.extend(options, { uri: uri.toString(), }); LOG.debug('Send request', requestOptions); try { return await this.defaultRequest(requestOptions); } catch (e) { LOG.error('Got error from request', e); throw e; } } async get(options) { return this.request(_.extend({}, options, GET_METHOD)); } async post(options) { return this.request(_.extend({}, options, POST_METHOD)); } }; RequestService = __decorate([ kiws.Service(), __metadata("design:paramtypes", [applet_info_service_1.AppletInfoService]) ], RequestService); exports.RequestService = RequestService; //# sourceMappingURL=request.service.js.map