Skip to content

Commit

Permalink
[Fleet] Use type-only imports where possible (#92979) (#93268)
Browse files Browse the repository at this point in the history
## Summary

Use [type-only `import`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export) to differentiate between runtime values and (erasable) TS types.

<img width="1499" alt="Screen Shot 2021-02-27 at 6 34 46 PM" src="https://user-images.githubusercontent.com/57655/109403323-84e32c80-792a-11eb-94b7-854d0e5c3e99.png">

Allows Babel and `tsc` to make some optimizations since it can be sure what can be erased.

It can also be helpful in situations like below which highlights that even though `KibanaAssetType` and `KibanaSavedObjectType` are named a certain way and from `common/types`, they are JS values _not_ TS types. They are `enum`s which means they are JS objects at runtime; not eliminated at runtime.

https://github.com/elastic/kibana/blob/d92a1a08d89accc2fa303b1355a9e222f9b6d090/x-pack/plugins/fleet/common/types/models/epm.ts#L41-L63

# Conflicts:
#	x-pack/plugins/fleet/server/services/index.ts
  • Loading branch information
John Schulz authored Mar 2, 2021
1 parent da76f98 commit 9d971f1
Show file tree
Hide file tree
Showing 189 changed files with 483 additions and 470 deletions.
3 changes: 2 additions & 1 deletion x-pack/plugins/fleet/common/constants/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import { defaultPackages } from './epm';
import { AgentPolicy } from '../types';
import type { AgentPolicy } from '../types';

export const AGENT_POLICY_SAVED_OBJECT_TYPE = 'ingest-agent-policies';
export const AGENT_POLICY_INDEX = '.fleet-policies';
export const agentPolicyStatuses = {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/constants/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { NewOutput } from '../types';
import type { NewOutput } from '../types';

export const OUTPUT_SAVED_OBJECT_TYPE = 'ingest-outputs';

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { NewPackagePolicy, PackagePolicy } from './types';
import type { NewPackagePolicy, PackagePolicy } from './types';

export const createNewPackagePolicyMock = (): NewPackagePolicy => {
return {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/services/agent_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { AGENT_POLLING_THRESHOLD_MS, AGENT_SAVED_OBJECT_TYPE } from '../constants';
import { Agent, AgentStatus } from '../types';
import type { Agent, AgentStatus } from '../types';

export function getAgentStatus(agent: Agent, now: number = Date.now()): AgentStatus {
const { last_checkin: lastCheckIn } = agent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { FullAgentPolicyKibanaConfig } from '../types';
import type { FullAgentPolicyKibanaConfig } from '../types';

export function getFullAgentPolicyKibanaConfig(kibanaUrls: string[]): FullAgentPolicyKibanaConfig {
// paths and protocol are validated to be the same for all urls, so use the first to get them
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { safeDump } from 'js-yaml';
import { FullAgentPolicy } from '../types';
import type { FullAgentPolicy } from '../types';

const POLICY_KEYS_ORDER = [
'id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { isAgentUpgradeable } from './is_agent_upgradeable';
import { Agent } from '../types/models/agent';
import type { Agent } from '../types/models/agent';

const getAgent = ({
version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import semverCoerce from 'semver/functions/coerce';
import semverLt from 'semver/functions/lt';
import { Agent } from '../types';
import type { Agent } from '../types';

export function isAgentUpgradeable(agent: Agent, kibanaVersion: string) {
let agentVersion: string;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/services/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { Observable, Subscription } from 'rxjs';
import { ILicense } from '../../../licensing/common/types';
import type { ILicense } from '../../../licensing/common/types';

// Generic license service class that works with the license observable
// Both server and client plugins instancates a singleton version of this class
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/services/limited_package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { PackageInfo, AgentPolicy, PackagePolicy } from '../types';
import type { PackageInfo, AgentPolicy, PackagePolicy } from '../types';

// Assume packages only ever include 1 config template for now
export const isPackageLimited = (packageInfo: PackageInfo): boolean => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { PackagePolicy, PackagePolicyInput } from '../types';
import type { PackagePolicy, PackagePolicyInput } from '../types';
import { storedPackagePoliciesToAgentInputs } from './package_policies_to_agent_inputs';

describe('Fleet - storedPackagePoliciesToAgentInputs', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { PackagePolicy, FullAgentPolicyInput, FullAgentPolicyInputStream } from '../types';
import type { PackagePolicy, FullAgentPolicyInput, FullAgentPolicyInputStream } from '../types';
import { DEFAULT_OUTPUT } from '../constants';

export const storedPackagePoliciesToAgentInputs = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { PackageInfo } from '../types';
import type { PackageInfo } from '../types';
import { packageToPackagePolicy, packageToPackagePolicyInputs } from './package_to_package_policy';

describe('Fleet - packageToPackagePolicy', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import {
import type {
PackageInfo,
RegistryPolicyTemplate,
RegistryVarsEntry,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/types/models/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { FullAgentPolicy } from './agent_policy';
import type { FullAgentPolicy } from './agent_policy';
import { AGENT_TYPE_EPHEMERAL, AGENT_TYPE_PERMANENT, AGENT_TYPE_TEMPORARY } from '../../constants';

export type AgentType =
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/fleet/common/types/models/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

import { agentPolicyStatuses } from '../../constants';
import { DataType, ValueOf } from '../../types';
import { PackagePolicy, PackagePolicyPackage } from './package_policy';
import { Output } from './output';
import type { DataType, ValueOf } from '../../types';
import type { PackagePolicy, PackagePolicyPackage } from './package_policy';
import type { Output } from './output';

export type AgentPolicyStatus = typeof agentPolicyStatuses;

Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/fleet/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// Follow pattern from https://github.com/elastic/kibana/pull/52447
// TODO: Update when https://github.com/elastic/kibana/issues/53021 is closed
import { SavedObject, SavedObjectAttributes, SavedObjectReference } from 'src/core/public';
import type { SavedObject, SavedObjectAttributes, SavedObjectReference } from 'src/core/public';
import {
ASSETS_SAVED_OBJECT_TYPE,
agentAssetTypes,
Expand All @@ -16,8 +16,8 @@ import {
installationStatuses,
requiredPackages,
} from '../../constants';
import { ValueOf } from '../../types';
import { PackageSpecManifest, PackageSpecScreenshot } from './package_spec';
import type { ValueOf } from '../../types';
import type { PackageSpecManifest, PackageSpecScreenshot } from './package_spec';

export type InstallationStatus = typeof installationStatuses;

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/types/models/package_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { RegistryPolicyTemplate } from './epm';
import type { RegistryPolicyTemplate } from './epm';

// Based on https://github.com/elastic/package-spec/blob/master/versions/1/manifest.spec.yml#L8
export interface PackageSpecManifest {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/types/models/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { SavedObjectAttributes } from 'src/core/public';
import type { SavedObjectAttributes } from 'src/core/public';

export interface BaseSettings {
agent_auto_upgrade: boolean;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/types/rest_spec/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import {
import type {
Agent,
AgentAction,
NewAgentAction,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/common/types/rest_spec/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* 2.0.
*/

import { AgentPolicy, NewAgentPolicy, FullAgentPolicy } from '../models';
import { ListWithKuery } from './common';
import type { AgentPolicy, NewAgentPolicy, FullAgentPolicy } from '../models';
import type { ListWithKuery } from './common';

export interface GetAgentPoliciesRequest {
query: ListWithKuery & {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/types/rest_spec/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { HttpFetchQuery } from 'src/core/public';
import type { HttpFetchQuery } from 'src/core/public';

export interface ListWithKuery extends HttpFetchQuery {
page?: number;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/types/rest_spec/data_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { DataStream } from '../models';
import type { DataStream } from '../models';

export interface GetDataStreamsResponse {
data_streams: DataStream[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { EnrollmentAPIKey } from '../models';
import type { EnrollmentAPIKey } from '../models';

export interface GetEnrollmentAPIKeysRequest {
query: {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/types/rest_spec/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import {
import type {
AssetReference,
CategorySummaryList,
Installable,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/types/rest_spec/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { Output } from '../models';
import type { Output } from '../models';

export interface GetOneOutputResponse {
item: Output;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { PackagePolicy, NewPackagePolicy, UpdatePackagePolicy } from '../models';
import type { PackagePolicy, NewPackagePolicy, UpdatePackagePolicy } from '../models';

export interface GetPackagePoliciesRequest {
query: {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/common/types/rest_spec/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { Settings } from '../models';
import type { Settings } from '../models';

export interface GetSettingsResponse {
item: Settings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import React, { useContext } from 'react';
import { FleetConfigType } from '../../../plugin';
import type { FleetConfigType } from '../../../plugin';

export const ConfigContext = React.createContext<FleetConfigType | null>(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { FleetStartServices } from '../../../plugin';
import type { FleetStartServices } from '../../../plugin';
import { useKibana } from '../../../../../../../src/plugins/kibana_react/public';

export function useStartServices(): FleetStartServices {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
* 2.0.
*/

import {
BASE_PATH,
StaticPage,
DynamicPage,
DynamicPagePathValues,
pagePathGetters,
} from '../constants';
import { BASE_PATH, pagePathGetters } from '../constants';
import type { StaticPage, DynamicPage, DynamicPagePathValues } from '../constants';
import { useStartServices } from './';

const getPath = (page: StaticPage | DynamicPage, values: DynamicPagePathValues = {}): string => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { useEffect, useState } from 'react';
import { ICON_TYPES } from '@elastic/eui';
import { PackageInfo, PackageListItem } from '../types';
import type { PackageInfo, PackageListItem } from '../types';
import { useLinks } from '../sections/epm/hooks';
import { sendGetPackageInfoByKey } from './index';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
* 2.0.
*/

import {
useRequest,
sendRequest,
useConditionalRequest,
SendConditionalRequestConfig,
} from './use_request';
import { useRequest, sendRequest, useConditionalRequest } from './use_request';
import type { SendConditionalRequestConfig } from './use_request';
import { agentPolicyRouteService } from '../../services';
import {
import type {
GetAgentPoliciesRequest,
GetAgentPoliciesResponse,
GetOneAgentPolicyResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
* 2.0.
*/

import { useRequest, UseRequestConfig, sendRequest } from './use_request';
import { useRequest, sendRequest } from './use_request';
import type { UseRequestConfig } from './use_request';
import { agentRouteService } from '../../services';
import {
import type {
GetOneAgentResponse,
GetOneAgentEventsResponse,
GetOneAgentEventsRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { sendRequest } from './use_request';
import { appRoutesService } from '../../services';
import { CheckPermissionsResponse } from '../../types';
import type { CheckPermissionsResponse } from '../../types';

export const sendGetPermissionsCheck = () => {
return sendRequest<CheckPermissionsResponse>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { useRequest } from './use_request';
import { dataStreamRouteService } from '../../services';
import { GetDataStreamsResponse } from '../../types';
import type { GetDataStreamsResponse } from '../../types';

export const useGetDataStreams = () => {
return useRequest<GetDataStreamsResponse>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
* 2.0.
*/

import {
useRequest,
UseRequestConfig,
sendRequest,
useConditionalRequest,
SendConditionalRequestConfig,
} from './use_request';
import { useRequest, sendRequest, useConditionalRequest } from './use_request';
import type { UseRequestConfig, SendConditionalRequestConfig } from './use_request';
import { enrollmentAPIKeyRouteService } from '../../services';
import {
import type {
GetOneEnrollmentAPIKeyResponse,
GetEnrollmentAPIKeysResponse,
GetEnrollmentAPIKeysRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { useRequest, sendRequest } from './use_request';
import { epmRouteService } from '../../services';
import {
import type {
GetCategoriesRequest,
GetCategoriesResponse,
GetPackagesRequest,
Expand All @@ -17,7 +17,7 @@ import {
InstallPackageResponse,
DeletePackageResponse,
} from '../../types';
import { GetStatsResponse } from '../../../../../common';
import type { GetStatsResponse } from '../../../../../common';

export const useGetCategories = (query: GetCategoriesRequest['query'] = {}) => {
return useRequest<GetCategoriesResponse>({
Expand Down
Loading

0 comments on commit 9d971f1

Please sign in to comment.