diff --git a/src/shared/lib/index.ts b/src/shared/lib/index.ts index 65baf20..c4070ab 100644 --- a/src/shared/lib/index.ts +++ b/src/shared/lib/index.ts @@ -1,6 +1,7 @@ export type { ClassValue } from 'clsx'; export { CONTACT_LINKS } from './config/config'; export * from './fonts/fonts'; +export { buildFileUrl } from './utils/buildFileUrl/buildFileUrl'; export { cn } from './utils/cn/cn'; export * from './utils/formatDate/formatDate'; export { groupByKey } from './utils/groupByKey/groupByKey'; diff --git a/src/shared/lib/utils/buildFileUrl/buildFileUrl.test.ts b/src/shared/lib/utils/buildFileUrl/buildFileUrl.test.ts new file mode 100644 index 0000000..8d56a0a --- /dev/null +++ b/src/shared/lib/utils/buildFileUrl/buildFileUrl.test.ts @@ -0,0 +1,33 @@ +import { buildFileUrl } from './buildFileUrl'; + +describe('buildFileUrl', () => { + describe('default base URL', () => { + it('builds correct URL with default base', () => { + expect(buildFileUrl('site_settings', 'ss1', 'cv_2024.pdf')).toBe( + 'http://127.0.0.1:8090/api/files/site_settings/ss1/cv_2024.pdf', + ); + }); + }); + + describe('custom base URL', () => { + it('uses provided baseUrl when given', () => { + expect(buildFileUrl('photos', 'rec1', 'avatar.png', 'https://pb.example.com')).toBe( + 'https://pb.example.com/api/files/photos/rec1/avatar.png', + ); + }); + }); + + describe('different collections, records, filenames', () => { + it('handles projects collection', () => { + expect(buildFileUrl('projects', 'proj42', 'screenshot.jpg', 'http://127.0.0.1:8090')).toBe( + 'http://127.0.0.1:8090/api/files/projects/proj42/screenshot.jpg', + ); + }); + + it('handles contacts collection', () => { + expect(buildFileUrl('contacts', 'cid99', 'photo.webp', 'http://127.0.0.1:8090')).toBe( + 'http://127.0.0.1:8090/api/files/contacts/cid99/photo.webp', + ); + }); + }); +}); diff --git a/src/shared/lib/utils/buildFileUrl/buildFileUrl.ts b/src/shared/lib/utils/buildFileUrl/buildFileUrl.ts new file mode 100644 index 0000000..9ad792a --- /dev/null +++ b/src/shared/lib/utils/buildFileUrl/buildFileUrl.ts @@ -0,0 +1,11 @@ +/** + * Builds a URL for a file stored in a PocketBase record. + */ +export function buildFileUrl( + collectionId: string, + recordId: string, + filename: string, + baseUrl: string = process.env.NEXT_PUBLIC_PB_URL ?? 'http://127.0.0.1:8090', +): string { + return `${baseUrl}/api/files/${collectionId}/${recordId}/${filename}`; +}