This repository has been archived on 2024-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
wikipedia-game-solver/apps/api/app/controllers/wikipedia/pages/get_wikipedia_pages.ts
Théo LUDWIG 20ab889cf8
All checks were successful
Chromatic / chromatic (push) Successful in 2m3s
CI / ci (push) Successful in 4m16s
CI / commitlint (push) Successful in 14s
chore: improve type safety Tuyau
2024-08-18 01:31:02 +01:00

49 lines
1.2 KiB
TypeScript

import Page, { type PageRaw } from "#app/models/page.ts"
import { throttle } from "#start/limiter.ts"
import type { HttpContext } from "@adonisjs/core/http"
import router from "@adonisjs/core/services/router"
import { sanitizePageTitle } from "@repo/wikipedia"
import vine from "@vinejs/vine"
export const get_wikipedia_pages_validator = vine.compile(
vine.object({
title: vine
.string()
.minLength(1)
.maxLength(255)
.transform((value) => {
return sanitizePageTitle(value)
}),
limit: vine
.number()
.parse((value) => {
return value ?? 5
})
.withoutDecimals()
.range([1, 100]),
}),
)
export default class get_wikipedia_pages {
public async handle(context: HttpContext): Promise<{
__response: PageRaw[]
__status: 200
}> {
const payload = await context.request.validateUsing(
get_wikipedia_pages_validator,
)
const pages = await Page.query()
.whereLike("title", `${payload.title}%`)
.limit(payload.limit)
return context.response.ok(pages)
}
}
router
.get("/wikipedia/pages", [get_wikipedia_pages])
.use(throttle)
.openapi({
description: "Search Wikipedia pages by title.",
tags: ["wikipedia"],
})