Merge pull request #38 from TxtDot/new-validation

New validation
This commit is contained in:
Andrey 2023-08-25 13:32:22 +04:00 committed by GitHub
commit 9fd3fdfa7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 29 deletions

38
package-lock.json generated
View File

@ -19,7 +19,8 @@
"ejs": "^3.1.9", "ejs": "^3.1.9",
"fastify": "^4.21.0", "fastify": "^4.21.0",
"ip-range-check": "^0.2.0", "ip-range-check": "^0.2.0",
"jsdom": "^22.1.0" "jsdom": "^22.1.0",
"json-schema-to-ts": "^2.9.2"
}, },
"devDependencies": { "devDependencies": {
"@types/ejs": "^3.1.2", "@types/ejs": "^3.1.2",
@ -43,6 +44,17 @@
"node": ">=0.10.0" "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": { "node_modules/@eslint-community/eslint-utils": {
"version": "4.4.0", "version": "4.4.0",
"dev": true, "dev": true,
@ -401,7 +413,6 @@
}, },
"node_modules/@types/json-schema": { "node_modules/@types/json-schema": {
"version": "7.0.12", "version": "7.0.12",
"dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/mime": { "node_modules/@types/mime": {
@ -2142,6 +2153,19 @@
"url": "https://github.com/Eomm/json-schema-resolver?sponsor=1" "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": { "node_modules/json-schema-traverse": {
"version": "0.4.1", "version": "0.4.1",
"dev": true, "dev": true,
@ -2647,6 +2671,11 @@
"node": ">= 12.13.0" "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": { "node_modules/require-directory": {
"version": "2.1.1", "version": "2.1.1",
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
@ -3084,6 +3113,11 @@
"node": ">=14" "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": { "node_modules/ts-api-utils": {
"version": "1.0.1", "version": "1.0.1",
"dev": true, "dev": true,

View File

@ -15,7 +15,8 @@
"ejs": "^3.1.9", "ejs": "^3.1.9",
"fastify": "^4.21.0", "fastify": "^4.21.0",
"ip-range-check": "^0.2.0", "ip-range-check": "^0.2.0",
"jsdom": "^22.1.0" "jsdom": "^22.1.0",
"json-schema-to-ts": "^2.9.2"
}, },
"devDependencies": { "devDependencies": {
"@types/ejs": "^3.1.2", "@types/ejs": "^3.1.2",

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",