nd-common
Version:
417 lines (411 loc) • 17.3 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common/http'), require('rxjs'), require('rxjs/operators'), require('nd-common'), require('@angular/common'), require('@angular/http')) :
typeof define === 'function' && define.amd ? define('nd-common/api', ['exports', '@angular/core', '@angular/common/http', 'rxjs', 'rxjs/operators', 'nd-common', '@angular/common', '@angular/http'], factory) :
(factory((global['nd-common'] = global['nd-common'] || {}, global['nd-common'].api = {}),global.ng.core,global.ng.common.http,global.rxjs,global.Rx.Observable.prototype,global['nd-common'],global.ng.common,global.ng.http));
}(this, (function (exports,core,http,rxjs,operators,ndCommon,common,http$1) { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var ND_TOKEN_CONTROLLER = new core.InjectionToken('ndTokenController');
var RestAPIBaseService = /** @class */ (function () {
function RestAPIBaseService(http$$1, _token) {
this.http = http$$1;
this.DEFAULT_REST_VERSION = ndCommon.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 = ndCommon.HttpMethod.Get;
}
if (p.method == ndCommon.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(operators.catchError(function (err, caught) {
if (err.status == 401) {
return _this.tokenService.renew().pipe(operators.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 rxjs.of(new http.HttpResponse({
status: err.status
}));
}
else {
return rxjs.throwError(err);
}
}), operators.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 = ndCommon.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 http.HttpHeaders({
"Authorization": "Bearer " + this.AccessToken,
"Accept": "application/json"
});
},
enumerable: true,
configurable: true
});
RestAPIBaseService.urlEncodeString = function (attribute) {
if (attribute != null) {
if (ndCommon.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: core.Injectable },
];
RestAPIBaseService.ctorParameters = function () { return [
{ type: http.HttpClient },
{ type: undefined, decorators: [{ type: core.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 rxjs.throwError("id is required.");
var param = new ndCommon.CallParameters();
param.apiVersion = ndCommon.APIVersions.two;
param.method = ndCommon.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 rxjs.throwError("id is required.");
var param = new ndCommon.CallParameters();
param.apiVersion = ndCommon.APIVersions.two;
param.method = ndCommon.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 ndCommon.CallParameters();
if (documentType === null || documentType === undefined) {
documentType = ndCommon.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/" + ndCommon.RecentDocumentType[documentType].toLowerCase();
param.method = ndCommon.HttpMethod.Get;
param.apiVersion = ndCommon.APIVersions.two;
return this.call(param, false);
};
DocumentControllerService.prototype.getGrantAccessRequestInfo = function (p) {
var param = new ndCommon.CallParameters();
if (p["Id"] == undefined)
throw "id is required.";
else {
param.path = this.controllerUrl + "/" + p["Id"] + "/grant_access_request_info";
}
param.method = ndCommon.HttpMethod.Get;
param.apiVersion = ndCommon.APIVersions.two;
return this.call(param);
};
DocumentControllerService.prototype.getLinkedDocuments = function (p) {
if (p.id == undefined) {
return rxjs.throwError("Document getLinkedDocuments id is required.");
}
var param = new ndCommon.CallParameters();
param.apiVersion = ndCommon.APIVersions.two;
param.method = ndCommon.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 rxjs.Observable.throw("Document putNewVersion id is required.");
}
var param = new ndCommon.CallParameters();
param.apiVersion = ndCommon.APIVersions.one;
param.method = ndCommon.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 rxjs.Observable.throw("Document checkIn id is required.");
}
var actionName = "checkin";
var param = new ndCommon.CallParameters();
param.apiVersion = ndCommon.APIVersions.one;
param.method = ndCommon.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 += "¬ify=" + p["notify"];
}
}
return this.call(param);
};
DocumentControllerService.prototype.putNewVersionContents = function (p) {
if (p.id == undefined) {
return rxjs.Observable.throw("Document putFileContents id is required.");
}
if (p["file"] == undefined) {
return rxjs.Observable.throw("Document putFileContents file is required.");
}
if (p["extension"] == undefined) {
return rxjs.Observable.throw("Document putFileContents extension is required.");
}
var param = new ndCommon.CallParameters();
param.apiVersion = ndCommon.APIVersions.one;
param.method = ndCommon.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 rxjs.throwError("Document getLinkedDocuments id is required.");
}
var param = new ndCommon.CallParameters();
param.apiVersion = ndCommon.APIVersions.one;
param.method = ndCommon.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: core.Injectable },
];
DocumentControllerService.ctorParameters = function () { return [
{ type: http.HttpClient },
{ type: undefined, decorators: [{ type: core.Inject, args: [ND_TOKEN_CONTROLLER,] }] }
]; };
var ndApiModule = /** @class */ (function () {
function ndApiModule() {
}
ndApiModule.forRoot = function (tokenControllerClass) {
return {
ngModule: ndApiModule,
providers: [
{ provide: ND_TOKEN_CONTROLLER, useExisting: core.forwardRef(function () { return tokenControllerClass; }) },
DocumentControllerService
]
};
};
return ndApiModule;
}());
ndApiModule.decorators = [
{ type: core.NgModule, args: [{
imports: [
common.CommonModule,
http$1.HttpModule
]
},] },
];
exports.ndApiModule = ndApiModule;
exports.DocumentControllerService = DocumentControllerService;
exports.ND_TOKEN_CONTROLLER = ND_TOKEN_CONTROLLER;
exports.ɵa = RestAPIBaseService;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=nd-common-api.umd.js.map