chore: standardize security and quality foundations
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
export interface RecentRequest {
|
||||
id: number;
|
||||
title: string;
|
||||
year?: number;
|
||||
type?: string;
|
||||
statusLabel?: string;
|
||||
artwork?: { poster_url?: string; backdrop_url?: string };
|
||||
createdAt?: string | null;
|
||||
}
|
||||
|
||||
export interface RequestSearchResult {
|
||||
title: string;
|
||||
year?: number;
|
||||
type?: string;
|
||||
requestId?: number;
|
||||
statusLabel?: string;
|
||||
requestedBy?: string | null;
|
||||
accessible?: boolean;
|
||||
}
|
||||
|
||||
const recordValue = (value: unknown): Record<string, unknown> | null =>
|
||||
value !== null && typeof value === "object" ? (value as Record<string, unknown>) : null;
|
||||
|
||||
const optionalString = (value: unknown) => (typeof value === "string" ? value : undefined);
|
||||
const optionalNumber = (value: unknown) => (typeof value === "number" && Number.isFinite(value) ? value : undefined);
|
||||
|
||||
export const normalizeRecentResults = (items: unknown): RecentRequest[] => {
|
||||
if (!Array.isArray(items)) return [];
|
||||
return items.flatMap((value) => {
|
||||
const item = recordValue(value);
|
||||
const id = optionalNumber(item?.id);
|
||||
if (!item || id === undefined) return [];
|
||||
const rawTitle = optionalString(item.title);
|
||||
const placeholder = rawTitle?.trim().toLowerCase() === `request ${id}`;
|
||||
const rawArtwork = recordValue(item.artwork);
|
||||
const artwork = rawArtwork
|
||||
? {
|
||||
poster_url: optionalString(rawArtwork.poster_url),
|
||||
backdrop_url: optionalString(rawArtwork.backdrop_url),
|
||||
}
|
||||
: undefined;
|
||||
return [
|
||||
{
|
||||
id,
|
||||
title: !rawTitle || placeholder ? `Request #${id}` : rawTitle,
|
||||
year: optionalNumber(item.year),
|
||||
type: optionalString(item.type),
|
||||
statusLabel: optionalString(item.statusLabel),
|
||||
artwork,
|
||||
createdAt: item.createdAt === null ? null : optionalString(item.createdAt),
|
||||
},
|
||||
];
|
||||
});
|
||||
};
|
||||
|
||||
export const normalizeSearchResults = (items: unknown): RequestSearchResult[] => {
|
||||
if (!Array.isArray(items)) return [];
|
||||
return items.flatMap((value) => {
|
||||
const item = recordValue(value);
|
||||
const title = optionalString(item?.title);
|
||||
if (!item || !title) return [];
|
||||
return [
|
||||
{
|
||||
title,
|
||||
year: optionalNumber(item.year),
|
||||
type: optionalString(item.type),
|
||||
requestId: optionalNumber(item.requestId),
|
||||
statusLabel: optionalString(item.statusLabel),
|
||||
requestedBy: item.requestedBy === null ? null : optionalString(item.requestedBy),
|
||||
accessible: Boolean(item.accessible),
|
||||
},
|
||||
];
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user