submission api client
This commit is contained in:
@@ -5,10 +5,23 @@ import type {
|
||||
QuestionDetail,
|
||||
QuestionListResponse,
|
||||
Stats,
|
||||
SubmissionRequest,
|
||||
SubmissionResponse,
|
||||
} from "@/types";
|
||||
|
||||
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000";
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
public status: number,
|
||||
public statusText: string,
|
||||
public detail?: string
|
||||
) {
|
||||
super(detail || `API error: ${status} ${statusText}`);
|
||||
this.name = "ApiError";
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchApi<T>(
|
||||
endpoint: string,
|
||||
options?: RequestInit
|
||||
@@ -23,7 +36,14 @@ async function fetchApi<T>(
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`API error: ${response.status} ${response.statusText}`);
|
||||
let detail: string | undefined;
|
||||
try {
|
||||
const errorBody = await response.json();
|
||||
detail = errorBody.detail;
|
||||
} catch {
|
||||
// Ignore JSON parse errors
|
||||
}
|
||||
throw new ApiError(response.status, response.statusText, detail);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
@@ -77,3 +97,13 @@ export async function getPattern(slug: string): Promise<Pattern> {
|
||||
export async function getStats(): Promise<Stats> {
|
||||
return fetchApi<Stats>("/api/stats");
|
||||
}
|
||||
|
||||
export async function submitSolution(
|
||||
slug: string,
|
||||
submission: SubmissionRequest
|
||||
): Promise<SubmissionResponse> {
|
||||
return fetchApi<SubmissionResponse>(`/api/questions/${slug}/submit`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(submission),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user