- Consolidate 5 duplicate format functions (formatDuration, formatRelativeTime, formatFileSize, formatSubscriberCount) into shared utils/format.ts - Extract parseIdParam() route helper, replacing 22 copy-paste blocks across 9 route files - Remove dead exports: useScanStatus, useChannelContent (non-paginated), getContentItemsByStatus, deleteQueueItem, deletePlaylistsByChannelId - Fix as-any type assertion in system.ts (queueService already typed on FastifyInstance) - Net: -411 lines, 23 files touched
26 lines
588 B
TypeScript
26 lines
588 B
TypeScript
import type { FastifyReply } from 'fastify';
|
|
|
|
/**
|
|
* Parse a numeric ID from route params.
|
|
* Returns the parsed number, or sends a 400 response and returns null.
|
|
*
|
|
* Usage:
|
|
* const id = parseIdParam(request.params.id, reply);
|
|
* if (id === null) return;
|
|
*/
|
|
export function parseIdParam(
|
|
raw: string,
|
|
reply: FastifyReply,
|
|
label = 'ID',
|
|
): number | null {
|
|
const id = parseInt(raw, 10);
|
|
if (isNaN(id)) {
|
|
reply.status(400).send({
|
|
statusCode: 400,
|
|
error: 'Bad Request',
|
|
message: `${label} must be a number`,
|
|
});
|
|
return null;
|
|
}
|
|
return id;
|
|
}
|