chore: format codebase and move SectionAccordion to entities/Section

This commit is contained in:
Ilia Mashkov
2026-04-23 20:52:43 +03:00
parent 8aff27f8ac
commit 1d333fd945
73 changed files with 1201 additions and 1153 deletions
+20 -26
View File
@@ -1,9 +1,9 @@
import type { ListResponse } from './types'
import type { ListResponse } from './types';
/**
* Native fetch wrapper for PocketBase API requests.
*/
const PB_URL = process.env.NEXT_PUBLIC_PB_URL || 'http://127.0.0.1:8090'
const PB_URL = process.env.NEXT_PUBLIC_PB_URL || 'http://127.0.0.1:8090';
/**
* Options for PocketBase collection fetching.
@@ -12,61 +12,55 @@ export type PBFetchOptions = {
/**
* Sorting criteria (e.g., "-created,order")
*/
sort?: string
sort?: string;
/**
* Filter query string
*/
filter?: string
filter?: string;
/**
* Fields to expand (e.g., "stack")
*/
expand?: string
expand?: string;
/**
* Cache revalidation time in seconds
* @default 3600
*/
revalidate?: number
}
revalidate?: number;
};
/**
* Fetch a list of records from a PocketBase collection.
*/
export async function getCollection<T>(
collection: string,
options: PBFetchOptions = {}
): Promise<ListResponse<T>> {
const { sort, filter, expand, revalidate = 3600 } = options
export async function getCollection<T>(collection: string, options: PBFetchOptions = {}): Promise<ListResponse<T>> {
const { sort, filter, expand, revalidate = 3600 } = options;
const params = new URLSearchParams()
if (sort) params.set('sort', sort)
if (filter) params.set('filter', filter)
if (expand) params.set('expand', expand)
const params = new URLSearchParams();
if (sort) params.set('sort', sort);
if (filter) params.set('filter', filter);
if (expand) params.set('expand', expand);
const url = `${PB_URL}/api/collections/${collection}/records?${params.toString()}`
const url = `${PB_URL}/api/collections/${collection}/records?${params.toString()}`;
const res = await fetch(url, {
next: { revalidate },
})
});
if (!res.ok) {
throw new Error(`Failed to fetch collection: ${collection}`)
throw new Error(`Failed to fetch collection: ${collection}`);
}
return res.json()
return res.json();
}
/**
* Fetch a single record from a PocketBase collection by ID or filter.
*/
export async function getFirstRecord<T>(
collection: string,
options: PBFetchOptions = {}
): Promise<T | null> {
export async function getFirstRecord<T>(collection: string, options: PBFetchOptions = {}): Promise<T | null> {
const data = await getCollection<T>(collection, {
...options,
// PocketBase convention for "first" or "singleton" patterns
filter: options.filter,
})
});
return data.items[0] || null
return data.items[0] || null;
}