- Added E2eExportService to package scenarios as plug-and-play zip archives containing package.json, playwright.config.ts, test.spec.ts, .env, credentials.json, snippets, and referenced scenario files - Only bundles credentials, snippets, and files actually referenced (directly or transitively) by the scenario's steps, reducing archive size - Exported test runs entirely offline using a context shim that mirrors liqa's API (page, getCredential, runSnippet, getScenarioFiles, downloadFile, assert, etc.) - Added GET /scenarios/:id/export-e2e HTTP endpoint and export_e2e_test MCP tool - Added getUsedSnippets() endpoint to list snippets referenced by a scenario - Added "Used Snippets" section on scenario detail page - Added archiver@^7.0.1 dependency for zip archive creation - Bumped version to 1.11.0
392 lines
14 KiB
TypeScript
392 lines
14 KiB
TypeScript
import type {
|
|
PaginatedResponse,
|
|
Credential,
|
|
Environment,
|
|
FileMetadata,
|
|
FileListResponse,
|
|
ScenarioCredential,
|
|
Session,
|
|
Scenario,
|
|
ScenarioRun,
|
|
ScenarioRunDetail,
|
|
ScenarioRunStep,
|
|
ScenarioStep,
|
|
Snippet,
|
|
} from './types';
|
|
import { emitApiErrorToast } from '../lib/toast-events';
|
|
|
|
// API calls are versioned under /api/v1 by default.
|
|
// Set VITE_API_URL (for example, http://localhost:13000/api/v1) to use a different origin.
|
|
const BASE_URL = ((import.meta.env.VITE_API_URL as string | undefined) ?? '/api/v1').replace(
|
|
/\/$/,
|
|
'',
|
|
);
|
|
|
|
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
|
let res: Response;
|
|
try {
|
|
const headers = new Headers(init?.headers);
|
|
if (!(init?.body instanceof FormData) && !headers.has('Content-Type')) {
|
|
headers.set('Content-Type', 'application/json');
|
|
}
|
|
|
|
res = await fetch(`${BASE_URL}${path}`, {
|
|
...init,
|
|
headers,
|
|
});
|
|
} catch (err) {
|
|
const message = (err as Error).message || 'Network request failed';
|
|
emitApiErrorToast(message);
|
|
throw new Error(message);
|
|
}
|
|
if (!res.ok) {
|
|
const text = await res.text().catch(() => res.statusText);
|
|
const message = `${res.status} ${text}`;
|
|
emitApiErrorToast(message);
|
|
throw new Error(message);
|
|
}
|
|
const contentLength = res.headers.get('content-length');
|
|
if (res.status === 204 || contentLength === '0') return undefined as T;
|
|
const contentType = res.headers.get('content-type')?.toLowerCase() ?? '';
|
|
const text = await res.text();
|
|
if (!text) return undefined as T;
|
|
if (contentType.includes('application/json')) {
|
|
return JSON.parse(text) as T;
|
|
}
|
|
return text as T;
|
|
}
|
|
|
|
// ── Credentials ───────────────────────────────────────────────────────────────
|
|
|
|
export const credentials = {
|
|
list(page = 1, limit = 50): Promise<PaginatedResponse<Credential>> {
|
|
return request(`/credentials?page=${page}&limit=${limit}`);
|
|
},
|
|
get(id: string): Promise<Credential> {
|
|
return request(`/credentials/${id}`);
|
|
},
|
|
create(name: string, data?: string): Promise<Credential> {
|
|
return request('/credentials', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ name, data }),
|
|
});
|
|
},
|
|
update(id: string, patch: Partial<Pick<Credential, 'name' | 'data'>>): Promise<Credential> {
|
|
return request(`/credentials/${id}`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify(patch),
|
|
});
|
|
},
|
|
remove(id: string): Promise<void> {
|
|
return request(`/credentials/${id}`, { method: 'DELETE' });
|
|
},
|
|
exportCredential(id: string): Promise<unknown> {
|
|
return request(`/credentials/${id}/export`);
|
|
},
|
|
importCredential(payload: unknown): Promise<Credential> {
|
|
return request('/credentials/import', {
|
|
method: 'POST',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
},
|
|
};
|
|
|
|
// ── Snippets ──────────────────────────────────────────────────────────────────
|
|
|
|
export const snippets = {
|
|
list(page = 1, limit = 50): Promise<PaginatedResponse<Snippet>> {
|
|
return request(`/snippets?page=${page}&limit=${limit}`);
|
|
},
|
|
get(id: string): Promise<Snippet> {
|
|
return request(`/snippets/${id}`);
|
|
},
|
|
create(
|
|
payload: Pick<Snippet, 'alias' | 'title' | 'code'> & { description?: string },
|
|
): Promise<Snippet> {
|
|
return request('/snippets', {
|
|
method: 'POST',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
},
|
|
update(
|
|
id: string,
|
|
patch: Partial<Pick<Snippet, 'alias' | 'title' | 'description' | 'code'>>,
|
|
): Promise<Snippet> {
|
|
return request(`/snippets/${id}`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify(patch),
|
|
});
|
|
},
|
|
remove(id: string): Promise<void> {
|
|
return request(`/snippets/${id}`, { method: 'DELETE' });
|
|
},
|
|
exportSnippet(id: string): Promise<unknown> {
|
|
return request(`/snippets/${id}/export`);
|
|
},
|
|
importSnippet(payload: unknown): Promise<Snippet> {
|
|
return request('/snippets/import', {
|
|
method: 'POST',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
},
|
|
};
|
|
|
|
// ── Environments ──────────────────────────────────────────────────────────────
|
|
|
|
export const environments = {
|
|
list(page = 1, limit = 50): Promise<PaginatedResponse<Environment>> {
|
|
return request(`/environments?page=${page}&limit=${limit}`);
|
|
},
|
|
get(id: string): Promise<Environment> {
|
|
return request(`/environments/${id}`);
|
|
},
|
|
create(name: string, data: Environment['data'], description?: string): Promise<Environment> {
|
|
return request('/environments', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ name, description, data }),
|
|
});
|
|
},
|
|
update(
|
|
id: string,
|
|
patch: Partial<Pick<Environment, 'name' | 'description' | 'data'>>,
|
|
): Promise<Environment> {
|
|
return request(`/environments/${id}`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify(patch),
|
|
});
|
|
},
|
|
remove(id: string): Promise<void> {
|
|
return request(`/environments/${id}`, { method: 'DELETE' });
|
|
},
|
|
exportEnvironment(id: string): Promise<unknown> {
|
|
return request(`/environments/${id}/export`);
|
|
},
|
|
importEnvironment(payload: unknown): Promise<Environment> {
|
|
return request('/environments/import', {
|
|
method: 'POST',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
},
|
|
};
|
|
|
|
// ── Sessions ──────────────────────────────────────────────────────────────────
|
|
|
|
export const sessions = {
|
|
list(
|
|
page = 1,
|
|
limit = 50,
|
|
orderBy = 'id',
|
|
orderDir: 'ASC' | 'DESC' = 'DESC',
|
|
): Promise<PaginatedResponse<Session>> {
|
|
return request(`/sessions?page=${page}&limit=${limit}&orderBy=${orderBy}&orderDir=${orderDir}`);
|
|
},
|
|
get(id: string): Promise<Session> {
|
|
return request(`/sessions/${id}`);
|
|
},
|
|
remove(id: string): Promise<void> {
|
|
return request(`/sessions/${id}`, { method: 'DELETE' });
|
|
},
|
|
};
|
|
|
|
// ── Scenarios ─────────────────────────────────────────────────────────────────
|
|
|
|
export const scenarios = {
|
|
list(
|
|
page = 1,
|
|
limit = 50,
|
|
orderBy = 'updatedAt',
|
|
orderDir: 'ASC' | 'DESC' = 'DESC',
|
|
): Promise<PaginatedResponse<Scenario>> {
|
|
return request(
|
|
`/scenarios?page=${page}&limit=${limit}&orderBy=${orderBy}&orderDir=${orderDir}`,
|
|
);
|
|
},
|
|
get(id: string): Promise<Scenario & { steps: ScenarioStep[] }> {
|
|
return request(`/scenarios/${id}`);
|
|
},
|
|
create(name: string, description?: string, environmentId?: string): Promise<Scenario> {
|
|
return request('/scenarios', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ name, description, environmentId }),
|
|
});
|
|
},
|
|
update(
|
|
id: string,
|
|
patch: Partial<Pick<Scenario, 'name' | 'description' | 'environmentId' | 'timeoutSeconds'>>,
|
|
): Promise<Scenario> {
|
|
return request(`/scenarios/${id}`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify(patch),
|
|
});
|
|
},
|
|
remove(id: string): Promise<void> {
|
|
return request(`/scenarios/${id}`, { method: 'DELETE' });
|
|
},
|
|
run(id: string, environmentId: string, saveSession?: boolean): Promise<ScenarioRun> {
|
|
return request(`/scenarios/${id}/run`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ environmentId, saveSession }),
|
|
});
|
|
},
|
|
getRun(scenarioId: string, runId: string): Promise<ScenarioRunDetail> {
|
|
return request(`/scenarios/${scenarioId}/run/${runId}`);
|
|
},
|
|
waitForRun(scenarioId: string, runId: string): Promise<ScenarioRunDetail> {
|
|
return request(`/scenarios/${scenarioId}/run/${runId}/wait`, { method: 'POST' });
|
|
},
|
|
listRuns(
|
|
scenarioId: string,
|
|
page = 1,
|
|
limit = 20,
|
|
): Promise<PaginatedResponse<ScenarioRun & { stepRuns: ScenarioRunStep[] }>> {
|
|
return request(`/scenarios/${scenarioId}/runs?page=${page}&limit=${limit}`);
|
|
},
|
|
exportScenario(
|
|
id: string,
|
|
options: { includeEnvironment: boolean; credentialIds: string[] } = {
|
|
includeEnvironment: false,
|
|
credentialIds: [],
|
|
},
|
|
): Promise<string> {
|
|
const params = new URLSearchParams();
|
|
params.set('includeEnvironment', String(options.includeEnvironment));
|
|
for (const cid of options.credentialIds) {
|
|
params.append('credentialIds', cid);
|
|
}
|
|
return request(`/scenarios/${id}/export?${params.toString()}`);
|
|
},
|
|
importScenario(payload: unknown): Promise<Scenario> {
|
|
return request('/scenarios/import', {
|
|
method: 'POST',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
},
|
|
exportE2eTestUrl(id: string): string {
|
|
return `${BASE_URL}/scenarios/${id}/export-e2e`;
|
|
},
|
|
usedSnippets(id: string): Promise<Snippet[]> {
|
|
return request(`/scenarios/${id}/used-snippets`);
|
|
},
|
|
};
|
|
|
|
// ── Runs ─────────────────────────────────────────────────────────────────────
|
|
|
|
export const runs = {
|
|
listAll(
|
|
page = 1,
|
|
limit = 20,
|
|
status?: string,
|
|
orderBy = 'createdAt',
|
|
orderDir: 'ASC' | 'DESC' = 'DESC',
|
|
): Promise<PaginatedResponse<ScenarioRun & { scenario: { id: string; name: string } }>> {
|
|
const q = new URLSearchParams({ page: String(page), limit: String(limit), orderBy, orderDir });
|
|
if (status) q.set('status', status);
|
|
return request(`/scenarios/runs?${q}`);
|
|
},
|
|
list(
|
|
scenarioId: string,
|
|
page = 1,
|
|
limit = 20,
|
|
orderBy = 'createdAt',
|
|
orderDir: 'ASC' | 'DESC' = 'DESC',
|
|
): Promise<PaginatedResponse<ScenarioRun & { stepRuns: ScenarioRunStep[] }>> {
|
|
return request(
|
|
`/scenarios/${scenarioId}/runs?page=${page}&limit=${limit}&orderBy=${orderBy}&orderDir=${orderDir}`,
|
|
);
|
|
},
|
|
get(scenarioId: string, runId: string, q?: string): Promise<ScenarioRunDetail> {
|
|
const qs = q?.trim() ? `?q=${encodeURIComponent(q.trim())}` : '';
|
|
return request(`/scenarios/${scenarioId}/run/${runId}${qs}`);
|
|
},
|
|
exportLogs(scenarioId: string, runId: string, format: 'csv' | 'md'): Promise<string> {
|
|
return request(`/scenarios/${scenarioId}/run/${runId}/logs/export?format=${format}`);
|
|
},
|
|
};
|
|
|
|
// ── Scenario Credentials ──────────────────────────────────────────────────────
|
|
|
|
export const scenarioCredentials = {
|
|
list(scenarioId: string): Promise<ScenarioCredential[]> {
|
|
return request(`/scenarios/${scenarioId}/credentials`);
|
|
},
|
|
add(scenarioId: string, credentialId: string, alias: string): Promise<ScenarioCredential> {
|
|
return request(`/scenarios/${scenarioId}/credentials`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ credentialId, alias }),
|
|
});
|
|
},
|
|
remove(scenarioId: string, scCredId: string): Promise<void> {
|
|
return request(`/scenarios/${scenarioId}/credentials/${scCredId}`, {
|
|
method: 'DELETE',
|
|
});
|
|
},
|
|
};
|
|
|
|
// ── Scenario Steps ────────────────────────────────────────────────────────────
|
|
export interface CreateStepPayload {
|
|
title?: string;
|
|
execCode?: string;
|
|
timeoutSeconds?: number | null;
|
|
}
|
|
|
|
export interface UpdateStepPayload {
|
|
title?: string;
|
|
order?: number;
|
|
execCode?: string;
|
|
timeoutSeconds?: number | null;
|
|
}
|
|
|
|
export const steps = {
|
|
get(scenarioId: string, stepId: string): Promise<ScenarioStep> {
|
|
return request(`/scenarios/${scenarioId}/steps/${stepId}`);
|
|
},
|
|
create(scenarioId: string, payload: CreateStepPayload): Promise<ScenarioStep> {
|
|
return request(`/scenarios/${scenarioId}/steps`, {
|
|
method: 'POST',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
},
|
|
update(scenarioId: string, stepId: string, payload: UpdateStepPayload): Promise<ScenarioStep> {
|
|
return request(`/scenarios/${scenarioId}/steps/${stepId}`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
},
|
|
remove(scenarioId: string, stepId: string): Promise<void> {
|
|
return request(`/scenarios/${scenarioId}/steps/${stepId}`, { method: 'DELETE' });
|
|
},
|
|
};
|
|
|
|
// ── Scenario Files ────────────────────────────────────────────────────────
|
|
|
|
export const scenarioFiles = {
|
|
upload(scenarioId: string, file: File, expiresAt?: Date): Promise<FileMetadata> {
|
|
const form = new FormData();
|
|
form.append('file', file);
|
|
if (expiresAt) form.append('expiresAt', expiresAt.toISOString());
|
|
return request(`/scenarios/${scenarioId}/files`, {
|
|
method: 'POST',
|
|
body: form,
|
|
});
|
|
},
|
|
list(scenarioId: string, limit = 20, offset = 0): Promise<FileListResponse> {
|
|
return request(`/scenarios/${scenarioId}/files?limit=${limit}&offset=${offset}`);
|
|
},
|
|
/** Returns a URL that can be used as an <a href> for direct download. */
|
|
contentUrl(scenarioId: string, fileId: string): string {
|
|
return `${BASE_URL}/scenarios/${scenarioId}/files/${fileId}/content`;
|
|
},
|
|
};
|
|
|
|
// ── Run Files ─────────────────────────────────────────────────────────────
|
|
|
|
export const runFiles = {
|
|
list(scenarioId: string, runId: string, limit = 20, offset = 0): Promise<FileListResponse> {
|
|
return request(`/scenarios/${scenarioId}/runs/${runId}/files?limit=${limit}&offset=${offset}`);
|
|
},
|
|
/** Returns a URL that can be used as an <a href> for direct download. */
|
|
contentUrl(scenarioId: string, runId: string, fileId: string): string {
|
|
return `${BASE_URL}/scenarios/${scenarioId}/runs/${runId}/files/${fileId}/content`;
|
|
},
|
|
};
|