un-oj
Version:
Unified Online Judge information collector.
40 lines (38 loc) • 1.31 kB
JavaScript
import { NotFoundError, Platform, UnOJError, UnexpectedResponseError } from "../platform-BncnsqE0.js";
import { parseHydroProblem } from "../hydro-KDebFwl5.js";
import { FetchError } from "ofetch";
//#region src/platforms/mxoj.ts
const ProblemTypeOverridingTags = {
客观题: "objective",
提交答案题: "submission",
交互题: "interactive"
};
const DEFAULT_BASE_URL = "https://oier.team";
/** MXOJ platform. */
var MXOJ = class extends Platform {
constructor(options) {
super(options, DEFAULT_BASE_URL);
}
/** Fetches a problem from MXOJ using internal API. */
async getProblem(id) {
const path = `/api/p/${id}`;
let pdoc;
try {
const response = await this.ofetch(path, { responseType: "json" });
pdoc = response.pdoc;
} catch (e) {
if (e instanceof FetchError && e.statusCode === 404) throw new NotFoundError("problem");
throw new UnOJError(`Failed to fetch problem ${id}`, { cause: e });
}
if (!pdoc) throw new UnexpectedResponseError(pdoc);
const problem = parseHydroProblem(pdoc);
problem.link = new URL(`/problems/${id}`, this.baseURL).href;
for (const tag of problem.tags) if (tag in ProblemTypeOverridingTags) {
problem.type = ProblemTypeOverridingTags[tag];
break;
}
return problem;
}
};
//#endregion
export { DEFAULT_BASE_URL, MXOJ as default };