From 2e4e9ce93371a82532ffa2dc83b6ff47d6888abb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9goire=20Aubert?= Date: Wed, 19 Jul 2017 16:01:25 +0200 Subject: [PATCH] Automatically remove nil values from api calls. --- server/sonar-web/src/main/js/helpers/request.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) 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; + } } } -- 2.39.5