import { SummitGlobal } from 'summitjs'; /** * summitjs/net — the HTTP client. * * A thin, dependency-free wrapper over `fetch` that fixes fetch's real-world * footguns: it throws a structured error on non-2xx, sends and parses JSON * automatically, serializes params, supports a baseURL, default headers, * per-request timeout, and cancellation. No eval, no codegen, so it stays * CSP-safe like the rest of Summit. */ /** A structured error thrown for non-2xx responses, timeouts, and aborts. */ declare class NetError extends Error { status: number; body: unknown; url: string; method: string; aborted: boolean; constructor(message: string, init: { status: number; body: unknown; url: string; method: string; aborted: boolean; }); } type NetParams = Record; interface NetRequest { method?: string; headers?: Record; params?: NetParams; /** A value: objects/arrays are JSON-encoded; strings are sent as-is. */ body?: unknown; /** Abort this request when the given signal aborts. */ signal?: AbortSignal; /** Abort automatically after this many milliseconds. */ timeout?: number; } interface NetClientOptions { baseURL?: string; /** Static headers, or a function evaluated per request (e.g. for auth tokens). */ headers?: Record | (() => Record); timeout?: number; /** Inspect or replace the request just before it is sent. */ beforeRequest?: (req: NetInit) => void | NetInit; /** Inspect or replace the response before the body is read. */ afterResponse?: (res: Response) => void | Response; /** Called with every NetError (e.g. redirect on 401). Re-throws afterward. */ onError?: (err: NetError) => void; } type NetInit = RequestInit & { url: string; }; interface NetClient { request(path: string, options?: NetRequest): Promise; get(path: string, options?: NetRequest): Promise; post(path: string, body?: unknown, options?: NetRequest): Promise; put(path: string, body?: unknown, options?: NetRequest): Promise; patch(path: string, body?: unknown, options?: NetRequest): Promise; delete(path: string, options?: NetRequest): Promise; /** The client's live defaults; mutate to reconfigure baseURL/headers at runtime. */ options: NetClientOptions; } declare function createClient(defaults?: NetClientOptions): NetClient; /** * summitjs/net — the reactive resource. * * A resource wraps a request in Summit signals: `data`, `error`, `loading`, * and `status` are exposed as auto-unwrapping getters, so reading them inside a * directive (`s-text="user.data.name"`, `s-show="user.loading"`) subscribes the * DOM to exactly that field. This is the key win over a promise wrapper: the * view updates itself. * * It also closes the footguns the stress test found: * - out-of-order responses: a monotonic sequence means the latest wins; * - non-2xx: surfaces through `error` (the client already throws); * - unmount leaks: `onCleanup` aborts the in-flight request; * - unhandled rejections: refetch never rejects — failures land in `error`. */ type ResourceStatus = "idle" | "loading" | "success" | "error"; interface ResourceOptions extends NetRequest { /** Fetch immediately on creation. Default true. */ immediate?: boolean; /** Register a teardown callback (Summit passes the element's cleanup here). */ onCleanup?: (fn: () => void) => void; } interface MutateOptions { /** Persist the optimistic change; on rejection the previous value is restored. */ request?: () => Promise; /** Roll back on a failed request. Default true. */ rollbackOnError?: boolean; } interface Resource { readonly data: T | undefined; readonly error: NetError | undefined; readonly loading: boolean; readonly status: ResourceStatus; /** Re-run the request (optionally overriding params/headers). Never rejects. */ refetch(overrides?: NetRequest): Promise; /** Optimistically set data; with `request`, roll back if the request fails. */ mutate(next: T | ((prev: T | undefined) => T), options?: MutateOptions): void; /** Cancel the in-flight request and return to idle. */ abort(): void; } declare function createResource(client: NetClient, url: string | (() => string), options?: ResourceOptions): Resource; /** The default client shared by the `$fetch` magic and `s-resource` directive. */ declare const http: NetClient; /** A client bound to an element: resources it creates auto-abort on unmount. */ interface BoundClient extends NetClient { resource(url: string | (() => string), options?: ResourceOptions): Resource; } /** Ready-to-use plugin backed by the default client: `Summit.plugin(net)`. */ declare function net(summit: SummitGlobal): void; /** A configured plugin: `Summit.plugin(createNet({ baseURL: "/api" }))`. */ declare function createNet(options: NetClientOptions): (summit: SummitGlobal) => void; export { type BoundClient, type MutateOptions, type NetClient, type NetClientOptions, NetError, type NetInit, type NetParams, type NetRequest, type Resource, type ResourceOptions, type ResourceStatus, createClient, createNet, createResource, http, net };