@salaxy/jquery
Version:
Salaxy general plain JavaScript / TypeScript libraries with JQuery -ajax component (Palkkaus.fi)
3 lines (2 loc) • 2.41 kB
JavaScript
import{Configs as e,Cookies as t}from"@salaxy/core";class r{constructor(){this.useCookie=!0,this.useCredentials=!1,this.serverAddress="https://test-api.salaxy.com";const t=e.current;t&&(t.apiServer&&(this.serverAddress=t.apiServer),null!=t.useCredentials&&(this.useCredentials=t.useCredentials),null!=t.useCookie&&(this.useCookie=t.useCookie))}getApiAddress(){return this.serverAddress+"/v02/api"}getServerAddress(){return this.serverAddress}getJSON(e){const t=this.getCurrentToken();return this.getJQuery().ajax({dataType:"json",url:this.getUrl(e),xhrFields:{withCredentials:!t&&this.useCredentials},beforeSend:e=>{t&&e.setRequestHeader("Authorization","Bearer "+t)}})}getHTML(e){const t=this.getCurrentToken();return this.getJQuery().ajax({dataType:"html",url:this.getUrl(e),xhrFields:{withCredentials:!t&&this.useCredentials},beforeSend:e=>{t&&e.setRequestHeader("Authorization","Bearer "+t)}})}postJSON(e,t){const r=this.getCurrentToken();return this.getJQuery().ajax({type:"POST",url:this.getUrl(e),data:t,dataType:"json",xhrFields:{withCredentials:!r&&this.useCredentials},beforeSend:e=>{r&&e.setRequestHeader("Authorization","Bearer "+r)}})}postHTML(e,t){const r=this.getCurrentToken();return this.getJQuery().ajax({type:"POST",url:this.getUrl(e),data:t,dataType:"html",xhrFields:{withCredentials:!r&&this.useCredentials},beforeSend:e=>{r&&e.setRequestHeader("Authorization","Bearer "+r)}})}remove(e){const t=this.getCurrentToken();return this.getJQuery().ajax({type:"DELETE",url:this.getUrl(e),xhrFields:{withCredentials:!t&&this.useCredentials},beforeSend:e=>{t&&e.setRequestHeader("Authorization","Bearer "+t)}})}getCurrentToken(){return!this.token&&this.useCookie&&(this.token=(new t).get("salaxy-token")||""),this.token}setCurrentToken(e){this.useCookie&&(new t).setCookie("salaxy-token",e||""),this.token=e}resourceOwnerLogin(e,t){return this.getJQuery().ajax({type:"POST",url:this.getServerAddress()+"/oauth2/token",data:{grant_type:"password",username:e,password:t},success:e=>{this.setCurrentToken(e.access_token)},dataType:"json"})}getJQuery(){const t=e.global.jQuery;if(t)return t;throw new Error("ERROR: No jQuery. AjaxJQuery requires jQuery to be referenced.")}getUrl(e){return e&&""!==e.trim()?e.toLowerCase().startsWith("http")?e:e.toLowerCase().startsWith("/v")?this.getServerAddress()+e:this.getApiAddress()+e:null}}export{r as AjaxJQuery};
//# sourceMappingURL=index.esm.min.js.map