Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate index pattern server side to typescript #43155

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
* under the License.
*/

export { IndexPatternsService } from './service';
export { indexPatternsMixin } from './mixin';
export { IndexPatternsService, FieldDescriptor } from './service';
export { IndexPatternsServiceFactory } from './mixin';
24 changes: 0 additions & 24 deletions src/legacy/server/index_patterns/mixin.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,38 @@
* under the License.
*/

import Hapi from 'hapi';
import { APICaller } from 'src/core/server';
import { Legacy } from 'kibana';
import KbnServer from 'src/legacy/server/kbn_server';
import { IndexPatternsService } from './service';

import {
createFieldsForWildcardRoute,
createFieldsForTimePatternRoute,
} from './routes';
import { createFieldsForWildcardRoute, createFieldsForTimePatternRoute } from './routes';

export function indexPatternsMixin(kbnServer, server) {
const pre = {
export type IndexPatternsServiceFactory = (args: {
callCluster: (endpoint: string, clientParams: any, options: any) => Promise<any>;
}) => IndexPatternsService;

interface IndexPatternRequest extends Legacy.Request {
getIndexPatternsService: () => IndexPatternsService;
}

interface ServerWithFactory extends Legacy.Server {
addMemoizedFactoryToRequest: (...args: any) => void;
}

export function indexPatternsMixin(kbnServer: KbnServer, server: ServerWithFactory) {
const pre: Record<string, Hapi.RouteOptionsPreAllOptions> = {
/**
* Create an instance of the `indexPatterns` service
* @type {Hapi.Pre}
*/
* Create an instance of the `indexPatterns` service
* @type {Hapi.Pre}
*/
getIndexPatternsService: {
assign: 'indexPatterns',
method(request) {
method(request: IndexPatternRequest) {
return request.getIndexPatternsService();
}
}
},
},
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukeelmers There's an error I can't figure about the Request types here, can you take a look?


/**
Expand All @@ -44,9 +57,13 @@ export function indexPatternsMixin(kbnServer, server) {
* @method server.indexPatternsServiceFactory
* @type {IndexPatternsService}
*/
server.decorate('server', 'indexPatternsServiceFactory', ({ callCluster }) => {
return new IndexPatternsService(callCluster);
});
server.decorate(
'server',
'indexPatternsServiceFactory',
({ callCluster }: { callCluster: APICaller }) => {
return new IndexPatternsService(callCluster);
}
);

/**
* Get an instance of the IndexPatternsService configured for use
Expand All @@ -55,9 +72,10 @@ export function indexPatternsMixin(kbnServer, server) {
* @method request.getIndexPatternsService
* @type {IndexPatternsService}
*/
server.addMemoizedFactoryToRequest('getIndexPatternsService', request => {
server.addMemoizedFactoryToRequest('getIndexPatternsService', (request: Legacy.Request) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addMemoizedFactoryToRequest code doesn't seem to be present in any existing type definitions, what is it supposed to do?

const { callWithRequest } = request.server.plugins.elasticsearch.getCluster('data');
const callCluster = (...args) => callWithRequest(request, ...args);
const callCluster = (endpoint: string, params: any, options: any) =>
callWithRequest(request, endpoint, params, options);
return server.indexPatternsServiceFactory({ callCluster });
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,56 @@
* under the License.
*/

import Hapi from 'hapi';
import Joi from 'joi';
import { IndexPatternsService } from '../service';

export const createFieldsForTimePatternRoute = pre => ({
interface FieldsForTimePatternRequest extends Hapi.Request {
pre: {
indexPatterns: IndexPatternsService;
};
query: {
pattern: string;
interval: string;
look_back: number;
meta_fields: string[];
};
}

interface Prerequisites {
getIndexPatternsService: Hapi.RouteOptionsPreAllOptions;
}

export const createFieldsForTimePatternRoute = (pre: Prerequisites) => ({
path: '/api/index_patterns/_fields_for_time_pattern',
method: 'GET',
config: {
pre: [pre.getIndexPatternsService],
validate: {
query: Joi.object().keys({
pattern: Joi.string().required(),
look_back: Joi.number().min(1).required(),
meta_fields: Joi.array().items(Joi.string()).default([]),
}).default()
query: Joi.object()
.keys({
pattern: Joi.string().required(),
look_back: Joi.number()
.min(1)
.required(),
meta_fields: Joi.array()
.items(Joi.string())
.default([]),
})
.default(),
},
async handler(req) {
async handler(req: FieldsForTimePatternRequest) {
const { indexPatterns } = req.pre;
const {
pattern,
interval,
look_back: lookBack,
meta_fields: metaFields,
} = req.query;
const { pattern, interval, look_back: lookBack, meta_fields: metaFields } = req.query;

const fields = await indexPatterns.getFieldsForTimePattern({
pattern,
interval,
lookBack,
metaFields
metaFields,
});

return { fields };
}
}
},
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,49 @@
* under the License.
*/

import Hapi from 'hapi';
import Joi from 'joi';
import { IndexPatternsService } from '../service';

export const createFieldsForWildcardRoute = pre => ({
interface FieldsForWildcardPatternRequest extends Hapi.Request {
pre: {
indexPatterns: IndexPatternsService;
};
query: {
pattern: string;
meta_fields: string[];
};
}

interface Prerequisites {
getIndexPatternsService: Hapi.RouteOptionsPreAllOptions;
}

export const createFieldsForWildcardRoute = (pre: Prerequisites) => ({
path: '/api/index_patterns/_fields_for_wildcard',
method: 'GET',
config: {
pre: [pre.getIndexPatternsService],
validate: {
query: Joi.object().keys({
pattern: Joi.string().required(),
meta_fields: Joi.array().items(Joi.string()).default([]),
}).default()
query: Joi.object()
.keys({
pattern: Joi.string().required(),
meta_fields: Joi.array()
.items(Joi.string())
.default([]),
})
.default(),
},
async handler(req) {
async handler(req: FieldsForWildcardPatternRequest) {
const { indexPatterns } = req.pre;
const {
pattern,
meta_fields: metaFields,
} = req.query;
const { pattern, meta_fields: metaFields } = req.query;

const fields = await indexPatterns.getFieldsForWildcard({
pattern,
metaFields
metaFields,
});

return { fields };
}
}
},
},
});
20 changes: 0 additions & 20 deletions src/legacy/server/index_patterns/service/index.d.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/legacy/server/index_patterns/service/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
* under the License.
*/

export { indexPatternsMixin } from './mixin';
export * from './index_patterns_service';

This file was deleted.

Loading