Skip to content

Commit 30a0497

Browse files
committed
[sc-11931] resolve 22 issues listed by SonarQube
1 parent 716dad3 commit 30a0497

File tree

3 files changed

+39
-60
lines changed

3 files changed

+39
-60
lines changed

src/apifetch.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'es6-promise/auto';
22
import { apiInstance, RESPONSE_BAD_REQUEST, RESPONSE_SERVER_ERROR } from './api';
3-
import { Settings, SortByOptions, SortOrder, SortOrderOptions } from './settings';
3+
import { Settings } from './settings';
44
import { AxiosResponse } from 'axios';
55
import { isEmptyObject } from './util';
66

@@ -184,8 +184,8 @@ const executeApiFetch: ExecuteApiFetch = function (
184184
requestPayloadObject = {
185185
...requestPayloadObject,
186186
language: settings?.lang,
187-
fuzzy: fuzzy !== true && fuzzy !== false ? fuzzy : JSON.stringify(fuzzy),
188-
// fuzzy: fuzzy,
187+
// fuzzy: fuzzy !== true && fuzzy !== false ? fuzzy : JSON.stringify(fuzzy),
188+
fuzzy: fuzzy,
189189
collectAnalytics: settings?.collectAnalytics,
190190
postfixWildcard: settings?.postfixWildcard,
191191
categories: settings?.categories ? settings?.categories.split(',') : undefined,
@@ -194,33 +194,28 @@ const executeApiFetch: ExecuteApiFetch = function (
194194
dateFrom: settings?.dateFrom,
195195
dateTo: settings?.dateTo,
196196
paging: {
197-
page: settings?.paging.page ? settings?.paging.page : 1,
198-
pageSize: settings?.paging.pageSize ? settings?.paging.pageSize : 10,
199-
shuffleAndLimitTo: settings?.shuffleAndLimitTo || undefined,
197+
page: settings?.paging.page ?? 1,
198+
pageSize: settings?.paging.pageSize ?? 10,
199+
shuffleAndLimitTo: settings?.shuffleAndLimitTo ?? undefined,
200200
sortByField: settings?.paging.sortBy,
201201
sortOrder: settings?.paging.sortOrder
202202
},
203203
jwt: settings?.jwt,
204204
resultType: settings?.resultType,
205-
userToken: settings?.userToken || undefined,
205+
userToken: settings?.userToken ?? undefined,
206206
numFacets: settings?.numFacets,
207-
cacheResponseWithTtlSeconds: settings?.cacheResponseTime || undefined,
208-
defaultOperator: settings?.searchOperator || undefined,
207+
cacheResponseWithTtlSeconds: settings?.cacheResponseTime ?? undefined,
208+
defaultOperator: settings?.searchOperator ?? undefined,
209209
analyticsTag: settings?.analyticsTag
210210
};
211211

212212
// Add sortBy and sortOrder
213213
if (Array.isArray(settings?.paging.sortBy) && settings?.paging.sortBy.length > 1) {
214-
const sortByValues: SortByOptions = [];
215-
const sortOrderValues: SortOrderOptions[] = [];
216214
settings?.paging.sortBy.forEach(function (value, index) {
217215
queryParamsString =
218216
queryParamsString +
219217
settingToQueryParam(value, 'sort') +
220218
settingToQueryParam(settings?.paging.sortOrder[index], 'order');
221-
222-
sortByValues.push(value);
223-
sortOrderValues.push(settings?.paging.sortOrder[index] as SortOrder);
224219
});
225220
} else {
226221
queryParamsString =
@@ -235,10 +230,10 @@ const executeApiFetch: ExecuteApiFetch = function (
235230
for (let i = 0; i < settings?.customFieldFilters.length; i++) {
236231
queryParamsString = queryParamsString + '&customField=' + settings?.customFieldFilters[i];
237232

238-
var decodedCustomFieldFilter = decodeURIComponent(settings?.customFieldFilters[i]);
239-
var customFieldFilterPair = decodedCustomFieldFilter.split('=');
240-
var customFieldName = customFieldFilterPair[0];
241-
var customFieldValue = customFieldFilterPair[1];
233+
const decodedCustomFieldFilter = decodeURIComponent(settings?.customFieldFilters[i]);
234+
const customFieldFilterPair = decodedCustomFieldFilter.split('=');
235+
const customFieldName = customFieldFilterPair[0];
236+
const customFieldValue = customFieldFilterPair[1];
242237
customFieldFiltersValues[customFieldName] = customFieldValue;
243238
}
244239

@@ -434,7 +429,7 @@ const executeApiFetch: ExecuteApiFetch = function (
434429

435430
requestPayloadObject = {
436431
...requestPayloadObject,
437-
itemId: recommendOptions.itemId ? recommendOptions.itemId : undefined,
432+
itemId: recommendOptions.itemId ?? undefined,
438433
blockId: recommendOptions.blockId
439434
};
440435
} else if (recommendOptions?.type === 'FREQUENTLY_BOUGHT_TOGETHER') {
@@ -448,7 +443,7 @@ const executeApiFetch: ExecuteApiFetch = function (
448443

449444
requestPayloadObject = {
450445
...requestPayloadObject,
451-
itemId: recommendOptions.itemId ? recommendOptions.itemId : undefined,
446+
itemId: recommendOptions.itemId ?? undefined,
452447
configurationKey: recommendOptions.configurationKey
453448
};
454449
}
@@ -495,7 +490,7 @@ const executeApiFetch: ExecuteApiFetch = function (
495490
if (settings?.apiMethod === 'POST' && ['search', 'suggest', 'autocomplete'].includes(type)) {
496491
apiEndpoint = 'https://' + apiHostname + '/v1/' + apiPath + '/' + sitekey;
497492
requestPayloadObject = {
498-
term: keyword,
493+
term: decodeURIComponent(keyword),
499494
...requestPayloadObject
500495
};
501496
apiInstance

src/index.ts

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,10 @@ class AddSearchClient {
108108
this.settings.setCallback(() => callback);
109109
this.settings.setKeyword(keyword as string);
110110

111-
if (!this.throttledSearchFetch) {
112-
this.throttledSearchFetch = throttle(
113-
this.settings.getSettings().throttleTimeMs,
114-
executeApiFetch
115-
);
116-
}
111+
this.throttledSearchFetch ??= throttle(
112+
this.settings.getSettings().throttleTimeMs,
113+
executeApiFetch
114+
);
117115

118116
this.throttledSearchFetch(
119117
this.apiHostname,
@@ -128,12 +126,10 @@ class AddSearchClient {
128126
this.settings.setCallback(() => callback);
129127
this.settings.setKeyword(keyword);
130128

131-
if (!this.throttledAiAnswersFetch) {
132-
this.throttledAiAnswersFetch = throttle(
133-
this.settings.getSettings().throttleTimeMs,
134-
executeApiFetch
135-
);
136-
}
129+
this.throttledAiAnswersFetch ??= throttle(
130+
this.settings.getSettings().throttleTimeMs,
131+
executeApiFetch
132+
);
137133

138134
this.throttledAiAnswersFetch(
139135
this.apiHostname,
@@ -157,12 +153,10 @@ class AddSearchClient {
157153
}
158154
this.settings.setSuggestionsPrefix(prefix);
159155

160-
if (!this.throttledSuggestionsFetch) {
161-
this.throttledSuggestionsFetch = throttle(
162-
this.settings.getSettings().throttleTimeMs,
163-
executeApiFetch
164-
);
165-
}
156+
this.throttledSuggestionsFetch ??= throttle(
157+
this.settings.getSettings().throttleTimeMs,
158+
executeApiFetch
159+
);
166160

167161
this.throttledSuggestionsFetch(
168162
this.apiHostname,
@@ -181,12 +175,10 @@ class AddSearchClient {
181175
}
182176
this.settings.setAutocompleteParams(field, prefix);
183177

184-
if (!this.throttledAutocompleteFetch) {
185-
this.throttledAutocompleteFetch = throttle(
186-
this.settings.getSettings().throttleTimeMs,
187-
executeApiFetch
188-
);
189-
}
178+
this.throttledAutocompleteFetch ??= throttle(
179+
this.settings.getSettings().throttleTimeMs,
180+
executeApiFetch
181+
);
190182

191183
this.throttledAutocompleteFetch(
192184
this.apiHostname,
@@ -225,9 +217,7 @@ class AddSearchClient {
225217
): void {
226218
const settingsCloned = { ...this.settings.getSettings() };
227219

228-
if (!settingsCloned.rangeFacets) {
229-
settingsCloned.rangeFacets = [];
230-
}
220+
settingsCloned.rangeFacets ??= [];
231221
settingsCloned.rangeFacets.push({ field: options.field, ranges: options.ranges });
232222

233223
executeApiFetch(
@@ -246,12 +236,10 @@ class AddSearchClient {
246236
throw new Error('Illegal recommendations parameters. Should be (options, callbackFunction)');
247237
}
248238

249-
if (!this.throttledRecommendationFetch) {
250-
this.throttledRecommendationFetch = throttle(
251-
this.settings.getSettings().throttleTimeMs,
252-
executeApiFetch
253-
);
254-
}
239+
this.throttledRecommendationFetch ??= throttle(
240+
this.settings.getSettings().throttleTimeMs,
241+
executeApiFetch
242+
);
255243

256244
this.throttledRecommendationFetch(
257245
this.apiHostname,

src/settings.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,12 @@ class SettingsManager {
247247
}
248248

249249
addRangeFacet(field: string, ranges: FromToRange[]): void {
250-
if (!this.settings.rangeFacets) {
251-
this.settings.rangeFacets = [];
252-
}
250+
this.settings.rangeFacets ??= [];
253251
this.settings.rangeFacets.push({ field, ranges });
254252
}
255253

256254
addStatsField(field: string): void {
257-
if (!this.settings.statsFields) {
258-
this.settings.statsFields = [];
259-
}
255+
this.settings.statsFields ??= [];
260256
if (!this.settings.statsFields.includes(field)) {
261257
this.settings.statsFields.push(field);
262258
}

0 commit comments

Comments
 (0)