/** * Includes JSON data types and `undefined`. * * @see {@link https://fibo.github.io/dflow/#dflowdata} */ export type DflowData = undefined | null | boolean | number | string | DflowArray | DflowObject; export type DflowObject = { [Key in string]: DflowData; }; export type DflowArray = DflowData[]; /** * Dflow data types represent values that can be serialized as JSON. * * @see {@link https://fibo.github.io/dflow/#dflowdatatype} */ export type DflowDataType = "null" | "boolean" | "number" | "string" | "array" | "object"; /** * Connects two nodes in the graph. * * @see {@link https://fibo.github.io/dflow/#dflowlink} */ export type DflowLink = [ sourceNodeId: string, sourceOutputIndex: number, targetNodeId: string, targetInputIndex: number ]; /** * Defines a node input. * * @example * * ```json * { "name": "label", "types": ["string"] } * ``` * * @see {@link https://fibo.github.io/dflow/#dflowinput} */ export type DflowInput = { /** Ignored by Dflow, but could be used by UI. */ name?: string; /** An input can be connected to an output only if the data types match. */ types: DflowDataType[]; /** * An input is **required** by default. * If it is not connected or the data passed is not valid according to its types, * then its node will not be executed. * If an input is **optional** the checks are skipped. */ optional?: boolean; }; /** * Defines a node output. * * @example * * ```json * { "name": "sum", "types": ["number"] } * ``` * * @see {@link https://fibo.github.io/dflow/#dflowoutput} */ export type DflowOutput = { /** Ignored by Dflow, but could be used by UI. */ name?: string; /** An output can be connected to an input only if the data types match. */ types: DflowDataType[]; }; /** * Defines a block of code: it can have inputs and outputs. * * @see {@link https://fibo.github.io/dflow/#dflownode} */ export type DflowNode = { kind: string; inputs?: DflowInput[]; outputs?: DflowOutput[]; run(..._args: DflowArray): unknown | Promise; }; export type DflowGraph = { /** Key is node id, value is node kind. */ node: Record; /** Key is link id. */ link: Record; /** Data nodes: key is node id, value is data. */ data: Record; }; /** * A `Dflow` represents a program as an executable graph. * A graph can contain nodes and links. * Nodes are executed, sorted by their connections. * * @see {@link https://fibo.github.io/dflow/#api} */ export declare class Dflow { #private; /** * Dflow context is bound to every node at runtime, * hence it is accessible via `this` inside node `run`. * * @example * * ```ts * type Context = { * foo: string; * } * * const node: DflowNode & Partial = { * kind: "example", * run() { * console.log(this.foo) * } * } * * const dflow = new Dflow([node]) * dflow.context.foo = "bar" * dflow.run() // Outputs "bar" * ``` * * @see {@link https://fibo.github.io/dflow/#dflow.context} */ readonly context: Record; /** * Error logger. * * @see {@link https://fibo.github.io/dflow/#dflow.err} */ ERR: (arg: any) => void; /** * Dflow constructor requires a list of node definitions which is an `Array`. * * @see {@link https://fibo.github.io/dflow/#constructor} */ constructor(nodeDefinitions: Array); /** Check that source types are compatible with target types. */ canConnect([sourceNodeId, sourceOutputIndex, targetNodeId, targetInputIndex]: DflowLink): boolean; /** * Create a new node. Returns node id. * * @see {@link https://fibo.github.io/dflow/#dflow.node} */ node(kind: string, wantedId?: string): string; /** * Delete node or link with given id. * * @see {@link https://fibo.github.io/dflow/#dflow.delete} */ delete(id: string): void; /** * Create a new data node. Returns node id. * * @see {@link https://fibo.github.io/dflow/#dflow.data} */ data(value: unknown, wantedId?: string): string; /** * Create a new link and connect two nodes. Returns link id. * * @see {@link https://fibo.github.io/dflow/#dflow.link} */ link(source: string | [nodeId: string, index: number], target: string | [nodeId: string, index: number], wantedId?: string): string; /** * Execute all nodes, sorted by their connections. * * @example * * ```ts * try { * await dflow.run(); * } catch (aggregatedError) { * console.error(dflow.error); * for (const error of aggregatedError.errors) { * console.error(error); * } * } * ``` * * @see {@link https://fibo.github.io/dflow/#dflow.run} */ run(): Promise; /** * A graph contains nodes and links. * * @see {@link https://fibo.github.io/dflow/#dflow.graph} */ get graph(): DflowGraph; /** * Get error messages from last run, indexed by node id. * * @see {@link https://fibo.github.io/dflow/#dflow.error} */ get error(): Record; /** * Get output data of last run, indexed by node id. * * @see {@link https://fibo.github.io/dflow/#dflow.out} */ get out(): Record; /** * Helper to define inputs. * * @example Input with type `array` and name. * * ```ts * Dflow.input("array", { name: "list" }) * ``` * * @see {@link https://fibo.github.io/dflow/#dflow.input} for more examples. */ static input(typing?: DflowDataType | DflowDataType[], rest?: Omit): DflowInput; /** * Helper to define outputs. * * @example Output with type `number` and named "count". * * ```ts * Dflow.output("number", { name: "count" }) * ``` * * @see {@link https://fibo.github.io/dflow/#dflow.output} for more examples. */ static output(typing?: DflowDataType | DflowDataType[], rest?: Omit): DflowOutput; /** * Type guard for `DflowArray`. * It checks recursively that every element is some `DflowData`. */ static isArray(arg: unknown): arg is DflowArray; /** * Type guard for `DflowObject`. * It checks recursively that every value is some `DflowData`. */ static isObject(arg: unknown): arg is DflowObject; /** Type guard for a valid number, i.e. finite and not `NaN`. */ static isNumber(arg: unknown): arg is number; /** Type guard for `DflowData`. */ static isData(arg: unknown): arg is Exclude; /** Validate that data belongs to some of given types. */ static isValidData(types: DflowDataType[], data: unknown): boolean; } //# sourceMappingURL=dflow.d.ts.map