UNPKG

astro-loader-pocketbase

Version:

A content loader for Astro that uses the PocketBase API

22 lines (20 loc) 818 B
import type { LiveDataEntry } from "astro"; import type { PocketBaseEntry } from "../types/pocketbase-entry.type"; import type { ExperimentalPocketBaseLiveLoaderOptions } from "../types/pocketbase-loader-options.type"; import { fetchEntry } from "./fetch-entry"; import { parseLiveEntry } from "./parse-live-entry"; /** * Loads and parses a single PocketBase entry for live data, returning the entry or an error. */ export async function liveEntryLoader<TEntry extends PocketBaseEntry>( id: string, options: ExperimentalPocketBaseLiveLoaderOptions, token: string | undefined ): Promise<LiveDataEntry<TEntry> | { error: Error }> { try { const entry = await fetchEntry<TEntry>(id, options, token); return parseLiveEntry(entry, options); } catch (error) { return { error: error as Error }; } }