-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
setup_expressions.ts
60 lines (51 loc) · 1.9 KB
/
setup_expressions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { CoreSetup } from '@kbn/core/public';
import { serializeProvider } from '@kbn/expressions-plugin/common';
import { API_ROUTE_FUNCTIONS } from '../common/lib/constants';
import { CanvasSetupDeps } from './plugin';
let cached: Promise<void> | null = null;
// TODO: clintandrewhall - This is getting refactored shortly. https://github.com/elastic/kibana/issues/105675
export const setupExpressions = async ({
coreSetup,
setupPlugins,
}: {
coreSetup: CoreSetup;
setupPlugins: CanvasSetupDeps;
}) => {
const { expressions } = setupPlugins;
const loadServerFunctionWrappers = async () => {
if (!cached) {
cached = (async () => {
const serverFunctionList = await coreSetup.http.get<any>(API_ROUTE_FUNCTIONS, {
version: '1',
});
const { serialize } = serializeProvider(expressions.getTypes());
// For every sever-side function, register a client-side
// function that matches its definition, but which simply
// calls the server-side function endpoint.
Object.keys(serverFunctionList).forEach((functionName) => {
if (expressions.getFunction(functionName)) {
return;
}
const fn = () => ({
...serverFunctionList[functionName],
fn: (input: any, args: any) => {
return coreSetup.http.post(API_ROUTE_FUNCTIONS, {
body: JSON.stringify({ functionName, args, context: serialize(input) }),
version: '1',
});
},
});
expressions.registerFunction(fn);
});
})();
}
return cached;
};
await loadServerFunctionWrappers();
};