From b0bbcd0bba629ef068ebdf2be5d2604b20e9595c Mon Sep 17 00:00:00 2001 From: Artemy Date: Wed, 16 Aug 2023 12:07:12 +0300 Subject: [PATCH] doc: all available pages schema --- src/handlers/handler.interface.ts | 18 ++++++++++++ src/routes/index.ts | 3 +- src/routes/parse.ts | 30 ++++++++++--------- src/routes/raw-html.ts | 28 ++++++++++-------- src/types/requests.ts | 48 +++++++++++++++++++++++++++++-- templates/index.ejs | 6 ++-- 6 files changed, 101 insertions(+), 32 deletions(-) diff --git a/src/handlers/handler.interface.ts b/src/handlers/handler.interface.ts index 26c2053..14e69d3 100644 --- a/src/handlers/handler.interface.ts +++ b/src/handlers/handler.interface.ts @@ -4,3 +4,21 @@ export interface IHandlerOutput { title: string; lang: string; } + +export const handlerSchema = { + type: "object", + properties: { + content: { + type: "string", + }, + textContent: { + type: "string", + }, + title: { + type: "string", + }, + lang: { + type: "string", + }, + }, +}; diff --git a/src/routes/index.ts b/src/routes/index.ts index 200463a..ea3ff2d 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,8 +1,9 @@ import { FastifyInstance } from "fastify"; import { engineList } from "../handlers/main"; +import { indexSchema } from "../types/requests"; export default async function indexRoute(fastify: FastifyInstance) { - fastify.get("/", async (_, reply) => { + fastify.get("/", { schema: indexSchema }, async (_, reply) => { return reply.view("/templates/index.ejs", { engineList }); }); } diff --git a/src/routes/parse.ts b/src/routes/parse.ts index 87858ec..5523ff3 100644 --- a/src/routes/parse.ts +++ b/src/routes/parse.ts @@ -1,20 +1,22 @@ -import { EngineRequest } from "../types/requests"; +import { EngineRequest, IParseSchema, parseSchema } from "../types/requests"; import { FastifyInstance } from "fastify"; import handlePage from "../handlers/main"; import { generateRequestUrl } from "../utils"; export default async function parseRoute(fastify: FastifyInstance) { - fastify.get("/parse", async (request: EngineRequest) => { - const parsed = await handlePage( - request.query.url, - generateRequestUrl( - request.protocol, - request.hostname, - request.originalUrl - ), - request.query.engine - ); - - return parsed; - }); + fastify.get( + "/parse", + { schema: parseSchema }, + async (request: EngineRequest) => { + return await handlePage( + request.query.url, + generateRequestUrl( + request.protocol, + request.hostname, + request.originalUrl + ), + request.query.engine + ); + } + ); } diff --git a/src/routes/raw-html.ts b/src/routes/raw-html.ts index 4ac60d8..38fad4b 100644 --- a/src/routes/raw-html.ts +++ b/src/routes/raw-html.ts @@ -1,20 +1,24 @@ import { FastifyInstance } from "fastify"; -import { GetRequest } from "../types/requests"; +import { GetRequest, IParseSchema, rawHtmlSchema } from "../types/requests"; import handlePage from "../handlers/main"; import { generateRequestUrl } from "../utils"; export default async function rawHtml(fastify: FastifyInstance) { - fastify.get("/raw-html", async (request: GetRequest) => { - return ( - await handlePage( - request.query.url, - generateRequestUrl( - request.protocol, - request.hostname, - request.originalUrl + fastify.get( + "/raw-html", + { schema: rawHtmlSchema }, + async (request: GetRequest) => { + return ( + await handlePage( + request.query.url, + generateRequestUrl( + request.protocol, + request.hostname, + request.originalUrl + ) ) - ) - ).content; - }); + ).content; + } + ); } diff --git a/src/types/requests.ts b/src/types/requests.ts index 1639525..157754d 100644 --- a/src/types/requests.ts +++ b/src/types/requests.ts @@ -1,4 +1,6 @@ import { FastifyRequest, FastifySchema } from "fastify"; +import { handlerSchema } from "../handlers/handler.interface"; +import { engineList } from "../handlers/main"; export type GetRequest = FastifyRequest<{ Querystring: { @@ -14,10 +16,23 @@ export interface IGetQuery { engine?: string; } +export interface IParseQuery { + url: string; + engine?: string; +} + export interface IGetSchema { Querystring: IGetQuery; } +export interface IParseSchema { + Querystring: IParseQuery; +} + +export const indexSchema = { + produces: ["text/html"], +}; + export const getQuerySchema = { type: "object", required: ["url"], @@ -33,8 +48,22 @@ export const getQuerySchema = { }, engine: { type: "string", - enum: ["readability", "google", ""], - default: "readability", + enum: [...engineList, ""], + }, + }, +}; + +export const parseQuerySchema = { + type: "object", + required: ["url"], + properties: { + url: { + type: "string", + description: "URL", + }, + engine: { + type: "string", + enum: [...engineList, ""], }, }, }; @@ -42,6 +71,21 @@ export const getQuerySchema = { export const GetSchema: FastifySchema = { description: "Get page", querystring: getQuerySchema, + produces: ["text/html", "text/plain"], +}; + +export const parseSchema: FastifySchema = { + description: "Parse page", + querystring: parseQuerySchema, + response: { + "2xx": handlerSchema, + }, + produces: ["text/json"], +}; + +export const rawHtmlSchema: FastifySchema = { + description: "Get raw HTML", + querystring: parseQuerySchema, produces: ["text/html"], }; diff --git a/templates/index.ejs b/templates/index.ejs index 42e1bae..d996086 100644 --- a/templates/index.ejs +++ b/templates/index.ejs @@ -30,9 +30,9 @@ <% engineList.forEach((engine)=> { %> - + <% }) %>