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,11 +1,14 @@
import { FastifyInstance } from "fastify"; import { FastifyInstance } from "fastify";
import { GetRequest } from "../types/requests"; import { GetSchema, IGetSchema } 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 getRoute(fastify: FastifyInstance) { export default async function getRoute(fastify: FastifyInstance) {
fastify.get("/get", async (request: GetRequest, reply) => { fastify.get<IGetSchema>(
"/get",
{ schema: GetSchema },
async (request, reply) => {
const remoteUrl = request.query.url; const remoteUrl = request.query.url;
const engine = request.query.engine; const engine = request.query.engine;
@ -34,5 +37,6 @@ export default async function getRoute(fastify: FastifyInstance) {
} else { } else {
return reply.view("/templates/get.ejs", { parsed: parsed }); 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<{ export type GetRequest = FastifyRequest<{
Querystring: { 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<{ export type EngineRequest = FastifyRequest<{
Querystring: { Querystring: {
url: string; url: string;