ozone-api-request
Advanced tools
Comparing version 5.0.0 to 5.1.0
@@ -76,4 +76,4 @@ "use strict"; | ||
*/ | ||
class OzoneAPIRequest { | ||
constructor() { | ||
var OzoneAPIRequest = /** @class */ (function () { | ||
function OzoneAPIRequest() { | ||
this._url = ''; | ||
@@ -97,43 +97,67 @@ this._method = 'GET'; | ||
*/ | ||
static setup(option) { | ||
OzoneAPIRequest.setup = function (option) { | ||
OzoneAPIRequest.option = option; | ||
} | ||
/** | ||
* Resolve with current XMLHttpRequest on achieved | ||
*/ | ||
get result() { | ||
if (this._resultPromise) | ||
return this._resultPromise; | ||
else | ||
throw new Error("Request has not been send"); | ||
} | ||
set url(url) { | ||
this._url = url; | ||
} | ||
get url() { | ||
let url = this._url; | ||
if (!OzoneAPIRequest.option.cache && this.method === 'GET') { | ||
const [urlPath, param] = url.split('?'); | ||
const urlParam = new URLSearchParams(param); | ||
urlParam.append('_', Date.now().toString()); | ||
url = [urlPath, urlParam.toString()].join('?'); | ||
} | ||
return url; | ||
} | ||
set body(body) { | ||
this._body = body; | ||
} | ||
get body() { return this._body; } | ||
set method(method) { | ||
this._method = method; | ||
} | ||
get method() { return this._method; } | ||
set responseType(responseType) { | ||
this._responseType = responseType; | ||
} | ||
get responseType() { return this._responseType; } | ||
set onreadystatechange(callback) { | ||
this._onreadystatechange = callback; | ||
} | ||
abort() { | ||
}; | ||
Object.defineProperty(OzoneAPIRequest.prototype, "result", { | ||
/** | ||
* Resolve with current XMLHttpRequest on achieved | ||
*/ | ||
get: function () { | ||
if (this._resultPromise) | ||
return this._resultPromise; | ||
else | ||
throw new Error("Request has not been send"); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(OzoneAPIRequest.prototype, "url", { | ||
get: function () { | ||
var url = this._url; | ||
if (!OzoneAPIRequest.option.cache && this.method === 'GET') { | ||
var _a = url.split('?'), urlPath = _a[0], param = _a[1]; | ||
var urlParam = new URLSearchParams(param); | ||
urlParam.append('_', Date.now().toString()); | ||
url = [urlPath, urlParam.toString()].join('?'); | ||
} | ||
return url; | ||
}, | ||
set: function (url) { | ||
this._url = url; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(OzoneAPIRequest.prototype, "body", { | ||
get: function () { return this._body; }, | ||
set: function (body) { | ||
this._body = body; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(OzoneAPIRequest.prototype, "method", { | ||
get: function () { return this._method; }, | ||
set: function (method) { | ||
this._method = method; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(OzoneAPIRequest.prototype, "responseType", { | ||
get: function () { return this._responseType; }, | ||
set: function (responseType) { | ||
this._responseType = responseType; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(OzoneAPIRequest.prototype, "onreadystatechange", { | ||
set: function (callback) { | ||
this._onreadystatechange = callback; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
OzoneAPIRequest.prototype.abort = function () { | ||
if (this.currentRequest) | ||
@@ -143,9 +167,13 @@ this.currentRequest.abort(); | ||
this.resolveCurrentRequest(this.currentRequest); | ||
} | ||
get readyState() { | ||
if (this.currentRequest) | ||
return this.currentRequest.readyState; | ||
else | ||
return 0; | ||
} | ||
}; | ||
Object.defineProperty(OzoneAPIRequest.prototype, "readyState", { | ||
get: function () { | ||
if (this.currentRequest) | ||
return this.currentRequest.readyState; | ||
else | ||
return 0; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
/** | ||
@@ -155,4 +183,5 @@ * Create and open an XMLHttpRequest | ||
*/ | ||
createXMLHttpRequest(withHeader = true) { | ||
const xmlhttp = new XMLHttpRequest(); | ||
OzoneAPIRequest.prototype.createXMLHttpRequest = function (withHeader) { | ||
if (withHeader === void 0) { withHeader = true; } | ||
var xmlhttp = new XMLHttpRequest(); | ||
xmlhttp.withCredentials = true; | ||
@@ -166,7 +195,7 @@ xmlhttp.open(this.method, this.url, true); | ||
return xmlhttp; | ||
} | ||
setEventTarget(element) { | ||
}; | ||
OzoneAPIRequest.prototype.setEventTarget = function (element) { | ||
this.eventTarget = element; | ||
} | ||
dispatchEvent(eventName, xmlhttp) { | ||
}; | ||
OzoneAPIRequest.prototype.dispatchEvent = function (eventName, xmlhttp) { | ||
if (this.eventTarget) { | ||
@@ -177,3 +206,3 @@ this.eventTarget.dispatchEvent(new CustomEvent(eventName, { | ||
} | ||
} | ||
}; | ||
/** | ||
@@ -184,19 +213,20 @@ * | ||
*/ | ||
send(request) { | ||
const xmlhttp = this.currentRequest = request || this.createXMLHttpRequest(); | ||
OzoneAPIRequest.prototype.send = function (request) { | ||
var _this = this; | ||
var xmlhttp = this.currentRequest = request || this.createXMLHttpRequest(); | ||
if (this._onreadystatechange) | ||
xmlhttp.onreadystatechange = this._onreadystatechange; | ||
this._resultPromise = new Promise((resolve, reject) => { | ||
this.resolveCurrentRequest = resolve; | ||
const handleResponse = () => { | ||
this._resultPromise = new Promise(function (resolve, reject) { | ||
_this.resolveCurrentRequest = resolve; | ||
var handleResponse = function () { | ||
if (xmlhttp.status >= 200 && xmlhttp.status < 300) { | ||
this.dispatchEvent('ozone-api-request-success', xmlhttp); | ||
_this.dispatchEvent('ozone-api-request-success', xmlhttp); | ||
resolve(xmlhttp); | ||
} | ||
else if (xmlhttp.status === 403) { | ||
this.dispatchEvent('ozone-api-request-unauthorized', xmlhttp); | ||
_this.dispatchEvent('ozone-api-request-unauthorized', xmlhttp); | ||
reject(xmlhttp); | ||
} | ||
else { | ||
this.dispatchEvent('ozone-api-request-error', xmlhttp); | ||
_this.dispatchEvent('ozone-api-request-error', xmlhttp); | ||
reject(xmlhttp); | ||
@@ -206,11 +236,11 @@ } | ||
xmlhttp.onload = handleResponse; | ||
xmlhttp.ontimeout = () => { | ||
this.dispatchEvent('ozone-api-request-timeout', xmlhttp); | ||
xmlhttp.ontimeout = function () { | ||
_this.dispatchEvent('ozone-api-request-timeout', xmlhttp); | ||
reject(xmlhttp); | ||
}; | ||
xmlhttp.onerror = handleResponse; | ||
xmlhttp.send(this.body); | ||
xmlhttp.send(_this.body); | ||
}); | ||
return this; | ||
} | ||
}; | ||
/** | ||
@@ -221,9 +251,10 @@ * | ||
*/ | ||
sendRequest(request) { | ||
OzoneAPIRequest.prototype.sendRequest = function (request) { | ||
return this.send(request).result; | ||
} | ||
} | ||
OzoneAPIRequest.option = { | ||
cache: true | ||
}; | ||
}; | ||
OzoneAPIRequest.option = { | ||
cache: true | ||
}; | ||
return OzoneAPIRequest; | ||
}()); | ||
exports.OzoneAPIRequest = OzoneAPIRequest; |
{ | ||
"name": "ozone-api-request", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"publishConfig": { | ||
@@ -25,3 +25,3 @@ "access": "public" | ||
}, | ||
"gitHead": "28a7a70caa6c80c22ba6d67b867a55c94a123326" | ||
"gitHead": "d11ca259166381d398811f4a84a7d859b0b19202" | ||
} |
{ | ||
"compilerOptions": { | ||
"target": "es6", | ||
"target": "es5", | ||
"module": "commonjs", | ||
@@ -23,2 +23,2 @@ "moduleResolution": "node", | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24413
633