| |
| |
| |
| |
| |
|
|
| import type { Request, Response, NextFunction } from 'express'; |
| import { validatePath, PathNotAllowedError } from '@automaker/platform'; |
|
|
| |
| |
| |
| function getParamValue(req: Request, paramName: string): unknown { |
| |
| if (req.body && req.body[paramName] !== undefined) { |
| return req.body[paramName]; |
| } |
| |
| if (req.query && req.query[paramName] !== undefined) { |
| return req.query[paramName]; |
| } |
| return undefined; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function validatePathParams(...paramNames: string[]) { |
| return (req: Request, res: Response, next: NextFunction): void => { |
| try { |
| for (const paramName of paramNames) { |
| |
| if (paramName.endsWith('?')) { |
| const actualName = paramName.slice(0, -1); |
| const value = getParamValue(req, actualName); |
| if (value && typeof value === 'string') { |
| validatePath(value); |
| } |
| continue; |
| } |
|
|
| |
| if (paramName.endsWith('[]')) { |
| const actualName = paramName.slice(0, -2); |
| const values = getParamValue(req, actualName); |
| if (Array.isArray(values) && values.length > 0) { |
| for (const value of values) { |
| if (typeof value === 'string') { |
| validatePath(value); |
| } |
| } |
| } |
| continue; |
| } |
|
|
| |
| const value = getParamValue(req, paramName); |
| if (value && typeof value === 'string') { |
| validatePath(value); |
| } |
| } |
|
|
| next(); |
| } catch (error) { |
| if (error instanceof PathNotAllowedError) { |
| res.status(403).json({ |
| success: false, |
| error: error.message, |
| }); |
| return; |
| } |
|
|
| |
| throw error; |
| } |
| }; |
| } |
|
|