tubearr/src/server/routes/helpers.ts
jlightner b1e90ea8d6 refactor: consolidate format utils, extract route helpers, remove dead code
- 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
2026-04-03 22:55:43 +00:00

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;
}