Skip to content

Commit

Permalink
fix(bug): prevent modify options object through reference
Browse files Browse the repository at this point in the history
  • Loading branch information
zack9433 committed Apr 13, 2017
1 parent 898a09f commit 971d300
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/component/restful.provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ class RestProvider {
get: (uri, options) => {
return makeRequest('get', uri, null, options);
},
post: (uri, data, files, options) => {
post: (uri, data, files, options = {}) => {
if ('http' === config.service && Array.isArray(files)) {
options = options || {};
const base = options.basePath || config.basePath;
const setting = {
url: base + uri,
Expand All @@ -32,12 +31,11 @@ class RestProvider {
};
transformUploadSetting(setting, data, files);
return Upload.upload(Object.assign(setting, options));
}
else {
} else {
return makeRequest('post', uri, data, options);
}
},
put: (uri, data, files, options) => {
put: (uri, data, files, options = {}) => {
if ('http' === config.service && Array.isArray(files)) {
options = options || {};
const base = options.basePath || config.basePath;
Expand All @@ -48,8 +46,7 @@ class RestProvider {
};
transformUploadSetting(setting, data, files);
return Upload.upload(Object.assign(setting, options));
}
else {
} else {
return makeRequest('put', uri, data, options);
}
},
Expand All @@ -60,7 +57,7 @@ class RestProvider {

function transformUploadSetting(setting, data, files) {
if (files[0] instanceof File) {
(files.length === 1) ? setting.data.file = files[0] : setting.data.files = files;
files.length === 1 ? (setting.data.file = files[0]) : (setting.data.files = files);
} else {
files.forEach(item => {
if (item.key) {
Expand All @@ -73,9 +70,9 @@ class RestProvider {
}
}

function makeRequest(verb, uri, data, options) {
function makeRequest(verb, uri, data, options = {}) {
let defer = $q.defer();
let reqConfig = options || {};
let reqConfig = JSON.parse(JSON.stringify(options));
let base = reqConfig.basePath || config.basePath;
//start with the uri
let args = [base + uri];
Expand All @@ -97,13 +94,14 @@ class RestProvider {

args.push(reqConfig);

rest[verb](args)
.then(function(res) {
defer.resolve(res);
})
.catch(function(res) {
defer.reject(res);
});
rest
[verb](args)
.then(function(res) {
defer.resolve(res);
})
.catch(function(res) {
defer.reject(res);
});

return defer.promise;
}
Expand Down

0 comments on commit 971d300

Please sign in to comment.