| export const COMPILER_API_BASE = '/api/compiler'; |
|
|
| export interface TestCase { |
| input: string; |
| expectedOutput: string; |
| isHidden: boolean; |
| } |
|
|
| export interface Problem { |
| id: string; |
| title: string; |
| description: string; |
| difficulty: string; |
| testCases: TestCase[]; |
| } |
|
|
| export interface ExecutionResult { |
| stdout: string; |
| stderr: string; |
| exitCode: number; |
| timeLimitExceeded: boolean; |
| durationMs: number; |
| } |
|
|
| export interface RunRequest { |
| language: string; |
| code: string; |
| customInput: string; |
| } |
|
|
| export interface TestCaseResult { |
| passed: boolean; |
| input: string; |
| expectedOutput: string; |
| actualOutput: string; |
| error?: string; |
| durationMs: number; |
| timeLimitExceeded: boolean; |
| } |
|
|
| export interface SubmitResult { |
| verdict: string; |
| passed: number; |
| total: number; |
| results: TestCaseResult[]; |
| } |
|
|
| async function readError(res: Response) { |
| const text = await res.text(); |
| return text.trim() || res.statusText; |
| } |
|
|
| export async function runCode(req: RunRequest): Promise<ExecutionResult> { |
| const res = await fetch(`${COMPILER_API_BASE}/run`, { |
| method: 'POST', |
| headers: { 'Content-Type': 'application/json' }, |
| body: JSON.stringify(req), |
| }); |
|
|
| if (!res.ok) { |
| throw new Error(`Execution failed: ${await readError(res)}`); |
| } |
|
|
| return res.json(); |
| } |
|
|
| export async function submitCode(problemId: string, language: string, code: string): Promise<SubmitResult> { |
| const res = await fetch(`${COMPILER_API_BASE}/submit`, { |
| method: 'POST', |
| headers: { 'Content-Type': 'application/json' }, |
| body: JSON.stringify({ problemId, language, code }), |
| }); |
|
|
| if (!res.ok) { |
| throw new Error(`Submit failed: ${await readError(res)}`); |
| } |
|
|
| return res.json(); |
| } |
|
|
| export async function fetchProblems(): Promise<Problem[]> { |
| const res = await fetch(`${COMPILER_API_BASE}/problems`); |
| if (!res.ok) throw new Error('Failed to fetch problems'); |
| return res.json(); |
| } |
|
|
| export async function fetchProblem(id: string): Promise<Problem> { |
| const res = await fetch(`${COMPILER_API_BASE}/problems/${id}`); |
| if (!res.ok) throw new Error('Failed to fetch problem'); |
| return res.json(); |
| } |
|
|