-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
functions.ts
68 lines (59 loc) · 2.16 KB
/
functions.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
61
62
63
64
65
66
67
68
/*
* 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 { serializeProvider } from '@kbn/expressions-plugin/common';
import { RouteInitializerDeps } from '..';
import { API_ROUTE_FUNCTIONS } from '../../../common/lib/constants';
interface FunctionCall {
functionName: string;
args: Record<string, any>;
context: Record<string, any>;
}
export function initializeGetFunctionsRoute(deps: RouteInitializerDeps) {
const { router, expressions } = deps;
router.versioned
.get({
path: API_ROUTE_FUNCTIONS,
access: 'internal',
})
.addVersion({ version: '1', validate: false }, async (context, request, response) => {
const functions = expressions.getFunctions('canvas');
const body = JSON.stringify(functions);
return response.ok({
body,
});
});
}
export function initializeBatchFunctionsRoute(deps: RouteInitializerDeps) {
const { bfetch, expressions } = deps;
async function runFunction(handlers: { environment: string }, fnCall: FunctionCall) {
const { functionName, args, context } = fnCall;
const { deserialize } = serializeProvider(expressions.getTypes());
const fnDef = expressions.getFunctions('canvas')[functionName];
if (!fnDef) throw new Error(`Function "${functionName}" could not be found.`);
const deserialized = deserialize(context);
const result = fnDef.fn(deserialized, args, handlers);
return result;
}
/**
* Register an endpoint that executes a batch of functions, and streams the
* results back using ND-JSON.
*/
bfetch.addBatchProcessingRoute(API_ROUTE_FUNCTIONS, (request) => {
return {
onBatchItem: async (fnCall: FunctionCall) => {
const handlers = {
environment: 'server',
};
const result = await runFunction(handlers, fnCall);
if (typeof result === 'undefined') {
throw new Error(`Function ${fnCall.functionName} did not return anything.`);
}
return result;
},
};
});
}