Skip to content

Commit

Permalink
Upgrade fp-ts to 2.8.6 (#83866) (#84436)
Browse files Browse the repository at this point in the history
* Upgrade fp-ts to 2.8.6

* reduce import size from io-ts

* removed unused imports

* remove usage of fpts from alerts

Co-authored-by: Gidi Meir Morris <github@gidi.io>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Gidi Meir Morris <github@gidi.io>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 26, 2020
1 parent 7dbfd26 commit aba0b31
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 105 deletions.
66 changes: 1 addition & 65 deletions x-pack/plugins/alerts/public/alert_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { AlertType } from '../common';
import { httpServiceMock } from '../../../../src/core/public/mocks';
import { loadAlert, loadAlertState, loadAlertType, loadAlertTypes } from './alert_api';
import { loadAlert, loadAlertType, loadAlertTypes } from './alert_api';
import uuid from 'uuid';

const http = httpServiceMock.createStartContract();
Expand Down Expand Up @@ -114,67 +114,3 @@ describe('loadAlert', () => {
expect(http.get).toHaveBeenCalledWith(`/api/alerts/alert/${alertId}`);
});
});

describe('loadAlertState', () => {
test('should call get API with base parameters', async () => {
const alertId = uuid.v4();
const resolvedValue = {
alertTypeState: {
some: 'value',
},
alertInstances: {
first_instance: {},
second_instance: {},
},
};
http.get.mockResolvedValueOnce(resolvedValue);

expect(await loadAlertState({ http, alertId })).toEqual(resolvedValue);
expect(http.get).toHaveBeenCalledWith(`/api/alerts/alert/${alertId}/state`);
});

test('should parse AlertInstances', async () => {
const alertId = uuid.v4();
const resolvedValue = {
alertTypeState: {
some: 'value',
},
alertInstances: {
first_instance: {
state: {},
meta: {
lastScheduledActions: {
group: 'first_group',
date: '2020-02-09T23:15:41.941Z',
},
},
},
},
};
http.get.mockResolvedValueOnce(resolvedValue);

expect(await loadAlertState({ http, alertId })).toEqual({
...resolvedValue,
alertInstances: {
first_instance: {
state: {},
meta: {
lastScheduledActions: {
group: 'first_group',
date: new Date('2020-02-09T23:15:41.941Z'),
},
},
},
},
});
expect(http.get).toHaveBeenCalledWith(`/api/alerts/alert/${alertId}/state`);
});

test('should handle empty response from api', async () => {
const alertId = uuid.v4();
http.get.mockResolvedValueOnce('');

expect(await loadAlertState({ http, alertId })).toEqual({});
expect(http.get).toHaveBeenCalledWith(`/api/alerts/alert/${alertId}/state`);
});
});
41 changes: 7 additions & 34 deletions x-pack/plugins/alerts/public/alert_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@
*/

import { HttpSetup } from 'kibana/public';
import * as t from 'io-ts';
import { pipe } from 'fp-ts/lib/pipeable';
import { fold } from 'fp-ts/lib/Either';
import { findFirst } from 'fp-ts/lib/Array';
import { isNone } from 'fp-ts/lib/Option';

import { i18n } from '@kbn/i18n';
import { BASE_ALERT_API_PATH, alertStateSchema } from '../common';
import { Alert, AlertType, AlertTaskState } from '../common';
import { BASE_ALERT_API_PATH } from '../common';
import type { Alert, AlertType } from '../common';

export async function loadAlertTypes({ http }: { http: HttpSetup }): Promise<AlertType[]> {
return await http.get(`${BASE_ALERT_API_PATH}/list_alert_types`);
Expand All @@ -26,10 +20,10 @@ export async function loadAlertType({
http: HttpSetup;
id: AlertType['id'];
}): Promise<AlertType> {
const maybeAlertType = findFirst<AlertType>((type) => type.id === id)(
await http.get(`${BASE_ALERT_API_PATH}/list_alert_types`)
);
if (isNone(maybeAlertType)) {
const maybeAlertType = ((await http.get(
`${BASE_ALERT_API_PATH}/list_alert_types`
)) as AlertType[]).find((type) => type.id === id);
if (!maybeAlertType) {
throw new Error(
i18n.translate('xpack.alerts.loadAlertType.missingAlertTypeError', {
defaultMessage: 'Alert type "{id}" is not registered.',
Expand All @@ -39,7 +33,7 @@ export async function loadAlertType({
})
);
}
return maybeAlertType.value;
return maybeAlertType;
}

export async function loadAlert({
Expand All @@ -51,24 +45,3 @@ export async function loadAlert({
}): Promise<Alert> {
return await http.get(`${BASE_ALERT_API_PATH}/alert/${alertId}`);
}

type EmptyHttpResponse = '';
export async function loadAlertState({
http,
alertId,
}: {
http: HttpSetup;
alertId: string;
}): Promise<AlertTaskState> {
return await http
.get(`${BASE_ALERT_API_PATH}/alert/${alertId}/state`)
.then((state: AlertTaskState | EmptyHttpResponse) => (state ? state : {}))
.then((state: AlertTaskState) => {
return pipe(
alertStateSchema.decode(state),
fold((e: t.Errors) => {
throw new Error(`Alert "${alertId}" has invalid state`);
}, t.identity)
);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { HttpSetup } from 'kibana/public';
import * as t from 'io-ts';
import { Errors, identity } from 'io-ts';
import { pipe } from 'fp-ts/lib/pipeable';
import { fold } from 'fp-ts/lib/Either';
import { pick } from 'lodash';
Expand Down Expand Up @@ -48,9 +48,9 @@ export async function loadAlertState({
.then((state: AlertTaskState) => {
return pipe(
alertStateSchema.decode(state),
fold((e: t.Errors) => {
fold((e: Errors) => {
throw new Error(`Alert "${alertId}" has invalid state`);
}, t.identity)
}, identity)
);
});
}
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14180,9 +14180,9 @@ fp-ts@^1.0.0:
integrity sha512-fWwnAgVlTsV26Ruo9nx+fxNHIm6l1puE1VJ/C0XJ3nRQJJJIgRHYw6sigB3MuNFZL1o4fpGlhwFhcbxHK0RsOA==

fp-ts@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/fp-ts/-/fp-ts-2.3.1.tgz#8068bfcca118227932941101e062134d7ecd9119"
integrity sha512-KevPBnYt0aaJiuUzmU9YIxjrhC9AgJ8CLtLlXmwArovlNTeYM5NtEoKd86B0wHd7FIbzeE8sNXzCoYIOr7e6Iw==
version "2.8.6"
resolved "https://registry.yarnpkg.com/fp-ts/-/fp-ts-2.8.6.tgz#1a0e6c3f29f5b0fbfa3120f034ea266aa73c811b"
integrity sha512-fGGpKf/Jy3UT4s16oM+hr/8F5QXFcZ+20NAvaZXH5Y5jsiLPMDCaNqffXq0z1Kr6ZUJj0346cH9tq+cI2SoJ4w==

fragment-cache@^0.2.1:
version "0.2.1"
Expand Down

0 comments on commit aba0b31

Please sign in to comment.