From 8d03d450b05ab933b1722ca17297b87ca6435e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Er=20Galv=C3=A3o=20Abbott?= Date: Tue, 15 Jan 2019 03:43:08 -0200 Subject: [PATCH] Version 1.0.0 - Turning to the class syntax sepcified by ECMAScript2015; Better code documentation (PHPDocumentor style). --- src/JHRW.js | 133 ++++++++++++++++++++++------------- src/example/requestTest.html | 4 +- 2 files changed, 88 insertions(+), 49 deletions(-) diff --git a/src/JHRW.js b/src/JHRW.js index 216a7bf..57095f6 100644 --- a/src/JHRW.js +++ b/src/JHRW.js @@ -1,61 +1,84 @@ /** - * JHRW - JavaScript HTTP Reuqest Wrapper + * JHRW - JavaScript HTTP Request Wrapper * * A wrapper around so-called "AJAX" requests. * * @author Er Galvão Abbott * @link https://github.com/galvao/JHRW + * @license Apache 2.0 */ 'use strict'; -function JHRW(destination, lazyExecution) +class JHRW { - if (typeof destination === 'undefined') { - throw new Error('Destination is a required parameter.'); - } + /** + * Constructor + * + * @param string destination The request's destination + * @param boolean lazyExecution If the request should be immediately initialized and sent + * + * @return object An instance of the JHRW class + * @throws Error If the destination parameter is undefined + */ + constructor(destination, lazyExecution) + { + if (typeof destination === 'undefined') { + throw new Error('Destination is a required parameter.'); + } - lazyExecution = typeof lazyExecution !== 'boolean' ? false : lazyExecution; - - this.availableHandlers = [ - 'loadstart', - 'progress', - 'abort', - 'error', - 'load', - 'timeout', - 'loadend', - 'onreadystatechange' - ]; - - this.config = { - URI : destination, - asynchronous : true, - verb : 'get', - data : null, - requestHeaders : {}, - responseType : 'text/plain', - handlers : {'load': defaultHandler, 'error': defaultHandler}, - attempts : 0, - attemptInterval : 3000, - timeout : 5000 - }; - - this.request = new XMLHttpRequest(); - - if (this.lazy === true) { - this.init(); - this.send(); + lazyExecution = typeof lazyExecution !== 'boolean' ? false : lazyExecution; + + this.availableHandlers = [ + 'loadstart', + 'progress', + 'abort', + 'error', + 'load', + 'timeout', + 'loadend', + 'onreadystatechange' + ]; + + this.config = { + URI : destination, + asynchronous : true, + verb : 'get', + data : null, + requestHeaders : {}, + responseType : 'text/plain', + handlers : {'load': defaultHandler, 'error': defaultHandler}, + attempts : 0, + attemptInterval : 3000, + timeout : 5000 + }; + + this.request = new XMLHttpRequest(); + + if (this.lazy === true) { + this.init(); + this.send(); + } } - this.configure = function (configureObject) { - for (let c in configureObject) { - if ((c in this.config) === false) { + /** + * Configure - Configure the various settings for the request + * + * @param object A configuration object (JSON) + * + * @return void + * @throws ReferenceError If a configuration index inside the configuration object is not defined by the class or + * if a handler defined in the configuration object is not made available. + */ + configure(configureObject) + { + for (let configAttr in configureObject) { + if ((configAttr in this.config) === false) { let configList = Object.getOwnPropertyNames(this.config).join("\n"); - throw new ReferenceError(c + " is not a config attribute.\nAccepted config attributes:\n" + configList); + throw new ReferenceError(configAttr + " is not a config attribute.\nAccepted config attributes:\n" + configList); } - if (c === 'handlers') { + if (configAttr === 'handlers') { for (let handler in configureObject.handlers) { if (this.availableHandlers.includes(handler) === false) { let handlerList = this.availableHandlers.join("\n"); @@ -65,14 +88,21 @@ function JHRW(destination, lazyExecution) this.config.handlers[handler] = configureObject.handlers[handler]; } } else { - this.config[c] = configureObject[c]; + this.config[configAttr] = configureObject[configAttr]; } } return; - }; + } - this.init = function () { + /** + * init - Initializes the Request object (overrides MimeType, adds event listeners for handlers, opens the request and sets + * the request headers, if available). + * + * @return void + */ + init() + { this.request.overrideMimeType(this.config.responseType); for (let handler in this.config.handlers) { @@ -85,11 +115,20 @@ function JHRW(destination, lazyExecution) for (let header in this.config.requestHeaders) { this.request.setRequestHeader(header, this.config.requestHeaders[header]); } - }; - this.send = function () { + return; + } + + /** + * send - Sends the request + * + * @return void + */ + send() + { this.request.send(this.config.data); - }; + return; + } } /** diff --git a/src/example/requestTest.html b/src/example/requestTest.html index be38d5a..d224c5b 100644 --- a/src/example/requestTest.html +++ b/src/example/requestTest.html @@ -2,11 +2,11 @@ - JHRW - Request Tester + JHRW - Request Test Example - +