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

Added shorthands for PATCH and DELETE methods #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 8 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function request(options, callback) {
else if(typeof options.body !== 'string')
options.body = JSON.stringify(options.body)
}

//BEGIN QS Hack
var serialize = function(obj) {
var str = [];
Expand All @@ -88,7 +88,7 @@ function request(options, callback) {
}
return str.join("&");
}

if(options.qs){
var qs = (typeof options.qs == 'string')? options.qs : serialize(options.qs);
if(options.uri.indexOf('?') !== -1){ //no get params
Expand All @@ -98,7 +98,7 @@ function request(options, callback) {
}
}
//END QS Hack

//BEGIN FORM Hack
var multipart = function(obj) {
//todo: support file type (useful?)
Expand All @@ -121,7 +121,7 @@ function request(options, callback) {
result.type = 'multipart/form-data; boundary='+result.boundry;
return result;
}

if(options.form){
if(typeof options.form == 'string') throw('form name unsupported');
if(options.method === 'POST'){
Expand Down Expand Up @@ -305,6 +305,8 @@ request.defaults = function(options, requester) {
de.get = def(request.get)
de.post = def(request.post)
de.put = def(request.put)
de.patch = def(request.patch)
de.delete = def(request.delete)
de.head = def(request.head)
return de
}
Expand All @@ -313,7 +315,7 @@ request.defaults = function(options, requester) {
// HTTP method shortcuts
//

var shortcuts = [ 'get', 'put', 'post', 'head' ];
var shortcuts = [ 'get', 'put', 'patch', 'post', 'delete', 'head'];
shortcuts.forEach(function(shortcut) {
var method = shortcut.toUpperCase();
var func = shortcut.toLowerCase();
Expand Down Expand Up @@ -403,16 +405,7 @@ function formatted(obj, method) {
function is_crossDomain(url) {
var rurl = /^([\w\+\.\-]+:)(?:\/\/([^\/?#:]*)(?::(\d+))?)?/

// jQuery #8138, IE may throw an exception when accessing
// a field from window.location if document.domain has been set
var ajaxLocation
try { ajaxLocation = location.href }
catch (e) {
// Use the href attribute of an A element since IE will modify it given document.location
ajaxLocation = document.createElement( "a" );
ajaxLocation.href = "";
ajaxLocation = ajaxLocation.href;
}
var ajaxLocation = location.href;

var ajaxLocParts = rurl.exec(ajaxLocation.toLowerCase()) || []
, parts = rurl.exec(url.toLowerCase() )
Expand Down