doc: all available pages schema

This commit is contained in:
Artemy 2023-08-16 12:07:12 +03:00
parent 20755f2b89
commit b0bbcd0bba
6 changed files with 101 additions and 32 deletions

View File

@ -4,3 +4,21 @@ export interface IHandlerOutput {
title: string; title: string;
lang: string; lang: string;
} }
export const handlerSchema = {
type: "object",
properties: {
content: {
type: "string",
},
textContent: {
type: "string",
},
title: {
type: "string",
},
lang: {
type: "string",
},
},
};

View File

@ -1,8 +1,9 @@
import { FastifyInstance } from "fastify"; import { FastifyInstance } from "fastify";
import { engineList } from "../handlers/main"; import { engineList } from "../handlers/main";
import { indexSchema } from "../types/requests";
export default async function indexRoute(fastify: FastifyInstance) { export default async function indexRoute(fastify: FastifyInstance) {
fastify.get("/", async (_, reply) => { fastify.get("/", { schema: indexSchema }, async (_, reply) => {
return reply.view("/templates/index.ejs", { engineList }); return reply.view("/templates/index.ejs", { engineList });
}); });
} }

View File

@ -1,11 +1,14 @@
import { EngineRequest } from "../types/requests"; import { EngineRequest, IParseSchema, parseSchema } from "../types/requests";
import { FastifyInstance } from "fastify"; import { FastifyInstance } from "fastify";
import handlePage from "../handlers/main"; import handlePage from "../handlers/main";
import { generateRequestUrl } from "../utils"; import { generateRequestUrl } from "../utils";
export default async function parseRoute(fastify: FastifyInstance) { export default async function parseRoute(fastify: FastifyInstance) {
fastify.get("/parse", async (request: EngineRequest) => { fastify.get<IParseSchema>(
const parsed = await handlePage( "/parse",
{ schema: parseSchema },
async (request: EngineRequest) => {
return await handlePage(
request.query.url, request.query.url,
generateRequestUrl( generateRequestUrl(
request.protocol, request.protocol,
@ -14,7 +17,6 @@ export default async function parseRoute(fastify: FastifyInstance) {
), ),
request.query.engine request.query.engine
); );
}
return parsed; );
});
} }

View File

@ -1,11 +1,14 @@
import { FastifyInstance } from "fastify"; import { FastifyInstance } from "fastify";
import { GetRequest } from "../types/requests"; import { GetRequest, IParseSchema, rawHtmlSchema } from "../types/requests";
import handlePage from "../handlers/main"; import handlePage from "../handlers/main";
import { generateRequestUrl } from "../utils"; import { generateRequestUrl } from "../utils";
export default async function rawHtml(fastify: FastifyInstance) { export default async function rawHtml(fastify: FastifyInstance) {
fastify.get("/raw-html", async (request: GetRequest) => { fastify.get<IParseSchema>(
"/raw-html",
{ schema: rawHtmlSchema },
async (request: GetRequest) => {
return ( return (
await handlePage( await handlePage(
request.query.url, request.query.url,
@ -16,5 +19,6 @@ export default async function rawHtml(fastify: FastifyInstance) {
) )
) )
).content; ).content;
}); }
);
} }

View File

@ -1,4 +1,6 @@
import { FastifyRequest, FastifySchema } from "fastify"; import { FastifyRequest, FastifySchema } from "fastify";
import { handlerSchema } from "../handlers/handler.interface";
import { engineList } from "../handlers/main";
export type GetRequest = FastifyRequest<{ export type GetRequest = FastifyRequest<{
Querystring: { Querystring: {
@ -14,10 +16,23 @@ export interface IGetQuery {
engine?: string; engine?: string;
} }
export interface IParseQuery {
url: string;
engine?: string;
}
export interface IGetSchema { export interface IGetSchema {
Querystring: IGetQuery; Querystring: IGetQuery;
} }
export interface IParseSchema {
Querystring: IParseQuery;
}
export const indexSchema = {
produces: ["text/html"],
};
export const getQuerySchema = { export const getQuerySchema = {
type: "object", type: "object",
required: ["url"], required: ["url"],
@ -33,8 +48,22 @@ export const getQuerySchema = {
}, },
engine: { engine: {
type: "string", type: "string",
enum: ["readability", "google", ""], enum: [...engineList, ""],
default: "readability", },
},
};
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 = { export const GetSchema: FastifySchema = {
description: "Get page", description: "Get page",
querystring: getQuerySchema, 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"], produces: ["text/html"],
}; };