add chnanges
This commit is contained in:
@@ -4,7 +4,6 @@ import config from "./config/config";
|
||||
import morgan from "./config/morgan";
|
||||
import path from 'path';
|
||||
import logger from './config/logger';
|
||||
import productRouter from "./routes/productRoutes";
|
||||
import ApiError from './utils/helper/ApiError';
|
||||
import error from './middleware/error';
|
||||
import routes from './routes';
|
||||
|
||||
@@ -72,7 +72,7 @@ function getConfig() {
|
||||
|
||||
},
|
||||
};
|
||||
} catch (error: any) {
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof yup.ValidationError) {
|
||||
console.error("Validation Errors:", error.errors.join(", "));
|
||||
} else {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { NextFunction, Request, Response } from 'express';
|
||||
import { Request, Response } from 'express';
|
||||
import { IProductInteractor } from '../interfaces/IProductInteractor';
|
||||
import ApiResponse from '../utils/helper/ApiResponse';
|
||||
import { AsyncHandler } from '../utils/handler/async.handler';
|
||||
@@ -11,16 +11,16 @@ export class ProductController {
|
||||
}
|
||||
|
||||
@AsyncHandler()
|
||||
async onCreateProduct(req: Request, res: Response, next: NextFunction) {
|
||||
async onCreateProduct(req: Request, res: Response) {
|
||||
const data = await this.interactor.createProduct(req.body);
|
||||
res.status(201).json(new ApiResponse(201, data, 'Successfully created'));
|
||||
}
|
||||
|
||||
@AsyncHandler()
|
||||
async onGetProducts(req: Request, res: Response, next: NextFunction) {
|
||||
async onGetProducts(req: Request, res: Response) {
|
||||
const offset = Number.isInteger(Number(req.query.offset)) ? parseInt(`${req.query.offset}`, 10) : 0;
|
||||
const limit = Number.isInteger(Number(req.query.limit)) ? parseInt(`(${req.query.limit}`, 10) : 10;
|
||||
|
||||
|
||||
const data = await this.interactor.getProducts(limit, offset);
|
||||
|
||||
if (data.length === 0) {
|
||||
@@ -32,7 +32,7 @@ export class ProductController {
|
||||
}
|
||||
|
||||
@AsyncHandler()
|
||||
async onUpdateStock(req: Request, res: Response, next: NextFunction) {
|
||||
async onUpdateStock(req: Request, res: Response) {
|
||||
const data = await this.interactor.updateStock(parseInt(`${req.params.id}`, 10), parseInt(`${req.body.stock}`, 10));
|
||||
res.status(200).json(new ApiResponse(200, data, 'Successfully updated'));
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ export class ProductInteractor implements IProductInteractor {
|
||||
this.repository = respository
|
||||
}
|
||||
|
||||
async createProduct(input: any): Promise<Product> {
|
||||
async createProduct(input: never): Promise<Product> {
|
||||
return await this.repository.create(input)
|
||||
}
|
||||
async updateStock(id: number, stock: number): Promise<Product> {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Product } from "../entities/Product";
|
||||
|
||||
export interface IProductInteractor {
|
||||
createProduct(input: any): Promise<Product>;
|
||||
createProduct(input: never): Promise<Product>;
|
||||
updateStock(id: number, stock: number): Promise<Product>;
|
||||
getProducts(limit: number, offset: number): Promise<Product[]>;
|
||||
}
|
||||
@@ -5,40 +5,42 @@ import logger from '../config/logger';
|
||||
|
||||
class error {
|
||||
static errorConverter(
|
||||
err: any,
|
||||
err: ApiError,
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): void {
|
||||
let error = err;
|
||||
// Define a broader type for error
|
||||
let error: ApiError | Error & { statusCode?: number; errors?: Error[] } = err;
|
||||
|
||||
// Handle Sequelize validation and unique constraint errors
|
||||
const messages = error.errors.map((e: Error) => e.message);
|
||||
error = new ApiError(
|
||||
400,
|
||||
messages.join(', '),
|
||||
messages,
|
||||
true,
|
||||
err.stack
|
||||
);
|
||||
if (error.errors && Array.isArray(error.errors)) {
|
||||
const messages = error.errors.map((e: Error) => e.message);
|
||||
error = new ApiError(
|
||||
400,
|
||||
messages.join(', '),
|
||||
messages,
|
||||
true,
|
||||
err.stack
|
||||
);
|
||||
}
|
||||
|
||||
if (!(error instanceof ApiError)) {
|
||||
// Handle other errors
|
||||
const statusCode =
|
||||
error.statusCode
|
||||
? 400
|
||||
: 500;
|
||||
const statusCode = error.statusCode || 500;
|
||||
const message = error.message || "Something went wrong";
|
||||
error = new ApiError(statusCode, message, error, false, err.stack);
|
||||
error = new ApiError(statusCode, message, [], false, err.stack);
|
||||
}
|
||||
|
||||
next(error);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static errorHandler(
|
||||
err: any,
|
||||
err: ApiError,
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
res: Response
|
||||
): void {
|
||||
let { statusCode, message } = err;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { pick } from '../utils/handler/pick.handler';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import * as Yup from 'yup';
|
||||
import { ObjectSchema, ValidationError } from 'yup';
|
||||
import ApiError from '../utils/helper/ApiError';
|
||||
import { pick } from '../utils/handler/pick.handler';
|
||||
|
||||
/**
|
||||
* Validation middleware for Express routes.
|
||||
@@ -9,7 +9,7 @@ import ApiError from '../utils/helper/ApiError';
|
||||
* @returns Middleware function to validate request properties.
|
||||
*/
|
||||
const validate =
|
||||
(schema: Partial<Record<keyof Request, Yup.ObjectSchema<any>>>) =>
|
||||
(schema: Partial<Record<keyof Request, ObjectSchema<never>>>) =>
|
||||
(req: Request, res: Response, next: NextFunction): void => {
|
||||
// Define valid request keys explicitly
|
||||
const validRequestKeys = ['params', 'query', 'body', 'file', 'files'] as (keyof Request)[];
|
||||
@@ -31,11 +31,12 @@ const validate =
|
||||
// Assign validated values back to the request object
|
||||
validatedValues.forEach((value, index) => {
|
||||
const key = Object.keys(validSchema)[index];
|
||||
(req as any)[key] = value; // Type assertion since req is mutable
|
||||
// Safely assign the validated value to the request object
|
||||
req[key as keyof Request] = value; // Use `Request` here instead of `Request.ResBody`
|
||||
});
|
||||
next();
|
||||
})
|
||||
.catch((err: Yup.ValidationError) => {
|
||||
.catch((err: ValidationError) => {
|
||||
// Collect and format error messages
|
||||
const errorMessage = err.inner.map((detail) => detail.message).join(', ');
|
||||
next(new ApiError(400, errorMessage));
|
||||
|
||||
Reference in New Issue
Block a user