@glyphtek/unspecd
Version:
A declarative UI framework for building internal tools and dashboards with TypeScript. Create interactive tables, forms, and dashboards using simple specifications.
52 lines • 2.36 kB
TypeScript
/**
* Data Handler Utilities for Unspec'd Framework
*
* This file contains utilities for safely invoking developer-provided functions
* from ToolSpec objects, managing promise lifecycles, and handling errors gracefully.
* This decouples data-calling logic from component rendering logic.
*/
/**
* Parameters for the invokeDataSource function.
*/
export interface InvokeDataSourceParams {
/** The functions object from the ToolSpec containing available functions */
specFunctions: Record<string, (params: any) => Promise<any>>;
/** The name of the function to execute from the specFunctions record */
functionName: string;
/** Parameters to pass to the target function */
params: any;
/** Callback function called immediately before the operation starts */
onPending: () => void;
/** Callback function called with the result upon successful resolution */
onFulfilled: (result: any) => void;
/** Callback function called with the error if the promise rejects or execution fails */
onRejected: (error: Error) => void;
}
/**
* Safely invokes a developer-provided function by name and manages the promise lifecycle
* using callbacks. This function provides a consistent interface for executing async
* operations defined in ToolSpec objects while handling errors gracefully.
*
* The function follows this execution flow:
* 1. Calls onPending() to signal the start of the operation
* 2. Validates that the requested function exists in specFunctions
* 3. Executes the function with the provided parameters
* 4. Calls onFulfilled() with the result on success
* 5. Calls onRejected() with an error on any failure
*
* @param params - Configuration object containing function reference, parameters, and callbacks
*
* @example
* ```typescript
* await invokeDataSource({
* specFunctions: myToolSpec.functions,
* functionName: 'getUserProfile',
* params: { userId: '123' },
* onPending: () => console.log('Loading user profile...'),
* onFulfilled: (user) => console.log('User loaded:', user),
* onRejected: (error) => console.error('Failed to load user:', error)
* });
* ```
*/
export declare function invokeDataSource({ specFunctions, functionName, params, onPending, onFulfilled, onRejected, }: InvokeDataSourceParams): Promise<void>;
//# sourceMappingURL=data-handler.d.ts.map