From 4cf8987276d6eeb10ee3770bed2decb7bdd60971 Mon Sep 17 00:00:00 2001 From: Jean-Louis Leysens Date: Tue, 7 Sep 2021 20:07:27 +0200 Subject: [PATCH] [Reporting] Removed `any` from public (#110993) (#111413) * removed anys and ran TS organize imports * updated jest snapshots * fix import paths for non-type imports Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- x-pack/plugins/reporting/common/types.ts | 41 +- .../public/lib/license_check.test.ts | 3 +- .../reporting_api_client.ts | 16 +- .../report_listing.test.tsx.snap | 702 ++++++++++++++++-- .../public/management/report_listing.test.tsx | 31 +- .../get_csv_panel_action.test.ts | 27 +- .../panel_actions/get_csv_panel_action.tsx | 2 +- .../reporting_panel_content.tsx | 6 +- .../reporting/public/shared_imports.ts | 10 +- 9 files changed, 722 insertions(+), 116 deletions(-) diff --git a/x-pack/plugins/reporting/common/types.ts b/x-pack/plugins/reporting/common/types.ts index c324cf363faa1..4051c006a5034 100644 --- a/x-pack/plugins/reporting/common/types.ts +++ b/x-pack/plugins/reporting/common/types.ts @@ -5,7 +5,7 @@ * 2.0. */ -import type { SerializableRecord } from '@kbn/utility-types'; +import type { Ensure, SerializableRecord } from '@kbn/utility-types'; export interface PageSizeParams { pageMarginTop: number; @@ -21,15 +21,21 @@ export interface PdfImageSize { height?: number; } -export interface Size { - width: number; - height: number; -} +export type Size = Ensure< + { + width: number; + height: number; + }, + SerializableRecord +>; -export interface LayoutParams { - id: string; - dimensions?: Size; -} +export type LayoutParams = Ensure< + { + id: string; + dimensions?: Size; + }, + SerializableRecord +>; export interface ReportDocumentHead { _id: string; @@ -50,13 +56,16 @@ export interface TaskRunResult { warnings?: string[]; } -export interface BaseParams { - layout?: LayoutParams; - objectType: string; - title: string; - browserTimezone: string; // to format dates in the user's time zone - version: string; // to handle any state migrations -} +export type BaseParams = Ensure< + { + layout?: LayoutParams; + objectType: string; + title: string; + browserTimezone: string; // to format dates in the user's time zone + version: string; // to handle any state migrations + }, + SerializableRecord +>; // base params decorated with encrypted headers that come into runJob functions export interface BasePayload extends BaseParams { diff --git a/x-pack/plugins/reporting/public/lib/license_check.test.ts b/x-pack/plugins/reporting/public/lib/license_check.test.ts index d1f0b56cdfa62..8f46ca5616f19 100644 --- a/x-pack/plugins/reporting/public/lib/license_check.test.ts +++ b/x-pack/plugins/reporting/public/lib/license_check.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import type { LicenseCheck } from '../shared_imports'; import { checkLicense } from './license_check'; describe('License check', () => { @@ -42,7 +43,7 @@ describe('License check', () => { }); it('shows and enables links if state is not known', () => { - expect(checkLicense({ state: 'PONYFOO' } as any)).toEqual({ + expect(checkLicense(({ state: 'PONYFOO' } as unknown) as LicenseCheck)).toEqual({ enableLinks: true, showLinks: true, message: '', diff --git a/x-pack/plugins/reporting/public/lib/reporting_api_client/reporting_api_client.ts b/x-pack/plugins/reporting/public/lib/reporting_api_client/reporting_api_client.ts index 5c8327df171bd..7a391368f65b3 100644 --- a/x-pack/plugins/reporting/public/lib/reporting_api_client/reporting_api_client.ts +++ b/x-pack/plugins/reporting/public/lib/reporting_api_client/reporting_api_client.ts @@ -4,11 +4,11 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - import { i18n } from '@kbn/i18n'; import moment from 'moment'; import { stringify } from 'query-string'; -import rison, { RisonObject } from 'rison-node'; +import rison from 'rison-node'; +import type { HttpFetchQuery } from 'src/core/public'; import { HttpSetup, IUiSettingsClient } from 'src/core/public'; import { API_BASE_GENERATE, @@ -45,7 +45,7 @@ interface IReportingAPI { // Helpers getReportURL(jobId: string): string; getReportingJobPath(exportType: string, jobParams: BaseParams & T): string; // Return a URL to queue a job, with the job params encoded in the query string of the URL. Used for copying POST URL - createReportingJob(exportType: string, jobParams: any): Promise; // Sends a request to queue a job, with the job params in the POST body + createReportingJob(exportType: string, jobParams: BaseParams & T): Promise; // Sends a request to queue a job, with the job params in the POST body getServerBasePath(): string; // Provides the raw server basePath to allow it to be stripped out from relativeUrls in job params // CRUD @@ -93,7 +93,7 @@ export class ReportingAPIClient implements IReportingAPI { } public async list(page = 0, jobIds: string[] = []) { - const query = { page } as any; + const query: HttpFetchQuery = { page }; if (jobIds.length > 0) { // Only getting the first 10, to prevent URL overflows query.ids = jobIds.slice(0, 10).join(','); @@ -143,14 +143,14 @@ export class ReportingAPIClient implements IReportingAPI { } public getReportingJobPath(exportType: string, jobParams: BaseParams) { - const risonObject: RisonObject = jobParams as Record; - const params = stringify({ jobParams: rison.encode(risonObject) }); + const params = stringify({ + jobParams: rison.encode(jobParams), + }); return `${this.http.basePath.prepend(API_BASE_GENERATE)}/${exportType}?${params}`; } public async createReportingJob(exportType: string, jobParams: BaseParams) { - const risonObject: RisonObject = jobParams as Record; - const jobParamsRison = rison.encode(risonObject); + const jobParamsRison = rison.encode(jobParams); const resp: { job: ReportApiJSON } = await this.http.post( `${API_BASE_GENERATE}/${exportType}`, { diff --git a/x-pack/plugins/reporting/public/management/__snapshots__/report_listing.test.tsx.snap b/x-pack/plugins/reporting/public/management/__snapshots__/report_listing.test.tsx.snap index e9fd76eb62c79..26cee9e5ebb48 100644 --- a/x-pack/plugins/reporting/public/management/__snapshots__/report_listing.test.tsx.snap +++ b/x-pack/plugins/reporting/public/management/__snapshots__/report_listing.test.tsx.snap @@ -693,9 +693,31 @@ exports[`ReportListing Report job listing with some items 1`] = ` { return { @@ -206,11 +207,11 @@ const mockJobs: ReportApiJSON[] = [ }), ]; -const reportingAPIClient = { - list: () => Promise.resolve(mockJobs.map((j) => new Job(j))), - total: () => Promise.resolve(18), +const reportingAPIClient = ({ + list: jest.fn(() => Promise.resolve(mockJobs.map((j) => new Job(j)))), + total: jest.fn(() => Promise.resolve(18)), migrateReportingIndicesIlmPolicy: jest.fn(), -} as any; +} as unknown) as DeeplyMockedKeys; const validCheck = { check: () => ({ @@ -220,8 +221,8 @@ const validCheck = { }; const license$ = { - subscribe: (handler: any) => { - return handler(validCheck); + subscribe: (handler: unknown) => { + return (handler as Function)(validCheck); }, } as Observable; @@ -239,7 +240,7 @@ const mockPollConfig = { describe('ReportListing', () => { let httpService: ReturnType; let applicationService: ReturnType; - let ilmLocator: undefined | LocatorPublic; + let ilmLocator: undefined | LocatorPublic; let urlService: SharePluginSetup['url']; let testBed: UnwrapPromise>; let toasts: NotificationsSetup['toasts']; @@ -303,7 +304,7 @@ describe('ReportListing', () => { }; ilmLocator = ({ getUrl: jest.fn(), - } as unknown) as LocatorPublic; + } as unknown) as LocatorPublic; urlService = ({ locators: { @@ -325,11 +326,11 @@ describe('ReportListing', () => { it('subscribes to license changes, and unsubscribes on dismount', async () => { const unsubscribeMock = jest.fn(); - const subMock = { + const subMock = ({ subscribe: jest.fn().mockReturnValue({ unsubscribe: unsubscribeMock, }), - } as any; + } as unknown) as Observable; await runSetup({ license$: subMock }); @@ -344,7 +345,7 @@ describe('ReportListing', () => { httpService = httpServiceMock.createSetupContract(); ilmLocator = ({ getUrl: jest.fn(), - } as unknown) as LocatorPublic; + } as unknown) as LocatorPublic; urlService = ({ locators: { diff --git a/x-pack/plugins/reporting/public/panel_actions/get_csv_panel_action.test.ts b/x-pack/plugins/reporting/public/panel_actions/get_csv_panel_action.test.ts index 45bd20df85660..654d46cdfbcb1 100644 --- a/x-pack/plugins/reporting/public/panel_actions/get_csv_panel_action.test.ts +++ b/x-pack/plugins/reporting/public/panel_actions/get_csv_panel_action.test.ts @@ -8,9 +8,12 @@ import * as Rx from 'rxjs'; import { first } from 'rxjs/operators'; import { CoreStart } from 'src/core/public'; +import type { SearchSource } from 'src/plugins/data/common'; +import type { SavedSearch } from 'src/plugins/discover/public'; import { coreMock } from '../../../../../src/core/public/mocks'; -import { LicensingPluginSetup } from '../../../licensing/public'; +import type { ILicense, LicensingPluginSetup } from '../../../licensing/public'; import { ReportingAPIClient } from '../lib/reporting_api_client'; +import type { ActionContext } from './get_csv_panel_action'; import { ReportingCsvPanelAction } from './get_csv_panel_action'; type LicenseResults = 'valid' | 'invalid' | 'unavailable' | 'expired'; @@ -19,9 +22,9 @@ const core = coreMock.createSetup(); let apiClient: ReportingAPIClient; describe('GetCsvReportPanelAction', () => { - let context: any; - let mockLicense$: any; - let mockSearchSource: any; + let context: ActionContext; + let mockLicense$: (state?: LicenseResults) => Rx.Observable; + let mockSearchSource: SearchSource; let mockStartServicesPayload: [CoreStart, object, unknown]; let mockStartServices$: Rx.Subject; @@ -54,15 +57,15 @@ describe('GetCsvReportPanelAction', () => { null, ]; - mockSearchSource = { + mockSearchSource = ({ createCopy: () => mockSearchSource, removeField: jest.fn(), setField: jest.fn(), getField: jest.fn(), getSerializedFields: jest.fn().mockImplementation(() => ({})), - }; + } as unknown) as SearchSource; - context = { + context = ({ embeddable: { type: 'search', getSavedSearch: () => { @@ -78,7 +81,7 @@ describe('GetCsvReportPanelAction', () => { }, }), }, - } as any; + } as unknown) as ActionContext; }); it('translates empty embeddable context into job params', async () => { @@ -105,18 +108,18 @@ describe('GetCsvReportPanelAction', () => { }); it('translates embeddable context into job params', async () => { - mockSearchSource = { + mockSearchSource = ({ createCopy: () => mockSearchSource, removeField: jest.fn(), setField: jest.fn(), getField: jest.fn(), getSerializedFields: jest.fn().mockImplementation(() => ({ testData: 'testDataValue' })), - }; + } as unknown) as SearchSource; context.embeddable.getSavedSearch = () => { - return { + return ({ searchSource: mockSearchSource, columns: ['column_a', 'column_b'], - }; + } as unknown) as SavedSearch; }; const panel = new ReportingCsvPanelAction({ diff --git a/x-pack/plugins/reporting/public/panel_actions/get_csv_panel_action.tsx b/x-pack/plugins/reporting/public/panel_actions/get_csv_panel_action.tsx index 8b6e258c06535..eb14e32160869 100644 --- a/x-pack/plugins/reporting/public/panel_actions/get_csv_panel_action.tsx +++ b/x-pack/plugins/reporting/public/panel_actions/get_csv_panel_action.tsx @@ -29,7 +29,7 @@ function isSavedSearchEmbeddable( return embeddable.type === SEARCH_EMBEDDABLE_TYPE; } -interface ActionContext { +export interface ActionContext { embeddable: ISearchEmbeddable; } diff --git a/x-pack/plugins/reporting/public/share_context_menu/reporting_panel_content.tsx b/x-pack/plugins/reporting/public/share_context_menu/reporting_panel_content.tsx index 64f1ecddcbb41..59afa91aaa9c3 100644 --- a/x-pack/plugins/reporting/public/share_context_menu/reporting_panel_content.tsx +++ b/x-pack/plugins/reporting/public/share_context_menu/reporting_panel_content.tsx @@ -18,7 +18,7 @@ import { import { i18n } from '@kbn/i18n'; import { FormattedMessage, InjectedIntl, injectI18n } from '@kbn/i18n/react'; import React, { Component, ReactElement } from 'react'; -import { ToastsSetup, IUiSettingsClient } from 'src/core/public'; +import { IUiSettingsClient, ToastsSetup } from 'src/core/public'; import url from 'url'; import { toMountPoint } from '../../../../../src/plugins/kibana_react/public'; import { @@ -41,7 +41,7 @@ export interface ReportingPanelProps { layoutId?: string; objectId?: string; getJobParams: () => Omit; - options?: ReactElement | null; + options?: ReactElement | null; isDirty?: boolean; onClose?: () => void; } @@ -277,7 +277,7 @@ class ReportingPanelContentUi extends Component { this.props.onClose(); } }) - .catch((error: any) => { + .catch((error) => { this.props.toasts.addError(error, { title: intl.formatMessage({ id: 'xpack.reporting.panelContent.notification.reportingErrorTitle', diff --git a/x-pack/plugins/reporting/public/shared_imports.ts b/x-pack/plugins/reporting/public/shared_imports.ts index a18ceaf151c7d..e719d720a7895 100644 --- a/x-pack/plugins/reporting/public/shared_imports.ts +++ b/x-pack/plugins/reporting/public/shared_imports.ts @@ -5,18 +5,14 @@ * 2.0. */ -export type { - SharePluginSetup, - SharePluginStart, - LocatorPublic, -} from '../../../../src/plugins/share/public'; +export type { SharePluginSetup, SharePluginStart, LocatorPublic } from 'src/plugins/share/public'; export { useRequest, UseRequestResponse } from '../../../../src/plugins/es_ui_shared/public'; export { KibanaContextProvider } from '../../../../src/plugins/kibana_react/public'; import { useKibana as _useKibana } from '../../../../src/plugins/kibana_react/public'; -import { KibanaContext } from './types'; +import type { KibanaContext } from './types'; export const useKibana = () => _useKibana(); export type { SerializableRecord } from '@kbn/utility-types'; @@ -24,3 +20,5 @@ export type { SerializableRecord } from '@kbn/utility-types'; export type { UiActionsSetup, UiActionsStart } from 'src/plugins/ui_actions/public'; export type { ManagementAppMountParams } from 'src/plugins/management/public'; + +export type { LicenseCheck } from '../../licensing/public';