processmaker-builder
Version:
The gulp task runner for ProcessMaker building
1 lines • 12.4 kB
JavaScript
var RCBase64={keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t,r,s,i,o,n,a,h="",u=0;for(e=this.utf8_encode(e);u<e.length;)t=e.charCodeAt(u++),r=e.charCodeAt(u++),s=e.charCodeAt(u++),i=t>>2,o=(3&t)<<4|r>>4,n=(15&r)<<2|s>>6,a=63&s,isNaN(r)?n=a=64:isNaN(s)&&(a=64),h=h+this.keyStr.charAt(i)+this.keyStr.charAt(o)+this.keyStr.charAt(n)+this.keyStr.charAt(a);return h},decode:function(e){var t,r,s,i,o,n,a,h="",u=0;for(e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");u<e.length;)i=this.keyStr.indexOf(e.charAt(u++)),o=this.keyStr.indexOf(e.charAt(u++)),n=this.keyStr.indexOf(e.charAt(u++)),a=this.keyStr.indexOf(e.charAt(u++)),t=i<<2|o>>4,r=(15&o)<<4|n>>2,s=(3&n)<<6|a,h+=String.fromCharCode(t),64!==n&&(h+=String.fromCharCode(r)),64!==a&&(h+=String.fromCharCode(s));return h=this.utf8_decode(h)},utf8_encode:function(e){e=e.replace(/\r\n/g,"\n");var t,r,s="";for(t=0;t<e.length;t++)r=e.charCodeAt(t),128>r?s+=String.fromCharCode(r):r>127&&2048>r?(s+=String.fromCharCode(r>>6|192),s+=String.fromCharCode(63&r|128)):(s+=String.fromCharCode(r>>12|224),s+=String.fromCharCode(r>>6&63|128),s+=String.fromCharCode(63&r|128));return s},utf8_decode:function(e){for(var t="",r=0,s=0,i=0,o=0;r<e.length;)s=e.charCodeAt(r),128>s?(t+=String.fromCharCode(s),r++):s>191&&224>s?(i=e.charCodeAt(r+1),t+=String.fromCharCode((31&s)<<6|63&i),r+=2):(i=e.charCodeAt(r+1),o=e.charCodeAt(r+2),t+=String.fromCharCode((15&s)<<12|(63&i)<<6|63&o),r+=3);return t}},RestClient;if(RestClient=function(){this.VERSION="0.1.7\n",this.authorization={},this.server={},this.response={},this.headers={},this.accessToken={},this.autoUseRefreshToken=!0,this.autoStoreAccessToken=!0,this.authorizationType="none",this.contentType="application/json",this.sendOAuthBearerAuthorization=!1,this.dataType="json",this.oauth2NeedsAuthorization=!0,this.expiredAccessTokenMessage="",this.restfulBehavior=!0,this.backupAJAXURL=null,this.acceptType=null,this.RESTMethods={create:"POST",read:"GET",update:"PUT","delete":"DELETE"},this.OAUTH2GrantTypes={code:"authorization_code",implicit:"token",user:"password",client:"client_credentials",refresh:"refresh_token"},RestClient.prototype.initObject.call(this)},RestClient.prototype.HTTP_SUCCESS=["200","201","202","204","207"],RestClient.prototype.HTTP_BAD_REQUEST=400,RestClient.prototype.HTTP_UNAUTHORIZED=401,RestClient.prototype.OAUTH2_INVALID_GRANT="invalid_grant",RestClient.prototype.initObject=function(){this.authorization={},this.server={},this.response={},this.headers={},this.accessToken={},this.autoUseRefreshToken=!0,this.autoStoreAccessToken=!0,this.authorizationType="none",this.contentType="application/json",this.acceptType="application/json",this.sendOAuthBearerAuthorization=!1,this.oauth2NeedsAuthorization=!0,this.dataType="json",this.expiredAccessTokenMessage="The access token provided has expired."},RestClient.prototype.setUseRefreshTokenAutomatically=function(e){return _.isBoolean(e)&&(this.autoUseRefreshToken=e),this},RestClient.prototype.setStoreAccessTokenAutomatically=function(e){return _.isBoolean(e)&&(this.autoStoreAccessToken=e),this},RestClient.prototype.setAuthorizationType=function(e){var t={none:1,basic:1,oauth2:1};return t[e]&&(this.authorizationType=e),this},RestClient.prototype.setContentType=function(e){return this.contentType=e,this},RestClient.prototype.setAcceptType=function(e){var t={plain:"text/plain",xhtml_xml:"application/xhtml+xml",json:"application/json",xml:"application/xml",all:"*/*"};return t[e]&&(this.acceptType=t[e]),this},RestClient.prototype.setSendBearerAuthorization=function(e){return _.isBoolean(e)&&(this.sendOAuthBearerAuthorization=e),this},RestClient.prototype.setOAuth2NeedsAuthorization=function(e){return _.isBoolean(e)&&(this.oauth2NeedsAuthorization=e),this},RestClient.prototype.setDataType=function(e){var t={json:"application/json",plain:"text/plain",form:"application/x-www-form-urlencoded",html:"text/html"};return t[e]&&(this.dataType=e,this.contentType=t[e]),this},RestClient.prototype.setAccessTokenExpiredMessage=function(e){return this.expiredAccessTokenMessage=e,this},RestClient.prototype.getVersion=function(){return this.VERSION},RestClient.prototype.setClient=function(e,t,r){return this.authorization.client_id=e,this.authorization.client_secret=t,this.authorization.client_url="undefined"!==r?r:null,this},RestClient.prototype.setGrantType=function(e,t){return this.authorization.grant_type="undefined"!==this.OAUTH2GrantTypes[e]?this.OAUTH2GrantTypes[e]:null,this.authorization=_.extend(this.authorization,t),this},RestClient.prototype.setAuthorizationServer=function(e){var t,r=!0;return"undefined"==typeof e||null===e?r=!1:(t=/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/,e.match(t)?this.server.rest_auth_uri=e:r=!1),r},RestClient.prototype.setHeader=function(e,t){var r,s=!0;return e&&t?(r=JSON.parse('{"'+e+'" : "'+t+'"}'),this.headers=_.extend(this.headers,r)):s=!1,s},RestClient.prototype.JSONParse=function(e){var t;try{t=e?""===e?"":JSON.parse(e):""}catch(r){throw new Error("Parsing error",r)}return t},RestClient.prototype.setBasicCredentials=function(e,t){return this.authorization.basic_user=e,this.authorization.basic_password=t,this},RestClient.prototype.setAccessToken=function(e){return"object"==typeof e&&(this.accessToken=e),this},RestClient.prototype.setRestfulBehavior=function(e){return _.isBoolean(e)&&(this.restfulBehavior=e),this},RestClient.prototype.setBackupAjaxUrl=function(e){return this.backupAJAXURL=e,this},RestClient.prototype.toParams=function(e){var t=_.keys(e),r=[];return _.each(t,function(t){r.push(t+"="+e[t])}),r.join("&")},RestClient.prototype.prepareBody=function(e){var t="";return"json"===this.dataType||"jsonp"===this.dataType?"object"==typeof e&&(t=JSON.stringify(e)):t=this.toParams(e),t},RestClient.prototype.createXHR=function(){var e;if(window.XMLHttpRequest)e=new XMLHttpRequest;else try{e=new ActiveXObject("MSXML2.XMLHTTP")}catch(t){try{e=new ActiveXObject("Microsoft.XMLHTTP")}catch(r){}}return e?e:!1},RestClient.prototype.authorize=function(e){var t,r,s,i,o=this,n=!1,a="create",h=this.RESTMethods[a];t=RCBase64.encode(this.authorization.client_id+":"+this.authorization.client_secret),r=this.createXHR();try{r.open(h,this.server.rest_auth_uri,!1)}catch(u){return e.xhrfailure?e.xhrfailure(u,{}):this.XHRFailure(u,{}),n}if(r.onreadystatechange=function(){if(e.ready?e.ready(r):o.AuthorizeReady(r),4===r.readyState)if(-1!=o.HTTP_SUCCESS.indexOf(String(r.status)))try{i=o.JSONParse(r.responseText),o.autoStoreAccessToken&&(o.accessToken=i.token||{}),n=!0,e.success?e.success(r,i):o.AuthorizeSuccess(r,i)}catch(t){i={success:!1,error:{error:o.HTTP_BAD_REQUEST,error_description:"Response is not a valid JSON"}},e.failure?e.failure(r,i):o.AuthorizeFailure(r,i)}else{i={};try{i=o.JSONParse(r.responseText)}catch(s){}e.failure?e.failure(r,i):o.AuthorizeFailure(r,i)}},s={},this.authorization.grant_type)switch(s.grant_type=this.authorization.grant_type,this.authorization.grant_type){case"authorization_code":s.code=this.authorization.code;break;case"token":s.token=this.authorization.token;break;case"password":s.username=this.authorization.username,s.password=this.authorization.password;break;case"client_credentials":s.client_id=this.authorization.client_id,s.client_id=this.authorization.client_secret;break;case"refresh_token":s.refresh_token=this.authorization.refresh_token}return this.oauth2NeedsAuthorization&&r.setRequestHeader("Authorization","Basic "+t),r.setRequestHeader("Accept",this.acceptType),r.setRequestHeader("Content-Type",this.contentType),_.each(this.headers,function(e,t){r.setRequestHeader(t,e)}),r.send(this.prepareBody(s)),n},RestClient.prototype.prepareReqFields=function(e){var t;return t={success:!1,error:{error:this.HTTP_BAD_REQUEST,error_description:"Required fields not found"},fields:e}},RestClient.prototype.prepareConsumeUrl=function(e,t,r,s){var i,o,n=this.acceptType,a=this.contentType,h=!1;if(this.restfulBehavior)switch(e){case"read":i=t,r&&(i+=r),"oauth2"!==this.authorizationType||this.sendOAuthBearerAuthorization||(h=!0,i+="?access_token="+this.accessToken.access_token),s&&s!=={}&&(i+=h?"&":"?",i+=this.toParams(s)),o=null,null===a&&(a="application/json");break;case"create":i=t,o=s||{},"oauth2"!==this.authorizationType||this.sendOAuthBearerAuthorization||(o.access_token=this.accessToken.access_token),o=this.prepareBody(o);break;case"update":i=t,r&&(i+=r),o=s||{},"oauth2"!==this.authorizationType||this.sendOAuthBearerAuthorization||(o.access_token=this.accessToken.access_token),o=this.prepareBody(o);break;case"delete":i=t,r&&(i+=r),o=s||{},"oauth2"!==this.authorizationType||this.sendOAuthBearerAuthorization||(o.access_token=this.accessToken.access_token),o=this.prepareBody(o)}else i=this.backupAJAXURL,o={operation:e,url:t,id:r,data:s},o="data='"+encodeURIComponent(JSON.stringify(o))+"'",null===a&&(a="application/json");return n||(n="*/*"),{url:i,body:o,content_type:a,acceptType:n}},RestClient.prototype.getCall=function(e){return e.operation="read",this.consume(e)},RestClient.prototype.postCall=function(e){return e.operation="create",this.consume(e)},RestClient.prototype.putCall=function(e){return e.operation="update",this.consume(e)},RestClient.prototype.deleteCall=function(e){return e.operation="delete",this.consume(e)},RestClient.prototype.consume=function(e){var t,r,s,i,o,n,a,h,u,c,p,l,d,f,y,T={},C=!0,R=[],A=!1;if(e.operation?s=e.operation:(C=!1,R.push("operation")),e.url?u=e.url:(C=!1,R.push("url")),c=e.data||null,p=e.id||null,!C)return e.failure?e.failure(null,this.prepareReqFields(R)):this.ConsumeFailure(null,this.prepareReqFields(R)),C;l=this.prepareConsumeUrl(s,u,p,c),n=l.url,o=l.body,f=l.content_type,y=l.acceptType,r=this.createXHR(),i=this.restfulBehavior?this.RESTMethods[s]:this.RESTMethods.create;try{switch(r.open(i,n,!1),this.authorizationType){case"none":break;case"basic":t=RCBase64.encode(this.authorization.basic_user+":"+this.authorization.basic_password),r.setRequestHeader("Authorization","Basic "+t);break;case"oauth2":if(!this.accessToken.access_token)return C=!1,R.push("access_token"),h={success:!1,error:{error:this.HTTP_BAD_REQUEST,error_description:"Access Token not defined"}},e.failure?e.failure(null,this.prepareReqFields(R)):this.ConsumeFailure(null,this.prepareReqFields(R)),C;this.sendOAuthBearerAuthorization&&(d="Bearer "+this.accessToken.access_token,r.setRequestHeader("Authorization",d))}}catch(k){return e.xhrfailure?e.xhrfailure(k,c):this.XHRFailure(k,c),!1}return a=this,r.onreadystatechange=function(){if(e.ready?e.ready(r):a.ConsumeReady(r),4===r.readyState)if(-1!=a.HTTP_SUCCESS.indexOf(String(r.status))){try{T=a.JSONParse(r.responseText)}catch(t){T={success:!1,error:{error:a.HTTP_BAD_REQUEST,error_description:"Response is not a valid JSON"}},e.failure?e.failure(r,T):a.ConsumeFailure(r,T)}e.success?e.success(r,T):a.ConsumeSuccess(r,T)}else try{if(T=a.JSONParse(r.responseText),T.error===a.OAUTH2_INVALID_GRANT&&T.error_description===a.expiredAccessTokenMessage&&(A=!0),r.status===a.HTTP_UNAUTHORIZED&&a.autoUseRefreshToken&&A)a.accessToken.refresh_token?(a.setGrantType("refresh",{refresh_token:a.accessToken.refresh_token}),a.authorize({success:function(){C=a.consume(e),C&&(e.autorefresh?e.autorefresh(a.accessToken):a.AuthorizeAutoRefresh(a.accessToken))},failure:function(t,r){C=!1,e.failure?e.failure(null,r):a.ConsumeFailure(null,r)}})):(C=!1,T={success:!1,error:{error:a.HTTP_UNAUTHORIZED,error_description:"Refresh token is not defined"}},e.failure?e.failure(r,T):a.ConsumeFailure(r,T));else{C=!1,T={};try{T=a.JSONParse(r.responseText)}catch(s){}e.failure?e.failure(r,T):a.ConsumeFailure(r,T)}}catch(s){C=!1,T={};try{T=a.JSONParse(r.responseText)}catch(t){}e.failure?e.failure(r,T):a.ConsumeFailure(r,T)}},r.setRequestHeader("Accept",y),r.setRequestHeader("Content-Type",f),_.each(this.headers,function(e,t){r.setRequestHeader(t,e)}),r.send(o),C},RestClient.prototype.XHRFailure=function(){},RestClient.prototype.AuthorizeSuccess=function(){},RestClient.prototype.AuthorizeFailure=function(){},RestClient.prototype.AuthorizeReady=function(){},RestClient.prototype.AuthorizeAutoRefresh=function(){},RestClient.prototype.ConsumeSuccess=function(){},RestClient.prototype.ConsumeFailure=function(){},RestClient.prototype.ConsumeReady=function(){},"undefined"!=typeof exports){module.exports={RestClient:RestClient,RCBase64:RCBase64};var _=require("underscore")}