@corebos/ws-lib
Version:
CoreBOS Web service client Library
1 lines • 12.3 kB
JavaScript
const fetch=require("node-fetch");var cbWSClient=function(a){this._servicebase="webservice.php",""!=a&&"/"!=a.substr(a.length-1)&&(a+="/"),this._serviceurl=a+this._servicebase,this._serviceuser=!1,this._servicekey=!1,this._servertime=!1,this._expiretime=!1,this._servicetoken=!1,this._sessionid=!1,this._userid=!1,this._lasterror=!1,this.version="coreBOS2.1",this.fetchOptions={mode:"cors",headers:{"Content-type":"application/x-www-form-urlencoded; charset=UTF-8"}},this.getRecordId=function(a){if("undefined"==typeof a)return 0;var b=a.split("x");return b[1]},this.hasError=function(a){return null!=a&&!1==a.success?(this._lasterror=a.error.code+": "+a.error.message,!0):(this._lasterror="none",!1)},this.lastError=function(){return this._lasterror},this.status=function(a){return 200<=a.status&&300>a.status?Promise.resolve(a):Promise.reject(new Error(a.statusText))},this.getData=function(a){return a.json()},this.__doChallenge=function(a){return this.fetchOptions.method="get",fetch(this._serviceurl+("?operation=getchallenge&username="+a),this.fetchOptions).then(this.status).then(this.getData)},this.__checkLogin=function(){return!0},this.doLogin=function(a,b,c){this._serviceuser=a,this._servicekey=b,c==null&&(c=!1);let d=this;return new Promise((e,f)=>{this.__doChallenge(a).then(function(g){if(!1==d.hasError(g)){let h=g.result;d._servicetoken=h.token,d._servertime=h.serverTime,d._expiretime=h.expireTime,d.fetchOptions.method="post";let i="operation=login&username="+a;i+="&accessKey="+(c?d._servicetoken+b:cbMD5(d._servicetoken+b)),d.fetchOptions.body=i,fetch(d._serviceurl,d.fetchOptions).then(d.status).then(d.getData).then(a=>{if(!1==d.hasError(a)){var b=a.result;d._sessionid=b.sessionName,d._userid=b.userId,e(a)}else f(new Error("incorrect response: "+d.lastError()))}).catch(a=>f(a))}else f(new Error("incorrect response: "+d.lastError()))}).catch(a=>f(a))})},this.doLogout=function(){this.__checkLogin();let a="operation=logout&sessionName="+this._sessionid;this.fetchOptions.body=a,this.fetchOptions.method="post";let b=this;return fetch(this._serviceurl,this.fetchOptions).then(this.status).then(this.getData).then(function(a){return!1==b.hasError(a)?(b._servicetoken=!1,b._servertime=!1,b._expiretime=!1,b._sessionid=!1,b._userid=!1,Promise.resolve(a.result)):Promise.reject(new Error("incorrect response: "+b.lastError()))}).catch(function(a){return Promise.reject(a)})},this.extendSession=function(){this.fetchOptions.body="operation=extendsession",this.fetchOptions.credentials="include",this.fetchOptions.method="post";let a=this;return fetch(this._serviceurl,this.fetchOptions).then(this.status).then(this.getData).then(function(b){return!1==a.hasError(b)?(a._sessionid=b.result.sessionName,a._userid=b.result.userId,delete a.fetchOptions.credentials,Promise.resolve(b.result)):Promise.reject(new Error("incorrect response: "+a.lastError()))}).catch(function(a){return Promise.reject(a)})},this.doQuery=function(a){this.__checkLogin(),-1==a.indexOf(";")&&(a+=";");let b="?operation=query&sessionName="+this._sessionid+"&query="+a;this.fetchOptions.method="get",delete this.fetchOptions.body;let c=this;return fetch(this._serviceurl+b,this.fetchOptions).then(this.status).then(this.getData).then(function(a){return!1==c.hasError(a)?Promise.resolve(a.result):Promise.reject(new Error("incorrect response: "+c.lastError()))}).catch(function(a){return Promise.reject(a)})},this.getResultColumns=function(a){let b=[];if(null!=a&&0!=a.length){let c=a[0];for(let a in c)b.push(a)}return b},this.doListTypes=function(){this.__checkLogin();let a="?operation=listtypes&sessionName="+this._sessionid;this.fetchOptions.method="get",delete this.fetchOptions.body;let b=this;return fetch(this._serviceurl+a,this.fetchOptions).then(this.status).then(this.getData).then(function(a){if(!1==b.hasError(a)){let b=a.result,c=b.types,d={};for(let a,b=0;b<c.length;++b)a=c[b],d[a]={name:a};return Promise.resolve(d)}return Promise.reject(new Error("incorrect response: "+b.lastError()))}).catch(function(a){return Promise.reject(a)})},this.doDescribe=function(a){this.__checkLogin();let b="?operation=describe&sessionName="+this._sessionid+"&elementType="+a;this.fetchOptions.method="get",delete this.fetchOptions.body;let c=this;return fetch(this._serviceurl+b,this.fetchOptions).then(this.status).then(this.getData).then(function(a){return!1==c.hasError(a)?Promise.resolve(a.result):Promise.reject(new Error("incorrect response: "+c.lastError()))}).catch(function(a){return Promise.reject(a)})},this.doRetrieve=function(a){this.__checkLogin();let b="?operation=retrieve&sessionName="+this._sessionid+"&id="+a;this.fetchOptions.method="get",delete this.fetchOptions.body;let c=this;return fetch(this._serviceurl+b,this.fetchOptions).then(this.status).then(this.getData).then(function(a){return!1==c.hasError(a)?Promise.resolve(a.result):Promise.reject(new Error("incorrect response: "+c.lastError()))}).catch(function(a){return Promise.reject(a)})},this.doCreate=function(a,b){this.__checkLogin(),null==b.assigned_user_id&&(b.assigned_user_id=this._userid);let c="operation=create&sessionName="+this._sessionid+"&elementType="+a+"&element="+JSON.stringify(b);this.fetchOptions.body=c,this.fetchOptions.method="post";let d=this;return fetch(this._serviceurl,this.fetchOptions).then(this.status).then(this.getData).then(function(a){return!1==d.hasError(a)?Promise.resolve(a.result):Promise.reject(new Error("incorrect response: "+d.lastError()))}).catch(function(a){return Promise.reject(a)})},this.doUpdate=function(a,b){this.__checkLogin(),null==b.assigned_user_id&&(b.assigned_user_id=this._userid);let c="operation=update&sessionName="+this._sessionid+"&elementType="+a+"&element="+JSON.stringify(b);this.fetchOptions.body=c,this.fetchOptions.method="post";let d=this;return fetch(this._serviceurl,this.fetchOptions).then(this.status).then(this.getData).then(function(a){return!1==d.hasError(a)?Promise.resolve(a.result):Promise.reject(new Error("incorrect response: "+d.lastError()))}).catch(function(a){return Promise.reject(a)})},this.doRevise=function(a,b){this.__checkLogin();let c="operation=revise&sessionName="+this._sessionid+"&elementType="+a+"&element="+JSON.stringify(b);this.fetchOptions.body=c,this.fetchOptions.method="post";let d=this;return fetch(this._serviceurl,this.fetchOptions).then(this.status).then(this.getData).then(function(a){return!1==d.hasError(a)?Promise.resolve(a.result):Promise.reject(new Error("incorrect response: "+d.lastError()))}).catch(function(a){return Promise.reject(a)})},this.doDelete=function(a){this.__checkLogin();let b="operation=delete&sessionName="+this._sessionid+"&id="+a;this.fetchOptions.body=b,this.fetchOptions.method="post";let c=this;return fetch(this._serviceurl,this.fetchOptions).then(this.status).then(this.getData).then(function(a){return!1==c.hasError(a)?Promise.resolve(a.result):Promise.reject(new Error("incorrect response: "+c.lastError()))}).catch(function(a){return Promise.reject(a)})},this.doInvoke=function(a,b,c){this.__checkLogin(),"undefined"==typeof b&&(b={});var d="post";"undefined"!=typeof c&&(d=c.toUpperCase());let e="operation="+a+"&sessionName="+this._sessionid;for(let d in b)e+="&"+d+"="+b[d];let f="";"post"==d.toLowerCase()?this.fetchOptions.body=e:(delete this.fetchOptions.body,f="?"+e),this.fetchOptions.method=d;let g=this;return fetch(this._serviceurl+f,this.fetchOptions).then(this.status).then(this.getData).then(function(a){return!1==g.hasError(a)?Promise.resolve(a.result):Promise.reject(new Error("incorrect response: "+g.lastError()))}).catch(function(a){return Promise.reject(a)})},this.doGetRelatedRecords=function(a,b,c,d){this.__checkLogin();let e="operation=getRelatedRecords&sessionName="+this._sessionid+"&id="+a+"&module="+b;e+="&relatedModule="+c+"&queryParameters="+JSON.stringify(d),this.fetchOptions.body=e,this.fetchOptions.method="post";let f=this;return fetch(this._serviceurl,this.fetchOptions).then(this.status).then(this.getData).then(function(a){return!1==f.hasError(a)?Promise.resolve(a.result):Promise.reject(new Error("incorrect response: "+f.lastError()))}).catch(function(a){return Promise.reject(a)})},this.doSetRelated=function(a,b){this.__checkLogin();let c="operation=SetRelation&sessionName="+this._sessionid+"&relate_this_id="+a+"&with_these_ids="+JSON.stringify(b);this.fetchOptions.body=c,this.fetchOptions.method="post";let d=this;return fetch(this._serviceurl,this.fetchOptions).then(this.status).then(this.getData).then(function(a){return!1==d.hasError(a)?Promise.resolve(a.result):Promise.reject(new Error("incorrect response: "+d.lastError()))}).catch(function(a){return Promise.reject(a)})}};module.exports=cbWSClient;var cbMD5=function(a){function b(a,b){return a<<b|a>>>32-b}function c(a,b){var c,e,f,g,h;return f=2147483648&a,g=2147483648&b,c=1073741824&a,e=1073741824&b,h=(1073741823&a)+(1073741823&b),c&e?2147483648^h^f^g:c|e?1073741824&h?3221225472^h^f^g:1073741824^h^f^g:h^f^g}function d(a,b,c){return a&b|~a&c}function r(a,b,c){return a&c|b&~c}function q(a,b,c){return a^b^c}function p(a,b,c){return b^(a|~c)}function k(a,e,f,g,h,i,j){return a=c(a,c(c(d(e,f,g),h),j)),c(b(a,i),e)}function n(a,d,e,f,g,h,i){return a=c(a,c(c(r(d,e,f),g),i)),c(b(a,h),d)}function f(a,d,e,f,g,h,i){return a=c(a,c(c(q(d,e,f),g),i)),c(b(a,h),d)}function u(a,d,e,f,g,h,i){return a=c(a,c(c(p(d,e,f),g),i)),c(b(a,h),d)}function t(a){for(var b,c=a.length,e=c+8,f=16*((e-e%64)/64+1),g=Array(f-1),h=0,i=0;i<c;)b=(i-i%4)/4,h=8*(i%4),g[b]|=a.charCodeAt(i)<<h,i++;return b=(i-i%4)/4,h=8*(i%4),g[b]|=128<<h,g[f-2]=c<<3,g[f-1]=c>>>29,g}function e(a){var b,c,e="",f="";for(c=0;3>=c;c++)b=255&a>>>8*c,f="0"+b.toString(16),e+=f.substr(f.length-2,2);return e}function x(a){var b=String.fromCharCode;a=a.replace(/rn/g,"n");for(var c,e="",f=0;f<a.length;f++)c=a.charCodeAt(f),128>c?e+=b(c):127<c&&2048>c?(e+=b(192|c>>6),e+=b(128|63&c)):(e+=b(224|c>>12),e+=b(128|63&c>>6),e+=b(128|63&c));return e}var B,D,F,G,H,I,J,K,L,Z=[],$=7,S=12,Q=17,N=22,M=5,A=9,z=14,y=20,w=4,o=11,m=16,l=23,j=6,U=10,T=15,R=21;for(a=x(a),Z=t(a),I=1732584193,J=4023233417,K=2562383102,L=271733878,B=0;B<Z.length;B+=16)D=I,F=J,G=K,H=L,I=k(I,J,K,L,Z[B+0],$,3614090360),L=k(L,I,J,K,Z[B+1],S,3905402710),K=k(K,L,I,J,Z[B+2],Q,606105819),J=k(J,K,L,I,Z[B+3],N,3250441966),I=k(I,J,K,L,Z[B+4],$,4118548399),L=k(L,I,J,K,Z[B+5],S,1200080426),K=k(K,L,I,J,Z[B+6],Q,2821735955),J=k(J,K,L,I,Z[B+7],N,4249261313),I=k(I,J,K,L,Z[B+8],$,1770035416),L=k(L,I,J,K,Z[B+9],S,2336552879),K=k(K,L,I,J,Z[B+10],Q,4294925233),J=k(J,K,L,I,Z[B+11],N,2304563134),I=k(I,J,K,L,Z[B+12],$,1804603682),L=k(L,I,J,K,Z[B+13],S,4254626195),K=k(K,L,I,J,Z[B+14],Q,2792965006),J=k(J,K,L,I,Z[B+15],N,1236535329),I=n(I,J,K,L,Z[B+1],M,4129170786),L=n(L,I,J,K,Z[B+6],A,3225465664),K=n(K,L,I,J,Z[B+11],z,643717713),J=n(J,K,L,I,Z[B+0],y,3921069994),I=n(I,J,K,L,Z[B+5],M,3593408605),L=n(L,I,J,K,Z[B+10],A,38016083),K=n(K,L,I,J,Z[B+15],z,3634488961),J=n(J,K,L,I,Z[B+4],y,3889429448),I=n(I,J,K,L,Z[B+9],M,568446438),L=n(L,I,J,K,Z[B+14],A,3275163606),K=n(K,L,I,J,Z[B+3],z,4107603335),J=n(J,K,L,I,Z[B+8],y,1163531501),I=n(I,J,K,L,Z[B+13],M,2850285829),L=n(L,I,J,K,Z[B+2],A,4243563512),K=n(K,L,I,J,Z[B+7],z,1735328473),J=n(J,K,L,I,Z[B+12],y,2368359562),I=f(I,J,K,L,Z[B+5],w,4294588738),L=f(L,I,J,K,Z[B+8],o,2272392833),K=f(K,L,I,J,Z[B+11],m,1839030562),J=f(J,K,L,I,Z[B+14],l,4259657740),I=f(I,J,K,L,Z[B+1],w,2763975236),L=f(L,I,J,K,Z[B+4],o,1272893353),K=f(K,L,I,J,Z[B+7],m,4139469664),J=f(J,K,L,I,Z[B+10],l,3200236656),I=f(I,J,K,L,Z[B+13],w,681279174),L=f(L,I,J,K,Z[B+0],o,3936430074),K=f(K,L,I,J,Z[B+3],m,3572445317),J=f(J,K,L,I,Z[B+6],l,76029189),I=f(I,J,K,L,Z[B+9],w,3654602809),L=f(L,I,J,K,Z[B+12],o,3873151461),K=f(K,L,I,J,Z[B+15],m,530742520),J=f(J,K,L,I,Z[B+2],l,3299628645),I=u(I,J,K,L,Z[B+0],j,4096336452),L=u(L,I,J,K,Z[B+7],U,1126891415),K=u(K,L,I,J,Z[B+14],T,2878612391),J=u(J,K,L,I,Z[B+5],R,4237533241),I=u(I,J,K,L,Z[B+12],j,1700485571),L=u(L,I,J,K,Z[B+3],U,2399980690),K=u(K,L,I,J,Z[B+10],T,4293915773),J=u(J,K,L,I,Z[B+1],R,2240044497),I=u(I,J,K,L,Z[B+8],j,1873313359),L=u(L,I,J,K,Z[B+15],U,4264355552),K=u(K,L,I,J,Z[B+6],T,2734768916),J=u(J,K,L,I,Z[B+13],R,1309151649),I=u(I,J,K,L,Z[B+4],j,4149444226),L=u(L,I,J,K,Z[B+11],U,3174756917),K=u(K,L,I,J,Z[B+2],T,718787259),J=u(J,K,L,I,Z[B+9],R,3951481745),I=c(I,D),J=c(J,F),K=c(K,G),L=c(L,H);var O=e(I)+e(J)+e(K)+e(L);return O.toLowerCase()};