UNPKG

polen

Version:

A framework for delightful GraphQL developer portals

63 lines 1.86 kB
import { exitWithReport } from './report.js'; export const create = (fn, options) => { const definition = { name: options?.name ?? (fn.name || `anonymous`), mask: options?.mask, }; const task = async (input) => { const start = performance.now(); try { const output = await fn(input); const end = performance.now(); return { task: definition, execution: { input, output, timings: { start, end, duration: end - start, }, }, }; } catch (error) { const end = performance.now(); return { task: definition, execution: { input, output: error, timings: { start, end, duration: end - start, }, }, }; } }; task.definition = definition; return task; }; /** * Sugar method that creates a task, executes it, and exits with the report. * Equivalent to: create(fn, options) -> task(input) -> exitWithReport(report) * * @param fn - The function to wrap as a task * @param input - Input to pass to the task * @param options - Combined task creation and format options */ export const runAndExit = async (fn, input, options) => { const task = create(fn, { name: options?.name, mask: options?.mask, }); const report = await task(input); return exitWithReport(report, { debug: options?.debug, mask: options?.mask, }); }; //# sourceMappingURL=task.js.map