|
1 | | -import type { EvaluateOptions, Expression, FunctionObject, ReferenceObject } from "../types"; |
| 1 | +import type { EvaluateOptions, Expression, FunctionObject, FunctionReturn, ReferenceObject } from "../types"; |
2 | 2 | import { EndpointError } from "../types"; |
3 | | -import { callFunction } from "./callFunction"; |
| 3 | +import { customEndpointFunctions } from "./customEndpointFunctions"; |
| 4 | +import { endpointFunctions } from "./endpointFunctions"; |
4 | 5 | import { evaluateTemplate } from "./evaluateTemplate"; |
5 | 6 | import { getReferenceValue } from "./getReferenceValue"; |
6 | 7 |
|
7 | 8 | export const evaluateExpression = (obj: Expression, keyName: string, options: EvaluateOptions) => { |
8 | 9 | if (typeof obj === "string") { |
9 | 10 | return evaluateTemplate(obj, options); |
10 | 11 | } else if ((obj as FunctionObject)["fn"]) { |
11 | | - return callFunction(obj as FunctionObject, options); |
| 12 | + return group.callFunction(obj as FunctionObject, options); |
12 | 13 | } else if ((obj as ReferenceObject)["ref"]) { |
13 | 14 | return getReferenceValue(obj as ReferenceObject, options); |
14 | 15 | } |
15 | 16 | throw new EndpointError(`'${keyName}': ${String(obj)} is not a string, function or reference.`); |
16 | 17 | }; |
| 18 | + |
| 19 | +export const callFunction = ({ fn, argv }: FunctionObject, options: EvaluateOptions): FunctionReturn => { |
| 20 | + const evaluatedArgs = argv.map((arg) => |
| 21 | + ["boolean", "number"].includes(typeof arg) ? arg : group.evaluateExpression(arg as Expression, "arg", options) |
| 22 | + ); |
| 23 | + const fnSegments = fn.split("."); |
| 24 | + if (fnSegments[0] in customEndpointFunctions && fnSegments[1] != null) { |
| 25 | + // @ts-ignore Element implicitly has an 'any' type |
| 26 | + return customEndpointFunctions[fnSegments[0]][fnSegments[1]](...evaluatedArgs); |
| 27 | + } |
| 28 | + // @ts-ignore Element implicitly has an 'any' type |
| 29 | + return endpointFunctions[fn](...evaluatedArgs); |
| 30 | +}; |
| 31 | + |
| 32 | +export const group = { |
| 33 | + evaluateExpression, |
| 34 | + callFunction, |
| 35 | +}; |
0 commit comments