feat: add buildFileUrl utility with tests

Moved from ProjectsSection inline function to shared/lib/utils.
Accepts optional baseUrl for testability without env mocking.
This commit is contained in:
Ilia Mashkov
2026-05-18 20:45:06 +03:00
parent ba7395cb32
commit c9631f9905
3 changed files with 45 additions and 0 deletions
+1
View File
@@ -1,6 +1,7 @@
export type { ClassValue } from 'clsx'; export type { ClassValue } from 'clsx';
export { CONTACT_LINKS } from './config/config'; export { CONTACT_LINKS } from './config/config';
export * from './fonts/fonts'; export * from './fonts/fonts';
export { buildFileUrl } from './utils/buildFileUrl/buildFileUrl';
export { cn } from './utils/cn/cn'; export { cn } from './utils/cn/cn';
export * from './utils/formatDate/formatDate'; export * from './utils/formatDate/formatDate';
export { groupByKey } from './utils/groupByKey/groupByKey'; export { groupByKey } from './utils/groupByKey/groupByKey';
@@ -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',
);
});
});
});
@@ -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}`;
}