UNPKG

fabric8-analytics-dependency-editor-jyas

Version:
188 lines 7.65 kB
import { Injectable, EventEmitter, Output } from '@angular/core'; import { Headers, RequestOptions } from '@angular/http'; import { DependencyEditorTokenProvider } from './depeditor-tokenprovider'; import { URLProvider } from './url-provider'; import { Subject } from 'rxjs/Subject'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/catch'; import 'rxjs/operators/map'; import 'rxjs/add/observable/fromPromise'; import * as _ from 'lodash'; import { DependencySnapshot } from '../utils/dependency-snapshot'; import { HttpInterceptor } from '../shared/http-interceptor'; var DependencyEditorService = /** @class */ (function () { function DependencyEditorService(http, tokenProvider, urlProvider) { this.http = http; this.tokenProvider = tokenProvider; this.urlProvider = urlProvider; this.dependencySelected = new EventEmitter(); this.dependencyRemoved = new EventEmitter(); this.licenseSubscription = new Subject(); this.needsLicenseChange = new Subject(); this.securitySubscription = new Subject(); this.needsSecurityChange = new Subject(); this.cacheHiddenDependency = []; this.RECOMMENDER_API_BASE = ''; this.LICENSE_API_BASE = ''; this.URLS_HASH = {}; this.LICENSE_API_BASE = this.checkForTrailingSlashes(this.urlProvider.getLicenseAPIUrl()); this.RECOMMENDER_API_BASE = this.checkForTrailingSlashes(this.urlProvider.getRecommenderAPIUrl()); console.log('Check from environments', this.LICENSE_API_BASE, this.RECOMMENDER_API_BASE); this.URLS_HASH = { 'CVE': this.RECOMMENDER_API_BASE + 'api/v1/depeditor-cve-analyses', 'LICENSE': this.LICENSE_API_BASE + 'api/v1/license-recommender', 'DEPEDITORANALYSIS': this.RECOMMENDER_API_BASE + 'api/v1/depeditor-analyses/?persist=false' }; } DependencyEditorService.prototype.postStackAnalyses = function (githubUrl, githubRef) { var _this = this; var url = this.RECOMMENDER_API_BASE + 'api/v1/stack-analyses'; return this.options.flatMap(function (option) { // const payload = 'github_url=' + githubUrl; var payload; payload = 'github_url=' + githubUrl + '&source=osio' + '&github_ref=' + githubRef; option.headers.append('Content-Type', 'application/x-www-form-urlencoded'); return _this.http.post(url, payload, option) .map(_this.extractData) .map(function (data) { return data; }); }); }; DependencyEditorService.prototype.getStackAnalyses = function (stackId) { var _this = this; if (!stackId) return; var url = this.RECOMMENDER_API_BASE + ("api/v1/stack-analyses/" + stackId); return this.options.flatMap(function (option) { var stackReport = null; return _this.http.get(url, option) .map(_this.extractData) .map(function (data) { stackReport = data; return data; }); }); }; DependencyEditorService.prototype.getDependencies = function (component) { var _this = this; if (!component) return; var url = this.RECOMMENDER_API_BASE + ("api/v1/component-search/" + component); return this.options.flatMap(function (option) { return _this.http.get(url, option) .map(_this.extractData) .map(function (data) { return data; }); }); }; DependencyEditorService.prototype.getDependencyData = function (type, payload) { var _this = this; var url = this.URLS_HASH[type]; if (!url) return; return this.options.flatMap(function (option) { return _this.http.post(url, payload, option) .map(_this.extractData) .map(function (data) { return data; }); }); }; DependencyEditorService.prototype.getCategories = function (runtime) { var _this = this; if (!runtime) return; var url = this.RECOMMENDER_API_BASE + ("api/v1/categories/" + runtime); return this.options.flatMap(function (option) { return _this.http.get(url, option) .map(_this.extractData) .map(function (data) { return data; }); }); }; DependencyEditorService.prototype.updateDependencyAddedSnapshot = function (depObj) { var depToAdd = {}; if (depObj.depFull) { depToAdd = { package: depObj.depFull.name, version: depObj.depFull.version }; if (depObj.action === 'add') { DependencySnapshot.DEP_FULL_ADDED.push(depObj.depFull); } } else if (depObj.depSnapshot) { depToAdd = depObj.depSnapshot; } if (depObj.action === 'add') { DependencySnapshot.DEP_SNAPSHOT_ADDED.push(depToAdd); } else { _.remove(DependencySnapshot.DEP_SNAPSHOT_ADDED, function (dep) { return dep.package === depToAdd.package; }); _.remove(DependencySnapshot.DEP_FULL_ADDED, function (dep) { return dep.name === depToAdd.package; }); } }; DependencyEditorService.prototype.getPayload = function () { var payload = {}; var deps = DependencySnapshot.DEP_SNAPSHOT.concat(DependencySnapshot.DEP_SNAPSHOT_ADDED); payload['_resolved'] = deps; payload['ecosystem'] = DependencySnapshot.ECOSYSTEM; payload['request_id'] = DependencySnapshot.REQUEST_ID; return payload; }; DependencyEditorService.prototype.removeDependency = function (dependency) { var objToEmit = { depFull: dependency, depSnapshot: null, action: 'remove' }; this.dependencyRemoved.emit(objToEmit); }; Object.defineProperty(DependencyEditorService.prototype, "options", { get: function () { var headers = new Headers(); return Observable.fromPromise(this.tokenProvider.token.then(function (token) { headers.append('Authorization', 'Bearer ' + token); return new RequestOptions({ headers: headers }); })); }, enumerable: true, configurable: true }); DependencyEditorService.prototype.extractData = function (res) { var body = res.json() || {}; body['statusCode'] = res.status; body['statusText'] = res.statusText; return body; }; DependencyEditorService.prototype.checkForTrailingSlashes = function (url) { if (!url || url.length < 1) return; return url[url.length - 1] === '/' ? url : url + '/'; }; DependencyEditorService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ DependencyEditorService.ctorParameters = function () { return [ { type: HttpInterceptor, }, { type: DependencyEditorTokenProvider, }, { type: URLProvider, }, ]; }; DependencyEditorService.propDecorators = { 'dependencySelected': [{ type: Output },], 'dependencyRemoved': [{ type: Output },], }; return DependencyEditorService; }()); export { DependencyEditorService }; //# sourceMappingURL=dependency-editor.service.js.map