feat: convert json-schema to typescript

No more code duplication
This commit is contained in:
Artemy Egorov 2023-08-25 12:22:15 +03:00
parent 29604a2ed1
commit 8b8844f9fb
4 changed files with 12 additions and 26 deletions

View File

@ -1,7 +1,6 @@
export interface IFastifyValidationError {
statusCode?: number;
code?: string;
validation?: any;
}
export function getFastifyError(error: Error) {

View File

@ -1,7 +1,6 @@
import { FastifyInstance } from "fastify";
import { IParseSchema, rawHtmlSchema } from "../types/requests/api";
import { GetRequest } from "../types/requests/browser";
import handlePage from "../handlers/main";
import { generateRequestUrl } from "../utils/generate";
@ -10,7 +9,7 @@ export default async function rawHtml(fastify: FastifyInstance) {
fastify.get<IParseSchema>(
"/api/raw-html",
{ schema: rawHtmlSchema },
async (request: GetRequest) => {
async (request) => {
return (
await handlePage(
request.query.url,

View File

@ -2,21 +2,13 @@ import { FastifySchema, FastifyRequest } from "fastify";
import { IApiError, errorResponseSchema } from "../../errors/api";
import { handlerSchema } from "../../handlers/handler.interface";
import { engineList } from "../../handlers/main";
import { FromSchema } from "json-schema-to-ts";
export interface IApiResponse<T> {
data?: T;
error?: IApiError;
}
export interface IParseQuery {
url: string;
engine?: string;
}
export interface IParseSchema {
Querystring: IParseQuery;
}
export const parseQuerySchema = {
type: "object",
required: ["url"],
@ -30,7 +22,7 @@ export const parseQuerySchema = {
enum: [...engineList, ""],
},
},
};
} as const;
export const parseSchema: FastifySchema = {
description: "Parse the page and get all data from the engine",
@ -52,6 +44,10 @@ export const parseSchema: FastifySchema = {
produces: ["text/json"],
};
export interface IParseSchema {
Querystring: FromSchema<typeof parseQuerySchema>;
}
export const rawHtmlSchema: FastifySchema = {
description: "Parse the page and get raw HTML from the engine",
querystring: parseQuerySchema,

View File

@ -1,18 +1,9 @@
import { FastifyRequest, FastifySchema } from "fastify";
import { FastifySchema } from "fastify";
import { engineList } from "../../handlers/main";
export type GetRequest = FastifyRequest<{
Querystring: IGetQuery;
}>;
export interface IGetQuery {
url: string;
format?: string;
engine?: string;
}
import { FromSchema } from "json-schema-to-ts";
export interface IGetSchema {
Querystring: IGetQuery;
Querystring: IGetQuerySchema;
}
export const indexSchema = {
@ -38,7 +29,8 @@ export const getQuerySchema = {
enum: [...engineList, ""],
},
},
};
} as const;
export type IGetQuerySchema = FromSchema<typeof getQuerySchema>;
export const GetSchema: FastifySchema = {
description: "Get page",