Skip to content

Commit

Permalink
fixes for patch, put
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkneen committed Dec 8, 2021
1 parent 34a2157 commit 51bfdc1
Showing 1 changed file with 42 additions and 41 deletions.
83 changes: 42 additions & 41 deletions reste.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function main() {
function send() {
log(args.params);

if (args.params && (args.method === 'POST' || args.method === 'PUT')) {
if (args.params && (args.method === 'POST' || args.method === 'PUT' || args.method === 'PATCH' || args.method === 'DELETE')) {
if (formEncode) {
http.send(args.params);
} else {
Expand Down Expand Up @@ -198,8 +198,8 @@ function main() {
for (var header in headers) {
requestHeaders.push({
name: header,
value: headers[header]
});
value: headers[header] });


}
};
Expand All @@ -216,8 +216,8 @@ function main() {
if (!changed) {
requestHeaders.push({
name: Object.keys(header)[0],
value: header[Object.keys(header)[0]]
});
value: header[Object.keys(header)[0]] });


}
};
Expand All @@ -244,6 +244,7 @@ function main() {
if (args.post) method = 'POST';
if (args.get) method = 'GET';
if (args.put) method = 'PUT';
if (args.patch) method = 'PATCH';
if (args.delete) method = 'DELETE';

url = args[method.toLowerCase()] || args.get;
Expand Down Expand Up @@ -288,22 +289,22 @@ function main() {

if (args.expects) {
args.expects.forEach((expectedParam) => {
if (method === 'POST' && params.body ? !params.body[expectedParam] : !params[expectedParam]) {
if (params.body ? !params.body[expectedParam] : !params[expectedParam]) {
throw 'RESTe :: missing parameter ' + expectedParam + ' for method ' + args.name;
}
});

return makeHttpRequest({
url,
method,
timeout: args.timeout || config.timeout || 10000,
params: body,
headers: args.requestHeaders || args.headers,
beforePost: args.beforePost,
beforeSend: args.beforeSend
},
url,
method,
timeout: args.timeout || config.timeout || 10000,
params: body,
headers: args.requestHeaders || args.headers,
beforePost: args.beforePost,
beforeSend: args.beforeSend },

onLoad, onError);

onLoad, onError);
} else {
var m;
var missing = [];
Expand All @@ -323,16 +324,16 @@ function main() {
throw 'RESTe :: missing parameter/s ' + missing + ' for method ' + args.name;
} else {
return makeHttpRequest({
url,
method,
timeout: args.timeout || config.timeout || 10000,
params: body,
headers: args.requestHeaders || args.headers,
beforePost: args.beforePost,
beforeSend: args.beforeSend
},

onLoad, onError);
url,
method,
timeout: args.timeout || config.timeout || 10000,
params: body,
headers: args.requestHeaders || args.headers,
beforePost: args.beforePost,
beforeSend: args.beforeSend },


onLoad, onError);
}
}

Expand Down Expand Up @@ -406,8 +407,8 @@ function main() {
this.__transform = this.toJSON();
}
return this.__transform;
}
});
} });


if (args.collections) {
args.collections.forEach((collection) => {
Expand All @@ -428,8 +429,9 @@ function main() {

if (model instanceof Backbone.Collection && modelConfig && modelConfig.collections) {
var collectionConfig = _.where(modelConfig.collections, {
name: model._name
})[0];
name: model._name })[

0];

var methodCall = reste[collectionConfig.read];

Expand Down Expand Up @@ -542,24 +544,23 @@ function main() {
} : onError = null;

reste[modelConfig.create]({
body
},
body },

(e) => {

if (e.code > 200) {
onError(e);
} else {
e.id = e[modelConfig.id];
model.set('id', e[modelConfig.id]);
options.success(e);
}
}, onError);
(e) => {

if (e.code > 200) {
onError(e);
} else {
e.id = e[modelConfig.id];
model.set('id', e[modelConfig.id]);
options.success(e);
}
}, onError);
}

if (method === 'delete') {
body = {};

body[modelConfig.id] = model.get('id');
body.body = model.toJSON();

Expand Down

0 comments on commit 51bfdc1

Please sign in to comment.