mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-19 13:56:04 +00:00
public commit
This commit is contained in:
81
app/src/flows/tasks/presets/FetchTask.ts
Normal file
81
app/src/flows/tasks/presets/FetchTask.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { StringEnum, Type } from "core/utils";
|
||||
import type { InputsMap } from "../../flows/Execution";
|
||||
import { Task, dynamic } from "../Task";
|
||||
|
||||
const FetchMethods = ["GET", "POST", "PUT", "PATCH", "DELETE"];
|
||||
|
||||
export class FetchTask<Output extends Record<string, any>> extends Task<
|
||||
typeof FetchTask.schema,
|
||||
Output
|
||||
> {
|
||||
type = "fetch";
|
||||
|
||||
static override schema = Type.Object({
|
||||
url: Type.String({
|
||||
pattern: "^(http|https)://"
|
||||
}),
|
||||
//method: Type.Optional(Type.Enum(FetchMethodsEnum)),
|
||||
//method: Type.Optional(dynamic(Type.String({ enum: FetchMethods, default: "GET" }))),
|
||||
method: Type.Optional(dynamic(StringEnum(FetchMethods, { default: "GET" }))),
|
||||
headers: Type.Optional(
|
||||
dynamic(
|
||||
Type.Array(
|
||||
Type.Object({
|
||||
key: Type.String(),
|
||||
value: Type.String()
|
||||
})
|
||||
),
|
||||
JSON.parse
|
||||
)
|
||||
),
|
||||
body: Type.Optional(dynamic(Type.String())),
|
||||
normal: Type.Optional(dynamic(Type.Number(), Number.parseInt))
|
||||
});
|
||||
|
||||
protected getBody(): string | undefined {
|
||||
const body = this.params.body;
|
||||
if (!body) return;
|
||||
if (typeof body === "string") return body;
|
||||
if (typeof body === "object") return JSON.stringify(body);
|
||||
|
||||
throw new Error(`Invalid body type: ${typeof body}`);
|
||||
}
|
||||
|
||||
async execute() {
|
||||
//console.log(`method: (${this.params.method})`);
|
||||
if (!FetchMethods.includes(this.params.method ?? "GET")) {
|
||||
throw this.error("Invalid method", {
|
||||
given: this.params.method,
|
||||
valid: FetchMethods
|
||||
});
|
||||
}
|
||||
|
||||
const body = this.getBody();
|
||||
const headers = new Headers(this.params.headers?.map((h) => [h.key, h.value]));
|
||||
|
||||
/*console.log("[FETCH]", {
|
||||
url: this.params.url,
|
||||
method: this.params.method ?? "GET",
|
||||
headers,
|
||||
body
|
||||
});*/
|
||||
const result = await fetch(this.params.url, {
|
||||
method: this.params.method ?? "GET",
|
||||
headers,
|
||||
body
|
||||
});
|
||||
|
||||
//console.log("fetch:response", result);
|
||||
if (!result.ok) {
|
||||
throw this.error("Failed to fetch", {
|
||||
status: result.status,
|
||||
statusText: result.statusText
|
||||
});
|
||||
}
|
||||
|
||||
const data = (await result.json()) as Output;
|
||||
//console.log("fetch:response:data", data);
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
16
app/src/flows/tasks/presets/LogTask.ts
Normal file
16
app/src/flows/tasks/presets/LogTask.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Type } from "core/utils";
|
||||
import { Task } from "../Task";
|
||||
|
||||
export class LogTask extends Task<typeof LogTask.schema> {
|
||||
type = "log";
|
||||
|
||||
static override schema = Type.Object({
|
||||
delay: Type.Number({ default: 10 })
|
||||
});
|
||||
|
||||
async execute() {
|
||||
await new Promise((resolve) => setTimeout(resolve, this.params.delay));
|
||||
console.log(`[DONE] LogTask: ${this.name}`);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
17
app/src/flows/tasks/presets/RenderTask.ts
Normal file
17
app/src/flows/tasks/presets/RenderTask.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Type } from "core/utils";
|
||||
import { Task } from "../Task";
|
||||
|
||||
export class RenderTask<Output extends Record<string, any>> extends Task<
|
||||
typeof RenderTask.schema,
|
||||
Output
|
||||
> {
|
||||
type = "render";
|
||||
|
||||
static override schema = Type.Object({
|
||||
render: Type.String()
|
||||
});
|
||||
|
||||
async execute() {
|
||||
return this.params.render as unknown as Output;
|
||||
}
|
||||
}
|
||||
40
app/src/flows/tasks/presets/SubFlowTask.ts
Normal file
40
app/src/flows/tasks/presets/SubFlowTask.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { Type } from "core/utils";
|
||||
import { Flow } from "../../flows/Flow";
|
||||
import { Task, dynamic } from "../Task";
|
||||
|
||||
export class SubFlowTask<Output extends Record<string, any>> extends Task<
|
||||
typeof SubFlowTask.schema,
|
||||
Output
|
||||
> {
|
||||
type = "subflow";
|
||||
|
||||
static override schema = Type.Object({
|
||||
flow: Type.Any(),
|
||||
input: Type.Optional(dynamic(Type.Any(), JSON.parse)),
|
||||
loop: Type.Optional(Type.Boolean())
|
||||
});
|
||||
|
||||
async execute() {
|
||||
const flow = this.params.flow;
|
||||
if (!(flow instanceof Flow)) {
|
||||
throw new Error("Invalid flow provided");
|
||||
}
|
||||
|
||||
if (this.params.loop) {
|
||||
const _input = Array.isArray(this.params.input) ? this.params.input : [this.params.input];
|
||||
|
||||
const results: any[] = [];
|
||||
for (const input of _input) {
|
||||
const execution = flow.createExecution();
|
||||
await execution.start(input);
|
||||
results.push(await execution.getResponse());
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
const execution = flow.createExecution();
|
||||
await execution.start(this.params.input);
|
||||
return execution.getResponse();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user