UNPKG

astro-loader-hashnode

Version:

Astro content loader for seamlessly integrating Hashnode blog posts into your Astro website using the Content Layer API

114 lines (113 loc) 3.27 kB
/** * Base Loader Class - Common functionality for all Hashnode loaders */ import type { Loader, LoaderContext } from 'astro/loaders'; import { z } from 'zod'; import { type HashnodeClient } from '../api/client.js'; import type { BaseLoaderOptions } from '../types/loader.js'; /** * Digest calculation for content changes */ export declare function calculateDigest(content: unknown): string; /** * Enhanced error class for loader operations */ export declare class LoaderError extends Error { readonly code: string; readonly details?: Record<string, unknown> | undefined; constructor(message: string, code: string, details?: Record<string, unknown> | undefined); } /** * Result type for loader operations */ export interface LoaderResult<T = unknown> { success: boolean; data?: T; error?: LoaderError; cached?: boolean; } /** * Configuration for base loader */ export interface BaseLoaderConfig extends BaseLoaderOptions { /** * Collection name in Astro */ collection: string; /** * Zod schema for validation */ schema: z.ZodSchema; } /** * Abstract base class for all Hashnode loaders * * Provides common functionality like: * - Client initialization * - Error handling * - Caching with digests * - Data validation * - Astro loader interface */ export declare abstract class BaseHashnodeLoader { protected client: HashnodeClient; protected config: BaseLoaderConfig; constructor(config: BaseLoaderConfig); /** * Abstract method to fetch data from Hashnode API * Must be implemented by specific loaders */ protected abstract fetchData(): Promise<unknown[]>; /** * Abstract method to transform API data to Astro content format * Must be implemented by specific loaders */ protected abstract transformItem(item: unknown): unknown; /** * Generate unique ID for an item * Can be overridden by specific loaders */ protected generateId(item: unknown): string; /** * Validate transformed data against schema */ protected validateData(data: unknown): LoaderResult; /** * Safe data fetching with error handling */ protected safeFetch(): Promise<LoaderResult<unknown[]>>; /** * Process a single item with validation and transformation */ protected processItem(item: unknown): Promise<LoaderResult>; /** * Main load method - implements Astro Loader interface */ load(context: LoaderContext): Promise<void>; /** * Create an Astro Loader from this instance */ createLoader(): Loader; /** * Get client instance (for advanced usage) */ getClient(): HashnodeClient; /** * Clear client cache */ clearCache(): void; } /** * Utility function to create pagination handler */ export declare function paginateResults<T>(fetchPage: (cursor?: string) => Promise<{ items: T[]; pageInfo: { hasNextPage: boolean; endCursor?: string; }; }>, maxItems?: number): AsyncGenerator<T[], void, undefined>; /** * Utility function to flatten paginated results */ export declare function flattenPaginatedResults<T>(paginatedGenerator: AsyncGenerator<T[], void, undefined>): Promise<T[]>;