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;
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 { 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 });
});
}

View File

@ -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<IParseSchema>(
"/parse",
{ schema: parseSchema },
async (request: EngineRequest) => {
return await handlePage(
request.query.url,
generateRequestUrl(
request.protocol,
request.hostname,
request.originalUrl
),
request.query.engine
);
}
);
}

View File

@ -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<IParseSchema>(
"/raw-html",
{ schema: rawHtmlSchema },
async (request: GetRequest) => {
return (
await handlePage(
request.query.url,
generateRequestUrl(
request.protocol,
request.hostname,
request.originalUrl
)
)
)
).content;
});
).content;
}
);
}

View File

@ -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"],
};

View File

@ -30,9 +30,9 @@
<option value="" selected>Default</option>
<% engineList.forEach((engine)=> {
%>
<option value="<%= engine %>">
<%= engine %>
</option>
<option value="<%= engine %>">
<%= engine %>
</option>
<%
})
%>