175 lines
7.6 KiB
TypeScript
175 lines
7.6 KiB
TypeScript
// file: kb-app-demo-desktop/frontend/ts/demo_http.ts
|
|
// version: 7
|
|
|
|
import * as bootstrap from "bootstrap";
|
|
import "simplebar";
|
|
import ResizeObserver from "resize-observer-polyfill";
|
|
import { invoke } from "@tauri-apps/api/core";
|
|
import { frontendDebug, frontendError, installFrontendConsoleBridge } from "./frontend_log.ts";
|
|
import { renderJsonViewer } from "./json_viewer.ts";
|
|
import type { DemoHttpExecutionPayload } from "./bindings/kb_app_demo_desktop/demo_http/DemoHttpExecutionPayload.ts";
|
|
import type { DemoHttpMethodOption } from "./bindings/kb_app_demo_desktop/demo_http/DemoHttpMethodOption.ts";
|
|
import type { DemoHttpOptionsPayload } from "./bindings/kb_app_demo_desktop/demo_http/DemoHttpOptionsPayload.ts";
|
|
import type { DemoHttpRequest } from "./bindings/kb_app_demo_desktop/demo_http/DemoHttpRequest.ts";
|
|
import type { DemoHttpRoleOption } from "./bindings/kb_app_demo_desktop/demo_http/DemoHttpRoleOption.ts";
|
|
|
|
(window as Window & typeof globalThis & { bootstrap?: typeof bootstrap }).bootstrap = bootstrap;
|
|
(window as Window & typeof globalThis & { ResizeObserver?: typeof ResizeObserver }).ResizeObserver = ResizeObserver;
|
|
|
|
let roleOptions: DemoHttpRoleOption[] = [];
|
|
let methodOptions: DemoHttpMethodOption[] = [];
|
|
|
|
|
|
function textInputValue(selector: string): string {
|
|
const element = document.querySelector<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>(selector);
|
|
return element ? element.value.trim() : "";
|
|
}
|
|
|
|
function parseJsonValue(value: string): unknown {
|
|
try {
|
|
return JSON.parse(value) as unknown;
|
|
} catch {
|
|
return { raw: value };
|
|
}
|
|
}
|
|
|
|
function writeHttpResult(value: unknown): void {
|
|
renderJsonViewer("#httpResultOutput", value);
|
|
}
|
|
|
|
function clearHttpResult(): void {
|
|
writeHttpResult({ status: "idle", message: "Aucune requête exécutée." });
|
|
}
|
|
|
|
function roleSupportsMethod(role: DemoHttpRoleOption, method: DemoHttpMethodOption): boolean {
|
|
return role.requestKinds.includes("*") || role.requestKinds.includes(method.requestKind);
|
|
}
|
|
|
|
function selectedRole(): DemoHttpRoleOption | null {
|
|
const role = textInputValue("#httpRoleSelect");
|
|
return roleOptions.find(option => option.role === role) ?? null;
|
|
}
|
|
|
|
function populateSelect(selector: string, values: Array<{ value: string; label: string }>): void {
|
|
const select = document.querySelector<HTMLSelectElement>(selector);
|
|
if (!select) {
|
|
return;
|
|
}
|
|
select.textContent = "";
|
|
for (const item of values) {
|
|
const option = document.createElement("option");
|
|
option.value = item.value;
|
|
option.textContent = item.label;
|
|
select.appendChild(option);
|
|
}
|
|
}
|
|
|
|
function refreshMethodList(): void {
|
|
const role = selectedRole();
|
|
const filtered = role ? methodOptions.filter(method => roleSupportsMethod(role, method)) : methodOptions;
|
|
populateSelect("#httpMethodSelect", filtered.map(method => ({
|
|
value: method.method,
|
|
label: `${method.method} — ${method.label}`,
|
|
})));
|
|
refreshMethodFields();
|
|
}
|
|
|
|
function refreshMethodFields(): void {
|
|
const methodName = textInputValue("#httpMethodSelect");
|
|
const method = methodOptions.find(option => option.method === methodName) ?? null;
|
|
const firstArg = document.querySelector<HTMLInputElement>("#httpFirstArgInput");
|
|
const config = document.querySelector<HTMLTextAreaElement>("#httpConfigInput");
|
|
const params = document.querySelector<HTMLTextAreaElement>("#httpParamsInput");
|
|
if (firstArg) {
|
|
firstArg.disabled = method ? !method.requiresFirstArg : false;
|
|
firstArg.placeholder = method && method.requiresFirstArg ? "Argument requis" : "Non requis pour cette méthode";
|
|
}
|
|
if (config) {
|
|
config.disabled = method ? !method.supportsConfigJson : false;
|
|
}
|
|
if (params) {
|
|
params.disabled = false;
|
|
}
|
|
}
|
|
|
|
function formatHttpExecutionPayload(payload: DemoHttpExecutionPayload): unknown {
|
|
return {
|
|
endpointName: payload.endpointName,
|
|
provider: payload.provider,
|
|
endpointUrl: payload.endpointUrl,
|
|
role: payload.role,
|
|
method: payload.method,
|
|
requestKind: payload.requestKind,
|
|
methodClass: payload.methodClass,
|
|
response: parseJsonValue(payload.responseJson),
|
|
};
|
|
}
|
|
|
|
async function refreshHttpOptions(): Promise<void> {
|
|
try {
|
|
const options = await invoke<DemoHttpOptionsPayload>("demo_http_options");
|
|
roleOptions = options.roles;
|
|
methodOptions = options.methods;
|
|
populateSelect("#httpRoleSelect", roleOptions.map(role => ({
|
|
value: role.role,
|
|
label: role.role,
|
|
})));
|
|
refreshMethodList();
|
|
} catch (caughtError) {
|
|
const message = caughtError instanceof Error ? caughtError.message : String(caughtError);
|
|
writeHttpResult({ status: "error", stage: "options", message });
|
|
frontendError("kb-app-demo-desktop.frontend.demo_http", `HTTP options loading failed: ${message}`);
|
|
}
|
|
}
|
|
|
|
async function refreshHttpPool(): Promise<void> {
|
|
try {
|
|
const snapshots = await invoke("demo_http_list_pool_clients");
|
|
renderJsonViewer("#httpPoolOutput", snapshots);
|
|
} catch (caughtError) {
|
|
const message = caughtError instanceof Error ? caughtError.message : String(caughtError);
|
|
renderJsonViewer("#httpPoolOutput", { status: "error", message });
|
|
frontendError("kb-app-demo-desktop.frontend.demo_http", `HTTP pool refresh failed: ${message}`);
|
|
}
|
|
}
|
|
|
|
async function executeHttpRequest(): Promise<void> {
|
|
const request: DemoHttpRequest = {
|
|
role: textInputValue("#httpRoleSelect"),
|
|
method: textInputValue("#httpMethodSelect"),
|
|
firstArg: textInputValue("#httpFirstArgInput") || null,
|
|
configJson: textInputValue("#httpConfigInput") || null,
|
|
paramsJson: textInputValue("#httpParamsInput") || null,
|
|
};
|
|
writeHttpResult({ status: "running", message: "Exécution HTTP en cours." });
|
|
try {
|
|
const response = await invoke<DemoHttpExecutionPayload>("demo_http_execute_request", { request });
|
|
writeHttpResult(formatHttpExecutionPayload(response));
|
|
frontendDebug("kb-app-demo-desktop.frontend.demo_http", `HTTP request completed: ${response.method}`);
|
|
} catch (caughtError) {
|
|
const message = caughtError instanceof Error ? caughtError.message : String(caughtError);
|
|
writeHttpResult({ status: "error", message });
|
|
frontendError("kb-app-demo-desktop.frontend.demo_http", `HTTP request failed: ${message}`);
|
|
}
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
installFrontendConsoleBridge("kb-app-demo-desktop.frontend.demo_http");
|
|
frontendDebug("kb-app-demo-desktop.frontend.demo_http", "HTTP demo window loaded");
|
|
renderJsonViewer("#httpPoolOutput", { status: "loading", message: "Chargement des endpoints HTTP." });
|
|
clearHttpResult();
|
|
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
|
|
Array.from(tooltipTriggerList).map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl));
|
|
document.querySelector<HTMLSelectElement>("#httpRoleSelect")?.addEventListener("change", refreshMethodList);
|
|
document.querySelector<HTMLSelectElement>("#httpMethodSelect")?.addEventListener("change", refreshMethodFields);
|
|
document.querySelector<HTMLButtonElement>("#executeHttpButton")?.addEventListener("click", () => {
|
|
void executeHttpRequest();
|
|
});
|
|
document.querySelector<HTMLButtonElement>("#refreshHttpPoolButton")?.addEventListener("click", () => {
|
|
void refreshHttpPool();
|
|
});
|
|
document.querySelector<HTMLButtonElement>("#clearHttpResultButton")?.addEventListener("click", clearHttpResult);
|
|
void refreshHttpOptions();
|
|
void refreshHttpPool();
|
|
});
|