Skip to content

Commit

Permalink
add type guard to map API
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Mar 5, 2024
1 parent 835e3ae commit c8c6a4c
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/embeddable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

export * from './embeddable';

export { type HasLensConfig, type LensApi, isLensApi } from './interfaces/has_lens_config';
export { type HasLensConfig, type LensApi, isLensApi } from './interfaces/lens_api';
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { LensPlugin } from './plugin';

export { isLensApi } from './embeddable/interfaces/has_lens_config';
export { isLensApi } from './embeddable/interfaces/lens_api';
export type {
EmbeddableComponentProps,
EmbeddableComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import type {
PublishesPanelTitle,
PublishesLocalUnifiedSearch,
} from '@kbn/presentation-publishing';
import { apiIsOfType } from '@kbn/presentation-publishing';
import { apiIsOfType, apiPublishesLocalUnifiedSearch, apiPublishesPanelTitle } from '@kbn/presentation-publishing';
import type { ILayer } from '../classes/layers/layer';

export type HasMapConfig = HasType<'map'> & {
getLayerList: () => ILayer[];
};

export type MapApi = HasMapConfig &
export type MapApi = HasType<'map'> &
{
getLayerList: () => ILayer[];
} &
PublishesDataViews &
PublishesPanelTitle &
PublishesLocalUnifiedSearch &
Partial<HasParentApi<unknown>>;

export const apiHasMapConfig = (api: unknown): api is MapApi => {
export const isMapApi = (api: unknown): api is MapApi => {
return Boolean(
api && apiIsOfType(api, 'map') && typeof (api as HasMapConfig).getLayerList === 'function'
api && apiIsOfType(api, 'map') && typeof (api as MapApi).getLayerList === 'function' &&
apiPublishesPanelTitle(api) && apiPublishesLocalUnifiedSearch(api)
);
};
2 changes: 1 addition & 1 deletion x-pack/plugins/maps/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type {
export type { MapsSetupApi, MapsStartApi } from './api';

export type { MapEmbeddable, MapEmbeddableInput, MapEmbeddableOutput } from './embeddable';
export { type HasMapConfig, type MapApi, apiHasMapConfig } from './embeddable/has_map_config';
export { type HasMapConfig, type MapApi, isMapApi } from './embeddable/has_map_config';

export type { EMSTermJoinConfig, SampleValuesConfig } from './ems_autosuggest';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

import type { HasType } from '@kbn/presentation-publishing';
import type { HasLensConfig } from '@kbn/lens-plugin/public';
import type { LensApi } from '@kbn/lens-plugin/public';
import type { HasVisualizeConfig } from '@kbn/visualizations-plugin/public';

export type SynchronizeMovementActionApi =
| HasType<'map' | 'visualization' | 'lens'>
| Partial<HasLensConfig | HasVisualizeConfig>;
| Partial<LensApi | HasVisualizeConfig>;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { asyncForEach } from '@kbn/std';
import type { PublishesDataViews } from '@kbn/presentation-publishing';
import { type HasMapConfig } from '@kbn/maps-plugin/public';
import { type MapApi } from '@kbn/maps-plugin/public';
import type { EuiComboBoxOptionOption } from '@elastic/eui';
import type { DataView } from '@kbn/data-views-plugin/common';
import type { Query } from '@kbn/es-query';
Expand All @@ -27,7 +27,7 @@ export class VisualizationExtractor {
constructor() {}

public async getResultLayersFromEmbeddable(
embeddable: HasMapConfig & Partial<PublishesDataViews>
embeddable: MapApi & Partial<PublishesDataViews>
): Promise<LayerResult[]> {
const layers: LayerResult[] = [];
const dataViews: DataView[] = embeddable.dataViews?.value ?? [];
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/ml/public/ui_actions/open_vis_in_ml_action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { i18n } from '@kbn/i18n';
import { type EmbeddableApiContext, apiHasType } from '@kbn/presentation-publishing';
import type { UiActionsActionDefinition } from '@kbn/ui-actions-plugin/public';
import { isLensApi } from '@kbn/lens-plugin/public';
import { apiHasMapConfig } from '@kbn/maps-plugin/public';
import { isMapApi } from '@kbn/maps-plugin/public';
import type { ActionApi } from './types';
import { MlCoreSetup } from '../plugin';

Expand Down Expand Up @@ -43,7 +43,7 @@ export function createVisToADJobAction(
return;
}
await showLensVisToADJobFlyout(embeddable, coreStart, share, data, dashboard, lens);
} else if (apiHasMapConfig(embeddable)) {
} else if (isMapApi(embeddable)) {
const [{ showMapVisToADJobFlyout }, [coreStart, { share, data, dashboard }]] =
await Promise.all([import('../embeddables/job_creation/map'), getStartServices()]);
await showMapVisToADJobFlyout(embeddable, coreStart, share, data, dashboard);
Expand Down Expand Up @@ -81,7 +81,7 @@ export function createVisToADJobAction(
}
const chartInfo = await getChartInfoFromVisualization(lens, vis);
return isCompatibleVisualizationType(chartInfo);
} else if (apiHasMapConfig(embeddable)) {
} else if (isMapApi(embeddable)) {
return isCompatibleMapVisualization(embeddable);
}
return false;
Expand Down

0 comments on commit c8c6a4c

Please sign in to comment.