UNPKG

nd-common

Version:

388 lines (385 loc) 15.5 kB
import { __extends } from 'tslib'; import { InjectionToken, Injectable, Inject, NgModule, forwardRef } from '@angular/core'; import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http'; import { of, throwError, Observable } from 'rxjs'; import { mergeMap, map, catchError } from 'rxjs/operators'; import { CommonService, APIVersions, NetDocumentsService, HttpMethod, CallParameters, RecentDocumentType } from 'nd-common'; import { CommonModule } from '@angular/common'; import { HttpModule } from '@angular/http'; var ND_TOKEN_CONTROLLER = new InjectionToken('ndTokenController'); var RestAPIBaseService = /** @class */ (function () { function RestAPIBaseService(http, _token) { this.http = http; this.DEFAULT_REST_VERSION = APIVersions.two; this.tokenService = _token; } RestAPIBaseService.prototype.call = function (p, stripResponse) { var _this = this; if (stripResponse === void 0) { stripResponse = true; } p.url = this.getUrl(p.path, p.apiVersion); if (p.method == null) { p.method = HttpMethod.Get; } if (p.method == HttpMethod.Get && p.queryArgs) { var queryArgs = this.generateQueryArgs(p.queryArgs); var appendage = "?"; if (p.url.indexOf(appendage) > -1) { appendage = "&"; } p.url += "" + appendage + queryArgs; } var httpOptions = {}; httpOptions = { headers: this.BaseHeaders, responseType: 'json', observe: "response", body: p.body }; return this.http.request(p.method, p.url, httpOptions).pipe(catchError(function (err, caught) { if (err.status == 401) { return _this.tokenService.renew().pipe(mergeMap(function (token) { _this.AccessToken = token; p.init.headers = _this.BaseHeaders; httpOptions["headers"] = _this.BaseHeaders; return _this.http.request(p.method, p.url, httpOptions); })); } else if (err.status == 304) { return of(new HttpResponse({ status: err.status })); } else { return throwError(err); } }), map(function (response) { if (stripResponse) { return response.body; } else { return (response); } })); }; RestAPIBaseService.prototype.generateQueryArgs = function (obj) { var queryArgs = ""; Object.keys(obj).forEach(function (key) { var appendage = ""; if (queryArgs.length) { appendage = "&"; } var value = obj[key]; if (Array.isArray(value)) { if (typeof value[0] === "object") { value = value.map(function (data) { return data.ToString(); }).join(","); } else { value = value.join(","); } } queryArgs += appendage + key + "=" + value; }); return queryArgs; }; Object.defineProperty(RestAPIBaseService.prototype, "AccessToken", { get: function () { return this.tokenService.token; }, set: function (newToken) { this.tokenService.token = newToken.trim(); }, enumerable: true, configurable: true }); Object.defineProperty(RestAPIBaseService.prototype, "BaseUrl", { get: function () { if (!this._baseUrl) { this._baseUrl = RestAPIBaseService.GenerateBaseUrl(this.tokenService.host, this.tokenService.useHttps); } return this._baseUrl; }, enumerable: true, configurable: true }); RestAPIBaseService.GenerateBaseUrl = function (host, https, api) { if (host === void 0) { host = NetDocumentsService.vault; } if (https === void 0) { https = true; } if (api === void 0) { api = true; } return (https ? "https" : "http") + "://" + (api ? "api." : "") + host; }; RestAPIBaseService.prototype.getUrl = function (path, version) { var callUrl = this.BaseUrl; callUrl += version ? version : this.DEFAULT_REST_VERSION; callUrl += path; return callUrl; }; Object.defineProperty(RestAPIBaseService.prototype, "BaseHeaders", { get: function () { return new HttpHeaders({ "Authorization": "Bearer " + this.AccessToken, "Accept": "application/json" }); }, enumerable: true, configurable: true }); RestAPIBaseService.urlEncodeString = function (attribute) { if (attribute != null) { if (CommonService.stringContainsAny(attribute, this.ENCODING_SUBSTRINGS)) { attribute = encodeURIComponent(attribute); } attribute = encodeURIComponent(attribute); } return attribute; }; RestAPIBaseService.attributeQueryEncode = function (attribute) { if (attribute && attribute.length > 0) { if (attribute.indexOf(",") > -1 || attribute.indexOf('"') > -1) { attribute = attribute.replace(/"/g, '\"\"'); if (!attribute.startsWith('\"', 0)) { attribute = "\"" + attribute; } if (!attribute.endsWith('\"', attribute.length)) { attribute = attribute + "\""; } } attribute = encodeURIComponent(attribute); } return attribute; }; RestAPIBaseService.cleanEnv = function (envUrl) { var envNBReg = /\/NB\d/; if (!envUrl) { return envUrl; } if (getItemType(envUrl) == "NB" && !envNBReg.test(envUrl)) { if (envUrl.indexOf("|") < 0) envUrl += "|0"; envUrl = encodeURI(envUrl.split("|")[0]) + "|" + envUrl.split("|")[1]; } envUrl = envUrl.replace(/\//g, ":"); return envUrl; }; RestAPIBaseService.createFilterList = function (filter) { var seperator = " extension eq "; return (seperator + filter.join(seperator)).trim(); }; return RestAPIBaseService; }()); RestAPIBaseService.ENCODING_SUBSTRINGS = ["/", "&", "\\", "+", "?", "%"]; RestAPIBaseService.decorators = [ { type: Injectable }, ]; RestAPIBaseService.ctorParameters = function () { return [ { type: HttpClient }, { type: undefined, decorators: [{ type: Inject, args: [ND_TOKEN_CONTROLLER,] }] } ]; }; var DocumentControllerService = /** @class */ (function (_super) { __extends(DocumentControllerService, _super); function DocumentControllerService(h, _token) { var _this = _super.call(this, h, _token) || this; _this.h = h; _this.controllerUrl = "/document"; return _this; } DocumentControllerService.prototype.getInfo = function (p) { if (p.id == undefined) return throwError("id is required."); var param = new CallParameters(); param.apiVersion = APIVersions.two; param.method = HttpMethod.Get; param.path = this.controllerUrl + "/" + p.id + "/info"; if (p.queryArgs !== undefined) { param.queryArgs = p.queryArgs; } return this.call(param); }; DocumentControllerService.prototype.getListOfDocuments = function (p) { if (p.id == undefined) return throwError("id is required."); var param = new CallParameters(); param.apiVersion = APIVersions.two; param.method = HttpMethod.Post; param.path = this.controllerUrl + "/list"; param.body = "id=" + p.id + "&select=" + p.queryArgs.select; param.headers = this.BaseHeaders; param.headers.append("Content-Type", "application/x-www-form-urlencoded"); return this.call(param); }; DocumentControllerService.prototype.getRecent = function (p, documentType, lastModifiedDate) { if (lastModifiedDate === void 0) { lastModifiedDate = ''; } var param = new CallParameters(); if (documentType === null || documentType === undefined) { documentType = RecentDocumentType.All; } if (p.queryArgs !== undefined) { param.queryArgs = p.queryArgs; } param.headers = !param.headers ? this.BaseHeaders : param.headers; if (lastModifiedDate) { param.headers.append("If-Modified-Since", lastModifiedDate); } else { param.headers.append("Cache-Control", 'no-cache'); param.headers.append("Pragma", 'no-cache'); param.headers.append("Expires", 'Sat, 01 Jan 2000 00:00:00 GMT'); } param.path = this.controllerUrl + "/recent/" + RecentDocumentType[documentType].toLowerCase(); param.method = HttpMethod.Get; param.apiVersion = APIVersions.two; return this.call(param, false); }; DocumentControllerService.prototype.getGrantAccessRequestInfo = function (p) { var param = new CallParameters(); if (p["Id"] == undefined) throw "id is required."; else { param.path = this.controllerUrl + "/" + p["Id"] + "/grant_access_request_info"; } param.method = HttpMethod.Get; param.apiVersion = APIVersions.two; return this.call(param); }; DocumentControllerService.prototype.getLinkedDocuments = function (p) { if (p.id == undefined) { return throwError("Document getLinkedDocuments id is required."); } var param = new CallParameters(); param.apiVersion = APIVersions.two; param.method = HttpMethod.Get; param.path = this.controllerUrl + "/" + p.id + "/linked"; if (p.queryArgs !== undefined) { param.queryArgs = p.queryArgs; } return this.call(param); }; DocumentControllerService.prototype.putNewVersion = function (p) { if (p.id == undefined) { return Observable.throw("Document putNewVersion id is required."); } var param = new CallParameters(); param.apiVersion = APIVersions.one; param.method = HttpMethod.Put; param.path = this.controllerUrl + "/" + p.id + "/new"; if (p.body != null) { param.path += "?"; if (p.body.Name != undefined) { param.path += "vername=" + p.body.Name + "&"; } if (p.body.Description != undefined) { param.path += "version_description=" + p.body.Description + "&"; } if (p.body.Official) { param.path += "official=Y"; } } return this.call(param); }; DocumentControllerService.prototype.checkIn = function (p) { if (p.id == undefined) { return Observable.throw("Document checkIn id is required."); } var actionName = "checkin"; var param = new CallParameters(); param.apiVersion = APIVersions.one; param.method = HttpMethod.Post; param.path = "" + this.controllerUrl; param.headers = this.BaseHeaders; if (p["file"] != undefined) { var form = new FormData(); form.append("action", actionName); form.append("id", p.id); if (p["notify"] !== undefined) { form.append("notify", p["notify"]); } if (p["extension"] !== undefined) { form.append("extension", p["extension"]); } form.append("file", p["file"]); param.body = form; } else { param.headers.append("Content-Type", "application/x-www-form-urlencoded"); param.body = "id=" + p.id + "&action=" + actionName; if (p["notify"] !== undefined) { param.body += "&notify=" + p["notify"]; } } return this.call(param); }; DocumentControllerService.prototype.putNewVersionContents = function (p) { if (p.id == undefined) { return Observable.throw("Document putFileContents id is required."); } if (p["file"] == undefined) { return Observable.throw("Document putFileContents file is required."); } if (p["extension"] == undefined) { return Observable.throw("Document putFileContents extension is required."); } var param = new CallParameters(); param.apiVersion = APIVersions.one; param.method = HttpMethod.Put; param.path = this.controllerUrl + "/" + p.id + "/new?"; if (p["extension"] != undefined) { param.path += "extension=" + p["extension"]; } if (p["verName"] != undefined) { param.path += "&vername=" + p["verName"]; } param.headers = this.BaseHeaders; var form = new FormData(); form.append("file", p["file"]); form.append("processData", "false"); form.append("contentType", p["file"].type); param.body = form; return this.call(param); }; DocumentControllerService.prototype.getHistory = function (p) { if (p.id == undefined) { return throwError("Document getLinkedDocuments id is required."); } var param = new CallParameters(); param.apiVersion = APIVersions.one; param.method = HttpMethod.Get; param.path = this.controllerUrl + "/" + p.id + "/history"; if (p.queryArgs !== undefined) { param.queryArgs = p.queryArgs; } return this.call(param); }; return DocumentControllerService; }(RestAPIBaseService)); DocumentControllerService.decorators = [ { type: Injectable }, ]; DocumentControllerService.ctorParameters = function () { return [ { type: HttpClient }, { type: undefined, decorators: [{ type: Inject, args: [ND_TOKEN_CONTROLLER,] }] } ]; }; var ndApiModule = /** @class */ (function () { function ndApiModule() { } ndApiModule.forRoot = function (tokenControllerClass) { return { ngModule: ndApiModule, providers: [ { provide: ND_TOKEN_CONTROLLER, useExisting: forwardRef(function () { return tokenControllerClass; }) }, DocumentControllerService ] }; }; return ndApiModule; }()); ndApiModule.decorators = [ { type: NgModule, args: [{ imports: [ CommonModule, HttpModule ] },] }, ]; export { ndApiModule, DocumentControllerService, ND_TOKEN_CONTROLLER, RestAPIBaseService as ɵa }; //# sourceMappingURL=nd-common-api.js.map