diff --git a/typescript-client/client.ts b/typescript-client/client.ts index 86e02c04b2..73c1c1834e 100644 --- a/typescript-client/client.ts +++ b/typescript-client/client.ts @@ -123,6 +123,21 @@ export async function runScript( return await waitJob(jobId, verbose); } +export async function runFlow( + path: string | null = null, + args: Record | null = null, + verbose: boolean = false +): Promise { + args = args || {}; + + if (verbose) { + console.info(`running \`${path}\` synchronously with args:`, args); + } + + const jobId = await runFlowAsync(path, args); + return await waitJob(jobId, verbose); +} + export async function waitJob( jobId: string, verbose: boolean = false @@ -253,6 +268,50 @@ export async function runScriptAsync( body: JSON.stringify(args), }).then((res) => res.text()); } + +export async function runFlowAsync( + path: string | null, + args: Record | null, + scheduledInSeconds: number | null = null +): Promise { + // Create a script job and return its job id. + + args = args || {}; + const params: Record = {}; + + if (scheduledInSeconds) { + params["scheduled_in_secs"] = scheduledInSeconds; + } + + let parentJobId = getEnv("WM_JOB_ID"); + if (parentJobId !== undefined) { + params["parent_job"] = parentJobId; + } + + let rootJobId = getEnv("WM_ROOT_FLOW_JOB_ID"); + if (rootJobId != undefined && rootJobId != "") { + params["root_job"] = rootJobId; + } + + let endpoint: string; + if (path) { + endpoint = `/w/${getWorkspace()}/jobs/run/f/${path}`; + } else { + throw new Error("path must be provided"); + } + let url = new URL(OpenAPI.BASE + endpoint); + url.search = new URLSearchParams(params).toString(); + + return fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${OpenAPI.TOKEN}`, + }, + body: JSON.stringify(args), + }).then((res) => res.text()); +} + /** * Resolve a resource value in case the default value was picked because the input payload was undefined * @param obj resource value or path of the resource under the format `$res:path`