From 3e7a22f9e23d8927a67a6e230ef1088c27068d34 Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 14 Jul 2025 10:28:11 +0200 Subject: [PATCH] feat(document-service): add type-safe file extension handling with FileFormat union - Introduced `FileFormat` union type to restrict valid document extensions. - Added `validExtensions` array for strict extension checking in `Doc.extension` getter. - Ensured `Doc.iconSrc` reflects only recognized file types or defaults to 'unknown'. - Improved type safety and consistency in extension handling. --- .../dd-hub-react/src/api/document-service.ts | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/client/dd-hub-react/src/api/document-service.ts b/src/client/dd-hub-react/src/api/document-service.ts index 624891d..b995f8c 100644 --- a/src/client/dd-hub-react/src/api/document-service.ts +++ b/src/client/dd-hub-react/src/api/document-service.ts @@ -1,5 +1,27 @@ import { _documents } from "src/_mock" +export type FileFormat = + | 'pdf' + | 'docx' + | 'xlsx' + | 'csv' + | 'pptx' + | 'txt' + | 'json' + | 'xml' + | 'html' + | 'jpg' + | 'png' + | 'svg' + | 'zip' + | 'md'; + +const validExtensions: FileFormat[] = [ + 'pdf', 'docx', 'xlsx', 'csv', 'pptx', + 'txt', 'json', 'xml', 'html', 'jpg', + 'png', 'svg', 'zip', 'md' +]; + export class Doc { static map(source?: Partial): Doc { @@ -27,10 +49,13 @@ export class Doc { return [who, when].filter(Boolean).join(separator); } - get extension(): string | undefined { + get extension(): FileFormat | undefined { const parts = this.name.split('.'); if (parts.length > 1 && parts[parts.length - 1].trim() !== '') { - return parts[parts.length - 1].toLowerCase(); + const ext = parts[parts.length - 1].toLowerCase(); + + if (validExtensions.includes(ext as FileFormat)) + return ext as FileFormat; } return undefined; }