platform-codebase/features/frontend-showcase/e2e/helpers/api.ts

89 lines
2.3 KiB
TypeScript
Executable file

/**
* API Helper Functions for E2E Tests
*
* Direct API calls for setup/verification without going through UI
*/
// Get API URL from environment variable
// Docker compose sets API_URL=http://localhost:3016 in docker-compose.yml
// For local dev outside Docker, set this environment variable manually or update this constant
const API_URL = process.env.API_URL || 'http://localhost:3016';
export interface LocaleContent {
[key: string]: string | LocaleContent;
}
export async function readLocaleFile(file: string): Promise<LocaleContent> {
const response = await fetch(`${API_URL}/dev/read-locale`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ file }),
});
if (!response.ok) {
throw new Error(`Failed to read locale: ${response.statusText}`);
}
const result = await response.json();
// Backend wraps response in { success, content }
if (!result.success) {
throw new Error(result.error || 'Unknown error');
}
return result.content;
}
export async function writeLocaleFile(
file: string,
path: string,
content: string,
backup: boolean = true
): Promise<{ success: boolean; backup?: string }> {
const response = await fetch(`${API_URL}/dev/write-locale`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ file, path, content, backup }),
});
if (!response.ok) {
throw new Error(`Failed to write locale: ${response.statusText}`);
}
const result = await response.json();
if (!result.success) {
throw new Error(result.error || 'Unknown error');
}
return result;
}
export async function getImageMetadata(
type: string,
id: string,
variant: string,
family: string
): Promise<any> {
const response = await fetch(`${API_URL}/dev/image-metadata`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ type, id, variant, family }),
});
if (!response.ok) {
throw new Error(`Failed to get image metadata: ${response.statusText}`);
}
return await response.json();
}
export async function checkApiHealth(): Promise<boolean> {
try {
const response = await fetch(`${API_URL}/dev/health`, { method: 'GET' });
const result = await response.json();
return response.ok && result.status === 'ok';
} catch {
return false;
}
}