Skip to content
Merged
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
26 changes: 26 additions & 0 deletions src/constants/public-endpoint-cache.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Public endpoint cache settings.
*
* Keep this file limited to lightweight, reusable constants (no runtime logic).
*/
export const PUBLIC_ENDPOINT_CACHE_SECONDS = {
short: 300,
medium: 3600,
long: 86400,
} as const;

export const PUBLIC_ENDPOINT_CACHE_PRESETS = {
short: {
maxAge: PUBLIC_ENDPOINT_CACHE_SECONDS.short,
type: 'public' as const,
},
medium: {
maxAge: PUBLIC_ENDPOINT_CACHE_SECONDS.medium,
type: 'public' as const,
},
long: {
maxAge: PUBLIC_ENDPOINT_CACHE_SECONDS.long,
type: 'public' as const,
},
} as const;

7 changes: 4 additions & 3 deletions src/middlewares/cache-control.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// src/middlewares/cache-control.middleware.ts
import { Request, Response, NextFunction } from 'express';
import { PUBLIC_ENDPOINT_CACHE_PRESETS } from '../constants/public-endpoint-cache.constants';

/**
* Cache control options for different types of endpoints.
Expand Down Expand Up @@ -90,17 +91,17 @@ export const CachePresets = {
/**
* Short cache for frequently updated public data (5 minutes)
*/
publicShort: { maxAge: 300, type: 'public' as const },
publicShort: PUBLIC_ENDPOINT_CACHE_PRESETS.short,

/**
* Medium cache for moderately stable public data (1 hour)
*/
publicMedium: { maxAge: 3600, type: 'public' as const },
publicMedium: PUBLIC_ENDPOINT_CACHE_PRESETS.medium,

/**
* Long cache for stable public data (24 hours)
*/
publicLong: { maxAge: 86400, type: 'public' as const },
publicLong: PUBLIC_ENDPOINT_CACHE_PRESETS.long,

/**
* Private cache for user-specific data (5 minutes)
Expand Down
15 changes: 6 additions & 9 deletions src/modules/creator/creator.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// src/modules/creator/creator.controller.ts
import { Request, Response } from 'express';
import { ZodError } from 'zod';
import { z } from 'zod';
import {
sendPaginatedSuccess,
Expand All @@ -11,6 +10,7 @@ import {
import { getPaginatedCreators } from './creator.service';
import { parseCreatorSortOptions } from './creator.utils';
import { safeIntParam } from '../../utils/query.utils';
import { parsePublicQuery } from '../../utils/public-query-parse.utils';
import {
DEFAULT_PAGE,
DEFAULT_PAGE_SIZE,
Expand All @@ -37,7 +37,11 @@ const LegacyCreatorQuerySchema = z.object({

export async function listCreators(req: Request, res: Response) {
try {
const { page, limit, sortBy, sortOrder } = LegacyCreatorQuerySchema.parse(req.query);
const parsed = parsePublicQuery(LegacyCreatorQuerySchema, req.query);
if (!parsed.ok) {
return sendValidationError(res, 'Invalid query parameters', parsed.details);
}
const { page, limit, sortBy, sortOrder } = parsed.data;

const sort = parseCreatorSortOptions(sortBy, sortOrder);

Expand All @@ -55,13 +59,6 @@ export async function listCreators(req: Request, res: Response) {
'Creators retrieved successfully'
);
} catch (error) {
if (error instanceof ZodError) {
const details = error.errors.map(err => ({
field: err.path.join('.'),
message: err.message,
}));
return sendValidationError(res, 'Invalid query parameters', details);
}
console.error('Error listing creators:', error);
return sendError(
res,
Expand Down
15 changes: 6 additions & 9 deletions src/modules/creators/creators.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
sendSuccess,
sendValidationError,
} from '../../utils/api-response.utils';
import { ZodError } from 'zod';
import { parsePublicQuery } from '../../utils/public-query-parse.utils';

/**
* Controller for GET /api/v1/creators
Expand All @@ -21,7 +21,11 @@ import { ZodError } from 'zod';
export const httpListCreators: AsyncController = async (req, res, next) => {
try {
// Validate query parameters
const validatedQuery = CreatorListQuerySchema.parse(req.query);
const parsed = parsePublicQuery(CreatorListQuerySchema, req.query);
if (!parsed.ok) {
return sendValidationError(res, 'Invalid query parameters', parsed.details);
}
const validatedQuery = parsed.data;

// Fetch creators and total count
const [creators, total] = await fetchCreatorList(validatedQuery);
Expand All @@ -39,13 +43,6 @@ export const httpListCreators: AsyncController = async (req, res, next) => {

sendSuccess(res, response);
} catch (error) {
if (error instanceof ZodError) {
const details = error.errors.map(err => ({
field: err.path.join('.'),
message: err.message,
}));
return sendValidationError(res, 'Invalid query parameters', details);
}
next(error);
}
};
Expand Down
8 changes: 3 additions & 5 deletions src/modules/creators/creators.routes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Router } from 'express';
import { httpListCreators } from './creators.controllers';
import {
cacheControl,
CachePresets,
} from '../../middlewares/cache-control.middleware';
import { cacheControl } from '../../middlewares/cache-control.middleware';
import { PUBLIC_ENDPOINT_CACHE_PRESETS } from '../../constants/public-endpoint-cache.constants';

const creatorsRouter = Router();

Expand All @@ -15,7 +13,7 @@ const creatorsRouter = Router();
*/
creatorsRouter.get(
'/',
cacheControl(CachePresets.publicShort),
cacheControl(PUBLIC_ENDPOINT_CACHE_PRESETS.short),
httpListCreators
);

Expand Down
36 changes: 36 additions & 0 deletions src/utils/public-query-parse.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { z, ZodError, ZodTypeAny } from 'zod';

export type PublicQueryValidationDetail = {
field: string;
message: string;
};

export type PublicQueryParseResult<T> =
| { ok: true; data: T }
| { ok: false; details: PublicQueryValidationDetail[] };

/**
* Parse and validate public endpoint query params with a predictable output shape.
*
* This helper is intentionally small and focused:
* - maps `ZodError` into `{ field, message }[]` for API validation responses
* - does not add runtime behavior beyond schema parsing and error shaping
*/
export function parsePublicQuery<S extends ZodTypeAny>(
schema: S,
rawQuery: unknown
): PublicQueryParseResult<z.infer<S>> {
try {
return { ok: true, data: schema.parse(rawQuery) };
} catch (error) {
if (error instanceof ZodError) {
const details: PublicQueryValidationDetail[] = error.errors.map(err => ({
field: err.path.join('.'),
message: err.message,
}));
return { ok: false, details };
}
throw error;
}
}

Loading