Type-Safe Development
IdentityFlow is designed with TypeScript at its core, enabling you to build robust, maintainable, and type-safe workflows.
import * as v from '@identity-flow/sdk/valibot';import { defineWorkflow } from '@identity-flow/sdk';
// Example using built-in Valibot
// Define types for parameters and resultsinterface OrderParams { orderId: string; items: Array<{ id: string; quantity: number }>;}
interface ProcessResult { status: 'COMPLETED' | 'PENDING' | 'FAILED'; // Corrected status values reference: string;}
// Use generics on the defineWorkflow function itselfexport default defineWorkflow<OrderParams, ProcessResult>( { name: 'process-order', // Optionally define input schema for automatic validation schema: v.object({ orderId: v.string(), items: v.array(v.object({ id: v.string(), quantity: v.number() })), }), }, async (flow) => { // Full type inference for flow.params based on schema or generic const { orderId, items } = flow.params; flow.log(`Processing order ${orderId}`);
// Return type is enforced by the generic or schema const result: ProcessResult = await flow.do('process-order-step', async () => { const apiResult = await processOrder({ orderId, items }); // Call external logic // Map the result to the expected ProcessResult shape return { status: apiResult.status === 'success' ? 'COMPLETED' : 'FAILED', reference: apiResult.id, }; });
return result; },);Advantages of Type Safety:
- Early Error Detection: Catch type-related errors at compile time, long before they reach production.
- Improved Code Quality: Encourages clearer, more explicit code and data structures.
- Enhanced Maintainability: Makes refactoring and understanding existing workflows easier and safer.
- Better Developer Experience: Autocompletion and type inference in your IDE improve productivity.
- Schema Validation: Integrate with validation libraries like Valibot (shown), Zod, or Yup for runtime data validation based on your types.
By embracing TypeScript, IdentityFlow helps you build more reliable and scalable automation solutions.