From 3f07abac6e8b9342b95e4612f0b73e2d22a2503d Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Mon, 23 Dec 2019 15:47:09 +0300 Subject: [PATCH] fix PR commetns --- .../field_formats/converters/date_server.ts | 4 +-- .../common/field_formats/converters/url.ts | 4 +-- .../data/common/field_formats/field_format.ts | 11 +++++++- .../data/common/field_formats/index.ts | 7 +++++- .../field_formats_provider/field_formats.ts | 25 +++++++++++-------- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/plugins/data/common/field_formats/converters/date_server.ts b/src/plugins/data/common/field_formats/converters/date_server.ts index 0c214e424f163..34278ea9fe641 100644 --- a/src/plugins/data/common/field_formats/converters/date_server.ts +++ b/src/plugins/data/common/field_formats/converters/date_server.ts @@ -20,7 +20,7 @@ import { memoize, noop } from 'lodash'; import moment from 'moment-timezone'; import { KBN_FIELD_TYPES } from '../../kbn_field_types/types'; -import { FieldFormat } from '../field_format'; +import { FieldFormat, IFieldFormatMetaParams } from '../field_format'; import { TextContextTypeConvert, FIELD_FORMAT_IDS } from '../types'; export class DateFormat extends FieldFormat { @@ -32,7 +32,7 @@ export class DateFormat extends FieldFormat { private memoizedPattern: string = ''; private timeZone: string = ''; - constructor(params: Record, getConfig: Function) { + constructor(params: IFieldFormatMetaParams, getConfig: Function) { super(params, getConfig); this.memoizedConverter = memoize((val: any) => { diff --git a/src/plugins/data/common/field_formats/converters/url.ts b/src/plugins/data/common/field_formats/converters/url.ts index 3c88511a4c63e..21688dd8d1138 100644 --- a/src/plugins/data/common/field_formats/converters/url.ts +++ b/src/plugins/data/common/field_formats/converters/url.ts @@ -21,7 +21,7 @@ import { i18n } from '@kbn/i18n'; import { escape, memoize } from 'lodash'; import { getHighlightHtml } from '../utils'; import { KBN_FIELD_TYPES } from '../../kbn_field_types/types'; -import { FieldFormat } from '../field_format'; +import { FieldFormat, IFieldFormatMetaParams } from '../field_format'; import { TextContextTypeConvert, HtmlContextTypeConvert, FIELD_FORMAT_IDS } from '../types'; const templateMatchRE = /{{([\s\S]+?)}}/g; @@ -64,7 +64,7 @@ export class UrlFormat extends FieldFormat { ]; static urlTypes = URL_TYPES; - constructor(params: Record) { + constructor(params: IFieldFormatMetaParams) { super(params); this.compileTemplate = memoize(this.compileTemplate); } diff --git a/src/plugins/data/common/field_formats/field_format.ts b/src/plugins/data/common/field_formats/field_format.ts index 50f07846a3ceb..a0621b27f5bc0 100644 --- a/src/plugins/data/common/field_formats/field_format.ts +++ b/src/plugins/data/common/field_formats/field_format.ts @@ -37,6 +37,15 @@ import { HtmlContextTypeConvert, TextContextTypeConvert } from './types'; const DEFAULT_CONTEXT_TYPE = TEXT_CONTEXT_TYPE; +export interface IFieldFormatMetaParams { + [key: string]: any; + parsedUrl?: { + origin: string; + pathname?: string; + basePath?: string; + }; +} + export abstract class FieldFormat { /** * @property {string} - Field Format Id @@ -90,7 +99,7 @@ export abstract class FieldFormat { protected readonly _params: any; protected getConfig: Function | undefined; - constructor(_params: Record = {}, getConfig?: Function) { + constructor(_params: IFieldFormatMetaParams = {}, getConfig?: Function) { this._params = _params; if (getConfig) { diff --git a/src/plugins/data/common/field_formats/index.ts b/src/plugins/data/common/field_formats/index.ts index b751b097b5ed2..b1e8744e26745 100644 --- a/src/plugins/data/common/field_formats/index.ts +++ b/src/plugins/data/common/field_formats/index.ts @@ -18,7 +18,12 @@ */ export { HTML_CONTEXT_TYPE, TEXT_CONTEXT_TYPE } from './content_types'; -export { FieldFormat, IFieldFormatType, IFieldFormatId } from './field_format'; +export { + FieldFormat, + IFieldFormatType, + IFieldFormatId, + IFieldFormatMetaParams, +} from './field_format'; export { getHighlightRequest, asPrettyString, getHighlightHtml } from './utils'; export * from './converters'; export * from './constants'; diff --git a/src/plugins/data/public/field_formats_provider/field_formats.ts b/src/plugins/data/public/field_formats_provider/field_formats.ts index 38a29de1e2d58..d3d57d42028cd 100644 --- a/src/plugins/data/public/field_formats_provider/field_formats.ts +++ b/src/plugins/data/public/field_formats_provider/field_formats.ts @@ -27,6 +27,7 @@ import { IFieldFormatType, IFieldFormatId, FieldFormat, + IFieldFormatMetaParams, } from '../../common'; import { FieldType } from './types'; @@ -76,9 +77,9 @@ export class FieldFormatRegisty { * Get a derived FieldFormat class by its id. * * @param {IFieldFormatId} formatId - the format id - * @return {FieldFormat | void} + * @return {FieldFormat | undefined} */ - getType = (formatId: IFieldFormatId): IFieldFormatType | void => { + getType = (formatId: IFieldFormatId): IFieldFormatType | undefined => { const fieldFormat = this.fieldFormats.get(formatId); if (fieldFormat) { @@ -88,6 +89,8 @@ export class FieldFormatRegisty { return decoratedFieldFormat as IFieldFormatType; } } + + return undefined; }; /** @@ -97,12 +100,12 @@ export class FieldFormatRegisty { * * @param {KBN_FIELD_TYPES} fieldType * @param {ES_FIELD_TYPES[]} esTypes - Array of ES data types - * @return {FieldFormat | void} + * @return {FieldFormat | undefined} */ getDefaultType = ( fieldType: KBN_FIELD_TYPES, esTypes: ES_FIELD_TYPES[] - ): IFieldFormatType | void => { + ): IFieldFormatType | undefined => { const config = this.getDefaultConfig(fieldType, esTypes); return this.getType(config.id); @@ -113,11 +116,11 @@ export class FieldFormatRegisty { * using the format:defaultTypeMap config map * * @param {ES_FIELD_TYPES[]} esTypes - Array of ES data types - * @return {ES_FIELD_TYPES | void} + * @return {ES_FIELD_TYPES | undefined} */ - getTypeNameByEsTypes = (esTypes: ES_FIELD_TYPES[] | undefined): ES_FIELD_TYPES | void => { + getTypeNameByEsTypes = (esTypes: ES_FIELD_TYPES[] | undefined): ES_FIELD_TYPES | undefined => { if (!Array.isArray(esTypes)) { - return; + return undefined; } return esTypes.find(type => this.defaultMap[type] && this.defaultMap[type].es); @@ -241,11 +244,11 @@ export class FieldFormatRegisty { * * @private * @param {IFieldFormatType} fieldFormat - field format type - * @return {FieldFormat | void} + * @return {FieldFormat | undefined} */ private fieldFormatMetaParamsDecorator = ( fieldFormat: IFieldFormatType - ): IFieldFormatType | void => { + ): IFieldFormatType | undefined => { const getMetaParams = (customParams: Record) => this.buildMetaParams(customParams); if (fieldFormat) { @@ -258,6 +261,8 @@ export class FieldFormatRegisty { } }; } + + return undefined; }; /** @@ -267,7 +272,7 @@ export class FieldFormatRegisty { * @param {Record} custom params * @return {Record} */ - private buildMetaParams = (customParams: T = {}): T & {parsedUrl: ParsedUrl} => ({ + private buildMetaParams = (customParams: T): T => ({ parsedUrl: { origin: window.location.origin, pathname: window.location.pathname,