Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | 4x 2209x 2209x 4x 4x 4x 2209x 3x 3x 3x 2209x 3x 3x 3x 2209x 3x 3x 3x 2209x 3x 3x 3x 3x | import type { ScenarioRun } from "@/objects/ScenarioRun";
import { Dictionary } from "@/types/ext";
import { AxiosRequestConfig, isAxiosError } from "axios";
import { dispositionFilename, saveBlob } from "@/util/download";
import { APIRouter } from "../ApiRouter";
/** Why the list could not be read: the deployment has scenario runs off
* (404), or the caller lacks the `developer` platform role (403). */
export type ScenarioRunsUnavailable = "off" | "forbidden";
export type ScenarioRunList =
| { runs: ScenarioRun[]; unavailable: null }
| { runs: []; unavailable: ScenarioRunsUnavailable };
/** A refused request's reason, as svc-core wrote it (`detail`). Named
* `refused`, not `error`: a run has an `error` of its own. */
export type Refused = { refused: string };
export function isRefused(value: unknown): value is Refused {
return typeof value === "object" && value !== null && "refused" in value;
}
function refusal(e: unknown, fallback: string): Refused {
if (isAxiosError(e)) {
const detail = e.response?.data?.detail;
if (typeof detail === "string" && detail) return { refused: detail };
}
return { refused: fallback };
}
/**
* Scenario runs (svc-core `/app/scenario-runs`, docs/guides/scenario-runs.md):
* start one from a scenario file, follow it, and download its export.
*/
export class ScenarioRunRouter extends APIRouter {
constructor(
host: string,
token: string | null = null,
default_config: AxiosRequestConfig | null = null,
headers: Dictionary<string> = {},
) {
super("scenario-runs", host, token, default_config, headers);
}
/** The caller's newest runs, or why they cannot be listed here. */
list = async (): Promise<ScenarioRunList> => {
try {
const res = await this.api.get<ScenarioRun[]>(super.endpoint(""));
return { runs: Array.isArray(res.data) ? res.data : [], unavailable: null };
} catch (e) {
const status = isAxiosError(e) ? e.response?.status : undefined;
if (status === 404) return { runs: [], unavailable: "off" };
if (status === 403) return { runs: [], unavailable: "forbidden" };
throw e;
}
};
/** Start a run of `scenario` (a parsed scenario file). A 422 says what is
* wrong with the file; a 409 that another run is still going. */
start = async (scenario: unknown): Promise<ScenarioRun | Refused> => {
try {
const res = await this.api.post<ScenarioRun>(super.endpoint(""), { scenario });
return res.data;
} catch (e) {
return refusal(e, "The run could not be started.");
}
};
/** Stop the turn running now and start no more. */
cancel = async (id: string): Promise<ScenarioRun | Refused> => {
try {
const res = await this.api.post<ScenarioRun>(super.endpoint(`${id}/cancel`), {});
return res.data;
} catch (e) {
return refusal(e, "The run could not be cancelled.");
}
};
/** Delete a finished run and its project. */
remove = async (id: string): Promise<true | Refused> => {
try {
await this.api.delete(super.endpoint(id));
return true;
} catch (e) {
return refusal(e, "The run could not be deleted.");
}
};
/** Save the run's export (JSON) as a file. False when it failed. */
downloadExport = async (
id: string,
fallbackName = "scenario-run.json",
): Promise<boolean> => {
const res = await this.api
.get(super.endpoint(`${id}/export`), {
responseType: "blob",
withCredentials: true,
})
.catch(() => null);
Iif (res?.status !== 200) return false;
saveBlob(
res.data as Blob,
dispositionFilename(res.headers?.["content-disposition"]) ?? fallbackName,
);
return true;
};
}
|