import type { ListResponse } from './types'; /* * Native fetch wrapper for PocketBase API requests. */ const PB_URL = process.env.NEXT_PUBLIC_PB_URL || (process.env.NODE_ENV === 'production' ? (() => { throw new Error('NEXT_PUBLIC_PB_URL is not set'); })() : 'http://127.0.0.1:8090'); /** * Options for PocketBase collection fetching. */ export type PBFetchOptions = { /** * Sorting criteria (e.g., "-created,order") */ sort?: string; /** * Filter query string */ filter?: string; /** * Fields to expand (e.g., "stack") */ expand?: string; }; /** * Fetch a list of records from a PocketBase collection. */ export async function getCollection(collection: string, options: PBFetchOptions = {}): Promise> { const { sort, filter, expand } = options; 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()}`; /* force-cache deduplicates identical fetches during the static build phase; * it has no runtime effect in `output: 'export'` mode. */ const res = await fetch(url, { cache: 'force-cache' }); if (!res.ok) { throw new Error(`PocketBase ${res.status} ${res.statusText} on collection "${collection}"`); } return res.json(); } /** * Fetch the first record matching an optional filter from a PocketBase collection. */ export async function getFirstRecord(collection: string, options: PBFetchOptions = {}): Promise { const data = await getCollection(collection, options); return data.items[0] ?? null; }