1
import * as wmill from "https://deno.land/x/[email protected]/mod.ts"
2
3
export async function main() {
4
const state = await wmill.getInternalState();
5
const previousLatestId = state?.latestId ?? 0;
6
7
// Fetch and validate
8
const data = await (await fetch('https://hub.windmill.dev/searchFlowData')).json();
9
const flows = assertFlows(data.flows);
10
const newFlows = flows.filter(flow => flow.flow_id > previousLatestId);
11
12
// Update state
13
const latestId: number = flows.reduce((prevId, flow) => {
14
return Math.max(prevId, flow.flow_id);
15
}, previousLatestId);
16
const newState = {
17
...state,
18
latestId,
19
};
20
await wmill.setInternalState(newState);
21
console.info('old state', state);
22
console.info('new state', newState);
23
24
// Return new flows
25
return newFlows;
26
}
27
28
function assertFlows(items: any): { flow_id: number }[] {
29
if (!Array.isArray(items)) {
30
throw Error('"flows" must be an array');
31
32
if (!items.every((item: any) => !!item && Number.isInteger(item.flow_id))) {
33
throw Error('Not all flows have an integer "flow_id"');
34
35
return items;
36