mirror of
https://github.com/txtdot/txtdot
synced 2024-10-18 14:40:19 +03:00
doc: schema for get
This commit is contained in:
parent
9da9ea3ade
commit
20755f2b89
@ -1,38 +1,42 @@
|
|||||||
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>(
|
||||||
const remoteUrl = request.query.url;
|
"/get",
|
||||||
const engine = request.query.engine;
|
{ 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") {
|
if (request.query.format === "text") {
|
||||||
reply.type("text/plain; charset=utf-8");
|
reply.type("text/plain; charset=utf-8");
|
||||||
format = "text";
|
format = "text";
|
||||||
} else {
|
} else {
|
||||||
reply.type("text/html; charset=utf-8");
|
reply.type("text/html; charset=utf-8");
|
||||||
format = "html";
|
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 });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user