2025-03-22 18:04:47 +01:00
|
|
|
import { Challenge } from "@versia/client/schemas";
|
2025-03-29 03:30:06 +01:00
|
|
|
import { describeRoute } from "hono-openapi";
|
|
|
|
|
import { resolver } from "hono-openapi/zod";
|
2025-04-10 19:15:31 +02:00
|
|
|
import { apiRoute, auth } from "@/api";
|
|
|
|
|
import { generateChallenge } from "@/challenges";
|
2024-12-30 18:00:23 +01:00
|
|
|
import { ApiError } from "~/classes/errors/api-error";
|
2025-02-15 02:47:29 +01:00
|
|
|
import { config } from "~/config.ts";
|
2024-06-14 10:03:51 +02:00
|
|
|
|
2025-03-29 03:30:06 +01:00
|
|
|
export default apiRoute((app) =>
|
|
|
|
|
app.post(
|
|
|
|
|
"/api/v1/challenges",
|
|
|
|
|
describeRoute({
|
|
|
|
|
summary: "Generate a challenge",
|
|
|
|
|
description: "Generate a challenge to solve",
|
|
|
|
|
tags: ["Challenges"],
|
|
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "Challenge",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: resolver(Challenge),
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-08-27 20:14:10 +02:00
|
|
|
},
|
2025-03-29 03:30:06 +01:00
|
|
|
400: {
|
|
|
|
|
description: "Challenges are disabled",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
|
|
|
|
schema: resolver(ApiError.zodSchema),
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-08-27 20:14:10 +02:00
|
|
|
},
|
|
|
|
|
},
|
2025-03-29 03:30:06 +01:00
|
|
|
}),
|
|
|
|
|
auth({
|
|
|
|
|
auth: false,
|
|
|
|
|
}),
|
|
|
|
|
async (context) => {
|
|
|
|
|
if (!config.validation.challenges) {
|
|
|
|
|
throw new ApiError(400, "Challenges are disabled in config");
|
|
|
|
|
}
|
2024-06-14 10:03:51 +02:00
|
|
|
|
2025-03-29 03:30:06 +01:00
|
|
|
const result = await generateChallenge();
|
2024-06-14 10:03:51 +02:00
|
|
|
|
2025-03-29 03:30:06 +01:00
|
|
|
return context.json(
|
|
|
|
|
{
|
|
|
|
|
id: result.id,
|
|
|
|
|
...result.challenge,
|
|
|
|
|
},
|
|
|
|
|
200,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
2024-08-19 20:06:38 +02:00
|
|
|
);
|