microbejs
Version:
microbe.js - A modular JS library for DOM manipulation, and more
205 lines (183 loc) • 5.98 kB
JavaScript
/**
* http.js
*
* @author Mouse Braun <mouse@knoblau.ch>
* @author Nicolas Brugneaux <nicolas.brugneaux@gmail.com>
*
* @package Microbe
*/
module.exports = function( Microbe )
{
'use strict';
var Promise = require( 'promise' );
/**
* ## http
*
* Method takes as many as necessary parameters, with url being the only required.
* The return then has the methods `.then( _cb )` and `.error( _cb )`
*
* @param {Object} _parameters http parameters. possible properties
* method, url, data, user, password, headers, async
*
* @example µ.http( {url: './test.html', method: 'POST', data: { number: 67867} } ).then( function(){} ).catch( function(){} );
*/
Microbe.http = function( _parameters )
{
var req, method, url, data, user, password, headers, async;
if ( !_parameters )
{
return new Error( 'No parameters given' );
}
else
{
if ( typeof _parameters === 'string' )
{
_parameters = { url: _parameters };
}
req = new XMLHttpRequest();
method = _parameters.method || 'GET';
url = _parameters.url;
data = JSON.stringify( _parameters.data ) || null;
user = _parameters.user || '';
password = _parameters.password || '';
headers = _parameters.headers || null;
async = typeof _parameters.async === "boolean" ?
_parameters.async : true;
req.onreadystatechange = function()
{
if ( req.readyState === 4 )
{
return req;
}
};
}
req.open( method, url, async, user, password );
// weird Safari voodoo fix
if ( method === 'POST' )
{
req.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
}
if ( headers )
{
for ( var header in headers )
{
req.setRequestHeader( header, headers[header] );
}
}
if ( async )
{
return new Promise( function( resolve, reject )
{
req.onerror = function()
{
reject( new Error( 'Network error!' ) );
};
req.send( data );
req.onload = function()
{
if ( req.status === 200 )
{
resolve( req.response );
}
else
{
reject( new Error( req.status ) );
}
};
});
}
else
{
var _response = function( _val )
{
var _responses = {
/**
* ## .then
*
* Called after `http`, `http.get`, or `http.post`, this is
* called passing the result as the first parameter to the callback
*
* @param {Function} _cb function to call after http request
*
* @return _Object_ contains the `.catch` method
*/
then: function( _cb )
{
if ( _val.status === 200 )
{
_cb( _val.responseText );
}
return _responses;
},
/**
* ## .catch
*
* Called after `http`, `http.get`, or `http.post`, this is
* called passing the error as the first parameter to the callback
*
* @param {Function} _cb function to call after http request
*
* @return _Object_ contains the `.then` method
*/
catch: function( _cb )
{
if ( _val.status !== 200 )
{
_cb( {
status : _val.status,
statusText : _val.statusText
} );
}
return _responses;
}
};
return _responses;
};
req.send( data );
req.onloadend = function()
{
req.onreadystatechange();
return _response( req );
};
return req.onloadend();
}
};
/**
* ## http.get
*
* Syntactic shortcut for simple GET requests
*
* @param {String} _url file url
*
* @example µ.http.get( './test.html' ).then( function(){} ).catch( function(){} );
*
* @return _Object_ contains `.then` and `.catch`
*/
Microbe.http.get = function( _url )
{
return this( {
url : _url,
method : 'GET'
} );
};
/**
* ## http.post
*
* Syntactic shortcut for simple POST requests
*
* @param {String} _url file url
* @param {Mixed} _data data to post to location {Object or String}
*
* @example µ.http.post( './test.html', { number: 67867} ).then( function(){} ).catch( function(){} );
*
* @return _Object_ contains `.then` and `.catch`
*/
Microbe.http.post = function( _url, _data )
{
return this( {
url : _url,
data : _data,
method : 'POST'
} );
};
};