Skip to content

Commit d513a43

Browse files
committed
Better type safety for alert list API
1 parent c4b7147 commit d513a43

10 files changed

Lines changed: 47 additions & 41 deletions

File tree

x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { AlertListData } from './types';
7+
import { AlertListData } from '../../types';
88

99
interface ServerReturnedAlertsData {
1010
type: 'serverReturnedAlertsData';

x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
export { alertListReducer } from './reducer';
88
export { AlertAction } from './action';
9-
export * from './types';
9+
export * from '../../types';

x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/reducer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { Reducer } from 'redux';
8-
import { AlertListState } from './types';
8+
import { AlertListState } from '../../types';
99
import { AppAction } from '../action';
1010

1111
const initialState = (): AlertListState => {
@@ -25,7 +25,7 @@ export const alertListReducer: Reducer<AlertListState, AppAction> = (
2525
if (action.type === 'serverReturnedAlertsData') {
2626
return {
2727
...state,
28-
...action.payload,
28+
alerts: action.payload.alerts,
2929
};
3030
}
3131

x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/selectors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { AlertListState } from './types';
7+
import { AlertListState } from '../../types';
88

99
export const alertListData = (state: AlertListState) => state.alerts;

x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/types.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

x-pack/plugins/endpoint/public/applications/endpoint/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
import { Dispatch, MiddlewareAPI } from 'redux';
88
import { CoreStart } from 'kibana/public';
9-
import { AlertListState } from './store/alerts';
109
import { EndpointListState } from './store/endpoint_list';
1110
import { AppAction } from './store/action';
11+
import { AlertResultList } from '../../../common/types';
1212

1313
export type MiddlewareFactory = (
1414
coreStart: CoreStart
@@ -20,3 +20,6 @@ export interface GlobalState {
2020
readonly endpointList: EndpointListState;
2121
readonly alertList: AlertListState;
2222
}
23+
24+
export type AlertListData = AlertResultList;
25+
export type AlertListState = AlertResultList;

x-pack/plugins/endpoint/server/routes/alerts.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { IRouter } from 'kibana/server';
8-
import { RequestHandler } from 'kibana/server';
7+
import { IRouter, KibanaRequest, RequestHandler } from 'kibana/server';
98
import { SearchResponse } from 'elasticsearch';
10-
import { schema, TypeOf } from '@kbn/config-schema';
9+
import { schema } from '@kbn/config-schema';
1110

1211
import {
1312
getPagingProperties,
14-
kibanaRequestToAlertListQuery,
13+
buildAlertListESQuery,
1514
} from '../services/endpoint/alert_query_builders';
1615

1716
import { AlertData, AlertResultList } from '../../common/types';
18-
import { EndpointAppContext } from '../types';
17+
import { AlertRequestParams, EndpointAppContext } from '../types';
1918

2019
const ALERTS_ROUTE = '/api/endpoint/alerts';
2120

@@ -25,14 +24,13 @@ export const reqSchema = schema.object({
2524
});
2625

2726
export function registerAlertRoutes(router: IRouter, endpointAppContext: EndpointAppContext) {
28-
const alertsHandler: RequestHandler<unknown, TypeOf<typeof reqSchema>> = async (
29-
ctx,
30-
req,
31-
res
32-
) => {
27+
const alertsHandler: RequestHandler<unknown, AlertRequestParams> = async (ctx, req, res) => {
3328
try {
34-
const queryParams = await getPagingProperties(req, endpointAppContext);
35-
const reqBody = await kibanaRequestToAlertListQuery(queryParams, endpointAppContext);
29+
const queryParams = await getPagingProperties(
30+
req as KibanaRequest<unknown, AlertRequestParams, AlertRequestParams, any>,
31+
endpointAppContext
32+
);
33+
const reqBody = await buildAlertListESQuery(queryParams);
3634
const response = (await ctx.core.elasticsearch.dataClient.callAsCurrentUser(
3735
'search',
3836
reqBody

x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
*/
66
import { httpServerMock, loggingServiceMock } from 'src/core/server/mocks';
77
import { EndpointConfigSchema } from '../../config';
8-
import { getPagingProperties, kibanaRequestToAlertListQuery } from './alert_query_builders';
8+
import { getPagingProperties, buildAlertListESQuery } from './alert_query_builders';
99

1010
describe('test query builder', () => {
1111
describe('test query builder request processing', () => {
1212
it('should execute the correct Elasticsearch query for a default request', async () => {
13-
const mockRequest = httpServerMock.createKibanaRequest({
14-
body: {},
15-
});
13+
const mockRequest = httpServerMock.createKibanaRequest({});
1614
const mockCtx = {
1715
logFactory: loggingServiceMock.create(),
1816
config: () => Promise.resolve(EndpointConfigSchema.validate({})),
1917
};
2018
const queryParams = await getPagingProperties(mockRequest, mockCtx);
21-
const query = await kibanaRequestToAlertListQuery(queryParams, mockCtx);
19+
const query = await buildAlertListESQuery(queryParams);
2220

2321
expect(query).toEqual({
2422
body: {
@@ -51,7 +49,7 @@ describe('test query builder', () => {
5149
config: () => Promise.resolve(EndpointConfigSchema.validate({})),
5250
};
5351
const queryParams = await getPagingProperties(mockRequest, mockCtx);
54-
const query = await kibanaRequestToAlertListQuery(queryParams, mockCtx);
52+
const query = await buildAlertListESQuery(queryParams);
5553

5654
expect(query).toEqual({
5755
body: {

x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
*/
66
import { KibanaRequest } from 'kibana/server';
77
import { EndpointAppConstants } from '../../../common/types';
8-
import { EndpointAppContext } from '../../types';
8+
import { EndpointAppContext, AlertRequestParams, JSONish } from '../../types';
99

10-
export const kibanaRequestToAlertListQuery = async (
11-
pagingProperties: Record<string, any>,
12-
endpointAppContext: EndpointAppContext
13-
): Promise<Record<string, any>> => {
10+
export const buildAlertListESQuery = async (
11+
pagingProperties: Record<string, number>
12+
): Promise<JSONish> => {
1413
const DEFAULT_TOTAL_HITS = 10000;
1514

1615
// Calculate minimum total hits set to indicate there's a next page
@@ -40,9 +39,9 @@ export const kibanaRequestToAlertListQuery = async (
4039
};
4140

4241
export const getPagingProperties = async (
43-
request: KibanaRequest<any, any, any>,
42+
request: KibanaRequest<unknown, AlertRequestParams, AlertRequestParams>,
4443
endpointAppContext: EndpointAppContext
45-
): Promise<Record<string, any>> => {
44+
): Promise<Record<string, number>> => {
4645
const config = await endpointAppContext.config();
4746
const pagingProperties: { page_size?: number; page_index?: number } = {};
4847

x-pack/plugins/endpoint/server/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,25 @@
66
import { LoggerFactory } from 'kibana/server';
77
import { EndpointConfigType } from './config';
88

9+
/**
10+
* A JSON-like structure.
11+
*/
12+
export interface JSONish {
13+
[key: string]: number | string | null | undefined | JSONish | JSONish[];
14+
}
15+
16+
/**
17+
* The context for Endpoint apps.
18+
*/
919
export interface EndpointAppContext {
1020
logFactory: LoggerFactory;
1121
config(): Promise<EndpointConfigType>;
1222
}
23+
24+
/**
25+
* Request params for alert queries.
26+
*/
27+
export interface AlertRequestParams {
28+
page_index?: number;
29+
page_size?: number;
30+
}

0 commit comments

Comments
 (0)