From 51bfdc1fa4934deb98b59112f7e6c0a902fc7ec4 Mon Sep 17 00:00:00 2001 From: Jason Kneen Date: Wed, 8 Dec 2021 19:56:55 +0000 Subject: [PATCH] fixes for patch, put --- reste.js | 83 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/reste.js b/reste.js index 9d6fa26..0f2aaeb 100644 --- a/reste.js +++ b/reste.js @@ -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 { @@ -198,8 +198,8 @@ function main() { for (var header in headers) { requestHeaders.push({ name: header, - value: headers[header] - }); + value: headers[header] }); + } }; @@ -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]] }); + } }; @@ -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; @@ -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 = []; @@ -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); } } @@ -406,8 +407,8 @@ function main() { this.__transform = this.toJSON(); } return this.__transform; - } - }); + } }); + if (args.collections) { args.collections.forEach((collection) => { @@ -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]; @@ -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();