@directus/api
Version:
Directus is a real-time API and App dashboard for managing SQL database content
28 lines (26 loc) • 1.1 kB
JavaScript
//#region src/extensions/lib/sandbox/generate-host-function-reference.ts
/**
* Generate an anonymous function wrapper with the provided arguments that applies the args against a referenced function in the host
*
* @param index - Generator function that tracks the indexes used
* @param args - Named arguments of the host function
* @param options - Options to modify the output function
* @param options.async - Whether or not to generate the wrapper function as an async function
*/
function generateHostFunctionReference(index, args, options) {
const argsList = args.join(", ");
const i = index.next().value;
if (options.async) return `
async (${argsList}) => {
const { result, error } = await $${i}.apply(undefined, [${argsList}], { arguments: { reference: true }, result: { copy: true, promise: true } });
if (error) {
throw result;
} else {
return result;
}
};
`;
else return `(${argsList}) => $${i}.applySync(undefined, [${argsList}], { arguments: { reference: true }, result: { copy: true } });`;
}
//#endregion
export { generateHostFunctionReference };