Last active
March 14, 2019 16:42
-
-
Save yahyaerturan/5705786f171244b603b17aac3c333810 to your computer and use it in GitHub Desktop.
VayesRequest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
class VayesRequest { | |
constructor(uri, data) { | |
this.debug(VayesRequest.staticStrings.init); | |
this.supports(); | |
this.xhrRequest = null; | |
this.contentType = false; | |
this.post = null; | |
this.method = null; | |
this.locale = null; | |
this.xhrBeforeCallback = null; | |
this.xhrDoneCallback = null; | |
this.xhrFailCallback = null; | |
this.xhrAlwaysCallback = null; | |
this.uri = uri; | |
this.data = data; | |
this.validate(); | |
this.prepareData(); | |
this.setLocale(); | |
this._setMethod(); | |
this.httpMethodOverride(); | |
this.handleDataIfMethodNotGetOrPost(); | |
} | |
debug(context) { | |
console.log(context); | |
} | |
validate() { | |
if(typeof this.uri === "undefined") { | |
throw new Error(VayesRequest.staticStrings.invalidUri); | |
} | |
if(typeof this.data === "undefined") { | |
throw new Error(VayesRequest.staticStrings.invalidData); | |
} | |
return true; | |
} | |
/** | |
* Call like this.staticStrings | |
*/ | |
static get staticStrings() { | |
return { | |
init: "VayesRequest instantiated.", | |
notSupportedMessage: "Your browser does not support the new technologies. To proceed, please use one of the modern browsers. Exiting...", | |
invalidUri: 'You should define a valid "uri" parameter @ VayesRequest constructor.', | |
invalidData: 'You should define a valid "data" parameter @ VayesRequest constructor.', | |
METHOD_GET: "GET", | |
METHOD_POST: "POST", | |
METHOD_PUT: "PUT", | |
METHOD_PATCH: "PATCH", | |
METHOD_DELETE: "DELETE", | |
METHOD_FEED: "FEED", | |
} | |
} | |
supports() { | |
if(!("FormData" in window)){ | |
alert(VayesRequest.staticStrings.notSupportedMessage); | |
throw VayesRequest.staticStrings.notSupportedMessage; | |
} | |
} | |
prepareData() { | |
if(typeof this.data.get === "function") { | |
this.post = this.data; | |
} else { | |
this.post = new FormData(); | |
for (var key of Object.keys(this.data)) { | |
this.post.append(key,this.data[key]) | |
} | |
} | |
} | |
setLocale() { | |
if(typeof this.data.get === "function") { | |
this.post.append("_locale", window._locale); | |
} else { | |
if(!this.post.has("_locale")) { | |
this.post.append("_locale", window._locale) | |
} | |
} | |
this.locale = window._locale; | |
} | |
httpMethodOverride() { | |
if(this.post.has("_method")) | |
{ | |
this.setMethod(this.post.get("_method").toUpperCase()); | |
} | |
} | |
/** | |
* Automatically set request method initially | |
* @private | |
*/ | |
_setMethod() { | |
if(typeof this.method !== "string") | |
{ | |
this.setMethod(VayesRequest.staticStrings.METHOD_POST); | |
} | |
} | |
/** | |
* Set method explicitly | |
*/ | |
setMethod(verb) { | |
this.method = verb.toUpperCase(); | |
} | |
handleDataIfMethodNotGetOrPost() { | |
if($.inArray(this.method, ["GET","POST"]) === -1) { | |
let post = Array.from(this.post.entries()).reduce((memo, pair) => ({ | |
...memo, | |
[pair[0]]: pair[1], | |
}), {}); | |
this.post = JSON.stringify(post); | |
this.contentType = "json"; | |
} | |
} | |
handleCallbackFallbacks() { | |
this.getBeforeCallbackFalback(); | |
this.getDoneCallbackFallback(); | |
this.getFailCallbackFallback(); | |
this.getAlwaysCallbackFallback(); | |
} | |
send() { | |
this._fire(); | |
} | |
_fire() { | |
this.handleCallbackFallbacks(); | |
var method = this.method; | |
var uri = this.uri; | |
var contentType = this.contentType; | |
this.xhrRequest = $.ajax({ | |
method: method, | |
url: uri, | |
contentType: contentType, | |
processData: false, | |
data: this.post, | |
beforeSend: (xhr) => this.xhrBeforeCallback(xhr) | |
}); | |
this.xhrRequest.done( | |
(xhr) => this.xhrDoneCallback(xhr) | |
); | |
this.xhrRequest.fail( | |
(xhr) => this.xhrFailCallback(xhr) | |
); | |
this.xhrRequest.always( | |
(xhr) => this.xhrAlwaysCallback(xhr) | |
); | |
} | |
setBeforeCallback(callback) { | |
this.xhrBeforeCallback = (xhr) => callback(xhr); | |
} | |
getBeforeCallbackFalback() { | |
if(typeof this.xhrBeforeCallback !== "function") { | |
this.xhrBeforeCallback = function (xhr) { | |
alert("beforeCal"); | |
} | |
} | |
} | |
setDoneCallback(callback) { | |
this.xhrDoneCallback = (xhr) => callback(xhr); | |
} | |
getDoneCallbackFallback() { | |
if(typeof this.xhrDoneCallback !== "function") { | |
this.xhrDoneCallback = function (xhr) { | |
alert("doneCal"); | |
} | |
} | |
} | |
setFailCallback(callback) { | |
this.xhrFailCallback = (xhr) => callback(xhr); | |
} | |
getFailCallbackFallback() { | |
if(typeof this.xhrFailCallback !== "function") { | |
this.xhrFailCallback = function (xhr) { | |
alert("failCal"); | |
} | |
} | |
} | |
setAlwaysCallback(callback) { | |
this.xhrAlwaysCallback = (xhr) => callback(xhr); | |
} | |
getAlwaysCallbackFallback() { | |
if(typeof this.xhrAlwaysCallback !== "function") { | |
this.xhrAlwaysCallback = function (xhr) { | |
alert("alwaysCal"); | |
} | |
} | |
} | |
} | |
export default VayesRequest; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment