diff --git a/@types/express/index.d.ts b/@types/express/index.d.ts index 0296935..36a8ee3 100644 --- a/@types/express/index.d.ts +++ b/@types/express/index.d.ts @@ -12,6 +12,6 @@ declare module 'express-serve-static-core' { } interface Response { - bridgeResponse: TResponder + backResponse: TResponder } } \ No newline at end of file diff --git a/src/Middlewares/Extern/ContactValidator.ts b/src/Middlewares/Extern/ContactValidator.ts deleted file mode 100644 index bee965a..0000000 --- a/src/Middlewares/Extern/ContactValidator.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { BadRequestError } from "../../Errors/RequestErrors"; -import { body, validationResult } from "express-validator"; -import { Request, Response } from 'express' - -export const contactValidationRules = [ - body("name").exists().withMessage("Missing parameter name"), - body("email").exists().withMessage("Missing parameter email").isEmail().withMessage("Invalid email format"), - body("job").exists().withMessage("Missing parameter job"), - body("message").exists().withMessage("Missing parameter message"), - body("newsletter") - .exists() - .withMessage("Missing parameter newsletter") - .isBoolean() - .withMessage("Invalid newsletter format, expected a boolean") - .toBoolean(), - body("beta") - .exists() - .withMessage("Missing parameter beta") - .isBoolean() - .withMessage("Invalid beta format, expected a boolean") - .toBoolean() -]; - -export function validateContact(req : Request, res : Response, next : Function) { - let contactInfo = null; - - const errors = validationResult(req); - - if (errors.isEmpty()) { - contactInfo = { - name: req.body.name, - email: req.body.email, - job: req.body.job, - message: req.body.message, - newsletter: !(req.body.newsletter === "false" || req.body.newsletter === "0"), - beta: !(req.body.beta === "false" || req.body.beta === "0") - }; - - // Check if email is already registered - // mongo.col("contacts").findOne({ email: contactInfo.email }, (err : Error, res : any) => { - // if (err !== null) { - // return next(new InternalServerError("Failed to check contact info", err)); - // } - // if (res !== null) { - // return next(new ConflictRequestError("Email contact is already registered")); - // } - // }); - - req.body.contactInfo = contactInfo; - - return next(); - } - - const extractedErrors = []; - errors.array().map((err : any) => extractedErrors.push({ [err.param]: err.msg })); - - // TODO: better bad param error message - return next(new BadRequestError(errors.array())); -} \ No newline at end of file diff --git a/src/Middlewares/Extern/StripeValidator.ts b/src/Middlewares/Extern/StripeValidator.ts deleted file mode 100644 index edcdd2f..0000000 --- a/src/Middlewares/Extern/StripeValidator.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { BadRequestError } from "../../Errors/RequestErrors"; -import { body, query, validationResult } from "express-validator"; -import { Request, Response } from 'express' - -export const createStripeSessionRules = [ - body("priceId").exists().withMessage("Missing priceId"), -]; - -export const getStripeSessionRules = [ - query("sessionId").exists().withMessage("Missing sessionId"), -] - -export function validateStripeSession(req : Request, res : Response, next : Function) { - const errors = validationResult(req); - - if (!errors.isEmpty()) { - const extractedErrors = []; - errors.array().map((err : any) => extractedErrors.push({ [err.param]: err.msg })); - - return next(new BadRequestError(errors.array())); - } - req.body.sessionId = req.query.sessionId; - return next(); -} diff --git a/src/Middlewares/Physics/RunValidator.ts b/src/Middlewares/Physics/RunValidator.ts deleted file mode 100644 index 8706967..0000000 --- a/src/Middlewares/Physics/RunValidator.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { BadRequestError } from "../../Errors/RequestErrors"; -import { body, validationResult } from "express-validator"; -import { Request, Response } from 'express' - -export const calculValidationRules = [ - body("rn") - .exists() - .withMessage("Missing parameter rn") - .isString() - .withMessage("Invalid rn format, expected an integer"), - body("activity") - .exists() - .withMessage("Missing parameter activity") - .isFloat() - .withMessage("Invalid activity format, expected a float") - .toFloat(), - body("distance") - .exists() - .withMessage("Missing parameter distance") - .isFloat() - .withMessage("Invalid distance format, expected a float") - .toFloat(), - body("walls") - .optional() - // not usefull as the body is already parsed by JSON.parse(), leading to walls field beeing already of type array - // .customSanitizer(value => { - // try { - // return JSON.parse(value); - // } catch (error) { - // return new Error('Invalid walls format, expected an array'); - // } - // }) - .isArray() - .withMessage("Invalid walls format, expected an array"), - body("walls.*.materialID") - .exists() - .withMessage("One of the walls is missing the materialID value") - .isString() - .withMessage("Expected an integer for materialID value"), - body("walls.*.thickness") - .exists() - .withMessage("One of the walls is missing the thickness value") - .isFloat() - .withMessage("Expected a float for thickness value") - .toFloat(), - body("iso").exists().withMessage("ISO not specified").isString().withMessage("Expected an int for ISO value"), - body("coef") - .exists() - .withMessage("Missing parameter coef") - .isString() - .withMessage("Expected a string for coef value"), - body("multiplyer.activity") - .optional() - .isFloat() - .withMessage("Expected a number for multiplyer.activity value"), - body("multiplyer.distance") - .optional() - .isFloat() - .withMessage("Expected a number for multiplyer.distance value"), - body("multiplyer.thickness") - .optional() - .isArray() - .withMessage("Expected an array for multiplyer.thickness value"), -]; - -export function validateCalcul(req : Request, res : Response, next : Function) { - const errors = validationResult(req); - if (!errors.isEmpty()) { - const extractedErrors = []; - errors.array().map(err => extractedErrors.push({ [err.param]: err.msg })); - - // TODO: better bad param error message - return next(new BadRequestError(errors.array())); - } - return next(); -} \ No newline at end of file diff --git a/src/Middlewares/bridgeResponse.ts b/src/Middlewares/backResponse.ts similarity index 65% rename from src/Middlewares/bridgeResponse.ts rename to src/Middlewares/backResponse.ts index 6ade21d..5a22a42 100644 --- a/src/Middlewares/bridgeResponse.ts +++ b/src/Middlewares/backResponse.ts @@ -1,8 +1,8 @@ import { Response } from 'express'; import { IResponseType } from '../Types/responseTypes' -export default function setResponseFunction ({}, res : Response, next : Function) { - res.bridgeResponse = (response : IResponseType) : void => { +export function backResponse ({}, res : Response, next : Function) { + res.backResponse = (response : IResponseType) : void => { if (response.payload) res.status(response.status).json(response.payload); else diff --git a/src/Types/responseTypes.ts b/src/Types/responseTypes.ts index f1ca3ba..adcfa43 100644 --- a/src/Types/responseTypes.ts +++ b/src/Types/responseTypes.ts @@ -1,9 +1,6 @@ // import { Schema } from 'mongoose'; import { HttpStatusCode } from './httpStatus' -import { IUser } from './user' -import { ICode } from './code' - export type TResponder = (IResponseType: IResponseType) => void export interface IResponseType { @@ -18,41 +15,5 @@ export var ResponseType = { BAD_REQUEST: {status: HttpStatusCode.BAD_REQUEST, message:'bad request', code:'BAD_REQUEST'}, - USER_REGISTERED: {status: HttpStatusCode.CREATED, message:'User registered successfully', code:'USER_REGISTERED'}, - - FORBIDDEN: {status: HttpStatusCode.FORBIDDEN, message:'User not allowed', code:'FORBIDDEN'}, - - USER_ACTIVATED: {status: HttpStatusCode.OK, message:'user activated successfully', code:'USER_ACTIVATED'}, - - CODE_USED: {status: HttpStatusCode.BAD_REQUEST, message:'code already used', code:'CODE_USED'}, - - INTERNAL_SERVER_ERROR: {status: HttpStatusCode.INTERNAL_SERVER_ERROR, message:'Internal server error', code:'INTERNAL_SERVER_ERROR'}, - - BAD_IP: {status: HttpStatusCode.FORBIDDEN, message:'bad ip', code:'BAD_IP'}, - - NO_CONTENT: {status: HttpStatusCode.NO_CONTENT, message:'', code:'NO_CONTENT'}, - - NO_SUB : {status: HttpStatusCode.FORBIDDEN, message: 'User is not subscribed', code: 'NO_SUB'}, - - USER_INFO: (infos : IUser) : IResponseType => {return {status: HttpStatusCode.OK, payload: infos, code:'USER_INFO'}}, - - USER_HISTORY: (calculs : object[]) : IResponseType => {return {status: HttpStatusCode.OK, payload: calculs, code:'USER_HISTORY'}}, - - CODE_CREATED: (code : ICode) : IResponseType => {return {status: HttpStatusCode.OK, payload: code, code:'CODE_CREATED'}}, - - STRIPE_SESSION_CREATED: (session : object) : IResponseType => {return {status: HttpStatusCode.OK, payload: session, code:'STRIPE_SESSION_CREATED'}}, - - STRIPE_SESSION: (session : object) : IResponseType => {return {status: HttpStatusCode.OK, payload: session, code:'STRIPE_SESSION'}}, - - STRIPE_PORTAL_SESSION: (session : object) : IResponseType => {return {status: HttpStatusCode.OK, payload: session, code:'STRIPE_PORTAL_SESSION'}}, - - WALLS_LIST: (walls : object) : IResponseType => {return {status: HttpStatusCode.OK, payload: walls, code:'WALLS_LIST'}}, - - RNS_LIST: (rns : object) : IResponseType => {return {status: HttpStatusCode.OK, payload: rns, code:'RNS_LIST'}}, - - COEFS_LIST: (coefs : object) : IResponseType => {return {status: HttpStatusCode.OK, payload: coefs, code:'COEFS_LIST'}}, - - EXEC_CALCUL: (calcul : object) : IResponseType => {return {status: HttpStatusCode.OK, payload: calcul, code:'EXEC_CALCUL'}}, - BAD_REQUEST_NOT_FOUND: (attemptedRequest : string) : IResponseType => ({status: HttpStatusCode.NOT_FOUND, message:`endpoint not found : ${attemptedRequest}`, code: 'BAD_REQUEST_NOT_FOUND'}), }; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index f0918a6..9dea377 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,7 +18,7 @@ import { connect, connected } from './Helpers/MongoConnection'; const expressApp = express(); import * as bodyParser from 'body-parser'; import logger, { LoggerStream } from './Utils/Logger'; -import BridgeResponse from './Middlewares/bridgeResponse'; +import { backResponse } from './Middlewares/backResponse'; import { ResponseType } from './Types/responseTypes' /* @@ -27,7 +27,7 @@ import { ResponseType } from './Types/responseTypes' expressApp.use(cors()); expressApp.use(bodyParser.urlencoded({ extended: true })); -expressApp.use(BridgeResponse); +expressApp.use(backResponse); expressApp.use(morgan("combined", { stream: new LoggerStream() })); /* * ROUTES CONFIGURATION @@ -35,7 +35,7 @@ expressApp.use(morgan("combined", { stream: new LoggerStream() })); expressApp.use((req : Request, res : Response) => { - res.bridgeResponse(ResponseType.BAD_REQUEST_NOT_FOUND(req.url)); + res.backResponse(ResponseType.BAD_REQUEST_NOT_FOUND(req.url)); }); function mongoConnect() {