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 { export interface IFastifyValidationError {
statusCode?: number; statusCode?: number;
code?: string; code?: string;
validation?: any;
} }
export function getFastifyError(error: Error) { export function getFastifyError(error: Error) {

View File

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

View File

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

View File

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