UNPKG

hacker-news-reel

Version:

A lightweight, typed client for the Hacker News API with validation using Zod

86 lines (85 loc) 2.44 kB
import type { RequestInfo, RequestInit } from './types'; /** * Hook that runs before a fetch request is made * Can modify the request URL and options before sending */ export interface BeforeFetchHook { (url: RequestInfo, options?: RequestInit): { url: RequestInfo; options: RequestInit | undefined; }; } /** * Hook that runs after a successful fetch request * Can inspect or modify the response */ export interface AfterFetchHook { (response: Response): Response | Promise<Response>; } /** * Hook that runs when an error occurs during fetch * Can handle the error or throw a different one */ export interface OnErrorHook { (error: Error): Error | Promise<Error> | undefined; } /** * Complete set of hooks that can be registered with the client */ export interface ClientHooks { /** * Runs before each fetch request * Can modify the request URL and options */ beforeFetch?: BeforeFetchHook; /** * Runs after each successful fetch request * Can inspect or modify the response */ afterFetch?: AfterFetchHook; /** * Runs when an error occurs during fetch * Can handle the error or throw a different one */ onError?: OnErrorHook; } /** * Class to manage multiple hooks * Allows registering and sequentially running multiple hooks */ export declare class HooksManager { private beforeFetchHooks; private afterFetchHooks; private onErrorHooks; /** * Register hooks with the manager * @param hooks Object containing hook functions */ register(hooks: ClientHooks): void; /** * Unregister all hooks */ clear(): void; /** * Run all registered beforeFetch hooks in sequence * @param url The request URL * @param options The request options * @returns Modified URL and options */ runBeforeFetchHooks(url: RequestInfo, options?: RequestInit): Promise<{ url: RequestInfo; options: RequestInit | undefined; }>; /** * Run all registered afterFetch hooks in sequence * @param response The fetch response * @returns Modified response */ runAfterFetchHooks(response: Response): Promise<Response>; /** * Run all registered onError hooks in sequence * @param error The error that occurred * @returns The possibly modified error */ runOnErrorHooks(error: Error): Promise<Error>; }