194 lines
8.2 KiB
TypeScript
194 lines
8.2 KiB
TypeScript
// file: kb-app-demo-desktop/frontend/ts/demo_http.ts
|
|
// version: 5
|
|
|
|
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 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[] = [];
|
|
let httpResultBuffer = "Aucune requête exécutée.";
|
|
|
|
function textInputValue(selector: string): string {
|
|
const element = document.querySelector<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>(selector);
|
|
return element ? element.value.trim() : "";
|
|
}
|
|
|
|
function writeTextarea(selector: string, value: string): void {
|
|
const element = document.querySelector<HTMLTextAreaElement>(selector);
|
|
if (element) {
|
|
element.value = value;
|
|
}
|
|
}
|
|
|
|
function writeHttpResult(value: string): void {
|
|
httpResultBuffer = value;
|
|
writeTextarea("#httpResultOutput", httpResultBuffer);
|
|
}
|
|
|
|
function clearHttpResult(): void {
|
|
httpResultBuffer = "";
|
|
writeTextarea("#httpResultOutput", httpResultBuffer);
|
|
}
|
|
|
|
function jsonText(value: unknown): string {
|
|
if (typeof value === "string") {
|
|
return value;
|
|
}
|
|
return JSON.stringify(value, null, 2);
|
|
}
|
|
|
|
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): string {
|
|
const header = {
|
|
endpointName: payload.endpointName,
|
|
provider: payload.provider,
|
|
endpointUrl: payload.endpointUrl,
|
|
role: payload.role,
|
|
method: payload.method,
|
|
requestKind: payload.requestKind,
|
|
methodClass: payload.methodClass,
|
|
};
|
|
return `${JSON.stringify(header, null, 2)}\n\n--- response ---\n${payload.responseJson}`;
|
|
}
|
|
|
|
async function copyTextarea(selector: string): Promise<void> {
|
|
const element = document.querySelector<HTMLTextAreaElement>(selector);
|
|
if (!element) {
|
|
return;
|
|
}
|
|
await navigator.clipboard.writeText(element.value);
|
|
}
|
|
|
|
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(`Erreur options HTTP : ${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");
|
|
writeTextarea("#httpPoolOutput", jsonText(snapshots));
|
|
} catch (caughtError) {
|
|
const message = caughtError instanceof Error ? caughtError.message : String(caughtError);
|
|
writeTextarea("#httpPoolOutput", `Erreur : ${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("Exécution 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(`Erreur : ${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");
|
|
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>("#copyHttpPoolButton")?.addEventListener("click", () => {
|
|
void copyTextarea("#httpPoolOutput");
|
|
});
|
|
document.querySelector<HTMLButtonElement>("#copyHttpResultButton")?.addEventListener("click", () => {
|
|
void copyTextarea("#httpResultOutput");
|
|
});
|
|
document.querySelector<HTMLButtonElement>("#clearHttpResultButton")?.addEventListener("click", clearHttpResult);
|
|
void refreshHttpOptions();
|
|
void refreshHttpPool();
|
|
});
|