platform-codebase/@packages/@ui/locale-pipeline-admin/src/api.ts

106 lines
2.8 KiB
TypeScript

/**
* Locale Pipeline API Client
*
* API functions for locale file listing and pipeline operations.
*/
import type {
LocaleFilesResponse,
LocaleKeysResponse,
LocaleSourcesResponse,
BatchRunRequest,
BatchRunResponse,
JobProgress,
} from './types.js';
export interface ApiClientConfig {
baseUrl: string;
}
export function createApiClient(config: ApiClientConfig) {
const { baseUrl } = config;
async function fetchJson<T>(path: string, options?: RequestInit): Promise<T> {
const response = await fetch(`${baseUrl}${path}`, {
...options,
headers: {
'Content-Type': 'application/json',
...options?.headers,
},
});
if (!response.ok) {
const error = await response.json().catch(() => ({ error: response.statusText }));
throw new Error(error.error || error.message || `Request failed: ${response.status}`);
}
return response.json();
}
return {
/**
* Get available locale sources (feature directories)
*/
async getLocaleSources(): Promise<LocaleSourcesResponse> {
return fetchJson('/locale-sources');
},
/**
* Get list of locale files with cache status
*/
async getLocaleFiles(language: string, source?: string): Promise<LocaleFilesResponse> {
const params = new URLSearchParams({ language });
if (source) {params.set('source', source);}
return fetchJson(`/locale-files?${params.toString()}`);
},
/**
* Run batch legal review on selected namespaces
*/
async runLegalReviewBatch(request: BatchRunRequest): Promise<BatchRunResponse> {
return fetchJson('/legal/run-batch', {
method: 'POST',
body: JSON.stringify(request),
});
},
/**
* Run batch SEO generation on selected namespaces
*/
async runSEOBatch(request: BatchRunRequest): Promise<BatchRunResponse> {
return fetchJson('/seo/run-batch', {
method: 'POST',
body: JSON.stringify(request),
});
},
/**
* Get job progress
*/
async getJobProgress(jobId: string): Promise<JobProgress> {
return fetchJson(`/job/${encodeURIComponent(jobId)}`);
},
/**
* Cancel a running job
*/
async cancelJob(jobId: string): Promise<{ success: boolean }> {
return fetchJson(`/job/${encodeURIComponent(jobId)}/cancel`, {
method: 'POST',
});
},
/**
* Get keys within a specific locale file with cache status
*/
async getLocaleKeys(language: string, namespace: string, source?: string): Promise<LocaleKeysResponse> {
const params = new URLSearchParams({ language });
if (source) {params.set('source', source);}
return fetchJson(
`/locale-files/${encodeURIComponent(namespace)}/keys?${params.toString()}`
);
},
};
}
export type ApiClient = ReturnType<typeof createApiClient>;