Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.0.0 #3

Merged
merged 1 commit into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>
* @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