Skip to content

Commit

Permalink
Add inspector TabularData typings.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers committed Nov 11, 2020
1 parent ebfafcd commit c21656d
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 42 deletions.
4 changes: 1 addition & 3 deletions src/plugins/inspector/common/adapters/data/data_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { EventEmitter } from 'events';
import { TabularCallback, TabularHolder, TabularLoaderOptions } from './types';

class DataAdapter extends EventEmitter {
export class DataAdapter extends EventEmitter {
private tabular?: TabularCallback;
private tabularOptions?: TabularLoaderOptions;

Expand All @@ -38,5 +38,3 @@ class DataAdapter extends EventEmitter {
return Promise.resolve(this.tabular()).then((data) => ({ data, options }));
}
}

export { DataAdapter };
16 changes: 10 additions & 6 deletions src/plugins/inspector/common/adapters/data/data_adapters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,37 @@ describe('DataAdapter', () => {
});

it('should call the provided callback and resolve with its value', async () => {
const spy = jest.fn(() => 'foo');
const data = { columns: [], rows: [] };
const spy = jest.fn(() => data);
adapter.setTabularLoader(spy);
expect(spy).not.toBeCalled();
const result = await adapter.getTabular();
expect(spy).toBeCalled();
expect(result.data).toBe('foo');
expect(result.data).toBe(data);
});

it('should pass through options specified via setTabularLoader', async () => {
adapter.setTabularLoader(() => 'foo', { returnsFormattedValues: true });
const data = { columns: [], rows: [] };
adapter.setTabularLoader(() => data, { returnsFormattedValues: true });
const result = await adapter.getTabular();
expect(result.options).toEqual({ returnsFormattedValues: true });
});

it('should return options set when starting loading data', async () => {
adapter.setTabularLoader(() => 'foo', { returnsFormattedValues: true });
const data = { columns: [], rows: [] };
adapter.setTabularLoader(() => data, { returnsFormattedValues: true });
const waitForResult = adapter.getTabular();
adapter.setTabularLoader(() => 'bar', { returnsFormattedValues: false });
adapter.setTabularLoader(() => data, { returnsFormattedValues: false });
const result = await waitForResult;
expect(result.options).toEqual({ returnsFormattedValues: true });
});
});

it('should emit a "tabular" event when a new tabular loader is specified', () => {
const data = { columns: [], rows: [] };
const spy = jest.fn();
adapter.once('change', spy);
adapter.setTabularLoader(() => 42);
adapter.setTabularLoader(() => data);
expect(spy).toBeCalled();
});
});
4 changes: 1 addition & 3 deletions src/plugins/inspector/common/adapters/data/formatted_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* under the License.
*/

class FormattedData {
export class FormattedData {
constructor(public readonly raw: any, public readonly formatted: any) {}
}

export { FormattedData };
5 changes: 3 additions & 2 deletions src/plugins/inspector/common/adapters/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
* under the License.
*/

export { FormattedData } from './formatted_data';
export { DataAdapter } from './data_adapter';
export * from './data_adapter';
export * from './formatted_data';
export * from './types';
21 changes: 19 additions & 2 deletions src/plugins/inspector/common/adapters/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,25 @@
* under the License.
*/

// TODO: add a more specific TabularData type.
export type TabularData = any;
export interface TabularDataValue {
formatted: string;
raw: unknown;
}

export interface TabularDataColumn {
name: string;
field: string;
filter?: (value: TabularDataValue) => void;
filterOut?: (value: TabularDataValue) => void;
}

export type TabularDataRow = Record<TabularDataColumn['field'], TabularDataValue>;

export interface TabularData {
columns: TabularDataColumn[];
rows: TabularDataRow[];
}

export type TabularCallback = () => TabularData | Promise<TabularData>;

export interface TabularHolder {
Expand Down
12 changes: 3 additions & 9 deletions src/plugins/inspector/common/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
* under the License.
*/

export { Adapters } from './types';
export { DataAdapter, FormattedData } from './data';
export {
RequestAdapter,
RequestStatistic,
RequestStatistics,
RequestStatus,
RequestResponder,
} from './request';
export * from './data';
export * from './request';
export * from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { Request, RequestParams, RequestStatus } from './types';
* instead it offers a generic API to log requests of any kind.
* @extends EventEmitter
*/
class RequestAdapter extends EventEmitter {
export class RequestAdapter extends EventEmitter {
private requests: Map<string, Request>;

constructor() {
Expand Down Expand Up @@ -78,5 +78,3 @@ class RequestAdapter extends EventEmitter {
this.emit('change');
}
}

export { RequestAdapter };
5 changes: 5 additions & 0 deletions src/plugins/inspector/common/adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
* under the License.
*/

import type { DataAdapter } from './data';
import type { RequestAdapter } from './request';

/**
* The interface that the adapters used to open an inspector have to fullfill.
*/
export interface Adapters {
data: DataAdapter;
requests: RequestAdapter;
[key: string]: any;
}
15 changes: 14 additions & 1 deletion src/plugins/inspector/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@
* under the License.
*/

export * from './adapters';
export {
Adapters,
DataAdapter,
FormattedData,
RequestAdapter,
RequestStatistic,
RequestStatistics,
RequestStatus,
RequestResponder,
TabularData,
TabularDataColumn,
TabularDataRow,
TabularDataValue,
} from './adapters';
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { Adapters } from '../../../../common';
import {
TabularLoaderOptions,
TabularData,
TabularCallback,
TabularHolder,
} from '../../../../common/adapters/data/types';
import { IUiSettingsClient } from '../../../../../../core/public';
import { withKibana, KibanaReactContextValue } from '../../../../../kibana_react/public';
Expand All @@ -44,7 +44,7 @@ interface DataViewComponentState {
tabularData: TabularData | null;
tabularOptions: TabularLoaderOptions;
adapters: Adapters;
tabularPromise: TabularCallback | null;
tabularPromise: Promise<TabularHolder> | null;
}

interface DataViewComponentProps extends InspectorViewProps {
Expand Down Expand Up @@ -91,7 +91,7 @@ class DataViewComponent extends Component<DataViewComponentProps, DataViewCompon
const { tabularPromise } = this.state;

if (tabularPromise) {
const tabularData: TabularData = await tabularPromise;
const tabularData: TabularHolder = await tabularPromise;

if (this._isMounted) {
this.setState({
Expand Down
15 changes: 6 additions & 9 deletions src/plugins/inspector/public/views/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@
* under the License.
*/

import { TabularDataRow } from '../../../common/adapters';

type DataViewColumnRender = (value: string, _item: TabularDataRow) => string;

export interface DataViewColumn {
name: string;
field: string;
sortable: (item: DataViewRow) => string | number;
sortable: (item: TabularDataRow) => string | number;
render: DataViewColumnRender;
}

type DataViewColumnRender = (value: string, _item: DataViewRow) => string;

export interface DataViewRow {
[fields: string]: {
formatted: string;
raw: any;
};
}
export type DataViewRow = TabularDataRow;
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { RequestDetails } from './request_details';

interface RequestSelectorState {
requests: Request[];
request: Request;
request: Request | null;
}

export class RequestsViewComponent extends Component<InspectorViewProps, RequestSelectorState> {
Expand Down

0 comments on commit c21656d

Please sign in to comment.