forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Expressions] Create expressions function to get UI settings (elastic…
- Loading branch information
Showing
26 changed files
with
560 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/plugins/expressions/common/expression_functions/specs/tests/ui_setting.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
jest.mock('../../../../common'); | ||
|
||
import { IUiSettingsClient } from 'src/core/public'; | ||
import { getUiSettingFn } from '../ui_setting'; | ||
|
||
describe('uiSetting', () => { | ||
describe('fn', () => { | ||
let getStartDependencies: jest.MockedFunction< | ||
Parameters<typeof getUiSettingFn>[0]['getStartDependencies'] | ||
>; | ||
let uiSetting: ReturnType<typeof getUiSettingFn>; | ||
let uiSettings: jest.Mocked<IUiSettingsClient>; | ||
|
||
beforeEach(() => { | ||
uiSettings = ({ | ||
get: jest.fn(), | ||
} as unknown) as jest.Mocked<IUiSettingsClient>; | ||
getStartDependencies = (jest.fn(async () => ({ | ||
uiSettings, | ||
})) as unknown) as typeof getStartDependencies; | ||
|
||
uiSetting = getUiSettingFn({ getStartDependencies }); | ||
}); | ||
|
||
it('should return a value', () => { | ||
uiSettings.get.mockReturnValueOnce('value'); | ||
|
||
expect(uiSetting.fn(null, { parameter: 'something' }, {} as any)).resolves.toEqual({ | ||
type: 'ui_setting', | ||
key: 'something', | ||
value: 'value', | ||
}); | ||
}); | ||
|
||
it('should pass a default value', async () => { | ||
await uiSetting.fn(null, { parameter: 'something', default: 'default' }, {} as any); | ||
|
||
expect(uiSettings.get).toHaveBeenCalledWith('something', 'default'); | ||
}); | ||
|
||
it('should throw an error when parameter does not exist', () => { | ||
uiSettings.get.mockImplementationOnce(() => { | ||
throw new Error(); | ||
}); | ||
|
||
expect(uiSetting.fn(null, { parameter: 'something' }, {} as any)).rejects.toEqual( | ||
new Error('Invalid parameter "something".') | ||
); | ||
}); | ||
|
||
it('should get a request instance on the server-side', async () => { | ||
const request = {}; | ||
await uiSetting.fn(null, { parameter: 'something' }, { | ||
getKibanaRequest: () => request, | ||
} as any); | ||
|
||
const [[getKibanaRequest]] = getStartDependencies.mock.calls; | ||
|
||
expect(getKibanaRequest()).toBe(request); | ||
}); | ||
|
||
it('should throw an error if request is not provided on the server-side', async () => { | ||
await uiSetting.fn(null, { parameter: 'something' }, {} as any); | ||
|
||
const [[getKibanaRequest]] = getStartDependencies.mock.calls; | ||
|
||
expect(getKibanaRequest).toThrow(); | ||
}); | ||
}); | ||
}); |
94 changes: 94 additions & 0 deletions
94
src/plugins/expressions/common/expression_functions/specs/ui_setting.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
// eslint-disable-next-line @kbn/eslint/no-restricted-paths | ||
import type { KibanaRequest } from 'src/core/server'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common'; | ||
import { UiSetting } from '../../expression_types/specs/ui_setting'; | ||
|
||
interface UiSettingsClient { | ||
get<T>(key: string, defaultValue?: T): T | Promise<T>; | ||
} | ||
|
||
interface UiSettingStartDependencies { | ||
uiSettings: UiSettingsClient; | ||
} | ||
|
||
interface UiSettingFnArguments { | ||
getStartDependencies(getKibanaRequest: () => KibanaRequest): Promise<UiSettingStartDependencies>; | ||
} | ||
|
||
export interface UiSettingArguments { | ||
default?: unknown; | ||
parameter: string; | ||
} | ||
|
||
export type ExpressionFunctionUiSetting = ExpressionFunctionDefinition< | ||
'uiSetting', | ||
unknown, | ||
UiSettingArguments, | ||
Promise<UiSetting> | ||
>; | ||
|
||
export function getUiSettingFn({ | ||
getStartDependencies, | ||
}: UiSettingFnArguments): ExpressionFunctionUiSetting { | ||
return { | ||
name: 'uiSetting', | ||
help: i18n.translate('expressions.functions.uiSetting.help', { | ||
defaultMessage: 'Returns a UI settings parameter value.', | ||
}), | ||
args: { | ||
default: { | ||
help: i18n.translate('expressions.functions.uiSetting.args.default', { | ||
defaultMessage: 'A default value in case of the parameter is not set.', | ||
}), | ||
}, | ||
parameter: { | ||
aliases: ['_'], | ||
help: i18n.translate('expressions.functions.uiSetting.args.parameter', { | ||
defaultMessage: 'The parameter name.', | ||
}), | ||
required: true, | ||
types: ['string'], | ||
}, | ||
}, | ||
async fn(input, { default: defaultValue, parameter }, { getKibanaRequest }) { | ||
const { uiSettings } = await getStartDependencies(() => { | ||
const request = getKibanaRequest?.(); | ||
if (!request) { | ||
throw new Error( | ||
i18n.translate('expressions.functions.uiSetting.error.kibanaRequest', { | ||
defaultMessage: | ||
'A KibanaRequest is required to get UI settings on the server. ' + | ||
'Please provide a request object to the expression execution params.', | ||
}) | ||
); | ||
} | ||
|
||
return request; | ||
}); | ||
|
||
try { | ||
return { | ||
type: 'ui_setting', | ||
key: parameter, | ||
value: await uiSettings.get(parameter, defaultValue), | ||
}; | ||
} catch { | ||
throw new Error( | ||
i18n.translate('expressions.functions.uiSetting.error.parameter', { | ||
defaultMessage: 'Invalid parameter "{parameter}".', | ||
values: { parameter }, | ||
}) | ||
); | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.