doc: schema for get

This commit is contained in:
Artemy 2023-08-16 11:54:22 +03:00
parent 9da9ea3ade
commit 20755f2b89
2 changed files with 70 additions and 29 deletions

View File

@ -1,38 +1,42 @@
import { FastifyInstance } from "fastify";
import { GetRequest } from "../types/requests";
import { GetSchema, IGetSchema } from "../types/requests";
import handlePage from "../handlers/main";
import { generateRequestUrl } from "../utils";
export default async function getRoute(fastify: FastifyInstance) {
fastify.get("/get", async (request: GetRequest, reply) => {
const remoteUrl = request.query.url;
const engine = request.query.engine;
fastify.get<IGetSchema>(
"/get",
{ schema: GetSchema },
async (request, reply) => {
const remoteUrl = request.query.url;
const engine = request.query.engine;
let format: string;
let format: string;
if (request.query.format === "text") {
reply.type("text/plain; charset=utf-8");
format = "text";
} else {
reply.type("text/html; charset=utf-8");
format = "html";
if (request.query.format === "text") {
reply.type("text/plain; charset=utf-8");
format = "text";
} else {
reply.type("text/html; charset=utf-8");
format = "html";
}
const parsed = await handlePage(
remoteUrl,
generateRequestUrl(
request.protocol,
request.hostname,
request.originalUrl
),
engine
);
if (format === "text") {
return parsed.textContent;
} else {
return reply.view("/templates/get.ejs", { parsed: parsed });
}
}
const parsed = await handlePage(
remoteUrl,
generateRequestUrl(
request.protocol,
request.hostname,
request.originalUrl
),
engine
);
if (format === "text") {
return parsed.textContent;
} else {
return reply.view("/templates/get.ejs", { parsed: parsed });
}
});
);
}

View File

@ -1,4 +1,4 @@
import { FastifyRequest } from "fastify";
import { FastifyRequest, FastifySchema } from "fastify";
export type GetRequest = FastifyRequest<{
Querystring: {
@ -8,6 +8,43 @@ export type GetRequest = FastifyRequest<{
};
}>;
export interface IGetQuery {
url: string;
format?: string;
engine?: string;
}
export interface IGetSchema {
Querystring: IGetQuery;
}
export const getQuerySchema = {
type: "object",
required: ["url"],
properties: {
url: {
type: "string",
description: "URL",
},
format: {
type: "string",
enum: ["text", "html", ""],
default: "html",
},
engine: {
type: "string",
enum: ["readability", "google", ""],
default: "readability",
},
},
};
export const GetSchema: FastifySchema = {
description: "Get page",
querystring: getQuerySchema,
produces: ["text/html"],
};
export type EngineRequest = FastifyRequest<{
Querystring: {
url: string;