From 46031a5fd2de5065459a44e141782da350155762 Mon Sep 17 00:00:00 2001 From: AntoXa PRO Date: Wed, 20 Sep 2023 13:31:00 +0300 Subject: [PATCH] etc --- package.json | 12 ++++++------ src/app.ts | 1 + src/core/controllers.ts | 1 + src/core/errors.ts | 10 ++++++---- src/core/handlers.ts | 29 ++++++++++++++++++++--------- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 64210a9..0c48761 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "axp-server", - "version": "1.4.12", + "version": "1.4.13", "description": "My helper library", "author": "AntoXa PRO ", "homepage": "https://antoxahub.ru/antoxa/axp-server", @@ -20,16 +20,16 @@ "prepare": "npm run build" }, "dependencies": { - "axp-ts": "^1.9.6", + "axp-ts": "^1.9.10", "dotenv": "^16.3.1", "express": "^4.18.2", - "mongoose": "^6.11.2" + "mongoose": "^6.12.0" }, "devDependencies": { - "@rollup/plugin-typescript": "^11.1.2", + "@rollup/plugin-typescript": "^11.1.3", "@types/express": "^4.17.17", "prettier": "^2.8.8", - "rollup": "^3.26.2", - "tslib": "^2.6.0" + "rollup": "^3.29.2", + "tslib": "^2.6.2" } } diff --git a/src/app.ts b/src/app.ts index 27d25d2..d2c85da 100644 --- a/src/app.ts +++ b/src/app.ts @@ -8,6 +8,7 @@ import { api404Handler, resultHandler } from './core/handlers' export default async ( modules: (typeof AppModule)[] = [] ): Promise => { + // Express. const app = express() const apiRouter = Router() diff --git a/src/core/controllers.ts b/src/core/controllers.ts index 0e15769..428b820 100644 --- a/src/core/controllers.ts +++ b/src/core/controllers.ts @@ -3,6 +3,7 @@ import { DataResultEntity } from 'axp-ts' /** * Базовый контроллер. + * @deprecated */ export class BaseController { /** diff --git a/src/core/errors.ts b/src/core/errors.ts index 54aff37..53127a0 100644 --- a/src/core/errors.ts +++ b/src/core/errors.ts @@ -1,4 +1,4 @@ -import { TNotificationItem } from 'axp-ts' +import { NotificationItem, TNotificationItem } from 'axp-ts' /** * Тип - Http ошибка. @@ -13,15 +13,17 @@ export type THttpError = { * Http ошибка. */ export class HttpError implements THttpError { - status: number = 500 - message: string = 'Server Error' + status: number + message: string errors: TNotificationItem[] = [] constructor(args?: { text?: string; code?: string; statusCode?: number }) { this.status = args?.statusCode || 500 this.message = this.getStatusMessage(this.status) if (args?.text) { - this.errors.push({ code: args.code || 'error', text: args.text }) + this.errors.push( + new NotificationItem({ code: args.code || 'error', text: args.text }) + ) } } diff --git a/src/core/handlers.ts b/src/core/handlers.ts index 5eafc3d..2d196d9 100644 --- a/src/core/handlers.ts +++ b/src/core/handlers.ts @@ -1,4 +1,6 @@ -import { Request, Response, NextFunction } from 'express' +import type { Request, Response, NextFunction } from 'express' +import type { IFormModel } from 'axp-ts' + import { z } from 'zod' import { DataResultEntity } from 'axp-ts' @@ -9,9 +11,9 @@ import { HttpError } from './errors' */ export const resultHandler = ( result: any, - { }: Request, + {}: Request, res: Response, - { }: NextFunction + {}: NextFunction ) => { const dR = new DataResultEntity() @@ -46,8 +48,8 @@ export const resultHandler = ( * Обработчик 404 ошибки. */ export const api404Handler = ( - { }: Request, - { }: Response, + {}: Request, + {}: Response, next: NextFunction ) => { next( @@ -75,10 +77,6 @@ export const zodMiddle = (schemas: TZodMiddleArgs) => (req: Request, {}: Response, next: NextFunction) => { try { - // req.params._id = '' - // req.body.email = 'test' - // console.log(req.body) - if (schemas.query) req.query = schemas.query.parse(req.query) if (schemas.params) req.params = schemas.params.parse(req.params) if (schemas.body) req.body = schemas.body.parse(req.body) @@ -96,3 +94,16 @@ export const zodMiddle = next(httpError) } } + +export const validFormMiddle = + (model: IFormModel) => + (req: Request, {}: Response, next: NextFunction) => { + if (model.isValid()) { + req.body = model.obj + next() + } else { + const httpError = new HttpError({ statusCode: 400 }) + httpError.errors = model.errors + next(httpError) + } + }