Skip to content

Commit

Permalink
Convert ILM remove_lifecycle_confirm_modal component to TS. (#70382)
Browse files Browse the repository at this point in the history
- Also convert api and api_errors services, and improve typing of http service.
- Fix bug where fatalErrors service was improperly consumed in api_errors.
- Improve typing in Rollup api_errors service, for consistency.
  • Loading branch information
cjcenizal authored Jul 20, 2020
1 parent 85d8ec8 commit 88e8c30
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export const setupEnvironment = () => {
);

mockHttpClient.interceptors.response.use(({ data }) => data);
// This expects HttpSetup but we're giving it AxiosInstance.
// @ts-ignore
initHttp(mockHttpClient);
const { server, httpRequestsMockHelpers } = initHttpRequests();

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

import { METRIC_TYPE } from '@kbn/analytics';
import { trackUiMetric } from './ui_metric';

import {
UIM_POLICY_DELETE,
Expand All @@ -15,8 +14,13 @@ import {
UIM_INDEX_RETRY_STEP,
} from '../constants';

import { trackUiMetric } from './ui_metric';
import { sendGet, sendPost, sendDelete, useRequest } from './http';

interface GenericObject {
[key: string]: any;
}

export async function loadNodes() {
return await sendGet(`nodes/list`);
}
Expand All @@ -33,7 +37,7 @@ export async function loadPolicies(withIndices: boolean) {
return await sendGet('policies', { withIndices });
}

export async function savePolicy(policy: any) {
export async function savePolicy(policy: GenericObject) {
return await sendPost(`policies`, policy);
}

Expand All @@ -58,14 +62,14 @@ export const removeLifecycleForIndex = async (indexNames: string[]) => {
return response;
};

export const addLifecyclePolicyToIndex = async (body: any) => {
export const addLifecyclePolicyToIndex = async (body: GenericObject) => {
const response = await sendPost(`index/add`, body);
// Only track successful actions.
trackUiMetric(METRIC_TYPE.COUNT, UIM_POLICY_ATTACH_INDEX);
return response;
};

export const addLifecyclePolicyToTemplate = async (body: any) => {
export const addLifecyclePolicyToTemplate = async (body: GenericObject) => {
const response = await sendPost(`template`, body);
// Only track successful actions.
trackUiMetric(METRIC_TYPE.COUNT, UIM_POLICY_ATTACH_INDEX_TEMPLATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { IHttpFetchError } from 'src/core/public';
import { fatalErrors, toasts } from './notification';

function createToastConfig(error, errorTitle) {
function createToastConfig(error: IHttpFetchError, errorTitle: string) {
if (error && error.body) {
// Error body shape is defined by the API.
const { error: errorString, statusCode, message } = error.body;

return {
Expand All @@ -17,7 +19,7 @@ function createToastConfig(error, errorTitle) {
}
}

export function showApiWarning(error, errorTitle) {
export function showApiWarning(error: IHttpFetchError, errorTitle: string) {
const toastConfig = createToastConfig(error, errorTitle);

if (toastConfig) {
Expand All @@ -26,10 +28,10 @@ export function showApiWarning(error, errorTitle) {

// This error isn't an HTTP error, so let the fatal error screen tell the user something
// unexpected happened.
return fatalErrors(error, errorTitle);
return fatalErrors.add(error, errorTitle);
}

export function showApiError(error, errorTitle) {
export function showApiError(error: IHttpFetchError, errorTitle: string) {
const toastConfig = createToastConfig(error, errorTitle);

if (toastConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { HttpSetup } from 'src/core/public';
import {
UseRequestConfig,
useRequest as _useRequest,
Error,
} from '../../../../../../src/plugins/es_ui_shared/public';

let _httpClient: any;
interface GenericObject {
[key: string]: any;
}

let _httpClient: HttpSetup;

export function init(httpClient: any): void {
export function init(httpClient: HttpSetup): void {
_httpClient = httpClient;
}

Expand All @@ -26,15 +31,15 @@ function getFullPath(path: string): string {
return apiPrefix;
}

export function sendPost(path: string, payload: any): any {
export function sendPost(path: string, payload: GenericObject) {
return _httpClient.post(getFullPath(path), { body: JSON.stringify(payload) });
}

export function sendGet(path: string, query?: any): any {
export function sendGet(path: string, query?: GenericObject): any {
return _httpClient.get(getFullPath(path), { query });
}

export function sendDelete(path: string): any {
export function sendDelete(path: string) {
return _httpClient.delete(getFullPath(path));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ import { removeLifecycleForIndex } from '../../application/services/api';
import { showApiError } from '../../application/services/api_errors';
import { toasts } from '../../application/services/notification';

export class RemoveLifecyclePolicyConfirmModal extends Component {
constructor(props) {
super(props);
this.state = {
policies: [],
selectedPolicyName: null,
selectedAlias: null,
};
}
interface Props {
indexNames: string[];
closeModal: () => void;
reloadIndices: () => void;
}

export class RemoveLifecyclePolicyConfirmModal extends Component<Props> {
removePolicy = async () => {
const { indexNames, closeModal, reloadIndices } = this.props;

Expand Down
10 changes: 6 additions & 4 deletions x-pack/plugins/rollup/public/crud_app/services/api_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { IHttpFetchError } from 'src/core/public';
import { getNotifications, getFatalErrors } from '../../kibana_services';

function createToastConfig(error: any, errorTitle: string) {
// Expect an error in the shape provided by http service.
function createToastConfig(error: IHttpFetchError, errorTitle: string) {
if (error && error.body) {
// Error body shape is defined by the API.
const { error: errorString, statusCode, message } = error.body;

return {
title: errorTitle,
text: `${statusCode}: ${errorString}. ${message}`,
};
}
}

export function showApiWarning(error: any, errorTitle: string) {
export function showApiWarning(error: IHttpFetchError, errorTitle: string) {
const toastConfig = createToastConfig(error, errorTitle);

if (toastConfig) {
Expand All @@ -29,7 +31,7 @@ export function showApiWarning(error: any, errorTitle: string) {
return getFatalErrors().add(error, errorTitle);
}

export function showApiError(error: any, errorTitle: string) {
export function showApiError(error: IHttpFetchError, errorTitle: string) {
const toastConfig = createToastConfig(error, errorTitle);

if (toastConfig) {
Expand Down

0 comments on commit 88e8c30

Please sign in to comment.