import type { AsyncActionReturnType, AsyncFetchOptions } from "../actions/asyncActionTypes"; import { ZeroPromise } from "../ZeroPromise/zero"; export { setSSRApiLookup, setSSRRequest } from "../ssr/ssr-bridge"; export { triggerBrowserDownload } from "./download.js"; export { parseFetchResponse } from "./parseResponse.js"; export { readSseStream } from "./sse.js"; export { FetchHttpError, getFetchWireErrorHandler, handleWireRedirect, isExternalUrl, isWireRedirect, setFetchWireErrorHandler, setFetchWireRedirectHandler, } from "./wireError.js"; export type FetchRequest = Omit & { /** * URL for this request. * * - If it starts with `http:` or `https:` it is used as-is (absolute URL), * ignoring any `host` set on the client/base request. * - Otherwise it is treated as a path and appended to `host` * (e.g. `host = "https://"` + `url = "/users"` → * `"https:///users"`). * - Dynamic segments (`:id`) are substituted from `params`. * * @example * { url: "/users/:id/posts", params: { id: 7, page: 2 } } * // → /users/7/posts?page=2 */ url?: string; /** * Alias for `url`. When `url` is not provided, `path` is used as the URL * template. Useful for configs that use `{ host, path }` instead of * `{ host, url }`. */ path?: string; /** * Origin / base host, e.g. `"https://"`. * Used as a prefix when `url` is a relative path. * Defaults to `""` (no host prefix). */ host?: string; /** * Route + query-string params — a plain object. * Keys matching a `:token` in `url` are used as path params (via buildPath). * All remaining keys are appended as query-string parameters. */ params?: Record; /** Request body — plain objects are JSON-serialised automatically. */ body?: BodyInit | Record | null; }; /** * Convenience type — most call sites just need a URL string; the full * `FetchRequest` object is available for advanced configuration. */ export type FetchInput = string | FetchRequest; export type BeforeInterceptor = (url: string, options: RequestInit) => { url: string; options: RequestInit; } | Promise<{ url: string; options: RequestInit; }>; export type AfterInterceptor = (response: Response) => Response | Promise; export type ErrorInterceptor = (error: Error) => void; /** Build the final URL from a FetchRequest. */ export declare function buildRequestUrl(request: FetchRequest): string; /** Default CSRF cookie set by mates-fullstack `useCSRF()`. */ export declare const MATES_CSRF_COOKIE_NAME = "mates_token_csrf"; /** Header sent automatically by Fetch on mutating requests when the cookie is present. */ export declare const MATES_CSRF_HEADER_NAME = "x-csrf-token"; /** Read the mates CSRF token from `document.cookie` (browser only). */ export declare function readCsrfTokenFromCookie(cookieName?: string): string | undefined; export type FetchBody = BodyInit | Record | null; export type FetchActionConfig = FetchRequest & AsyncFetchOptions; export declare const interceptBefore: (fn: BeforeInterceptor) => () => void; export declare const interceptAfter: (fn: AfterInterceptor) => () => void; export declare const interceptError: (fn: ErrorInterceptor) => () => void; export declare const clearInterceptors: () => void; /** * A self-contained HTTP client. * * - Constructor accepts a base `FetchRequest` (host, default headers, etc.) * that is merged under every call-site request. * - Each instance maintains its own interceptor chain, which runs **after** * the global interceptors. * - All requests are logged to Mates DevTools when devtools is connected. * * @example * const api = new FetchClient({ host: "https://" }); * api.interceptBefore((url, opts) => ({ * url, * options: { ...opts, headers: { ...opts.headers, Authorization: `Bearer ${token}` } }, * })); * * const users = await api.Get({ url: "/users", params: { page: 1 } }); */ export declare class FetchClient { private readonly baseRequest; private readonly instanceBeforeFns; private readonly instanceAfterFns; private readonly instanceErrorFns; private readonly _scopeBeforeFns?; private readonly _scopeAfterFns?; private readonly _scopeErrorFns?; constructor(baseRequest?: FetchRequest, scopeBeforeFns?: Set, scopeAfterFns?: Set, scopeErrorFns?: Set); /** Add a before-interceptor scoped to this instance. Returns an unsubscribe fn. */ interceptBefore(fn: BeforeInterceptor): () => void; /** Add an after-interceptor scoped to this instance. Returns an unsubscribe fn. */ interceptAfter(fn: AfterInterceptor): () => void; /** Add an error-interceptor scoped to this instance. Returns an unsubscribe fn. */ interceptError(fn: ErrorInterceptor): () => void; /** Remove all interceptors scoped to this instance. */ clearInterceptors(): void; private get _beforeFns(); private get _afterFns(); private get _errorFns(); fetch(request: FetchRequest): ZeroPromise; /** Public alias for `fetch` — matches the exported global `Fetch` name. */ Fetch(request: FetchRequest): ZeroPromise; Get(request: FetchRequest): ZeroPromise; Post(request: FetchRequest): ZeroPromise; Put(request: FetchRequest): ZeroPromise; Patch(request: FetchRequest): ZeroPromise; Delete(request: FetchRequest): ZeroPromise; /** * Creates an asyncAction that calls this client's Fetch(). * * The base config (host, path, method, headers, …) is fixed at creation time. * The returned action accepts a plain `params` object whose keys are used to * fill dynamic path segments (`:id`) and/or appended as query-string params — * exactly as `buildRequestUrl` handles them. * * @example * const loadUser = api.fetchAction({ host: "https://", url: "/users/:id" }); * loadUser({ id: 7, expand: "posts" }); * // → GET https:///users/7?expand=posts */ fetchAction(config?: FetchActionConfig): AsyncActionReturnType<(params?: Record) => Promise>; /** * Creates an asyncAction that always issues a GET request via this client. * * @example * const loadUsers = api.getAction({ host: "https://", url: "/users" }); * loadUsers({ page: 2, limit: 20 }); */ getAction(config?: FetchActionConfig): AsyncActionReturnType<(params?: Record) => Promise>; /** * Creates an asyncAction that always issues a POST request via this client. * * @example * const createUser = api.postAction({ host: "https://", url: "/users" }); * createUser({ name: "Alice" }); */ postAction(config?: FetchActionConfig): AsyncActionReturnType<(params?: Record) => Promise>; /** * Creates an asyncAction that always issues a PUT request via this client. * * @example * const replaceUser = api.putAction({ host: "https://", url: "/users/:id" }); * replaceUser({ id: 7, name: "Bob" }); */ putAction(config?: FetchActionConfig): AsyncActionReturnType<(params?: Record) => Promise>; /** * Creates an asyncAction that always issues a PATCH request via this client. * * @example * const patchUser = api.patchAction({ host: "https://", url: "/users/:id" }); * patchUser({ id: 7, status: "inactive" }); */ patchAction(config?: FetchActionConfig): AsyncActionReturnType<(params?: Record) => Promise>; /** * Creates an asyncAction that always issues a DELETE request via this client. * * @example * const removeUser = api.deleteAction({ host: "https://", url: "/users/:id" }); * removeUser({ id: 7 }); */ deleteAction(config?: FetchActionConfig): AsyncActionReturnType<(params?: Record) => Promise>; } export declare const fetchClient: FetchClient; export declare const Fetch: (request: FetchInput) => ZeroPromise; export declare const Get: (request: FetchInput) => ZeroPromise; export declare const Post: (request: FetchInput) => ZeroPromise; export declare const Put: (request: FetchInput) => ZeroPromise; export declare const Patch: (request: FetchInput) => ZeroPromise; export declare const Delete: (request: FetchInput) => ZeroPromise; export declare const fetchAction: (config?: FetchActionConfig) => AsyncActionReturnType<(params?: Record) => Promise>; export declare const getAction: (config?: FetchActionConfig) => AsyncActionReturnType<(params?: Record) => Promise>; export declare const postAction: (config?: FetchActionConfig) => AsyncActionReturnType<(params?: Record) => Promise>; export declare const putAction: (config?: FetchActionConfig) => AsyncActionReturnType<(params?: Record) => Promise>; export declare const patchAction: (config?: FetchActionConfig) => AsyncActionReturnType<(params?: Record) => Promise>; export declare const deleteAction: (config?: FetchActionConfig) => AsyncActionReturnType<(params?: Record) => Promise>; //# sourceMappingURL=Fetch.d.ts.map