/** * @module HttpClient * Type-safe HTTP client built on fetch() with automatic JWT handling. * * @example * const api = new HttpClient({ baseUrl: '/api' }); * const response = await api.get('/users'); * const users = response.as(); */ /** * Configuration options for HttpClient. */ export interface HttpClientOptions { /** * Root URL to remote endpoint. Used so that each method only have to specify path in requests. */ baseUrl?: string; /** * Default content type to use if none is specified in the request method. */ contentType?: string; /*** * Checks for a JWT token in localStorage to automatically include it in requests. * * Undefined = use "jwt", null = disable. */ bearerTokenName?: string; } /** * HTTP client wrapper around fetch() with convenience features. * * Features: * - Automatic base URL prefixing * - JWT token handling from localStorage * - Type-safe response casting * - Consistent error handling * * @example * // Create client with base URL * const api = new HttpClient({ baseUrl: '/api/v1' }); * * // GET request * const users = await api.get('/users'); * const data = users.as(); * * // POST request * const result = await api.post('/users', JSON.stringify({ name: 'John' })); */ export declare class HttpClient { private options?; /** * * @param urlPrefix Can be used to specify the URL early (to allow only path to be used in all methods). * @param defaultContentType Content type to use if not specified in the constructor */ constructor(options?: HttpClientOptions); /** * * @param url URL to make the request against. * @param options Request options. * @returns Response from server. */ request(url: string, options?: RequestInit): Promise; /** * GET a resource. * @param url URL to get resource from. * @param queryString Optional query string. * @param options Request options. * @returns HTTP response. */ get(url: string, queryString?: Record, options?: RequestInit): Promise; /** * POST a resource. * @param url URL to post to. * @param data Data to post. * @param options Request options. * @returns HTTP response. */ post(url: string, data: BodyInit, options?: RequestInit): Promise; /** * DELETE a resource. * @param url url to resource. * @param options request options. * @returns response. */ put(url: string, data: BodyInit, options?: RequestInit): Promise; /** * DELETE a resource. * @param url url to resource. * @param options request options. * @returns response. */ delete(url: string, options?: RequestInit): Promise; } /** * Response for request methods in @see HttpClient */ export interface HttpResponse { /** * Http status code. */ statusCode: number; /** * Reason to why the status code was used. */ statusReason: string; /** * this is a 2x response. */ success: boolean; /** * Content type of response body. */ contentType: string | null; /** * Body returned. * * Body has been read and deserialized from json (if the request content type was 'application/json' which is per default is). */ body: unknown; /** * Charset used in body. */ charset: string | null; /** * Cast body to a type. */ as(): T; } /** * Error thrown when a request fails. */ export declare class HttpError extends Error { message: string; response: HttpResponse; constructor(response: HttpResponse); } /** * HTTP request options. */ export interface RequestOptions { method?: 'GET' | 'POST' | 'PUT' | 'DELETE'; mode?: 'cors' | 'no-cors' | '*cors' | 'same-origin'; cache: 'default' | 'no-store' | 'reload' | 'no-cache' | 'force-cache' | 'only-if-cached'; credentials: 'omit' | 'same-origin' | 'include'; headers: Map; redirect: 'follow' | 'manual' | '*follow' | 'error'; referrerPolicy: 'no-referrer' | '*no-referrer-when-downgrade' | 'origin' | 'origin-when-cross-origin' | 'same-origin' | 'strict-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url'; /** * Will be serialized if the content type is json (and the body is an object). */ body: unknown; }