From: Grégoire Aubert Date: Wed, 19 Jul 2017 14:01:25 +0000 (+0200) Subject: Automatically remove nil values from api calls. X-Git-Tag: 6.6-RC1~833 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2e4e9ce93371a82532ffa2dc83b6ff47d6888abb;p=sonarqube.git Automatically remove nil values from api calls. --- diff --git a/server/sonar-web/src/main/js/helpers/request.js b/server/sonar-web/src/main/js/helpers/request.js index 8c80e71df44..941532e4b57 100644 --- a/server/sonar-web/src/main/js/helpers/request.js +++ b/server/sonar-web/src/main/js/helpers/request.js @@ -19,6 +19,7 @@ */ // @flow import { stringify } from 'querystring'; +import { omitBy, isNil } from 'lodash'; import { getCookie } from './cookies'; type Response = { @@ -50,6 +51,10 @@ export function getCSRFToken(): Object { return value ? { [getCSRFTokenName()]: value } : {}; } +export function omitNil(obj: Object): Object { + return omitBy(obj, isNil); +} + /** * Default options for any request */ @@ -96,12 +101,14 @@ class Request { if (this.data) { if (this.data instanceof FormData) { options.body = this.data; - } else if (options.method === 'GET') { - url += '?' + stringify(this.data); } else { - customHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; - // $FlowFixMe complains that `data` is nullable - options.body = stringify(this.data); + const strData = stringify(omitNil(this.data)); + if (options.method === 'GET') { + url += '?' + strData; + } else { + customHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; + options.body = strData; + } } }