Skip to content

Commit 85f55bc

Browse files
authored
[7.x] [Watcher] New Platform (NP) Migration (#50908) (#52721)
* [Watcher] New Platform (NP) Migration (#50908) * First iteration of watch public -> new platform Still need to switch to np ready version of use_request * - Switched to using np ready request - Some updates after API changes * First attempt at server shim * Rename file and re-enable react hooks linting * Fix some public types and react hooks lint rules * Fix types * More ES lint react hooks fixes * Migrated server lib -> ts. Part way done with migrating routes to NP router and TS * Big subset of routes to TS and NP router - almost there * Delete legacy error wrappers and moved last set of routes to TS and NP router * Remove @ts-ignore's and update route registration to use shim with http router * Added routes validations, fixes for hooks and fixes for types * Fix more types and finish testing API routes * Fix usage of feature catalogue and fix time buckets types * Fix error message shape [skip ci] * Split legacy from new platform dependencies server-side * Refactor: Seperate client legacy and NP dependencies * Add file: added types file * Fix UISettings client type import * Update license pre-routing factory spec * Update variable names, use of I18nContext (use NP) and docs * Use NP elasticsearchclient * Simplify is_es_error_factory * Fix types * Improve code legibility and remove second use of `useAppContext` * Use @kbn/config-schema (not validate: false) on routes! * Fix watch create JSON spec * Create threshold test working * Unskip watch_edit.test.ts * Unskip watch_list.test.ts * Done re-enabling component integration tests * TimeBuckets typo + remove unnecessary // @ts-ignore * Backport for "xPack:defaultAdminEmail" 7.x specific behaviour
1 parent 9ed3fe0 commit 85f55bc

File tree

256 files changed

+2325
-2222
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+2325
-2222
lines changed

.eslintrc.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,6 @@ module.exports = {
209209
'react-hooks/rules-of-hooks': 'off',
210210
},
211211
},
212-
{
213-
files: ['x-pack/legacy/plugins/watcher/**/*.{js,ts,tsx}'],
214-
rules: {
215-
'react-hooks/rules-of-hooks': 'off',
216-
'react-hooks/exhaustive-deps': 'off',
217-
},
218-
},
219212

220213
/**
221214
* Prettier
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
declare module 'ui/time_buckets' {
21+
export const TimeBuckets: any;
22+
}

src/plugins/es_ui_shared/public/request/np_ready_request.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919

2020
import { useEffect, useState, useRef } from 'react';
2121

22-
import { HttpServiceBase } from '../../../../../src/core/public';
22+
import { HttpServiceBase, HttpFetchQuery } from '../../../../../src/core/public';
2323

2424
export interface SendRequestConfig {
2525
path: string;
2626
method: 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head';
27+
query?: HttpFetchQuery;
2728
body?: any;
2829
}
2930

@@ -48,10 +49,10 @@ export interface UseRequestResponse {
4849

4950
export const sendRequest = async (
5051
httpClient: HttpServiceBase,
51-
{ path, method, body }: SendRequestConfig
52+
{ path, method, body, query }: SendRequestConfig
5253
): Promise<SendRequestResponse> => {
5354
try {
54-
const response = await httpClient[method](path, { body });
55+
const response = await httpClient[method](path, { body, query });
5556

5657
return {
5758
data: response.data ? response.data : response,
@@ -70,6 +71,7 @@ export const useRequest = (
7071
{
7172
path,
7273
method,
74+
query,
7375
body,
7476
pollIntervalMs,
7577
initialData,
@@ -112,6 +114,7 @@ export const useRequest = (
112114
const requestBody = {
113115
path,
114116
method,
117+
query,
115118
body,
116119
};
117120

x-pack/dev-tools/jest/create_jest_config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export function createJestConfig({ kibanaDirectory, xPackKibanaDirectory }) {
2020
'uiExports/(.*)': fileMockPath,
2121
'^src/core/(.*)': `${kibanaDirectory}/src/core/$1`,
2222
'^src/legacy/(.*)': `${kibanaDirectory}/src/legacy/$1`,
23-
'^plugins/watcher/models/(.*)': `${xPackKibanaDirectory}/legacy/plugins/watcher/public/models/$1`,
23+
'^plugins/watcher/np_ready/application/models/(.*)':
24+
`${xPackKibanaDirectory}/legacy/plugins/watcher/public/np_ready/application/models/$1`,
2425
'^plugins/([^/.]*)(.*)': `${kibanaDirectory}/src/legacy/core_plugins/$1/public$2`,
2526
'^plugins/xpack_main/(.*);': `${xPackKibanaDirectory}/legacy/plugins/xpack_main/public/$1`,
2627
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': fileMockPath,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import React from 'react';
8+
import { ComponentType } from 'enzyme';
9+
import {
10+
chromeServiceMock,
11+
docLinksServiceMock,
12+
uiSettingsServiceMock,
13+
notificationServiceMock,
14+
httpServiceMock,
15+
} from '../../../../../../../src/core/public/mocks';
16+
import { AppContextProvider } from '../../../public/np_ready/application/app_context';
17+
18+
export const mockContextValue = {
19+
docLinks: docLinksServiceMock.createStartContract(),
20+
chrome: chromeServiceMock.createStartContract(),
21+
legacy: {
22+
TimeBuckets: class MockTimeBuckets {
23+
setBounds(_domain: any) {
24+
return {};
25+
}
26+
getInterval() {
27+
return {
28+
expression: {},
29+
};
30+
}
31+
},
32+
MANAGEMENT_BREADCRUMB: { text: 'test' },
33+
licenseStatus: {},
34+
},
35+
uiSettings: uiSettingsServiceMock.createSetupContract(),
36+
toasts: notificationServiceMock.createSetupContract().toasts,
37+
euiUtils: {
38+
useChartsTheme: jest.fn(),
39+
},
40+
// For our test harness, we don't use this mocked out http service
41+
http: httpServiceMock.createSetupContract(),
42+
};
43+
44+
export const withAppContext = (Component: ComponentType<any>) => (props: any) => {
45+
return (
46+
<AppContextProvider value={mockContextValue}>
47+
<Component {...props} />
48+
</AppContextProvider>
49+
);
50+
};

x-pack/legacy/plugins/watcher/server/routes/api/fields/index.js renamed to x-pack/legacy/plugins/watcher/__jest__/client_integration/helpers/body_response.ts

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

7-
export { registerFieldsRoutes } from './register_fields_routes';
7+
export const wrapBodyResponse = (obj: object) => JSON.stringify({ body: JSON.stringify(obj) });
8+
9+
export const unwrapBodyResponse = (string: string) => JSON.parse(JSON.parse(string).body);

x-pack/legacy/plugins/watcher/__jest__/client_integration/helpers/http_requests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
3434
const defaultResponse = { watchHistoryItems: [] };
3535
server.respondWith(
3636
'GET',
37-
`${API_ROOT}/watch/:id/history?startTime=*`,
37+
`${API_ROOT}/watch/:id/history`,
3838
mockResponse(defaultResponse, response)
3939
);
4040
};

x-pack/legacy/plugins/watcher/__jest__/client_integration/helpers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { setup as watchCreateThresholdSetup } from './watch_create_threshold.hel
1111
import { setup as watchEditSetup } from './watch_edit.helpers';
1212

1313
export { nextTick, getRandomString, findTestSubject, TestBed } from '../../../../../../test_utils';
14-
14+
export { wrapBodyResponse, unwrapBodyResponse } from './body_response';
1515
export { setupEnvironment } from './setup_environment';
1616

1717
export const pageHelpers = {

x-pack/legacy/plugins/watcher/__jest__/client_integration/helpers/setup_environment.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77
import axios from 'axios';
88
import axiosXhrAdapter from 'axios/lib/adapters/xhr';
99
import { init as initHttpRequests } from './http_requests';
10-
import { setHttpClient, setSavedObjectsClient } from '../../../public/lib/api';
10+
import { setHttpClient, setSavedObjectsClient } from '../../../public/np_ready/application/lib/api';
1111

1212
const mockHttpClient = axios.create({ adapter: axiosXhrAdapter });
13+
mockHttpClient.interceptors.response.use(
14+
res => {
15+
return res.data;
16+
},
17+
rej => {
18+
return Promise.reject(rej);
19+
}
20+
);
1321

1422
const mockSavedObjectsClient = () => {
1523
return {
@@ -23,7 +31,7 @@ export const setupEnvironment = () => {
2331
// @ts-ignore
2432
setHttpClient(mockHttpClient);
2533

26-
setSavedObjectsClient(mockSavedObjectsClient());
34+
setSavedObjectsClient(mockSavedObjectsClient() as any);
2735

2836
return {
2937
server,

x-pack/legacy/plugins/watcher/__jest__/client_integration/helpers/watch_create_json.helpers.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6+
import { withAppContext } from './app_context.mock';
67
import { registerTestBed, TestBed, TestBedConfig } from '../../../../../../test_utils';
7-
import { WatchEdit } from '../../../public/sections/watch_edit/components/watch_edit';
8+
import { WatchEdit } from '../../../public/np_ready/application/sections/watch_edit/components/watch_edit';
89
import { ROUTES, WATCH_TYPES } from '../../../common/constants';
9-
import { registerRouter } from '../../../public/lib/navigation';
10+
import { registerRouter } from '../../../public/np_ready/application/lib/navigation';
1011

1112
const testBedConfig: TestBedConfig = {
1213
memoryRouter: {
@@ -17,7 +18,7 @@ const testBedConfig: TestBedConfig = {
1718
doMountAsync: true,
1819
};
1920

20-
const initTestBed = registerTestBed(WatchEdit, testBedConfig);
21+
const initTestBed = registerTestBed(withAppContext(WatchEdit), testBedConfig);
2122

2223
export interface WatchCreateJsonTestBed extends TestBed<WatchCreateJsonTestSubjects> {
2324
actions: {

0 commit comments

Comments
 (0)