import { createFlow } from "@databite/flow";
const dataProcessingFlow = createFlow("processData")
.form("getConfig", {
title: "Processing Configuration",
fields: [
{ name: "apiKey", label: "API Key", type: "password", required: true },
{
name: "baseUrl",
label: "Base URL",
defaultValue: "https://api.example.com",
},
],
})
.http("fetchRawData", {
url: (input) => `${input.getConfig.baseUrl}/data/raw`,
method: "GET",
returnType: { items: [] },
headers: (input) => ({
Authorization: `Bearer ${input.getConfig.apiKey}`,
}),
})
.transform("validateData", (input) => {
const validItems = input.fetchRawData.items.filter(
(item) => item.id && item.name && item.status
);
return {
validItems,
totalCount: input.fetchRawData.items.length,
validCount: validItems.length,
invalidCount: input.fetchRawData.items.length - validItems.length,
};
})
.display("showValidation", {
title: "Data Validation Results",
content: (input) =>
`Found ${input.validateData.validCount} valid items out of ${input.validateData.totalCount} total items.`,
})
.confirm("proceedWithProcessing", {
title: "Continue Processing?",
message: (input) => `Process ${input.validateData.validCount} valid items?`,
})
.transform("enrichData", async (input) => {
if (!input.proceedWithProcessing) {
return { enrichedItems: [], processed: false };
}
// Simulate data enrichment
const enrichedItems = input.validateData.validItems.map((item) => ({
...item,
enrichedAt: new Date().toISOString(),
category: "default",
}));
return { enrichedItems, processed: true };
})
.http("saveProcessedData", {
url: (input) => `${input.getConfig.baseUrl}/data/processed`,
method: "POST",
returnType: { success: true, count: 0 },
headers: (input) => ({
Authorization: `Bearer ${input.getConfig.apiKey}`,
"Content-Type": "application/json",
}),
body: (input) => ({
items: input.enrichData.enrichedItems,
metadata: {
processedAt: new Date().toISOString(),
totalProcessed: input.enrichData.enrichedItems.length,
},
}),
})
.display("showCompletion", {
title: "Processing Complete!",
content: (input) =>
`Successfully processed ${input.saveProcessedData.count} items.`,
})
.returns((context) => ({
processed: context.enrichData.processed,
itemCount: context.saveProcessedData.count,
timestamp: new Date().toISOString(),
}))
.build();