49 lines
1.2 KiB
TypeScript
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"],
|
|
})
|