25 lines
683 B
TypeScript
25 lines
683 B
TypeScript
import { Router } from "@oak/oak";
|
|
import {
|
|
fetchRichContent,
|
|
isValidHttpUrl,
|
|
} from "../services/rich-content-service.ts";
|
|
import { APIErrorCode } from "../model/interfaces.ts";
|
|
|
|
const previewRouter = new Router();
|
|
|
|
previewRouter.get("/api/preview", async (ctx) => {
|
|
const url = ctx.request.url.searchParams.get("url") ?? "";
|
|
if (!isValidHttpUrl(url)) {
|
|
ctx.response.status = 400;
|
|
ctx.response.body = {
|
|
success: false,
|
|
error: { code: APIErrorCode.VALIDATION_ERROR, message: "Invalid URL" },
|
|
};
|
|
return;
|
|
}
|
|
const data = await fetchRichContent(url);
|
|
ctx.response.body = { success: true, data: data ?? null };
|
|
});
|
|
|
|
export default previewRouter;
|