Файли
openidea/src/features/http-client/store.ts
T
Orca 43c9053624 fix(http-client): open the variable of the selected environment (#7681)
Go to Declaration on {{name}} in a .http file listed the key of every environment. The lookup now receives the environment picked in Run with and follows the order a run reads it: the private file, the public file, $shared, then the last @name line of the file. With No Environment it still lists every key. Keys whose value is an object, an array or null are not variables and are skipped; escaped key names and a key written twice (the last one wins) are handled.

Closes #7672

Co-authored-by: og-orca <[email protected]>
2026-12-03 14:29:18 +08:00

254 рядки
8.7 KiB
TypeScript

/** HTTP Client state: selected environment per file, responses, history, running request. */
import { create } from "zustand";
import {
call,
commands,
isCancelled,
nextRequestId,
type HttpHistoryEntry,
type HttpExchange,
type HttpRequestBlock,
type RequestId,
} from "@/platform/ipc";
import { writeClipboardText } from "@/platform/clipboard";
import { editorService, notifications, showServiceView, showStatusInfo } from "@/workbench/contributions";
export const SERVICE_VIEW = "httpClient";
// Same bound as ide_http::history::MAX_ENTRIES. Only recent rows keep their bodies in the UI.
const HISTORY_LIMIT = 100;
const EXCHANGE_LIMIT = 4;
const ENV_KEY = "openidea.httpClient.environments";
function readEnvChoices(): Record<string, string> {
try {
const raw = localStorage.getItem(ENV_KEY);
return raw ? (JSON.parse(raw) as Record<string, string>) : {};
} catch {
return {};
}
}
interface Running {
path: string | null;
index: number;
request: RequestId;
/** The request line as written, once parsed. */
method?: string;
url?: string;
}
interface HttpClientState {
/** Selected environment per request file (absent: none). */
envByFile: Record<string, string>;
/** Environments available per request file. */
environments: Record<string, string[]>;
/** History entries, newest first (this session's runs included). */
history: HttpHistoryEntry[];
/** Loaded exchanges, most recently viewed first. */
exchanges: HttpExchange[];
selected: string | null;
running: Running | null;
}
export const useHttpClient = create<HttpClientState>(() => ({
envByFile: readEnvChoices(),
environments: {},
history: [],
exchanges: [],
selected: null,
running: null,
}));
export function setEnvironment(path: string, env: string | null) {
useHttpClient.setState((s) => {
const envByFile = { ...s.envByFile };
if (env) envByFile[path] = env;
else delete envByFile[path];
try {
localStorage.setItem(ENV_KEY, JSON.stringify(envByFile));
} catch {
// Per-viewer convenience only.
}
return { envByFile };
});
}
export async function loadEnvironments(path: string): Promise<string[]> {
try {
const names = await call(commands.httpEnvironments(path));
useHttpClient.setState((s) => ({ environments: { ...s.environments, [path]: names } }));
return names;
} catch (e) {
notifications().notify({
level: "warning",
title: "HTTP Client environments",
body: e instanceof Error ? e.message : String(e),
key: "http-env",
});
return [];
}
}
/** The environment a run of `path` uses: the selected one if it still exists. */
export function effectiveEnvironment(path: string | null, selection?: string | null): string | null {
if (!path) return null;
const s = useHttpClient.getState();
const chosen = selection === undefined ? s.envByFile[path] : selection;
const available = s.environments[path];
if (chosen && (!available || available.includes(chosen))) return chosen;
return null;
}
function rememberExchange(exchange: HttpExchange, recent: HttpExchange[]) {
return [exchange, ...recent.filter((e) => e.entry.id !== exchange.entry.id)].slice(0, EXCHANGE_LIMIT);
}
export async function refreshHistory() {
try {
const history = await call(commands.httpHistory());
useHttpClient.setState({ history });
} catch {
// No project.
}
}
export async function selectEntry(id: string) {
const { exchanges } = useHttpClient.getState();
const cached = exchanges.find((e) => e.entry.id === id);
if (cached) {
useHttpClient.setState({ selected: id, exchanges: rememberExchange(cached, exchanges) });
return;
}
useHttpClient.setState({ selected: id });
try {
const ex = await call(commands.httpHistoryGet(id));
useHttpClient.setState((s) => (s.selected === id ? { exchanges: rememberExchange(ex, s.exchanges) } : s));
} catch (e) {
if (useHttpClient.getState().selected !== id) return;
notifications().notify({
level: "error",
title: "Cannot open the response",
body: e instanceof Error ? e.message : String(e),
});
}
}
export async function clearHistory() {
await call(commands.httpHistoryClear());
useHttpClient.setState({ history: [], exchanges: [], selected: null });
}
export function parseRequests(text: string): Promise<HttpRequestBlock[]> {
return commands.httpRequests(text);
}
/**
* Sends request `index` of `text` (the buffer of `path`). The response opens
* in the HTTP Client tool window. Returns false when cancelled or failed early.
*/
export async function runRequest(
path: string | null,
text: string,
index: number,
batchEnvironment?: string | null,
): Promise<boolean> {
const busy = useHttpClient.getState().running;
if (busy) void commands.cancelRequest(busy.request);
const request = nextRequestId();
useHttpClient.setState({ running: { path, index, request } });
// The tool window shows the request as it is sent, not only once it has answered.
showServiceView(SERVICE_VIEW, false);
void parseRequests(text)
.then((blocks) => {
const block = blocks.find((b) => b.index === index);
const running = useHttpClient.getState().running;
if (block && running?.request === request)
useHttpClient.setState({ running: { ...running, method: block.method, url: block.url } });
})
.catch(() => {});
try {
if (batchEnvironment === undefined && path && !useHttpClient.getState().environments[path])
await loadEnvironments(path);
const env = batchEnvironment === undefined ? effectiveEnvironment(path) : batchEnvironment;
const ex = await call(commands.httpExecute(request, path, text, index, env));
useHttpClient.setState((s) => ({
history: [ex.entry, ...s.history.filter((h) => h.id !== ex.entry.id)].slice(0, HISTORY_LIMIT),
exchanges: rememberExchange(ex, s.exchanges),
selected: ex.entry.id,
}));
return true;
} catch (e) {
if (!isCancelled(e))
notifications().notify({
level: "error",
title: "Cannot send the request",
body: e instanceof Error ? e.message : String(e),
key: "http-run",
});
return false;
} finally {
if (useHttpClient.getState().running?.request === request) useHttpClient.setState({ running: null });
}
}
/** Convert to cURL and Copy: request `index` of `text`, with the file's environment applied. */
export async function copyAsCurl(path: string | null, text: string, index: number): Promise<void> {
try {
if (path && !useHttpClient.getState().environments[path]) await loadEnvironments(path);
await writeClipboardText(await call(commands.httpCurl(path, text, index, effectiveEnvironment(path))));
showStatusInfo("The request was copied as a cURL command");
} catch (e) {
notifications().notify({
level: "error",
title: "Cannot convert the request to cURL",
body: e instanceof Error ? e.message : String(e),
key: "http-curl",
});
}
}
/** A pasted `curl …` command as request text; null when it isn't one. */
export function requestFromCurl(command: string): Promise<string | null> {
return commands.httpFromCurl(command);
}
/** Add Environment to Public / Private File: opens the env file that applies to `path`, created if missing. */
export async function openEnvironmentFile(path: string, isPrivate: boolean): Promise<void> {
try {
const file = await call(commands.httpEnvironmentFile(path, isPrivate));
await editorService().openFile(file, { focus: true });
void loadEnvironments(path);
} catch (e) {
notifications().notify({
level: "error",
title: "Cannot create the environment file",
body: e instanceof Error ? e.message : String(e),
key: "http-env-file",
});
}
}
/** Runs every request of the file in order with the starting environment. */
export async function runAll(path: string | null, text: string): Promise<void> {
const selection = path ? (useHttpClient.getState().envByFile[path] ?? null) : null;
const blocks = await parseRequests(text);
if (!blocks.length) return;
if (path && !useHttpClient.getState().environments[path]) await loadEnvironments(path);
const environment = effectiveEnvironment(path, selection);
for (const b of blocks) {
// oxlint-disable-next-line no-await-in-loop -- requests run in order and stop at the first failure
if (!(await runRequest(path, text, b.index, environment))) return;
}
}
export function cancelRunning() {
const r = useHttpClient.getState().running;
if (r) void commands.cancelRequest(r.request);
}
/** The window switched projects: history, responses and environments belong to the previous one. */
export function resetHttpClient() {
cancelRunning();
useHttpClient.setState({ environments: {}, history: [], exchanges: [], selected: null, running: null });
}