export type TGetFromServerMethod = "GET" | "PUT" | "POST" | "DELETE" | "HEAD" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH"; export type TGetFromServerErrorKind = "abort" | "http" | "network" | "parse" | "request" | "timeout" | "transform"; export type TGetFromServerError = Error & { cause: unknown; kind: TGetFromServerErrorKind; method: TGetFromServerMethod; name: "GetFromServerError"; response: Response | null; status: number | null; url: string; }; export type TGetFromServerArgs = { contentType?: "auto" | "application/json" | "application/x-www-form-urlencoded" | "multipart/form-data"; isBubble?: boolean; timeout?: number; method?: TGetFromServerMethod; mode?: RequestMode; signal?: AbortSignal | null; data?: Record | FormData | null; getSuccessResp?: (data: TResp) => TSuccess; getResp?: (resp: Response) => Promise; type?: "text" | "json" | "blob" | "arrayBuffer"; url?: string; headers?: Record; allowedCodes?: number[]; credentials?: RequestCredentials; redirect?: RequestRedirect; cache?: RequestCache; referrerPolicy?: ReferrerPolicy; fetchProps?: Omit; }; export type TGetFromServerReturn = ReturnType; /** * Performs an HTTP request (`fetch`) with handy defaults, content-type handling, * query param building, and optional bubbling of a "getFromServer" event. * * @template T The expected response data type. * @param {Object} [props] Request parameters (all optional). * @param {string} [props.url] The URL to request. Defaults to current window location or './'. * @param {("auto"|"application/json"|"application/x-www-form-urlencoded"|"multipart/form-data")} [props.contentType="auto"] Content type header. If "auto", sets based on data/method. * @param {boolean} [props.isBubble=true] Whether to bubble a "getFromServer" event after success. * @param {number} [props.timeout=15000] Timeout in milliseconds (use Infinity to disable). * @param {("GET"|"PUT"|"POST"|"DELETE"|"HEAD"|"CONNECT"|"OPTIONS"|"TRACE")} [props.method="GET"] HTTP method. * @param {RequestMode} [props.mode="cors"] Fetch mode. * @param {AbortSignal|null} [props.signal=null] AbortSignal for cancellation. * @param {Record|FormData|null} [props.data=null] Request data. For GET-like methods, appended as query params. * @param {function(T): T} [props.getSuccessResp] Transform function for successful response. Defaults to identity function. * @param {function(Response): Promise} [props.getResp] Custom response parser. If provided, overrides `type` after HTTP status validation. * @param {("text"|"json"|"blob"|"arrayBuffer")} [props.type="json"] Response body parsing type (used when `getResp` not provided). * @param {Record} [props.headers={}] Additional headers. * @param {number[]} [props.allowedCodes=[]] Array of HTTP status codes to treat as success even if not 2xx. * @param {RequestCredentials} [props.credentials="same-origin"] Credentials mode. * @param {RequestRedirect} [props.redirect="follow"] Redirect mode. * @param {RequestCache} [props.cache="default"] Cache mode. * @param {ReferrerPolicy} [props.referrerPolicy="no-referrer-when-downgrade"] Referrer policy. * @param {Omit} [props.fetchProps={}] Additional fetch options. * @returns {Promise} Promise with parsed response (type depends on `type` option). * @throws {TypeError} getFromServer: url must be a string * @throws {TypeError} getFromServer: allowedCodes must be an array of integers * @throws {TypeError} getFromServer: data must be a plain object, FormData, or null * @throws {TypeError} getFromServer: timeout must be a non-negative number or Infinity * @throws {TGetFromServerError} Request lifecycle error with a discriminating `kind` * @see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API * @example * const user = await getFromServer<{ userId: number }>({ url: "/api/user?id=1", method: "GET" }); * @example * import type { TGetFromServerError } from "@web3r/flowerkit/net"; * * try { * await getFromServer({ url: "/api/user" }); * } catch (error) { * if (error instanceof Error && error.name === "GetFromServerError") { * const requestError = error as TGetFromServerError; * if (requestError.kind === "http") { * console.error(requestError.status, requestError.response); * } * } * } * @example * // Send typed JSON data and transform the successful API response * const productId = await getFromServer<{ product: { id: string } }, string>({ * url: "/api/products", * method: "POST", * contentType: "application/json", * data: { name: "Flower pot", price: 24 }, * getSuccessResp: ({ product }) => product.id, * }); */ export declare const getFromServer: (props?: TGetFromServerArgs) => Promise;