Files
bw-expo-app/lib/api/template-runs.ts
imeepos 7e3f94bae3 Initial commit: Expo app with Better Auth integration
- Complete Expo React Native app setup with TypeScript
- Better Auth authentication system integration
- Secure storage implementation for session tokens
- Authentication flow with login/logout functionality
- API client configuration for backend communication
- Responsive UI components with themed styling
- Expo Router navigation setup
- Development configuration and scripts

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-14 10:32:52 +08:00

56 lines
1.5 KiB
TypeScript

import { apiClient } from './client';
import { RunTemplateResponse, TemplateGenerationResponse, RunTemplateData } from '../types/template-run';
export async function runTemplate(templateId: string, data: RunTemplateData): Promise<RunTemplateResponse> {
return apiClient<RunTemplateResponse>(`/api/templates/${templateId}/run`, {
method: 'POST',
body: JSON.stringify({ data }),
});
}
export async function getTemplateGeneration(generationId: string): Promise<TemplateGenerationResponse> {
return apiClient<TemplateGenerationResponse>(`/api/template-generations/${generationId}`);
}
export async function pollTemplateGeneration(
generationId: string,
onComplete: (result: any) => void,
onError: (error: Error) => void,
maxAttempts: number = 100,
intervalMs: number = 3000
): Promise<void> {
let attempts = 0;
const poll = async () => {
try {
attempts++;
if (attempts > maxAttempts) {
throw new Error('轮询超时,请稍后重试');
}
const response = await getTemplateGeneration(generationId);
const generation = response.data;
// 检查是否完成
if (generation.status === 'completed') {
onComplete(generation);
return;
}
// 检查是否失败
if (generation.status === 'failed') {
throw new Error('任务执行失败');
}
// 继续轮询
setTimeout(poll, intervalMs);
} catch (error) {
onError(error as Error);
}
};
// 开始轮询
setTimeout(poll, intervalMs);
}