UNPKG

@aca-1/a2-composer

Version:
250 lines 9.6 kB
import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import { COMPOSER } from '../../settings'; import { CommsService } from '../auth'; import { ComposerDebugService } from '../debug.service'; import { COMMON } from './common'; import { ResourceFactory } from './resource-factory.class'; var Resources = (function () { function Resources(http, http_unauth, debug) { this.http = http; this.http_unauth = http_unauth; this.debug = debug; this.authLoaded = false; this.auth_promise = null; this.mock = false; debug.services.resources = this; } Object.defineProperty(Resources.prototype, "is_ready", { get: function () { return this.http.hasToken; }, enumerable: true, configurable: true }); Resources.prototype.initAuth = function (resolve, reject) { var _this = this; COMPOSER.log('Resources', "Loading Authority..."); if (this.mock) { this.authLoaded = true; return resolve(); } var parts = this.url.split('/'); var uri = parts.splice(0, 3).join('/'); var base_el = document.getElementsByTagName('base')[0]; var base = base_el ? (base_el.href ? base_el.href : '/') : '/'; var redirect = base.indexOf(location.origin) < 0 ? (location.origin + base) : base; console.log(this.url); if (!this.url || this.url === '' || this.url.indexOf('http') < 0) { setTimeout(function () { _this.initAuth(resolve, reject); }, 500); return; } this.get('Authority').get_authority().then(function (auth) { COMPOSER.log("Resources]", "Authority loaded. Session: " + (auth.session === true)); if (typeof auth !== 'object') { reject({ message: 'Auth details no valid.', }); return; } var url = encodeURIComponent(location.href); url = auth.login_url.replace('{{url}}', url); _this.http.setupOAuth({ loginRedirect: (url[0] === '/' ? (uri + url) : url), }); if (auth.session) { _this.http.setLoginStatus(auth.session); } _this.authLoaded = true; setTimeout(function () { _this.http.tryLogin(); resolve(); }, 200); }, function (err) { COMPOSER.error('Resources', 'Error getting authority.', err); _this.http.setupOAuth({ loginRedirect: uri + "/auth/login", }); _this.http.tryLogin(); reject(err); }); }; Resources.prototype.setup = function (options) { this.http.setupOAuth({ loginUrl: options.oauth_server, refreshUri: options.oauth_tokens, redirectUri: options.redirect_uri, clientId: this.http.hash(options.redirect_uri), login_local: options.login_local, }); this.url = options.api_endpoint; }; Resources.prototype.init = function (url_base, mock) { var _this = this; if (mock === void 0) { mock = false; } console.log(url_base); if (mock) { this.http.mock(); this.mock = mock; } return new Promise(function (resolve, reject) { if (!url_base && !_this.url) { _this.url = location.origin + '/control/'; } else { _this.url = url_base ? url_base : _this.url; } if (_this.url[_this.url.length - 1] !== '/') { _this.url += '/'; } var custom; _this.new('Module', _this.url + 'api/modules/:id/:task', { id: '@id', task: '@_task', }, COMMON.crud); _this.new('SystemModule', _this.url + 'api/systems/:sys_id/modules/:mod_id', { mod_id: '@module_id', sys_id: '@system_id', }, COMMON.crud); _this.new('Trigger', _this.url + 'api/triggers/:id', { id: '@id', }, COMMON.crud); custom = JSON.parse(JSON.stringify(COMMON.crud)); custom.query = { method: COMMON.cmd.GET, headers: COMMON.headers, url: _this.url + 'api/system_triggers', }; _this.new('SystemTrigger', _this.url + 'api/triggers/:id', { id: '@id', }, custom); custom = JSON.parse(JSON.stringify(COMMON.crud)); custom.funcs = { method: COMMON.cmd.GET, headers: COMMON.headers, url: _this.url + 'api/systems/:id/funcs', }; custom.exec = { method: COMMON.cmd.POST, headers: COMMON.headers, url: _this.url + 'api/systems/:id/exec', }; custom.types = { method: COMMON.cmd.GET, headers: COMMON.headers, url: _this.url + 'api/systems/:id/types', }; custom.count = { method: COMMON.cmd.GET, headers: COMMON.headers, url: _this.url + 'api/systems/:id/count', }; _this.new('System', _this.url + 'api/systems/:id/:task', { id: '@id', task: '@_task', }, custom); _this.new('Dependency', _this.url + 'api/dependencies/:id/:task', { id: '@id', task: '@_task', }, COMMON.crud); _this.new('Node', _this.url + 'api/nodes/:id', { id: '@id', }, COMMON.crud); _this.new('Group', _this.url + 'api/groups/:id', { id: '@id', }, COMMON.crud); _this.new('Zone', _this.url + 'api/zones/:id', { id: '@id', }, COMMON.crud); custom = JSON.parse(JSON.stringify(COMMON.crud)); custom.scan = { method: COMMON.cmd.POST, headers: COMMON.headers, url: _this.url + 'api/discovery/scan', }; _this.new('Discovery', _this.url + 'api/discovery/:id', { id: '@id', }, custom); custom = JSON.parse(JSON.stringify(COMMON.crud)); custom.missing_connections = { method: COMMON.cmd.GET, headers: COMMON.headers, url: _this.url + 'api/logs/missing_connections', }, custom.system_logs = { method: COMMON.cmd.GET, headers: COMMON.headers, url: _this.url + 'api/logs/system_logs', }; _this.new('Log', _this.url + 'api/logs/:id', { id: '@id', }, custom); custom = JSON.parse(JSON.stringify(COMMON.crud)); custom.current = { method: COMMON.cmd.GET, headers: COMMON.headers, url: _this.url + 'api/users/current', }; _this.new('User', _this.url + 'api/users/:id', { id: '@id', }, custom); var auth; auth = {}; auth.get_authority = function (auth_url) { if (!auth_url) { auth_url = _this.url; } if (auth_url.indexOf('http') < 0) { } return (new Promise(function (auth_res, auth_rej) { var authority; var parts = auth_url.split('/'); var url = parts.splice(0, 3).join('/') + '/'; _this.http_unauth.get(url + 'auth/authority') .map(function (res) { return res.json(); }) .subscribe(function (data) { return authority = data; }, function (err) { return auth_rej(err); }, function () { return auth_res(authority); }); })); }; if (_this.factories === undefined) { _this.factories = {}; } _this.factories.Authority = auth; _this.initAuth(resolve, reject); }); }; Resources.prototype.getToken = function () { return this.http.token; }; Resources.prototype.checkAuth = function () { this.http.checkAuth(function () { COMPOSER.log('Resources', 'Refreshed Auth'); }); }; Resources.prototype.new = function (name, url, params, methods) { var factory = new ResourceFactory(url, params, methods, this.http); factory.service = this; if (this.factories === undefined) { this.factories = {}; } this.factories[name] = factory; }; Resources.prototype.get = function (name) { if (!this.authLoaded) { COMPOSER.log("Resources", "Not ready to perform API requests.", null, 'warn'); } return this.factories && this.factories[name] ? this.factories[name] : null; }; return Resources; }()); export { Resources }; Resources.decorators = [ { type: Injectable }, ]; Resources.ctorParameters = function () { return [ { type: CommsService, }, { type: Http, }, { type: ComposerDebugService, }, ]; }; //# sourceMappingURL=resources.service.js.map