UNPKG

@opra/client

Version:

Opra Client package

120 lines (119 loc) 4.9 kB
import { ApiDocument, type URLSearchParamsInit } from '@opra/common'; import type { StrictOmit } from 'ts-gems'; import { kBackend } from './constants.js'; import { HttpBackend } from './http-backend.js'; import { HttpBundleObservable } from './http-bundle-observable.js'; import { HttpRequestObservable } from './http-request-observable.js'; /** * Namespace for {@link OpraClientBase} related types and interfaces. * * @namespace OpraClientBase */ export declare namespace OpraClientBase { /** Configuration options for OpraClientBase */ interface Options { /** The API document associated with this client */ document?: ApiDocument; } /** Generic request options for OPRA client */ type RequestOptions = Partial<StrictOmit<HttpBackend.RequestInit, 'url'>> & { /** URL parameters */ params?: URLSearchParamsInit; }; } /** * Base class for OPRA HTTP clients. * * @class OpraClientBase * @abstract */ export declare abstract class HttpClientBase<TRequestOptions = {}, TResponseExt = {}> { protected [kBackend]: HttpBackend; /** * Creates a new instance of HttpClientBase. * * @param backend The backend instance to use for requests. * @protected */ protected constructor(backend: HttpBackend); /** * Gets the base service URL. */ get serviceUrl(): string; /** * Fetches the API document from the service. * * @param options Fetch options. * @returns A promise that resolves to an ApiDocument. * @throws {@link Error} if there is an issue fetching or parsing the document. */ fetchDocument(options?: { documentId?: string; }): Promise<ApiDocument>; /** * Creates a new {@link HttpRequestObservable} for a specific path. * * @param path The path of the request. * @param options Request options. * @returns A new HttpRequestObservable instance. */ request<TBody = any>(path: string, options?: OpraClientBase.RequestOptions): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>; /** * Sends a DELETE request. * * @param path The path of the request. * @param options Request options. * @returns A new HttpRequestObservable instance. */ delete<TBody = any>(path: string, options?: StrictOmit<OpraClientBase.RequestOptions, 'method' | 'body'>): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>; /** * Sends a GET request. * * @param path The path of the request. * @param options Request options. * @returns A new HttpRequestObservable instance. */ get<TBody = any>(path: string, options?: StrictOmit<OpraClientBase.RequestOptions, 'method' | 'body'>): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>; /** * Sends a PATCH request. * * @param path The path of the request. * @param requestBody The request body. * @param options Request options. * @returns A new HttpRequestObservable instance. */ patch<TBody = any>(path: string, requestBody: any, options?: StrictOmit<OpraClientBase.RequestOptions, 'method' | 'body'>): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>; /** * Sends a POST request. * * @param path The path of the request. * @param requestBody The request body. * @param options Request options. * @returns A new HttpRequestObservable instance. */ post<TBody = any>(path: string, requestBody: any, options?: StrictOmit<OpraClientBase.RequestOptions, 'method' | 'body'>): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>; /** * Sends a PUT request. * * @param path The path of the request. * @param requestBody The request body. * @param options Request options. * @returns A new HttpRequestObservable instance. */ put<TBody = any>(path: string, requestBody: any, options?: StrictOmit<OpraClientBase.RequestOptions, 'method' | 'body'>): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>; /** * Bundles multiple requests into a single multipart HTTP request and * distributes the parsed sub-responses back to callers. * * @param requests The list of requests to bundle. * @returns A {@link HttpBundleObservable} that emits an ordered array of {@link HttpResponse}. */ bundle(requests: HttpRequestObservable<any>[]): HttpBundleObservable; /** * Executes a transaction composed of multiple HTTP requests. * * @param {HttpRequestObservable<any>[]} requests - An array of HTTP request observables that are part of the transaction. * @return {HttpBundleObservable} A new observable that represents the bundled transaction. */ transaction(requests: HttpRequestObservable<any>[]): HttpBundleObservable; }