@kweli/cs-rest
Version:
Simple authentication and REST calls for OpenText Content Server.
3 lines (2 loc) • 18.5 kB
JavaScript
function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function t(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach(function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}}),t.default=e,t}var r=/*#__PURE__*/e(require("axios"));function n(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(n=function(){return!!e})()}function o(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 i(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,o(n.key),n)}}function s(e,t,r){return t&&i(e.prototype,t),r&&i(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function u(){return u=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},u.apply(this,arguments)}function a(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,d(e,t)}function c(e){return c=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},c(e)}function d(e,t){return d=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},d(e,t)}function f(e){var t="function"==typeof Map?new Map:void 0;return f=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!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return function(e,t,r){if(n())return Reflect.construct.apply(null,arguments);var o=[null];o.push.apply(o,t);var i=new(e.bind.apply(e,o));return r&&d(i,r.prototype),i}(e,arguments,c(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),d(r,e)},f(e)}var p,h=/*#__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 a(t,e),t}(/*#__PURE__*/f(Error)),l={index:0,get next(){return this.index=this.index+1,this.index}},v=/*#__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 h(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=l.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=l.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=l.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=l.next),this.queue(t,{dataid:e,args:r},n)},e}(),m=/*#__PURE__*/function(){return s(function(e){this._session=void 0,this._session=e},[{key:"session",get:function(){return this._session}}])}(),y=/*#__PURE__*/function(e){function t(){return e.apply(this,arguments)||this}return a(t,e),t.prototype.auth=function(){return this.session.get("/api/v1/auth/")},t}(m);!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"}(p||(p={}));var b=function(){return"undefined"==typeof window},w=/*#__PURE__*/function(e){function r(){return e.apply(this,arguments)||this}a(r,e);var n=r.prototype;return n.addablenodetypes=function(e){return this.session.get("api/v1/nodes/"+e+"/addablenodetypes")},n.addDocument=function(e){var r=e.parent_id,n=e.fileHandler,o=e.name,i=void 0===o?void 0:o,s=e.description,a=void 0===s?void 0:s,c=e.options,d=void 0===c?{}:c;try{var f=this,h="api/v1/nodes";if(b()&&f.session._isString(n))return Promise.resolve(Promise.all([Promise.resolve().then(function(){/*#__PURE__*/return t(require("fs"))}),Promise.resolve().then(function(){/*#__PURE__*/return t(require("path"))})])).then(function(e){var t=e[1],o=e[0].createReadStream(n),s={body:u({},d,{type:p.Document,name:null!=i?i:t.basename(n),parent_id:r}),file:o};return Promise.resolve(f.session.postForm(h,s))});if(f.session._isFile(n)){var l={body:u({},d,{type:p.Document,name:i,description:a,parent_id:r}),file:n};return Promise.resolve(f.session.postForm(h,l))}throw new Error("Invalid file.")}catch(e){return Promise.reject(e)}},n.addDocumentMajor=function(e){var t=e.parent_id,r=e.fileHandler,n=e.name,o=void 0===n?void 0:n,i=e.description,s=void 0===i?void 0:i,a=e.options,c=void 0===a?{}:a;try{var d=this;return Promise.resolve(d.addDocument({parent_id:t,fileHandler:r,name:o,options:u({},c,{advanced_versioning:!0})})).then(function(e){var t=e.data.id;return Promise.resolve(d.session.versions.promote({dataid:t,versionNumber:1,description:s})).then(function(){return Promise.resolve(d.session.versions.deleteVersion({dataid:t,versionNumber:1})).then(function(){return e})})})}catch(e){return Promise.reject(e)}},n.addItem=function(e,t,r,n){return void 0===n&&(n={}),this.session.postBody("api/v2/nodes",u({type:e,parent_id:t,name:r},n))},n.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})},n.ancestors=function(e,t){return void 0===t&&(t={}),this.session.get("api/v1/nodes/"+e+"/ancestors",{params:t})},n.volumeInfo=function(e){return this.session.get("api/v1/volumes/"+e)},n.volumes=function(){return this.session.get("api/v2/volumes")},n.addFolder=function(e,t,r){return void 0===r&&(r={}),this.addItem(p.Folder,e,t,r)},n.addGeneration=function(e,t,r,n,o){return void 0===o&&(o={}),this.addItem(p.Generation,e,t,u({original_id:r,version_number:n},o))},n.nodes=function(e,t){return void 0===t&&(t={}),this.session.get("api/v2/nodes/"+e+"/nodes",{params:t})},n.children=function(e,t){return void 0===t&&(t={}),this.nodes(e,t)},n.delete=function(e){return this.session.delete("api/v1/nodes/"+e)},n.download=function(e){var r=e.dataid,n=e.apiVersion,o=void 0===n?"v1":n,i=e.filePath;return b()?this.session.get("api/"+o+"/nodes/"+r+"/content",{responseType:"stream"}).then(function(e){try{return Promise.resolve(Promise.resolve().then(function(){/*#__PURE__*/return t(require("fs"))})).then(function(t){var r=t.createWriteStream(i);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")},n.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})},r}(m),P=/*#__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=u({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)}},s(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}}])}(),_=/*#__PURE__*/function(e){function t(){return e.apply(this,arguments)||this}a(t,e);var r=t.prototype;return r.workflowInitiator=function(e){return new P(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}(m),g=/*#__PURE__*/function(e){function t(){return e.apply(this,arguments)||this}return a(t,e),t.prototype.scriptNode=function(e,t){return void 0===t&&(t={}),this.session.postForm("api/v1/rhcore/"+e,t)},t}(m),j=/*#__PURE__*/function(e){function t(){return e.apply(this,arguments)||this}a(t,e);var r=t.prototype;return r.search=function(e,t){return void 0===t&&(t={}),this.session.postBody("api/v2/search",u({where:e},t))},r.regions=function(e){return void 0===e&&(e={}),this.session.get("api/v1/regions",e)},t}(m),k=/*#__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}a(t,e);var r=t.prototype;return r.userQuery=function(e,t,r){void 0===t&&(t={}),void 0===r&&(r="v2");var n=u({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}(m),O=/*#__PURE__*/function(e){function r(){return e.apply(this,arguments)||this}a(r,e);var n=r.prototype;return n.addVersion=function(e){var r=e.dataid,n=e.fileHandler,o=e.options,i=void 0===o?{}:o;try{var s=this;console.assert(null!=r,"dataid cannot be null"),console.assert(null!=n,"fileHandler cannot be null");var a="api/v1/nodes/"+r+"/versions";if(b()&&s.session._isString(n))return Promise.resolve(Promise.resolve().then(function(){/*#__PURE__*/return t(require("fs"))})).then(function(e){var t=u({file:e.createReadStream(n)},i);return s.session.postForm(a,t)});if(s.session._isFile(n)){var c=u({file:n},i);return Promise.resolve(s.session.postForm(a,c))}throw new Error("Invalid file.")}catch(e){return Promise.reject(e)}},n.download=function(e){var r=e.dataid,n=e.version,o=e.filePath;return console.assert(null!=r,"dataid cannot be null"),console.assert(null!=n,"version cannot be null"),console.assert(null!=o,"filePath cannot be null"),b()?this.session.get("api/v1/nodes/"+r+"/versions/"+n+"/content",{responseType:"stream"}).then(function(e){try{return Promise.resolve(Promise.resolve().then(function(){/*#__PURE__*/return t(require("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")},n.list=function(e){return this.session.get("api/v1/nodes/"+e+"/versions")},n.version=function(e,t){return void 0===t&&(t="latest"),this.session.get("api/v1/nodes/"+e+"/versions/"+t)},n.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",u({},!!o&&{description:o}))},n.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)},n.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})},r}(m),F=/*#__PURE__*/function(e){function t(){return e.apply(this,arguments)||this}return a(t,e),t.prototype.run=function(e,t){return void 0===t&&(t={}),this.session.get("api/v1/nodes/"+e+"/output",{params:t})},t}(m),S=/*#__PURE__*/function(){function e(e){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=e.baseUrl,this.axios=function(e){var t=function(e){var t=r.default.create({baseURL:e.baseUrl,paramsSerializer:{indexes:null}});return t.interceptors.response.use(function(e){var r=null==e?void 0:e.headers.otcsticket;return r&&(t.defaults.headers.common.OTCSTicket=r),e},function(e){return Promise.reject(e)}),t}(e),n=e.username,o=e.password,i=e.otcsticket;if(i)t.defaults.headers.common.OTCSTicket=i;else{if(!n||!o)throw Error("You must provide an otcsticket or username and password.");t.interceptors.request.use(function(t){try{var i;return null!=(i=t.headers.common)&&i.OTCSTicket?Promise.resolve(t):Promise.resolve(r.default.post(e.baseUrl+"/api/v1/auth/",{username:n,password:o},{headers:{"Content-Type":"multipart/form-data"}})).then(function(e){return t.headers.OTCSTicket=e.data.ticket,t})}catch(e){return Promise.reject(e)}})}return t}(e)}var t=e.prototype;return t.rpcClient=function(e){return void 0===e&&(e="/api/v1/rh/rpc/rhnode/"),new v(this,e)},t._isObject=function(e){return e&&"object"==typeof e&&e.constructor===Object},t._isString=function(e){return"string"==typeof e||e instanceof String},t._isBoolean=function(e){return"boolean"==typeof e},t._isFile=function(e){return"string"==typeof(null==e?void 0:e.name)},t.putForm=function(e,t){return this.put(e,this.objectToForm(t),{headers:{"Content-Type":"multipart/form-data"}})},t.postForm=function(e,t){return this.post(e,this.objectToForm(t),{headers:{"Content-Type":"multipart/form-data"},maxBodyLength:Infinity})},t.patchForm=function(e,t){return this.patch(e,this.objectToForm(t),{headers:{"Content-Type":"multipart/form-data"}})},t.deleteForm=function(e,t){return this.delete(e)},t.objectToForm=function(e){var t=this;return Object.entries(e).reduce(function(e,r){var n,o=r[1];return u({},e,((n={})[r[0]]=Array.isArray(o)||t._isObject(o)?JSON.stringify(o):o,n))},{})},t.putBody=function(e,t){return this.putForm(e,{body:t})},t.postBody=function(e,t){return this.postForm(e,{body:t})},t.patchBody=function(e,t){return this.patchForm(e,{body:t})},t.deleteBody=function(e,t){return this.deleteForm(e,{body:t})},t.get=function(e,t){return this.axios.get(e,t)},t.post=function(e,t,r){return this.axios.post(e,t,r)},t.put=function(e,t,r){return this.axios.put(e,t,r)},t.patch=function(e,t,r){return this.axios.patch(e,t,r)},t.options=function(e,t){return this.axios.options(e,t)},t.delete=function(e,t){return this.axios.delete(e,t)},t.$get=function(e,t){try{return Promise.resolve(this.get(e,t)).then(function(e){return e.data})}catch(e){return Promise.reject(e)}},s(e,[{key:"nodes",get:function(){return void 0===this._nodes&&(this._nodes=new w(this)),this._nodes}},{key:"auth",get:function(){return void 0===this._auth&&(this._auth=new y(this)),this._auth}},{key:"workflow",get:function(){return void 0===this._workflow&&(this._workflow=new _(this)),this._workflow}},{key:"rhcore",get:function(){return void 0===this._rhcore&&(this._rhcore=new g(this)),this._rhcore}},{key:"members",get:function(){return void 0===this._members&&(this._members=new k(this)),this._members}},{key:"search",get:function(){return void 0===this._search&&(this._search=new j(this)),this._search}},{key:"webreports",get:function(){return void 0===this._webreports&&(this._webreports=new F(this)),this._webreports}},{key:"versions",get:function(){return void 0===this._versions&&(this._versions=new O(this)),this._versions}}])}();exports.RPCClient=v,exports.RPCError=h,exports.Session=S,exports.isRPCError=function(e){return void 0!==e.code&&void 0!==e.data};
//# sourceMappingURL=cs-rest.cjs.map