Giter Club home page Giter Club logo

jhrw's Introduction

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;
    • Adding interesting, simplified, feature, such as timeouts and retries.

Documentation

Object JHRW(String base, String urlPath [, Boolean lazyExecution = false [, Boolean bypassCache = false]]);

Parameters

  • 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 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

  • Object request - The native 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 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
void configure(Object configureObject);

Overwrites one or more configuration options (see the config object above)

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
void send();

Sends the request, including data, if available.

end
void end();

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

Basic Usage

try {
	var obj = new JHRW('http://localhost', /foo.php', true);
} catch (Error e) {
	// Do something
}

or

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

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

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

For a more advanced usage example see the testing page.

Credits

jhrw's People

Contributors

galvao avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

jhrw's Issues

Ability to replace specific handlers

Currently JHRW only allows for the programmer to replace the entire config.handlers object. It should allow for the replacement of specific handler(s) without destroying the ones not defined on the parameter passed to the configure method.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.