diff options
author | Ambroise C <ambroise.christea@sonarsource.com> | 2023-09-13 17:24:05 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-09-13 20:02:56 +0000 |
commit | 51c3da2b324930e5ebaf5ea6e7213ba6b31b885b (patch) | |
tree | 1a07ded5e4f2b7f6c1c0a91f9de8904e8e4014e0 /server/sonar-web/src/main/js/helpers | |
parent | f055071dede97c2195bc93f82b8e5e59abde899d (diff) | |
download | sonarqube-51c3da2b324930e5ebaf5ea6e7213ba6b31b885b.tar.gz sonarqube-51c3da2b324930e5ebaf5ea6e7213ba6b31b885b.zip |
SONAR-20327 Fix code smells following prettier upgrades
Co-authored-by: David Cho-Lerat <david.cho-lerat@sonarsource.com>
Diffstat (limited to 'server/sonar-web/src/main/js/helpers')
6 files changed, 137 insertions, 117 deletions
diff --git a/server/sonar-web/src/main/js/helpers/__tests__/measures-test.ts b/server/sonar-web/src/main/js/helpers/__tests__/measures-test.ts index 8781c8d195c..6fd146ecf54 100644 --- a/server/sonar-web/src/main/js/helpers/__tests__/measures-test.ts +++ b/server/sonar-web/src/main/js/helpers/__tests__/measures-test.ts @@ -17,6 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +import { MetricKey, MetricType } from '../../types/metrics'; import { Dict } from '../../types/types'; import { getMessages } from '../l10nBundle'; import { enhanceConditionWithMeasure, formatMeasure, isPeriodBestValue } from '../measures'; @@ -56,21 +57,29 @@ const ONE_DAY = HOURS_IN_DAY * ONE_HOUR; describe('enhanceConditionWithMeasure', () => { it('should correctly map enhance conditions with measure data', () => { const measures = [ - mockMeasureEnhanced({ metric: mockMetric({ key: 'bugs' }), period: undefined }), - mockMeasureEnhanced({ metric: mockMetric({ key: 'new_bugs' }) }), + mockMeasureEnhanced({ metric: mockMetric({ key: MetricKey.bugs }), period: undefined }), + mockMeasureEnhanced({ metric: mockMetric({ key: MetricKey.new_bugs }) }), ]; expect( - enhanceConditionWithMeasure(mockQualityGateStatusCondition({ metric: 'bugs' }), measures), + enhanceConditionWithMeasure( + mockQualityGateStatusCondition({ metric: MetricKey.bugs }), + measures, + ), ).toMatchObject({ - measure: expect.objectContaining({ metric: expect.objectContaining({ key: 'bugs' }) }), + measure: expect.objectContaining({ + metric: expect.objectContaining({ key: MetricKey.bugs }), + }), }); expect( - enhanceConditionWithMeasure(mockQualityGateStatusCondition({ metric: 'new_bugs' }), measures), + enhanceConditionWithMeasure( + mockQualityGateStatusCondition({ metric: MetricKey.new_bugs }), + measures, + ), ).toMatchObject({ measure: expect.objectContaining({ - metric: expect.objectContaining({ key: 'new_bugs' }), + metric: expect.objectContaining({ key: MetricKey.new_bugs }), }), period: 1, }); @@ -99,30 +108,30 @@ describe('isPeriodBestValue', () => { describe('#formatMeasure()', () => { it('should format INT', () => { - expect(formatMeasure(0, 'INT')).toBe('0'); - expect(formatMeasure(1, 'INT')).toBe('1'); - expect(formatMeasure(-5, 'INT')).toBe('-5'); - expect(formatMeasure(999, 'INT')).toBe('999'); - expect(formatMeasure(1000, 'INT')).toBe('1,000'); - expect(formatMeasure(1529, 'INT')).toBe('1,529'); - expect(formatMeasure(10000, 'INT')).toBe('10,000'); - expect(formatMeasure(1234567890, 'INT')).toBe('1,234,567,890'); + expect(formatMeasure(0, MetricType.Integer)).toBe('0'); + expect(formatMeasure(1, MetricType.Integer)).toBe('1'); + expect(formatMeasure(-5, MetricType.Integer)).toBe('-5'); + expect(formatMeasure(999, MetricType.Integer)).toBe('999'); + expect(formatMeasure(1000, MetricType.Integer)).toBe('1,000'); + expect(formatMeasure(1529, MetricType.Integer)).toBe('1,529'); + expect(formatMeasure(10000, MetricType.Integer)).toBe('10,000'); + expect(formatMeasure(1234567890, MetricType.Integer)).toBe('1,234,567,890'); }); it('should format SHORT_INT', () => { - expect(formatMeasure(0, 'SHORT_INT')).toBe('0'); - expect(formatMeasure(1, 'SHORT_INT')).toBe('1'); - expect(formatMeasure(999, 'SHORT_INT')).toBe('999'); - expect(formatMeasure(1000, 'SHORT_INT')).toBe('1k'); - expect(formatMeasure(1529, 'SHORT_INT')).toBe('1.5k'); - expect(formatMeasure(10000, 'SHORT_INT')).toBe('10k'); - expect(formatMeasure(10678, 'SHORT_INT')).toBe('11k'); - expect(formatMeasure(9467890, 'SHORT_INT')).toBe('9.5M'); - expect(formatMeasure(994567890, 'SHORT_INT')).toBe('995M'); - expect(formatMeasure(999000001, 'SHORT_INT')).toBe('999M'); - expect(formatMeasure(999567890, 'SHORT_INT')).toBe('1G'); - expect(formatMeasure(1234567890, 'SHORT_INT')).toBe('1.2G'); - expect(formatMeasure(11234567890, 'SHORT_INT')).toBe('11G'); + expect(formatMeasure(0, MetricType.ShortInteger)).toBe('0'); + expect(formatMeasure(1, MetricType.ShortInteger)).toBe('1'); + expect(formatMeasure(999, MetricType.ShortInteger)).toBe('999'); + expect(formatMeasure(1000, MetricType.ShortInteger)).toBe('1k'); + expect(formatMeasure(1529, MetricType.ShortInteger)).toBe('1.5k'); + expect(formatMeasure(10000, MetricType.ShortInteger)).toBe('10k'); + expect(formatMeasure(10678, MetricType.ShortInteger)).toBe('11k'); + expect(formatMeasure(9467890, MetricType.ShortInteger)).toBe('9.5M'); + expect(formatMeasure(994567890, MetricType.ShortInteger)).toBe('995M'); + expect(formatMeasure(999000001, MetricType.ShortInteger)).toBe('999M'); + expect(formatMeasure(999567890, MetricType.ShortInteger)).toBe('1G'); + expect(formatMeasure(1234567890, MetricType.ShortInteger)).toBe('1.2G'); + expect(formatMeasure(11234567890, MetricType.ShortInteger)).toBe('11G'); }); it('should format FLOAT', () => { @@ -145,28 +154,28 @@ describe('#formatMeasure()', () => { }); it('should format PERCENT', () => { - expect(formatMeasure(0.0, 'PERCENT')).toBe('0.0%'); - expect(formatMeasure(1.0, 'PERCENT')).toBe('1.0%'); - expect(formatMeasure(1.3, 'PERCENT')).toBe('1.3%'); - expect(formatMeasure(1.34, 'PERCENT')).toBe('1.3%'); - expect(formatMeasure(50.89, 'PERCENT')).toBe('50.9%'); - expect(formatMeasure(100.0, 'PERCENT')).toBe('100%'); - expect(formatMeasure(50.89, 'PERCENT', { decimals: 0 })).toBe('50.9%'); - expect(formatMeasure(50.89, 'PERCENT', { decimals: 1 })).toBe('50.9%'); - expect(formatMeasure(50.89, 'PERCENT', { decimals: 2 })).toBe('50.89%'); - expect(formatMeasure(50.89, 'PERCENT', { decimals: 3 })).toBe('50.890%'); - expect(formatMeasure(50, 'PERCENT', { decimals: 0, omitExtraDecimalZeros: true })).toBe( - '50.0%', - ); - expect(formatMeasure(50, 'PERCENT', { decimals: 1, omitExtraDecimalZeros: true })).toBe( - '50.0%', - ); - expect(formatMeasure(50, 'PERCENT', { decimals: 3, omitExtraDecimalZeros: true })).toBe( - '50.0%', - ); - expect(formatMeasure(50.89, 'PERCENT', { decimals: 3, omitExtraDecimalZeros: true })).toBe( - '50.89%', - ); + expect(formatMeasure(0.0, MetricType.Percent)).toBe('0.0%'); + expect(formatMeasure(1.0, MetricType.Percent)).toBe('1.0%'); + expect(formatMeasure(1.3, MetricType.Percent)).toBe('1.3%'); + expect(formatMeasure(1.34, MetricType.Percent)).toBe('1.3%'); + expect(formatMeasure(50.89, MetricType.Percent)).toBe('50.9%'); + expect(formatMeasure(100.0, MetricType.Percent)).toBe('100%'); + expect(formatMeasure(50.89, MetricType.Percent, { decimals: 0 })).toBe('50.9%'); + expect(formatMeasure(50.89, MetricType.Percent, { decimals: 1 })).toBe('50.9%'); + expect(formatMeasure(50.89, MetricType.Percent, { decimals: 2 })).toBe('50.89%'); + expect(formatMeasure(50.89, MetricType.Percent, { decimals: 3 })).toBe('50.890%'); + expect( + formatMeasure(50, MetricType.Percent, { decimals: 0, omitExtraDecimalZeros: true }), + ).toBe('50.0%'); + expect( + formatMeasure(50, MetricType.Percent, { decimals: 1, omitExtraDecimalZeros: true }), + ).toBe('50.0%'); + expect( + formatMeasure(50, MetricType.Percent, { decimals: 3, omitExtraDecimalZeros: true }), + ).toBe('50.0%'); + expect( + formatMeasure(50.89, MetricType.Percent, { decimals: 3, omitExtraDecimalZeros: true }), + ).toBe('50.89%'); }); it('should format WORK_DUR', () => { @@ -185,42 +194,48 @@ describe('#formatMeasure()', () => { }); it('should format SHORT_WORK_DUR', () => { - expect(formatMeasure(0, 'SHORT_WORK_DUR')).toBe('0'); - expect(formatMeasure(5 * ONE_DAY, 'SHORT_WORK_DUR')).toBe('5d'); - expect(formatMeasure(2 * ONE_HOUR, 'SHORT_WORK_DUR')).toBe('2h'); - expect(formatMeasure(ONE_MINUTE, 'SHORT_WORK_DUR')).toBe('1min'); - expect(formatMeasure(40 * ONE_MINUTE, 'SHORT_WORK_DUR')).toBe('40min'); - expect(formatMeasure(58 * ONE_MINUTE, 'SHORT_WORK_DUR')).toBe('1h'); - expect(formatMeasure(5 * ONE_DAY + 2 * ONE_HOUR, 'SHORT_WORK_DUR')).toBe('5d'); - expect(formatMeasure(2 * ONE_HOUR + ONE_MINUTE, 'SHORT_WORK_DUR')).toBe('2h'); - expect(formatMeasure(ONE_HOUR + 55 * ONE_MINUTE, 'SHORT_WORK_DUR')).toBe('2h'); - expect(formatMeasure(3 * ONE_DAY + 6 * ONE_HOUR, 'SHORT_WORK_DUR')).toBe('4d'); - expect(formatMeasure(7 * ONE_HOUR + 59 * ONE_MINUTE, 'SHORT_WORK_DUR')).toBe('1d'); - expect(formatMeasure(5 * ONE_DAY + 2 * ONE_HOUR + ONE_MINUTE, 'SHORT_WORK_DUR')).toBe('5d'); - expect(formatMeasure(15 * ONE_DAY + 2 * ONE_HOUR + ONE_MINUTE, 'SHORT_WORK_DUR')).toBe('15d'); - expect(formatMeasure(7 * ONE_MINUTE, 'SHORT_WORK_DUR')).toBe('7min'); - expect(formatMeasure(-5 * ONE_DAY, 'SHORT_WORK_DUR')).toBe('-5d'); - expect(formatMeasure(-2 * ONE_HOUR, 'SHORT_WORK_DUR')).toBe('-2h'); - expect(formatMeasure(-1 * ONE_MINUTE, 'SHORT_WORK_DUR')).toBe('-1min'); - - expect(formatMeasure(1529 * ONE_DAY, 'SHORT_WORK_DUR')).toBe('1.5kd'); - expect(formatMeasure(1234567 * ONE_DAY, 'SHORT_WORK_DUR')).toBe('1.2Md'); - expect(formatMeasure(12345670 * ONE_DAY + 4 * ONE_HOUR, 'SHORT_WORK_DUR')).toBe('12Md'); + expect(formatMeasure(0, MetricType.ShortWorkDuration)).toBe('0'); + expect(formatMeasure(5 * ONE_DAY, MetricType.ShortWorkDuration)).toBe('5d'); + expect(formatMeasure(2 * ONE_HOUR, MetricType.ShortWorkDuration)).toBe('2h'); + expect(formatMeasure(ONE_MINUTE, MetricType.ShortWorkDuration)).toBe('1min'); + expect(formatMeasure(40 * ONE_MINUTE, MetricType.ShortWorkDuration)).toBe('40min'); + expect(formatMeasure(58 * ONE_MINUTE, MetricType.ShortWorkDuration)).toBe('1h'); + expect(formatMeasure(5 * ONE_DAY + 2 * ONE_HOUR, MetricType.ShortWorkDuration)).toBe('5d'); + expect(formatMeasure(2 * ONE_HOUR + ONE_MINUTE, MetricType.ShortWorkDuration)).toBe('2h'); + expect(formatMeasure(ONE_HOUR + 55 * ONE_MINUTE, MetricType.ShortWorkDuration)).toBe('2h'); + expect(formatMeasure(3 * ONE_DAY + 6 * ONE_HOUR, MetricType.ShortWorkDuration)).toBe('4d'); + expect(formatMeasure(7 * ONE_HOUR + 59 * ONE_MINUTE, MetricType.ShortWorkDuration)).toBe('1d'); + expect( + formatMeasure(5 * ONE_DAY + 2 * ONE_HOUR + ONE_MINUTE, MetricType.ShortWorkDuration), + ).toBe('5d'); + expect( + formatMeasure(15 * ONE_DAY + 2 * ONE_HOUR + ONE_MINUTE, MetricType.ShortWorkDuration), + ).toBe('15d'); + expect(formatMeasure(7 * ONE_MINUTE, MetricType.ShortWorkDuration)).toBe('7min'); + expect(formatMeasure(-5 * ONE_DAY, MetricType.ShortWorkDuration)).toBe('-5d'); + expect(formatMeasure(-2 * ONE_HOUR, MetricType.ShortWorkDuration)).toBe('-2h'); + expect(formatMeasure(-1 * ONE_MINUTE, MetricType.ShortWorkDuration)).toBe('-1min'); + + expect(formatMeasure(1529 * ONE_DAY, MetricType.ShortWorkDuration)).toBe('1.5kd'); + expect(formatMeasure(1234567 * ONE_DAY, MetricType.ShortWorkDuration)).toBe('1.2Md'); + expect(formatMeasure(12345670 * ONE_DAY + 4 * ONE_HOUR, MetricType.ShortWorkDuration)).toBe( + '12Md', + ); }); it('should format RATING', () => { - expect(formatMeasure(1, 'RATING')).toBe('A'); - expect(formatMeasure(2, 'RATING')).toBe('B'); - expect(formatMeasure(3, 'RATING')).toBe('C'); - expect(formatMeasure(4, 'RATING')).toBe('D'); - expect(formatMeasure(5, 'RATING')).toBe('E'); + expect(formatMeasure(1, MetricType.Rating)).toBe('A'); + expect(formatMeasure(2, MetricType.Rating)).toBe('B'); + expect(formatMeasure(3, MetricType.Rating)).toBe('C'); + expect(formatMeasure(4, MetricType.Rating)).toBe('D'); + expect(formatMeasure(5, MetricType.Rating)).toBe('E'); }); it('should format LEVEL', () => { - expect(formatMeasure('ERROR', 'LEVEL')).toBe('Error'); - expect(formatMeasure('WARN', 'LEVEL')).toBe('Warning'); - expect(formatMeasure('OK', 'LEVEL')).toBe('Ok'); - expect(formatMeasure('UNKNOWN', 'LEVEL')).toBe('UNKNOWN'); + expect(formatMeasure('ERROR', MetricType.Level)).toBe('Error'); + expect(formatMeasure('WARN', MetricType.Level)).toBe('Warning'); + expect(formatMeasure('OK', MetricType.Level)).toBe('Ok'); + expect(formatMeasure('UNKNOWN', MetricType.Level)).toBe('UNKNOWN'); }); it('should format MILLISEC', () => { @@ -237,10 +252,10 @@ describe('#formatMeasure()', () => { }); it('should return null if value is empty string', () => { - expect(formatMeasure('', 'PERCENT')).toBe(''); + expect(formatMeasure('', MetricType.Percent)).toBe(''); }); it('should not fail with undefined', () => { - expect(formatMeasure(undefined, 'INT')).toBe(''); + expect(formatMeasure(undefined, MetricType.Integer)).toBe(''); }); }); diff --git a/server/sonar-web/src/main/js/helpers/constants.ts b/server/sonar-web/src/main/js/helpers/constants.ts index 393b037269e..4f974fccf3c 100644 --- a/server/sonar-web/src/main/js/helpers/constants.ts +++ b/server/sonar-web/src/main/js/helpers/constants.ts @@ -99,3 +99,5 @@ export const IMPORT_COMPATIBLE_ALMS = [ ]; export const GRADLE_SCANNER_VERSION = '4.3.1.3277'; + +export const ONE_SECOND = 1000; diff --git a/server/sonar-web/src/main/js/helpers/issues.ts b/server/sonar-web/src/main/js/helpers/issues.ts index df9c80dc754..bd4ba2385df 100644 --- a/server/sonar-web/src/main/js/helpers/issues.ts +++ b/server/sonar-web/src/main/js/helpers/issues.ts @@ -115,13 +115,13 @@ function splitFlows( }; } - const parsedFlows: FlowLocation[][] = (issue.flows || []) + const parsedFlows: FlowLocation[][] = (issue.flows ?? []) .filter((flow) => flow.locations !== undefined) .map((flow) => flow.locations!.filter((location) => location.textRange != null)) .map((flow) => flow.map((location) => { const component = components.find((component) => component.key === location.component); - return { ...location, componentName: component && component.name }; + return { ...location, componentName: component?.name }; }), ); @@ -139,8 +139,8 @@ function splitFlows( function orderLocations(locations: FlowLocation[]) { return sortBy( locations, - (location) => location.textRange && location.textRange.startLine, - (location) => location.textRange && location.textRange.startOffset, + (location) => location.textRange?.startLine, + (location) => location.textRange?.startOffset, ); } diff --git a/server/sonar-web/src/main/js/helpers/measures.ts b/server/sonar-web/src/main/js/helpers/measures.ts index 6eea6308412..3eda07fb334 100644 --- a/server/sonar-web/src/main/js/helpers/measures.ts +++ b/server/sonar-web/src/main/js/helpers/measures.ts @@ -17,12 +17,13 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { MetricKey } from '../types/metrics'; +import { MetricKey, MetricType } from '../types/metrics'; import { QualityGateStatusCondition, QualityGateStatusConditionEnhanced, } from '../types/quality-gates'; import { Dict, Measure, MeasureEnhanced, Metric } from '../types/types'; +import { ONE_SECOND } from './constants'; import { translate, translateWithParameters } from './l10n'; import { getCurrentLocale } from './l10nBundle'; import { isDefined } from './types'; @@ -48,7 +49,7 @@ export function enhanceConditionWithMeasure( // Make sure we have a period index. This is necessary when dealing with // applications. let { period } = condition; - if (measure && measure.period && !period) { + if (measure?.period && !period) { period = measure.period.index; } @@ -56,12 +57,12 @@ export function enhanceConditionWithMeasure( } export function isPeriodBestValue(measure: Measure | MeasureEnhanced): boolean { - return measure.period?.bestValue || false; + return measure.period?.bestValue ?? false; } /** Check if metric is differential */ export function isDiffMetric(metricKey: MetricKey | string): boolean { - return metricKey.indexOf('new_') === 0; + return metricKey.startsWith('new_'); } export function getDisplayMetrics(metrics: Metric[]) { @@ -74,9 +75,7 @@ export function findMeasure(measures: MeasureEnhanced[], metric: MetricKey | str const HOURS_IN_DAY = 8; -interface Formatter { - (value: string | number, options?: any): string; -} +type Formatter = (value: string | number, options?: Dict<unknown>) => string; /** * Format a measure value for a given type @@ -85,7 +84,7 @@ interface Formatter { export function formatMeasure( value: string | number | undefined, type: string, - options?: any, + options?: Dict<unknown>, ): string { const formatter = getFormatter(type); // eslint-disable-next-line react-hooks/rules-of-hooks @@ -114,10 +113,10 @@ export function localizeMetric(metricKey: string): string { /** Return corresponding "short" for better display in UI */ export function getShortType(type: string): string { - if (type === 'INT') { - return 'SHORT_INT'; + if (type === MetricType.Integer) { + return MetricType.ShortInteger; } else if (type === 'WORK_DUR') { - return 'SHORT_WORK_DUR'; + return MetricType.ShortWorkDuration; } return type; } @@ -125,7 +124,7 @@ export function getShortType(type: string): string { function useFormatter( value: string | number | undefined, formatter: Formatter, - options?: any, + options?: Dict<unknown>, ): string { return value !== undefined && value !== '' ? formatter(value, options) : ''; } @@ -181,7 +180,7 @@ function shortIntFormatter( value: string | number, option?: { roundingFunc?: (x: number) => number }, ): string { - const roundingFunc = (option && option.roundingFunc) || undefined; + const roundingFunc = option?.roundingFunc; if (typeof value === 'string') { value = parseFloat(value); } @@ -259,7 +258,6 @@ function millisecondsFormatter(value: string | number): string { if (typeof value === 'string') { value = parseInt(value, 10); } - const ONE_SECOND = 1000; const ONE_MINUTE = 60 * ONE_SECOND; if (value >= ONE_MINUTE) { const minutes = Math.round(value / ONE_MINUTE); @@ -329,7 +327,10 @@ function formatDurationShort( ): string { if (shouldDisplayDaysInShortFormat(days)) { const roundedDays = Math.round(days); - const formattedDays = formatMeasure(isNegative ? -1 * roundedDays : roundedDays, 'SHORT_INT'); + const formattedDays = formatMeasure( + isNegative ? -1 * roundedDays : roundedDays, + MetricType.ShortInteger, + ); return translateWithParameters('work_duration.x_days', formattedDays); } @@ -337,12 +338,15 @@ function formatDurationShort( const roundedHours = Math.round(hours); const formattedHours = formatMeasure( isNegative ? -1 * roundedHours : roundedHours, - 'SHORT_INT', + MetricType.ShortInteger, ); return translateWithParameters('work_duration.x_hours', formattedHours); } - const formattedMinutes = formatMeasure(isNegative ? -1 * minutes : minutes, 'SHORT_INT'); + const formattedMinutes = formatMeasure( + isNegative ? -1 * minutes : minutes, + MetricType.ShortInteger, + ); return translateWithParameters('work_duration.x_minutes', formattedMinutes); } diff --git a/server/sonar-web/src/main/js/helpers/projectLinks.ts b/server/sonar-web/src/main/js/helpers/projectLinks.ts index 0e63ce841a0..14b7872cbd2 100644 --- a/server/sonar-web/src/main/js/helpers/projectLinks.ts +++ b/server/sonar-web/src/main/js/helpers/projectLinks.ts @@ -32,7 +32,7 @@ export function orderLinks<T extends NameAndType>(links: T[]) { const [provided, unknown] = partition<T>(links, isProvided); return [ ...sortBy(provided, (link) => PROVIDED_TYPES.indexOf(link.type)), - ...sortBy(unknown, (link) => link.name && link.name.toLowerCase()), + ...sortBy(unknown, (link) => link.name?.toLowerCase()), ]; } diff --git a/server/sonar-web/src/main/js/helpers/request.ts b/server/sonar-web/src/main/js/helpers/request.ts index 9f0e415787f..95005676fcf 100644 --- a/server/sonar-web/src/main/js/helpers/request.ts +++ b/server/sonar-web/src/main/js/helpers/request.ts @@ -24,6 +24,9 @@ import { translate } from './l10n'; import { stringify } from './stringify-queryparams'; import { getBaseUrl } from './system'; +const FAST_RETRY_TIMEOUT = 500; +const SLOW_RETRY_TIMEOUT = 3000; + export function getCSRFTokenName(): string { return 'X-XSRF-TOKEN'; } @@ -117,12 +120,12 @@ class Request { return window.fetch(getBaseUrl() + url, options); } - setMethod(method: string): Request { + setMethod(method: string): this { this.options.method = method; return this; } - setData(data?: RequestData, isJSON = false): Request { + setData(data?: RequestData, isJSON = false): this { if (data) { this.data = data; this.isJSON = isJSON; @@ -154,7 +157,7 @@ export function corsRequest(url: string, mode: RequestMode = 'cors'): Request { /** * Check that response status is ok */ -export function checkStatus(response: Response, bypassRedirect?: boolean): Promise<Response> { +export function checkStatus(response: Response, bypassRedirect = false): Promise<Response> { return new Promise((resolve, reject) => { if (response.status === HttpStatus.Unauthorized && !bypassRedirect) { import('./handleRequiredAuthentication').then((i) => i.default()).then(reject, reject); @@ -193,7 +196,7 @@ export function parseError(response: Response): Promise<string> { /** * Shortcut to do a GET request and return a Response */ -export function get(url: string, data?: RequestData, bypassRedirect?: boolean): Promise<Response> { +export function get(url: string, data?: RequestData, bypassRedirect = false): Promise<Response> { return request(url) .setData(data) .submit() @@ -203,18 +206,14 @@ export function get(url: string, data?: RequestData, bypassRedirect?: boolean): /** * Shortcut to do a GET request and return response json */ -export function getJSON(url: string, data?: RequestData, bypassRedirect?: boolean): Promise<any> { +export function getJSON(url: string, data?: RequestData, bypassRedirect = false): Promise<any> { return get(url, data, bypassRedirect).then(parseJSON); } /** * Shortcut to do a GET request and return response text */ -export function getText( - url: string, - data?: RequestData, - bypassRedirect?: boolean, -): Promise<string> { +export function getText(url: string, data?: RequestData, bypassRedirect = false): Promise<string> { return get(url, data, bypassRedirect).then(parseText); } @@ -236,7 +235,7 @@ export function getCorsJSON(url: string, data?: RequestData): Promise<any> { /** * Shortcut to do a POST request and return response json */ -export function postJSON(url: string, data?: RequestData, bypassRedirect?: boolean): Promise<any> { +export function postJSON(url: string, data?: RequestData, bypassRedirect = false): Promise<any> { return request(url) .setMethod('POST') .setData(data) @@ -251,7 +250,7 @@ export function postJSON(url: string, data?: RequestData, bypassRedirect?: boole export function postJSONBody( url: string, data?: RequestData, - bypassRedirect?: boolean, + bypassRedirect = false, ): Promise<any> { return request(url) .setMethod('POST') @@ -264,7 +263,7 @@ export function postJSONBody( /** * Shortcut to do a POST request */ -export function post(url: string, data?: RequestData, bypassRedirect?: boolean): Promise<void> { +export function post(url: string, data?: RequestData, bypassRedirect = false): Promise<void> { return new Promise((resolve, reject) => { request(url) .setMethod('POST') @@ -298,7 +297,7 @@ function tryRequestAgain<T>( return new Promise<T>((resolve) => { setTimeout( () => resolve(requestTryAndRepeatUntil(repeatAPICall, tries, stopRepeat, repeatErrors)), - tries.max > tries.slowThreshold ? 500 : 3000, + tries.max > tries.slowThreshold ? FAST_RETRY_TIMEOUT : SLOW_RETRY_TIMEOUT, ); }); } |