Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/modules/creator/creator.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { NextFunction, Request, Response } from 'express';
import { z, ZodError } from 'zod';
import { sendValidationError } from '../../utils/api-response.utils';

export const CreatorIdParamsSchema = z.object({
id: z.string().trim().min(1, 'Creator ID is required'),
});

export type CreatorIdParams = z.infer<typeof CreatorIdParamsSchema>;

export const validateCreatorIdParam = (
req: Request,
res: Response,
next: NextFunction
): void => {
try {
const validatedParams = CreatorIdParamsSchema.parse(req.params);
req.params = {
...req.params,
...validatedParams,
};
next();
} catch (error) {
if (error instanceof ZodError) {
const details = error.errors.map(err => ({
field: err.path.join('.'),
message: err.message,
}));
return sendValidationError(
res,
'Invalid creator route parameters',
details
);
}

next(error);
}
};
7 changes: 7 additions & 0 deletions src/modules/creator/creator.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,11 @@ router.all('/:creatorId/profile', (_req, res) => {
res.set('Allow', 'GET, PUT').sendStatus(405);
});

/**
* @route GET /api/v1/creators/:id/stats
* @desc Get stats for a creator by id
* @access Public
*/
router.get('/:id/stats', validateCreatorIdParam, getCreatorStats);

export default router;
7 changes: 0 additions & 7 deletions src/modules/creators/creators.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,8 @@
*/
export const httpGetCreatorStats: AsyncController = async (req, res, next) => {
try {
const { id } = req.params;

Check failure on line 104 in src/modules/creators/creators.controllers.ts

View workflow job for this annotation

GitHub Actions / verify

'id' is assigned a value but never used. Allowed unused vars must match /^_/u

// Validate creator ID format (basic validation)
if (!id || typeof id !== 'string') {
return sendValidationError(res, 'Invalid creator ID', [
{ field: 'id', message: 'Creator ID must be a valid string' },
]);
}

// TODO: Fetch actual creator metrics from database/service
// For now, return placeholder data
const placeholderMetrics = {
Expand Down
52 changes: 52 additions & 0 deletions src/modules/creators/creators.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { NextFunction, Request, Response } from 'express';
import { z, ZodError } from 'zod';
import { sendValidationError } from '../../utils/api-response.utils';

/**
* Zod schema for creator route parameters.
* Validates that 'id' is a non-empty string.
*/
export const CreatorIdParamsSchema = z.object({
id: z.string().trim().min(1, 'Creator ID is required'),
});

export type CreatorIdParams = z.infer<typeof CreatorIdParamsSchema>;

/**
* Middleware to validate creator route parameters (e.g., :id).
*
* Uses Zod to parse and validate 'req.params'.
* If validation fails, returns a 400 Bad Request with details.
* If successful, attaches validated params back to 'req.params' and proceeds.
*
* @example
* router.get('/:id/stats', validateCreatorIdParam, httpGetCreatorStats);
*/
export const validateCreatorIdParam = (
req: Request,
res: Response,
next: NextFunction
): void => {
try {
const validatedParams = CreatorIdParamsSchema.parse(req.params);
req.params = {
...req.params,
...validatedParams,
};
next();
} catch (error) {
if (error instanceof ZodError) {
const details = error.errors.map(err => ({
field: err.path.join('.'),
message: err.message,
}));
return sendValidationError(
res,
'Invalid creator route parameters',
details
);
}

next(error);
}
};
2 changes: 1 addition & 1 deletion src/modules/creators/creators.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ creatorsRouter.all('/:id/stats', (_req, res) => {
res.set('Allow', 'GET').sendStatus(405);
});

export default creatorsRouter;
export default creatorsRouter;
Loading