UNPKG

@fluentity/core

Version:

Fluentity is a fluent, model-oriented, typed HTTP client for TypeScript and framework agnostic.

120 lines (119 loc) 3.91 kB
import { QueryBuilder } from '../QueryBuilder'; import { HttpAdapter, HttpAdapterOptions, HttpRequest, HttpResponse } from './HttpAdapter'; /** * REST adapter for making RESTful API requests. * Extends HttpAdapter to provide REST-specific functionality including URL building, * query parameter handling, and RESTful resource management. * * Features: * - Automatic URL construction from resource paths * - Query parameter building and encoding * - Parent-child relationship URL handling * - Standard RESTful HTTP method support * * @class * @extends {HttpAdapter} * @example * ```typescript * const adapter = new RestAdapter({ * baseUrl: 'https://api.example.com' * }); * * // Use with Fluentity * Fluentity.initialize({ * adapter: adapter * }); * ``` */ export declare class RestAdapter<T extends RestAdapterOptions> extends HttpAdapter<T> { options: T; /** * Constructor for the RestAdapter class. * Initializes the adapter with REST-specific configuration. * * @param options - Partial configuration options to merge with existing options * @example * ```typescript * const adapter = new RestAdapter({ * baseUrl: 'https://api.example.com', * options: { * headers: { * 'Authorization': 'Bearer token' * } * } * }); * ``` */ constructor(options: Partial<T>); /** * Builds an HTTP request from a query builder for REST operations. * Converts the query builder into a REST-specific HTTP request with proper URL construction. * * @param queryBuilder - The query builder containing request details * @returns An HttpRequest configured for REST * @protected */ protected buildRequest(queryBuilder: QueryBuilder<T>): HttpRequest; /** * Builds the final URL for the API request. * Constructs the complete URL including resource path, ID, and query parameters. * * @param queryBuilder - The query builder to build the URL from * @returns The constructed URL with query parameters * @private */ private buildUrl; /** * Recursively unwraps parent query builders to build the complete URL path. * Handles nested relationships by traversing up the parent chain. * * @param queryBuilder - The query builder to unwrap parents from * @param segments - Array to collect URL segments * @returns Array of URL segments representing the complete path * @private */ private unwrapParents; /** * Builds a query string from the query builder. * Converts query parameters, pagination, sorting, and filtering options into URL query string. * * @param queryBuilder - The query builder to build the query string from * @returns The constructed query string * @private */ private toQueryString; /** * Handles the actual HTTP request using the fetch API. * Processes the request and returns a standardized response. * * @param request - The HTTP request to execute * @returns Promise resolving to an HttpResponse * @throws {Error} If the HTTP request fails * @protected */ protected fetchRequestHandler(request: HttpRequest): Promise<HttpResponse>; } /** * Configuration options for the RestAdapter. * Extends HttpAdapterOptions with any additional REST-specific options. * Currently identical to HttpAdapterOptions but can be extended for REST-specific features. * * @interface * @extends {HttpAdapterOptions} * @example * ```typescript * const options: RestAdapterOptions = { * baseUrl: 'https://api.example.com', * options: { * headers: { * 'Authorization': 'Bearer token' * } * } * }; * * const adapter = new RestAdapter(options); * ``` */ export interface RestAdapterOptions extends HttpAdapterOptions { resource?: string; }