UNPKG

fexios

Version:

Fetch based HTTP client with similar API to axios for browser and Node.js

210 lines (205 loc) 8.81 kB
import { d as FexiosContext, I as IFexiosResponse } from '../shared/fexios.Dv4hmoGv.mjs'; export { o as FexiosResponse, r as createFexiosEventSourceResponse, p as createFexiosResponse, q as createFexiosWebSocketResponse } from '../shared/fexios.Dv4hmoGv.mjs'; import '../shared/fexios.uBjPg0Vb.mjs'; /** * Error codes for Fexios */ declare enum FexiosErrorCodes { BODY_USED = "BODY_USED", NO_BODY_READER = "NO_BODY_READER", TIMEOUT = "TIMEOUT", NETWORK_ERROR = "NETWORK_ERROR", BODY_NOT_ALLOWED = "BODY_NOT_ALLOWED", HOOK_CONTEXT_CHANGED = "HOOK_CONTEXT_CHANGED", ABORTED_BY_HOOK = "ABORTED_BY_HOOK", INVALID_HOOK_CALLBACK = "INVALID_HOOK_CALLBACK", UNEXPECTED_HOOK_RETURN = "UNEXPECTED_HOOK_RETURN", UNSUPPORTED_RESPONSE_TYPE = "UNSUPPORTED_RESPONSE_TYPE", BODY_TRANSFORM_ERROR = "BODY_TRANSFORM_ERROR" } /** * Base Fexios error class */ declare class FexiosError extends Error { readonly code: FexiosErrorCodes | string; readonly context?: FexiosContext; name: string; constructor(code: FexiosErrorCodes | string, message?: string, context?: FexiosContext, options?: ErrorOptions); static is(e: any, code?: FexiosErrorCodes): e is FexiosError; } /** * Fexios response error class for HTTP errors */ declare class FexiosResponseError<T> extends FexiosError { readonly response: IFexiosResponse<T>; name: string; constructor(message: string, response: IFexiosResponse<T>, options?: ErrorOptions); static is(e: any): e is FexiosResponseError<any>; } /** * Check if the error is a FexiosError that not caused by Response error * @deprecated Use FexiosError.is(e) instead */ declare const isFexiosError: (e: any) => boolean; /** * Static utility class for building and merging HTTP Headers * * Notes: * - Conversions are not guaranteed to be reversible. * - Header names are treated case-insensitively by the `Headers` interface. * - Only one-level object is considered when building from a record. */ declare namespace FexiosHeaderBuilder { /** * Build a Headers object from a plain record, a Map/ReadonlyMap, or an existing Headers. * * Rules: * - Only top-level keys are considered. * - Array values: append each element. * - Non-primitive values: use `toString()` even if it becomes "[object Object]". * - `undefined`/`null` values are ignored. * * @example * ```ts * makeHeaders({ 'x-foo': 'a', 'x-bar': ['b','c'], obj: { k: 1 } }) * // => Headers with: x-foo: "a"; x-bar: "b" (append) "c"; obj: "[object Object]" * ``` */ const makeHeaders: (input?: Record<string, unknown> | Headers | Map<string, unknown> | ReadonlyMap<string, unknown>) => Headers; /** * Convert Headers into a plain record of string arrays. * * Notes: * - Returns `Record<string, string[]>` (values are arrays). * - Multiple underlying header values may already be combined by the platform; * we do not attempt to split on commas. * * @example * ```ts * toHeaderRecord(new Headers({ 'x-foo': 'a' })) * // => { 'x-foo': ['a'] } * ``` */ const toHeaderRecord: (input: Headers | Map<string, unknown> | ReadonlyMap<string, unknown>) => Record<string, string[]>; /** * Merge multiple header representations into a new Headers. * * Semantics: * - `undefined` => no change for that key. * - `null` => remove the key. * - Array value => remove the key first, then append each element. * - Other value => set/overwrite the key (single value). * * Sources can be `Headers`, plain objects, or Map/ReadonlyMap; processing order is left-to-right. * The returned `Headers` is a fresh instance; the original is not mutated. * * @example * ```ts * // Starting from existing headers * const base = new Headers({ 'x-a': '1', 'x-b': '2' }) * * // Object patch: overwrite x-b, append two values for x-c * const merged = mergeHeaders(base, { 'x-b': '3', 'x-c': ['v1','v2'] }) * * // Deletion via null * const merged2 = mergeHeaders(merged, { 'x-a': null }) * ``` */ const mergeHeaders: (...incomes: Array<Record<string, unknown> | Headers | Map<string, unknown> | ReadonlyMap<string, unknown> | null | undefined>) => Headers; } /** * Static utility class for building URL search parameters * * @example * { foo: 'bar', baz: ['qux', 'quux'] } // ?foo=bar&baz=qux&baz=quux * @example * { 'foo[]': 'bar', 'baz[]': ['qux', 'quux'] } // ?foo[]=bar&baz[]=qux&baz[]=quux */ declare namespace FexiosQueryBuilder { /** * Transform a plain object into a URL search params string. * * @example * ``` * makeSearchParams({ str: '123' }) // str=123 * makeSearchParams({ num: 123 }) // num=123 * makeSearchParams({ bool: true }) // bool=true * makeSearchParams({ arr: [1, 2, 3] }) // arr=1&arr=2&arr=3 * makeSearchParams({ plainObj: { a: 1, b: 2 } }) // plainObj[a]=1&plainObj[b]=2 * makeSearchParams({ deepObj: { a: { b: { c: 3 } } } }) // deepObj[a][b][c]=3 * makeSearchParams({ obj: <object> }) // obj=<object>.toString() (if object is not a primitive type) * makeSearchParams({ empty: '' }) // empty= * makeSearchParams({ null: null, undefined: undefined }) // (ignored) * ``` */ const makeSearchParams: (params?: Record<string, any> | URLSearchParams) => URLSearchParams; /** * Build query string from a record object with proper array handling * @param query - The query object containing key-value pairs * @returns URL-encoded query string */ const makeQueryString: (query: Record<string, any>) => string; /** * Create a URL object with the given parameters. * * @example * ``` * makeURL('https://example.com?existing=1', { foo: 'bar' }, 'baz') // https://example.com?existing=1&foo=bar#baz */ const makeURL: (url: string | URL, params?: Record<string, any>, hash?: string, /** for SSR compatibility */ base?: string | URL) => URL; /** * Convert URLSearchParams back to a plain object * * @note numbers/booleans or special objects (e.g. Date) are not restored, all values are strings * * @example * ``` * // Basic key-value pairs * toQueryRecord(new URLSearchParams('foo=bar&baz=qux&number=123&bool=true')) * // -> { foo: 'bar', baz: 'qux', number: '123', bool: 'true' } * * // Repeated keys are converted to arrays * toQueryRecord(new URLSearchParams('foo=bar&foo=baz&single=value')) * // -> { foo: ['bar', 'baz'], single: 'value' } * * // Nested objects are reconstructed * toQueryRecord(new URLSearchParams('obj[foo]=bar&obj[baz]=qux&deep[dark][fantasy]=yes')) * // -> { obj: { foo: 'bar', baz: 'qux' }, deep: { dark: { fantasy: 'yes' } } } * * // Key with [] suffix always treated as array * toQueryRecord(new URLSearchParams('arr[]=only-one-value')) * // -> { 'arr[]': ['only-one-value'] } * ``` */ const toQueryRecord: <T = Record<string, unknown>>(input: string | URLSearchParams | FormData | Map<string, any> | ReadonlyMap<string, any>) => T; /** * Convert a string to a URLSearchParams object. * @param s - The string to convert. * @returns The URLSearchParams object. * @example * ``` * fromString('?a=1&b=2') // URLSearchParams { 'a' => '1', 'b' => '2' } * fromString('a=1&b=2') // URLSearchParams { 'a' => '1', 'b' => '2' } * fromString('https://x.com/path?a=1#hash') // URLSearchParams { 'a' => '1' } * ``` */ const fromString: (s: string) => URLSearchParams; /** * Merge multiple query representations into a single plain object. * * @note income `undefined` meaning no change, `null` meaning remove the key. * * @example * ``` * mergeQueries({ a: '1' }, { b: '2' }, { c: '3' }) // { a: '1', b: '2', c: '3' } * mergeQueries({ a: '1', b: '2' }, { b: '3', c: '4' }) // { a: '1', b: '3', c: '4' } * mergeQueries({ a: '1', b: '2' }, null, { c: '3' }) // { a: '1', b: '2', c: '3' } * mergeQueries({ a: '1', b: '2' }, { b: null }, { c: '3' }) // { a: '1', c: '3' } * mergeQueries(new URLSearchParams('a=1&b=2'), { b: '3', c: '4' }) // { a: '1', b: '3', c: '4' } * mergeQueries({ a: '1' }, new URLSearchParams('b=2&c=3')) // { a: '1', b: '2', c: '3' } */ const mergeQueries: <T = any>(...incomes: Array<Record<string, any> | URLSearchParams | FormData | Map<string, any> | ReadonlyMap<string, any> | string | null | undefined>) => T; } export { FexiosError, FexiosErrorCodes, FexiosHeaderBuilder, FexiosQueryBuilder, FexiosResponseError, isFexiosError };