From 832eb0811cdf3cf1ae28be31664910129c23d707 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Fri, 25 Aug 2023 11:42:18 +0300 Subject: [PATCH 1/2] feat: install jsdom --- package-lock.json | 38 ++++++++++++++++++++++++++++++++++++-- package.json | 3 ++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8ce5347..0ea6847 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,8 @@ "ejs": "^3.1.9", "fastify": "^4.21.0", "ip-range-check": "^0.2.0", - "jsdom": "^22.1.0" + "jsdom": "^22.1.0", + "json-schema-to-ts": "^2.9.2" }, "devDependencies": { "@types/ejs": "^3.1.2", @@ -43,6 +44,17 @@ "node": ">=0.10.0" } }, + "node_modules/@babel/runtime": { + "version": "7.22.11", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.22.11.tgz", + "integrity": "sha512-ee7jVNlWN09+KftVOu9n7S8gQzD/Z6hN/I8VBRXW4P1+Xe7kJGXMwu8vds4aGIMHZnNbdpSWCfZZtinytpcAvA==", + "dependencies": { + "regenerator-runtime": "^0.14.0" + }, + "engines": { + "node": ">=6.9.0" + } + }, "node_modules/@eslint-community/eslint-utils": { "version": "4.4.0", "dev": true, @@ -401,7 +413,6 @@ }, "node_modules/@types/json-schema": { "version": "7.0.12", - "dev": true, "license": "MIT" }, "node_modules/@types/mime": { @@ -2142,6 +2153,19 @@ "url": "https://github.com/Eomm/json-schema-resolver?sponsor=1" } }, + "node_modules/json-schema-to-ts": { + "version": "2.9.2", + "resolved": "https://registry.npmjs.org/json-schema-to-ts/-/json-schema-to-ts-2.9.2.tgz", + "integrity": "sha512-h9WqLkTVpBbiaPb5OmeUpz/FBLS/kvIJw4oRCPiEisIu2WjMh+aai0QIY2LoOhRFx5r92taGLcerIrzxKBAP6g==", + "dependencies": { + "@babel/runtime": "^7.18.3", + "@types/json-schema": "^7.0.9", + "ts-algebra": "^1.2.0" + }, + "engines": { + "node": ">=16" + } + }, "node_modules/json-schema-traverse": { "version": "0.4.1", "dev": true, @@ -2647,6 +2671,11 @@ "node": ">= 12.13.0" } }, + "node_modules/regenerator-runtime": { + "version": "0.14.0", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.14.0.tgz", + "integrity": "sha512-srw17NI0TUWHuGa5CFGGmhfNIeja30WMBfbslPNhf6JrqQlLN5gcrvig1oqPxiVaXb0oW0XRKtH6Nngs5lKCIA==" + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -3084,6 +3113,11 @@ "node": ">=14" } }, + "node_modules/ts-algebra": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/ts-algebra/-/ts-algebra-1.2.0.tgz", + "integrity": "sha512-kMuJJd8B2N/swCvIvn1hIFcIOrLGbWl9m/J6O3kHx9VRaevh00nvgjPiEGaRee7DRaAczMYR2uwWvXU22VFltw==" + }, "node_modules/ts-api-utils": { "version": "1.0.1", "dev": true, diff --git a/package.json b/package.json index 87583ee..5f4ba7d 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "ejs": "^3.1.9", "fastify": "^4.21.0", "ip-range-check": "^0.2.0", - "jsdom": "^22.1.0" + "jsdom": "^22.1.0", + "json-schema-to-ts": "^2.9.2" }, "devDependencies": { "@types/ejs": "^3.1.2", From 8b8844f9fb6df618e3a2895a57c5ea0183eca098 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Fri, 25 Aug 2023 12:22:15 +0300 Subject: [PATCH 2/2] feat: convert json-schema to typescript No more code duplication --- src/errors/validation.ts | 1 - src/routes/raw-html.ts | 3 +-- src/types/requests/api.ts | 16 ++++++---------- src/types/requests/browser.ts | 18 +++++------------- 4 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/errors/validation.ts b/src/errors/validation.ts index 5304da8..3283524 100644 --- a/src/errors/validation.ts +++ b/src/errors/validation.ts @@ -1,7 +1,6 @@ export interface IFastifyValidationError { statusCode?: number; code?: string; - validation?: any; } export function getFastifyError(error: Error) { diff --git a/src/routes/raw-html.ts b/src/routes/raw-html.ts index 4665fe2..32ccfe5 100644 --- a/src/routes/raw-html.ts +++ b/src/routes/raw-html.ts @@ -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( "/api/raw-html", { schema: rawHtmlSchema }, - async (request: GetRequest) => { + async (request) => { return ( await handlePage( request.query.url, diff --git a/src/types/requests/api.ts b/src/types/requests/api.ts index 9e13803..0472505 100644 --- a/src/types/requests/api.ts +++ b/src/types/requests/api.ts @@ -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 { 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; +} + export const rawHtmlSchema: FastifySchema = { description: "Parse the page and get raw HTML from the engine", querystring: parseQuerySchema, diff --git a/src/types/requests/browser.ts b/src/types/requests/browser.ts index df526d9..e9891ed 100644 --- a/src/types/requests/browser.ts +++ b/src/types/requests/browser.ts @@ -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; export const GetSchema: FastifySchema = { description: "Get page",