UNPKG

@salaxy/jquery

Version:

Salaxy general plain JavaScript / TypeScript libraries with JQuery -ajax component (Palkkaus.fi)

2 lines (1 loc) 2.71 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@salaxy/core")):"function"==typeof define&&define.amd?define(["exports","@salaxy/core"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).salaxy=e.salaxy||{},e.salaxy.jquery={}),e.salaxy.core)}(this,(function(e,t){"use strict";e.AjaxJQuery=class{constructor(){this.useCookie=!0,this.useCredentials=!1,this.serverAddress="https://test-api.salaxy.com";const e=t.Configs.current;e&&(e.apiServer&&(this.serverAddress=e.apiServer),null!=e.useCredentials&&(this.useCredentials=e.useCredentials),null!=e.useCookie&&(this.useCookie=e.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.Cookies).get("salaxy-token")||""),this.token}setCurrentToken(e){this.useCookie&&(new t.Cookies).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 e=t.Configs.global.jQuery;if(e)return e;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}},Object.defineProperty(e,"__esModule",{value:!0})}));