@kweli/cs-rest
Version:
Simple authentication and REST calls for OpenText Content Server.
3 lines (2 loc) • 17.9 kB
JavaScript
import e from"axios";function t(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(t=function(){return!!e})()}function r(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,"string");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}function n(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,r(o.key),o)}}function o(e,t,r){return t&&n(e.prototype,t),r&&n(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function i(){return i=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},i.apply(this,arguments)}function s(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,u(e,t)}function a(e){return a=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},a(e)}function u(e,t){return u=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},u(e,t)}function c(e){var r="function"==typeof Map?new Map:void 0;return c=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(e))return r.get(e);r.set(e,n)}function n(){return function(e,r,n){if(t())return Reflect.construct.apply(null,arguments);var o=[null];o.push.apply(o,r);var i=new(e.bind.apply(e,o));return n&&u(i,n.prototype),i}(e,arguments,a(this).constructor)}return n.prototype=Object.create(e.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),u(n,e)},c(e)}var d,p=/*#__PURE__*/function(e){function t(t,r,n){var o,i;if(void 0===t&&(t="Internal error"),void 0===r&&(r=null),void 0===n&&(n=-32603),"object"==typeof(i=t)&&null!==i){var s=t;(o=e.call(this,s.message)||this).code=void 0,o.data=void 0,o.code=s.code,o.data=s.data}else(o=e.call(this,t)||this).code=void 0,o.data=void 0,o.code=n,o.data=r;return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(o)}return s(t,e),t}(/*#__PURE__*/c(Error)),f={index:0,get next(){return this.index=this.index+1,this.index}},h=/*#__PURE__*/function(){function e(e,t){this.session=void 0,this.relativePath=void 0,this._batchQueue=void 0,this.session=e,this.relativePath=t,this.resetQueue()}var t=e.prototype;return t.requestObject=function(e,t,r){return{jsonrpc:"2.0",method:e,id:r,params:t}},t.handleResponse=function(e){if(Object.prototype.hasOwnProperty.call(e,"result"))return e.result;if(Object.prototype.hasOwnProperty.call(e,"error")){var t=e.error;throw new p(t.message,t.data,t.code)}throw Error("The server did not respond correctly.")},t.request=function(e,t,r){try{var n=this;return void 0===r&&(r=f.next),Promise.resolve(n.session.postBody(n.relativePath,{rpc:n.requestObject(e,t,r)})).then(function(e){return n.handleResponse(e.data)})}catch(e){return Promise.reject(e)}},t.resetQueue=function(){this._batchQueue=[]},t.queue=function(e,t,r){return void 0===r&&(r=f.next),this._batchQueue.push(this.requestObject(e,t,r)),this},t.batch=function(e){void 0===e&&(e=!1);try{var t=this,r=t._batchQueue;return t.resetQueue(),Promise.resolve(t.session.postBody(t.relativePath,{rpc:r})).then(function(r){var n;return(null!=(n=r.data.data)?n:[]).map(function(r){if(e)return t.handleResponse(r);try{return t.handleResponse(r)}catch(e){return e}})})}catch(e){return Promise.reject(e)}},t.rhnode=function(e,t,r,n){return void 0===r&&(r=[]),void 0===n&&(n=f.next),this.request(t,{dataid:e,args:r},n)},t.rhnodeQueue=function(e,t,r,n){return void 0===r&&(r=[]),void 0===n&&(n=f.next),this.queue(t,{dataid:e,args:r},n)},e}(),l=/*#__PURE__*/function(){return o(function(e){this._session=void 0,this._session=e},[{key:"session",get:function(){return this._session}}])}(),v=/*#__PURE__*/function(e){function t(){return e.apply(this,arguments)||this}return s(t,e),t.prototype.auth=function(){return this.session.get("/api/v1/auth/")},t}(l);!function(e){e[e.Alias=1]="Alias",e[e.Category=131]="Category",e[e.Collection=298]="Collection",e[e.CompoundDoc=136]="CompoundDoc",e[e.Document=144]="Document",e[e.Folder=0]="Folder",e[e.Generation=2]="Generation",e[e.Project=202]="Project",e[e.ProjectVol=201]="ProjectVol",e[e.Release=138]="Release",e[e.Revision=139]="Revision",e[e.ScriptNode=32901]="ScriptNode",e[e.Simplate=31678]="Simplate"}(d||(d={}));var m=function(){return"undefined"==typeof window},y=/*#__PURE__*/function(e){function t(){return e.apply(this,arguments)||this}s(t,e);var r=t.prototype;return r.addablenodetypes=function(e){return this.session.get("api/v1/nodes/"+e+"/addablenodetypes")},r.addDocument=function(e){var t=e.parent_id,r=e.fileHandler,n=e.name,o=void 0===n?void 0:n,s=e.description,a=void 0===s?void 0:s,u=e.options,c=void 0===u?{}:u;try{var p=this,f="api/v1/nodes";if(m()&&p.session._isString(r))return Promise.resolve(Promise.all([import("fs"),import("path")])).then(function(e){var n=e[1],s=e[0].createReadStream(r),a={body:i({},c,{type:d.Document,name:null!=o?o:n.basename(r),parent_id:t}),file:s};return Promise.resolve(p.session.postForm(f,a))});if(p.session._isFile(r)){var h={body:i({},c,{type:d.Document,name:o,description:a,parent_id:t}),file:r};return Promise.resolve(p.session.postForm(f,h))}throw new Error("Invalid file.")}catch(e){return Promise.reject(e)}},r.addDocumentMajor=function(e){var t=e.parent_id,r=e.fileHandler,n=e.name,o=void 0===n?void 0:n,s=e.description,a=void 0===s?void 0:s,u=e.options,c=void 0===u?{}:u;try{var d=this;return Promise.resolve(d.addDocument({parent_id:t,fileHandler:r,name:o,options:i({},c,{advanced_versioning:!0})})).then(function(e){var t=e.data.id;return Promise.resolve(d.session.versions.promote({dataid:t,versionNumber:1,description:a})).then(function(){return Promise.resolve(d.session.versions.deleteVersion({dataid:t,versionNumber:1})).then(function(){return e})})})}catch(e){return Promise.reject(e)}},r.addItem=function(e,t,r,n){return void 0===n&&(n={}),this.session.postBody("api/v2/nodes",i({type:e,parent_id:t,name:r},n))},r.node=function(e){var t=e.apiVersion,r=e.params;return this.session.get("api/"+(void 0===t?"v2":t)+"/nodes/"+e.dataid,{params:void 0===r?{}:r})},r.ancestors=function(e,t){return void 0===t&&(t={}),this.session.get("api/v1/nodes/"+e+"/ancestors",{params:t})},r.volumeInfo=function(e){return this.session.get("api/v1/volumes/"+e)},r.volumes=function(){return this.session.get("api/v2/volumes")},r.addFolder=function(e,t,r){return void 0===r&&(r={}),this.addItem(d.Folder,e,t,r)},r.addGeneration=function(e,t,r,n,o){return void 0===o&&(o={}),this.addItem(d.Generation,e,t,i({original_id:r,version_number:n},o))},r.nodes=function(e,t){return void 0===t&&(t={}),this.session.get("api/v2/nodes/"+e+"/nodes",{params:t})},r.children=function(e,t){return void 0===t&&(t={}),this.nodes(e,t)},r.delete=function(e){return this.session.delete("api/v1/nodes/"+e)},r.download=function(e){var t=e.dataid,r=e.apiVersion,n=void 0===r?"v1":r,o=e.filePath;return m()?this.session.get("api/"+n+"/nodes/"+t+"/content",{responseType:"stream"}).then(function(e){try{return Promise.resolve(import("fs")).then(function(t){var r=t.createWriteStream(o);return e.data.pipe(r),new Promise(function(e,t){r.on("finish",e),r.on("error",t)})})}catch(e){return Promise.reject(e)}}):Promise.reject("Not implemented yet")},r.audit=function(e){var t=e.apiVersion,r=e.params;return this.session.get("api/"+(void 0===t?"v2":t)+"/nodes/"+e.dataid+"/audit",{params:void 0===r?{}:r})},t}(l),b=/*#__PURE__*/function(){function e(e,t){this.session=void 0,this.mapId=void 0,this.workflowPropertiesInfo=null,this.session=e,this.mapId=t}var t=e.prototype;return t.start=function(){try{var e=this;return Promise.resolve(e.session.workflow.start(e.mapId)).then(function(t){e.workflowPropertiesInfo=t})}catch(e){return Promise.reject(e)}},t.findWorkflowAttribute=function(e){return Object.entries(this.form.schema.properties).find(function(t){return t[1].title===e})},t.setWorkflowAttribute=function(t,r){var n,o=null!=(n=this.findWorkflowAttribute(t))?n:[],i=o[0];if("array"===o[1].type===Array.isArray(r))return this.form.data[i]=r,e;throw new Error("Invalid type.")},t.formUpdate=function(){try{var e=this;return Promise.resolve(e.session.workflow.draftprocessesPut(e.processId,{action:"formUpdate",values:e.form.data}))}catch(e){return Promise.reject(e)}},t.initiate=function(e){var t=void 0===e?{}:e,r=t.comment,n=t.password;try{var o=this;return Promise.resolve(o.formUpdate()).then(function(){var e=i({action:"Initiate"},o.wantComments&&{comment:r},o.wantAuthentication&&{authentication_info:{password:n}});return Promise.resolve(o.session.workflow.draftprocessesPut(o.processId,e))})}catch(e){return Promise.reject(e)}},o(e,[{key:"form",get:function(){return this.workflowPropertiesInfo.forms[0]}},{key:"processId",get:function(){return this.workflowPropertiesInfo.data.process_id}},{key:"attachmentsFolderId",get:function(){var e=this.workflowPropertiesInfo.data.data_packages.find(function(e){return 1===e.type&&1===e.sub_type});return null==e?void 0:e.data.attachment_folder_id}},{key:"wantComments",get:function(){return this.workflowPropertiesInfo.data.comments_on}},{key:"wantAuthentication",get:function(){return this.workflowPropertiesInfo.data.authentication}}])}(),w=/*#__PURE__*/function(e){function t(){return e.apply(this,arguments)||this}s(t,e);var r=t.prototype;return r.workflowInitiator=function(e){return new b(this.session,e)},r.start=function(e){var t=this;return this.draftprocesses(e).then(function(e){return e.results.draftprocess_id}).then(function(e){return t.draftprocessesUpdate(e)})},r.draftprocesses=function(e){try{return Promise.resolve(this.session.postForm("api/v2/draftprocesses",{workflow_id:e})).then(function(e){return e.data})}catch(e){return Promise.reject(e)}},r.draftprocessesUpdate=function(e){try{return Promise.resolve(this.session.get("api/v1/forms/draftprocesses/update",{params:{draftprocess_id:e}})).then(function(e){return e.data})}catch(e){return Promise.reject(e)}},r.draftprocessesPut=function(e,t){try{return Promise.resolve(this.session.putForm("api/v2/draftprocesses/"+e,{body:t})).then(function(e){return e.data})}catch(e){return Promise.reject(e)}},t}(l),_=/*#__PURE__*/function(e){function t(){return e.apply(this,arguments)||this}return s(t,e),t.prototype.scriptNode=function(e,t){return void 0===t&&(t={}),this.session.postForm("api/v1/rhcore/"+e,t)},t}(l),P=/*#__PURE__*/function(e){function t(){return e.apply(this,arguments)||this}s(t,e);var r=t.prototype;return r.search=function(e,t){return void 0===t&&(t={}),this.session.postBody("api/v2/search",i({where:e},t))},r.regions=function(e){return void 0===e&&(e={}),this.session.get("api/v1/regions",e)},t}(l),g=/*#__PURE__*/function(e){function t(t){var r;return(r=e.call(this,t)||this).USER=void 0,r.GROUP=void 0,r.USER=0,r.GROUP=1,r}s(t,e);var r=t.prototype;return r.userQuery=function(e,t,r){void 0===t&&(t={}),void 0===r&&(r="v2");var n=i({limit:20,where_type:JSON.stringify([this.USER,this.GROUP]),query:e},t);return this.session.get("api/"+r+"/members",{params:n})},r.member=function(e,t){return void 0===t&&(t="v2"),this.session.get("api/"+t+"/members/"+e)},t}(l),j=/*#__PURE__*/function(e){function t(){return e.apply(this,arguments)||this}s(t,e);var r=t.prototype;return r.addVersion=function(e){var t=e.dataid,r=e.fileHandler,n=e.options,o=void 0===n?{}:n;try{var s=this;console.assert(null!=t,"dataid cannot be null"),console.assert(null!=r,"fileHandler cannot be null");var a="api/v1/nodes/"+t+"/versions";if(m()&&s.session._isString(r))return Promise.resolve(import("fs")).then(function(e){var t=i({file:e.createReadStream(r)},o);return s.session.postForm(a,t)});if(s.session._isFile(r)){var u=i({file:r},o);return Promise.resolve(s.session.postForm(a,u))}throw new Error("Invalid file.")}catch(e){return Promise.reject(e)}},r.download=function(e){var t=e.dataid,r=e.version,n=e.filePath;return console.assert(null!=t,"dataid cannot be null"),console.assert(null!=r,"version cannot be null"),console.assert(null!=n,"filePath cannot be null"),m()?this.session.get("api/v1/nodes/"+t+"/versions/"+r+"/content",{responseType:"stream"}).then(function(e){try{return Promise.resolve(import("fs")).then(function(t){var r=t.createWriteStream(n);return e.data.pipe(r),new Promise(function(e,t){r.on("finish",e),r.on("error",t)})})}catch(e){return Promise.reject(e)}}):Promise.reject("Not implemented yet")},r.list=function(e){return this.session.get("api/v1/nodes/"+e+"/versions")},r.version=function(e,t){return void 0===t&&(t="latest"),this.session.get("api/v1/nodes/"+e+"/versions/"+t)},r.promote=function(e){var t=e.dataid,r=e.versionNumber,n=e.description,o=void 0===n?null:n;return console.assert(null!=t,"dataid cannot be null"),console.assert(null!=r,"number_to_keep must be an integer"),this.session.postBody("api/v2/nodes/"+t+"/versions/"+r+"/promote",i({},!!o&&{description:o}))},r.deleteVersion=function(e){var t=e.dataid,r=e.versionNumber,n=e.apiVersion,o=void 0===n?"v1":n;return console.assert(null!=t,"dataid cannot be null"),console.assert(null!=r,"number_to_keep must be an integer"),this.session.delete("api/"+o+"/nodes/"+t+"/versions/"+r)},r.purge=function(e){var t=e.dataid,r=e.number_to_keep,n=void 0===r?1:r;return console.assert(null!=t,"dataid cannot be null"),console.assert(!isNaN(n),"number_to_keep must be an integer"),this.session.deleteForm("api/v2/nodes/"+t+"/versions",{number_to_keep:n})},t}(l),k=/*#__PURE__*/function(e){function t(){return e.apply(this,arguments)||this}return s(t,e),t.prototype.run=function(e,t){return void 0===t&&(t={}),this.session.get("api/v1/nodes/"+e+"/output",{params:t})},t}(l),O=/*#__PURE__*/function(){function t(t){this.axios=void 0,this._nodes=void 0,this._auth=void 0,this._workflow=void 0,this._rhcore=void 0,this._members=void 0,this._search=void 0,this._webreports=void 0,this._versions=void 0,this.baseUrl=void 0,this.baseUrl=t.baseUrl,this.axios=function(t){var r=function(t){var r=e.create({baseURL:t.baseUrl,paramsSerializer:{indexes:null}});return r.interceptors.response.use(function(e){var t=null==e?void 0:e.headers.otcsticket;return t&&(r.defaults.headers.common.OTCSTicket=t),e},function(e){return Promise.reject(e)}),r}(t),n=t.username,o=t.password,i=t.otcsticket;if(i)r.defaults.headers.common.OTCSTicket=i;else{if(!n||!o)throw Error("You must provide an otcsticket or username and password.");r.interceptors.request.use(function(r){try{var i;return null!=(i=r.headers.common)&&i.OTCSTicket?Promise.resolve(r):Promise.resolve(e.post(t.baseUrl+"/api/v1/auth/",{username:n,password:o},{headers:{"Content-Type":"multipart/form-data"}})).then(function(e){return r.headers.OTCSTicket=e.data.ticket,r})}catch(e){return Promise.reject(e)}})}return r}(t)}var r=t.prototype;return r.rpcClient=function(e){return void 0===e&&(e="/api/v1/rh/rpc/rhnode/"),new h(this,e)},r._isObject=function(e){return e&&"object"==typeof e&&e.constructor===Object},r._isString=function(e){return"string"==typeof e||e instanceof String},r._isBoolean=function(e){return"boolean"==typeof e},r._isFile=function(e){return"string"==typeof(null==e?void 0:e.name)},r.putForm=function(e,t){return this.put(e,this.objectToForm(t),{headers:{"Content-Type":"multipart/form-data"}})},r.postForm=function(e,t){return this.post(e,this.objectToForm(t),{headers:{"Content-Type":"multipart/form-data"},maxBodyLength:Infinity})},r.patchForm=function(e,t){return this.patch(e,this.objectToForm(t),{headers:{"Content-Type":"multipart/form-data"}})},r.deleteForm=function(e,t){return this.delete(e)},r.objectToForm=function(e){var t=this;return Object.entries(e).reduce(function(e,r){var n,o=r[1];return i({},e,((n={})[r[0]]=Array.isArray(o)||t._isObject(o)?JSON.stringify(o):o,n))},{})},r.putBody=function(e,t){return this.putForm(e,{body:t})},r.postBody=function(e,t){return this.postForm(e,{body:t})},r.patchBody=function(e,t){return this.patchForm(e,{body:t})},r.deleteBody=function(e,t){return this.deleteForm(e,{body:t})},r.get=function(e,t){return this.axios.get(e,t)},r.post=function(e,t,r){return this.axios.post(e,t,r)},r.put=function(e,t,r){return this.axios.put(e,t,r)},r.patch=function(e,t,r){return this.axios.patch(e,t,r)},r.options=function(e,t){return this.axios.options(e,t)},r.delete=function(e,t){return this.axios.delete(e,t)},r.$get=function(e,t){try{return Promise.resolve(this.get(e,t)).then(function(e){return e.data})}catch(e){return Promise.reject(e)}},o(t,[{key:"nodes",get:function(){return void 0===this._nodes&&(this._nodes=new y(this)),this._nodes}},{key:"auth",get:function(){return void 0===this._auth&&(this._auth=new v(this)),this._auth}},{key:"workflow",get:function(){return void 0===this._workflow&&(this._workflow=new w(this)),this._workflow}},{key:"rhcore",get:function(){return void 0===this._rhcore&&(this._rhcore=new _(this)),this._rhcore}},{key:"members",get:function(){return void 0===this._members&&(this._members=new g(this)),this._members}},{key:"search",get:function(){return void 0===this._search&&(this._search=new P(this)),this._search}},{key:"webreports",get:function(){return void 0===this._webreports&&(this._webreports=new k(this)),this._webreports}},{key:"versions",get:function(){return void 0===this._versions&&(this._versions=new j(this)),this._versions}}])}(),F=function(e){return void 0!==e.code&&void 0!==e.data};export{h as RPCClient,p as RPCError,O as Session,F as isRPCError};
//# sourceMappingURL=cs-rest.esm.js.map