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 2.0.0 #5

Merged
merged 2 commits into from
Apr 15, 2020
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.swp
*.~*
*.backup
27 changes: 27 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# CHANGELOG

## [2.0.0] - 2020-04-15

### Added
- A default timeout handler.
- The postTimeout configuration, meaning a callback to be executed after the default timout handler.
- The bypassCache parameter to the constructor.
- Object's properties list at the beginning of the class.
- The @version tag in code documentation
- This CHANGELOG.

### Changed
- There must be a function named 'JHRWHandler' to handle JHRW. The changing in name (previously 'defaultHandler') was done to prevent naming conflicts.
- The example handler (see above) now resides on the testing script, as it should.
- Timeouts are now explicitly handled by JHRW and therefore can't be configured anymore. See postTimeout above.
- The request is now created by the init method.
- The example tester is now more sophisticated, allowing for better testing.
- availableHandlers is now a static property.
- The project is now licensed under MIT (no big deal, I'm just standardizing my projects under the same license).
- Overall revamped documentation (README.md).
- JHRW now has a 'base' parameter and a 'urlPath' parameter, instead of the unified - and wrong - 'destination' parameter.
- All "timed" configurations (e.g.: ''timeout') are now handled in seconds.

### Fixed
- JHRW now correctly detects and retries timed out requests.
- The configure method documentation now states that it's parameter should be an Object (not JSON, since JSON doesn\'t allow functions).
107 changes: 73 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,96 @@
<p align="center">
<img src="media/logo.png" width="200">
</p>

# JHRW - JavaScript HTTP Request Wrapper
A wrapper for so-called "AJAX" Requests

## Goals
I've made JHRW to:

* Advance my JavaScript skills;
* Improve/Simplify the usage of the XMLHttpRequest object by:
* Adding default values to what's undefined;
* Adding additional Error checking and clarification;
* Improve/Simplify the usage of the [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) object by:
* Adding default values to what's undefined;
* Adding additional Error checking and clarification;
* Adding interesting, simplified, feature, such as timeouts and retries.

## Documentation
```JavaScript
Object JHRW(String destination [, Boolean lazyExecution = false]);
Object JHRW(String base, String urlPath [, Boolean lazyExecution = false [, Boolean bypassCache = false]]);
```

### Parameters
* `String destination` - The request's target
* `Boolean lazyExecution`(optional) - If the request should be initialized and sent right after instantiation
* `String base` - The request's base URL
* `String urlPath` - The request's endpoint
* `Boolean lazyExecution`(optional) - If the request should be initialized and sent right after instantiation. Default: false
* `Boolean bypassCache`(optional) - If the request URL should have a timed parameter added in order to bypass cache. Default: false

### Throws
A `new Error` if the destination parameter is `undefined`
* A `ReferenceError`
* If there's no JHRWHandler function defined.
* A `Error`
* if the base parameter is `undefined`
* if the urlPath parameter is `undefined`
* A `TypeError`
* if the base parameter is not a `String`
* if the urlPath parameter is not a `String`
* if the lazyExecution parameter is not a `Boolean`
* if the bypassCache parameter is not a `Boolean`


### Returns
An Object containing:

#### Properties
* request - The `XMLHttpRequest` Object
* config - The configuration Object
* URI - The re'uest's target
* asynchronous - If the request should be asynchronous
* verb - The HTTP verb
* data - Data to be sent along with the request
* requestHeaders - HTTP headers for the request
* responseType - Expected MIME type of the response
* handlers - Object containing the functions to handle the request
* attempts - Number of attempts to retry the request
* attemptInterval - Interval between attempts
* availableHandlers - Which handlers can be set
* `Object request` - The native [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) Object
* `Object config` - The configuration Object
* `String URI` - The request's target
* `Boolean asynchronous` - If the request should be asynchronous
* `String verb` - The HTTP verb
* `Mixed data` - Data to be sent along with the request
* `Object requestHeaders` - HTTP headers for the request
* `String responseType` - Expected MIME type of the response
* `Object handlers` - The functions to handle the request
* `Number attempts` - # of attempts to retry if the request fails
* `Number attemptInterval` - Interval between attempts, **in seconds**.
* `Number timeout` - The timeout, **in seconds**, for the request - after which it should be retried.
* `Function postTimeout`: The function to be executed if the request times out.
* `Number timer` - The [timer](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval) that controls the retry process.

##### Static Properties
* `Array JHRW.availableHandlers`: The types of handlers that can be [re-]defined.
* `String JHRW.handlerList`: Convenience property to be shown in error messages.

#### Methods
##### configure
Overwrites one or more configuration options (see the config object above)
```JavaScript
Undefined configure(Object configureObject);
void configure(Object configureObject);
```
Overwrites one or more configuration options (see the config object above)

##### init
Initializes the request: Sets the expected response MIME Type; Sets the handlers as listeners; Opens the request; Sets the request's headers.
```JavaScript
Undefined init();
void init();
```
Initializes the request: Sets the expected response MIME Type; Sets the handlers as listeners; Opens the request; Sets the request's headers.

##### send
```JavaScript
void send();
```
Sends the request, including data, if available.

##### end
```JavaScript
Undefined send();
void end();
```

Ends the request. Useful if you wish for JHRW to stop retrying on success.

### Basic Usage
```JavaScript
try {
var obj = new JHRW('foo.php', true);
var obj = new JHRW('http://localhost', /foo.php', true);
} catch (Error e) {
// Do something
}
Expand All @@ -67,16 +99,23 @@ or

```JavaScript
try {
var req = new JHRW('foo.php');
var req = new JHRW('http://localhost', 'foo.php');

try {
req.init();
req.init();
} catch (ReferenceError e) {
// Do something
}
req.send();
// Do something
}

req.send();
} catch (Error e) {
// Do something
// Do something
}
```
```

For a more advanced usage example see [the testing page](src/example/requestTester.html).

## Credits

* Developed by @galvao.
* Logo font: [Neutra Text Bold](http://fontsgeek.com/fonts/Neutra-Text-Bold)
Binary file added media/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions media/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading