Skip to content

Commit

Permalink
Wrap KibanaRequest execution context in a getter.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers committed Dec 2, 2020
1 parent 8b95a6a commit 7b09cbe
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,18 @@ describe('indexPattern expression function', () => {
test('returns serialized index pattern', async () => {
const indexPatternDefinition = getFunctionDefinition({ getStartDependencies });
const result = await indexPatternDefinition().fn(null, { id: '1' }, {
kibanaRequest: {},
getKibanaRequest: () => ({}),
} as any);
expect(result.type).toEqual('index_pattern');
expect(result.value.title).toEqual('value');
});

test('throws if getKibanaRequest is not available', async () => {
const indexPatternDefinition = getFunctionDefinition({ getStartDependencies });
expect(async () => {
await indexPatternDefinition().fn(null, { id: '1' }, {} as any);
}).rejects.toThrowErrorMatchingInlineSnapshot(
`"A KibanaRequest is required to execute this search on the server. Please provide a request object to the expression execution params."`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export function getFunctionDefinition({
}) {
return (): IndexPatternLoadExpressionFunctionDefinition => ({
...getIndexPatternLoadMeta(),
async fn(input, args, { kibanaRequest }) {
async fn(input, args, { getKibanaRequest }) {
const kibanaRequest = getKibanaRequest ? getKibanaRequest() : null;
if (!kibanaRequest) {
throw new Error(
i18n.translate('data.indexPatterns.indexPatternLoad.error.kibanaRequest', {
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/data/server/search/expressions/esaggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ export function getFunctionDefinition({
}): () => EsaggsExpressionFunctionDefinition {
return () => ({
...getEsaggsMeta(),
async fn(input, args, { inspectorAdapters, abortSignal, getSearchSessionId, kibanaRequest }) {
async fn(
input,
args,
{ inspectorAdapters, abortSignal, getSearchSessionId, getKibanaRequest }
) {
const kibanaRequest = getKibanaRequest ? getKibanaRequest() : null;
if (!kibanaRequest) {
throw new Error(
i18n.translate('data.search.esaggs.error.kibanaRequest', {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/expressions/common/execution/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export class Execution<
this.context = {
getSearchContext: () => this.execution.params.searchContext || {},
getSearchSessionId: () => execution.params.searchSessionId,
getKibanaRequest: () => execution.params.kibanaRequest,
variables: execution.params.variables || {},
types: executor.getTypes(),
abortSignal: this.abortController.signal,
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/expressions/common/execution/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ export interface ExecutionContext<
getSearchSessionId: () => string | undefined;

/**
* Makes a `KibanaRequest` object available to expression functions. Useful for
* functions which are running on the server and need to perform operations that
* are scoped to a specific user.
* Getter to retrieve the `KibanaRequest` object inside an expression function.
* Useful for functions which are running on the server and need to perform
* operations that are scoped to a specific user.
*/
kibanaRequest?: KibanaRequest;
getKibanaRequest?: () => KibanaRequest;

/**
* Allows to fetch saved objects from ElasticSearch. In browser `getSavedObject`
Expand Down

0 comments on commit 7b09cbe

Please sign in to comment.