Skip to content

Commit

Permalink
TypeScript project references for infra plugin (#90118)
Browse files Browse the repository at this point in the history
Co-authored-by: Felix Stürmer <[email protected]>
  • Loading branch information
smith and weltenwort authored Feb 8, 2021
1 parent 5176aa6 commit e4794af
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,21 @@ export type GetLogAlertsChartPreviewDataSuccessResponsePayload = rt.TypeOf<
typeof getLogAlertsChartPreviewDataSuccessResponsePayloadRT
>;

export const getLogAlertsChartPreviewDataAlertParamsSubsetRT = rt.intersection([
// This should not have an explicit `any` return type, but it's here because its
// inferred type includes `Comparator` which is a string enum exported from
// common/alerting/logs/log_threshold/types.ts.
//
// There's a bug that's fixed in TypeScript 4.2.0 that will allow us to remove
// the `:any` from this, so remove it when that update happens.
//
// If it's removed before then you get:
//
// x-pack/plugins/infra/common/http_api/log_alerts/chart_preview_data.ts:44:14 - error TS4023:
// Exported variable 'getLogAlertsChartPreviewDataAlertParamsSubsetRT' has or is using name 'Comparator'
// from external module "/Users/smith/Code/kibana/x-pack/plugins/infra/common/alerting/logs/log_threshold/types"
// but cannot be named.
//
export const getLogAlertsChartPreviewDataAlertParamsSubsetRT: any = rt.intersection([
rt.type({
criteria: countCriteriaRT,
timeUnit: timeUnitRT,
Expand Down
41 changes: 29 additions & 12 deletions x-pack/plugins/infra/common/http_api/shared/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,35 @@

import * as rt from 'io-ts';

const createErrorRuntimeType = <Attributes extends rt.Mixed = rt.UndefinedType>(
statusCode: number,
errorCode: string,
attributes?: Attributes
) =>
export const badRequestErrorRT = rt.intersection([
rt.type({
statusCode: rt.literal(statusCode),
error: rt.literal(errorCode),
statusCode: rt.literal(400),
error: rt.literal('Bad Request'),
message: rt.string,
...(!!attributes ? { attributes } : {}),
});
}),
rt.partial({
attributes: rt.unknown,
}),
]);

export const badRequestErrorRT = createErrorRuntimeType(400, 'Bad Request');
export const forbiddenErrorRT = createErrorRuntimeType(403, 'Forbidden');
export const conflictErrorRT = createErrorRuntimeType(409, 'Conflict');
export const forbiddenErrorRT = rt.intersection([
rt.type({
statusCode: rt.literal(403),
error: rt.literal('Forbidden'),
message: rt.string,
}),
rt.partial({
attributes: rt.unknown,
}),
]);

export const conflictErrorRT = rt.intersection([
rt.type({
statusCode: rt.literal(409),
error: rt.literal('Conflict'),
message: rt.string,
}),
rt.partial({
attributes: rt.unknown,
}),
]);
2 changes: 1 addition & 1 deletion x-pack/plugins/infra/common/inventory_models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export const ESTopHitsAggRT = rt.type({
top_hits: rt.object,
});

interface SnapshotTermsWithAggregation {
export interface SnapshotTermsWithAggregation {
terms: { field: string };
aggregations: MetricsUIAggregation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isEqual } from 'lodash';
import { useState } from 'react';
import { MetricsExplorerMetric } from '../../../../common/http_api/metrics_explorer';

interface MetricThresholdPrefillOptions {
export interface MetricThresholdPrefillOptions {
groupBy: string | string[] | undefined;
filterQuery: string | undefined;
metrics: MetricsExplorerMetric[];
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/infra/public/components/document_title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ const wrapWithSharedState = () => {
return null;
}

private getTitle(title: TitleProp) {
public getTitle(title: TitleProp) {
return typeof title === 'function' ? title(titles[this.state.index - 1]) : title;
}

private pushTitle(title: string) {
public pushTitle(title: string) {
titles[this.state.index] = title;
}

private removeTitle() {
public removeTitle() {
titles.pop();
}

private updateDocumentTitle() {
public updateDocumentTitle() {
const title = (titles[titles.length - 1] || '') + TITLE_SUFFIX;
if (title !== document.title) {
document.title = title;
Expand Down
18 changes: 12 additions & 6 deletions x-pack/plugins/infra/public/components/eui/toolbar/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
*/

import { EuiPanel } from '@elastic/eui';
import { FunctionComponent } from 'react';
import { StyledComponent } from 'styled-components';
import { euiStyled, EuiTheme } from '../../../../../../../src/plugins/kibana_react/common';

import { euiStyled } from '../../../../../../../src/plugins/kibana_react/common';

export const Toolbar = euiStyled(EuiPanel).attrs(() => ({
grow: false,
paddingSize: 'none',
}))`
// The return type of this component needs to be specified because the inferred
// return type depends on types that are not exported from EUI. You get a TS4023
// error if the return type is not specified.
export const Toolbar: StyledComponent<FunctionComponent, EuiTheme> = euiStyled(EuiPanel).attrs(
() => ({
grow: false,
paddingSize: 'none',
})
)`
border-top: none;
border-right: none;
border-left: none;
Expand Down
14 changes: 10 additions & 4 deletions x-pack/plugins/infra/public/components/fixed_datepicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
* 2.0.
*/

import React from 'react';

import { EuiDatePicker, EuiDatePickerProps } from '@elastic/eui';
import { euiStyled } from '../../../../../src/plugins/kibana_react/common';
import React, { FunctionComponent } from 'react';
import { StyledComponent } from 'styled-components';
import { euiStyled, EuiTheme } from '../../../../../src/plugins/kibana_react/common';

export const FixedDatePicker = euiStyled(
// The return type of this component needs to be specified because the inferred
// return type depends on types that are not exported from EUI. You get a TS4023
// error if the return type is not specified.
export const FixedDatePicker: StyledComponent<
FunctionComponent<EuiDatePickerProps>,
EuiTheme
> = euiStyled(
({
className,
inputClassName,
Expand Down
17 changes: 12 additions & 5 deletions x-pack/plugins/infra/public/components/toolbar_panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
* 2.0.
*/

import { FunctionComponent } from 'react';
import { EuiPanel } from '@elastic/eui';
import { euiStyled } from '../../../../../src/plugins/kibana_react/common';
import { StyledComponent } from 'styled-components';
import { EuiTheme, euiStyled } from '../../../../../src/plugins/kibana_react/common';

export const ToolbarPanel = euiStyled(EuiPanel).attrs(() => ({
grow: false,
paddingSize: 'none',
}))`
// The return type of this component needs to be specified because the inferred
// return type depends on types that are not exported from EUI. You get a TS4023
// error if the return type is not specified.
export const ToolbarPanel: StyledComponent<FunctionComponent, EuiTheme> = euiStyled(EuiPanel).attrs(
() => ({
grow: false,
paddingSize: 'none',
})
)`
border-top: none;
border-right: none;
border-left: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const initialState = {

type State = Readonly<typeof initialState>;

export const CustomFieldPanel = class extends React.PureComponent<Props, State> {
export class CustomFieldPanel extends React.PureComponent<Props, State> {
public static displayName = 'CustomFieldPanel';
public readonly state: State = initialState;
public render() {
Expand Down Expand Up @@ -86,4 +86,4 @@ export const CustomFieldPanel = class extends React.PureComponent<Props, State>
private handleFieldSelection = (selectedOptions: SelectedOption[]) => {
this.setState({ selectedOptions });
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface Props {
currentTime: number;
}

export const Node = class extends React.PureComponent<Props, State> {
export class Node extends React.PureComponent<Props, State> {
public readonly state: State = initialState;
public render() {
const { nodeType, node, options, squareSize, bounds, formatter, currentTime } = this.props;
Expand Down Expand Up @@ -164,7 +164,7 @@ export const Node = class extends React.PureComponent<Props, State> {
this.setState({ isPopoverOpen: false });
}
};
};
}

const NodeContainer = euiStyled.div`
position: relative;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const initialState = {

type State = Readonly<typeof initialState>;

export const WaffleGroupByControls = class extends React.PureComponent<Props, State> {
export class WaffleGroupByControls extends React.PureComponent<Props, State> {
public static displayName = 'WaffleGroupByControls';
public readonly state: State = initialState;

Expand Down Expand Up @@ -192,7 +192,7 @@ export const WaffleGroupByControls = class extends React.PureComponent<Props, St
}
this.handleClose();
};
};
}

const StyledContextMenu = euiStyled(EuiContextMenu)`
width: 320px;
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/infra/public/utils/use_tracked_promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,22 +235,22 @@ export const useTrackedPromise = <Arguments extends any[], Result>(
return [promiseState, execute] as [typeof promiseState, typeof execute];
};

interface UninitializedPromiseState {
export interface UninitializedPromiseState {
state: 'uninitialized';
}

interface PendingPromiseState<ResolvedValue> {
export interface PendingPromiseState<ResolvedValue> {
state: 'pending';
promise: Promise<ResolvedValue>;
}

interface ResolvedPromiseState<ResolvedValue> {
export interface ResolvedPromiseState<ResolvedValue> {
state: 'resolved';
promise: Promise<ResolvedValue>;
value: ResolvedValue;
}

interface RejectedPromiseState<ResolvedValue, RejectedValue> {
export interface RejectedPromiseState<ResolvedValue, RejectedValue> {
state: 'rejected';
promise: Promise<ResolvedValue>;
value: RejectedValue;
Expand Down
36 changes: 36 additions & 0 deletions x-pack/plugins/infra/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"outDir": "./target/types",
"emitDeclarationOnly": true,
"declaration": true,
"declarationMap": true
},
"include": [
"../../typings/**/*",
"common/**/*",
"public/**/*",
"scripts/**/*",
"server/**/*",
"types/**/*"
],
"references": [
{ "path": "../../../src/core/tsconfig.json" },
{ "path": "../../../src/plugins/data/tsconfig.json" },
{ "path": "../../../src/plugins/embeddable/tsconfig.json" },
{ "path": "../../../src/plugins/home/tsconfig.json" },
{ "path": "../../../src/plugins/kibana_utils/tsconfig.json" },
{ "path": "../../../src/plugins/kibana_react/tsconfig.json" },
{ "path": "../../../src/plugins/usage_collection/tsconfig.json" },
{ "path": "../../../src/plugins/vis_type_timeseries/tsconfig.json" },
{ "path": "../data_enhanced/tsconfig.json" },
{ "path": "../alerts/tsconfig.json" },
{ "path": "../features/tsconfig.json" },
{ "path": "../license_management/tsconfig.json" },
{ "path": "../ml/tsconfig.json" },
{ "path": "../observability/tsconfig.json" },
{ "path": "../spaces/tsconfig.json" },
{ "path": "../triggers_actions_ui/tsconfig.json" }
]
}
Loading

0 comments on commit e4794af

Please sign in to comment.