Skip to content

Commit

Permalink
Merge pull request #3 from galvao/1.0.0
Browse files Browse the repository at this point in the history
Version 1.0.0
  • Loading branch information
galvao authored Jan 15, 2019
2 parents 599ed4b + 8d03d45 commit 6a30d34
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 49 deletions.
133 changes: 86 additions & 47 deletions src/JHRW.js
Original file line number Diff line number Diff line change
@@ -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 <galvao@galvao.eti.br>
* @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");
Expand All @@ -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) {
Expand All @@ -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;
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/example/requestTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<html>
<head>
<meta charset="utf-8">
<title>JHRW - Request Tester</title>
<title>JHRW - Request Test Example</title>
</head>
<body>
<input type="button" value="Click me!">
<script type="text/javascript" src="path/to/JHRW.js"></script>
<script type="text/javascript" src="../JHRW.js"></script>
<script type="text/javascript">
document.querySelector('input').onclick = function () {
try {
Expand Down

0 comments on commit 6a30d34

Please sign in to comment.