import { Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { AxiosInstance, AxiosRequestConfig } from 'axios'; import { AuthenticationModuleOptions } from '../authentication'; import { InternalExternalUrl } from './base-urls'; /** * BaseHttpService * * A structured, type-safe abstraction over Axios that standardizes outbound HTTP requests * to other internal or external microservices. * * ### Key Principles: * - **Controlled Configuration:** Rather than subclassing Axios (which creates prototype issues), * this class wraps Axios to maintain predictable behavior across environments. * - **Environment-Aware Routing:** Selects internal vs. external base URLs automatically based on environment. * - **Secure Communication:** Injects a Kubernetes service token into every outbound request header. * - **Operational Transparency:** Implements structured logging for observability of inter-service communication. */ export declare abstract class BaseHttpService { private readonly serviceName; private readonly baseUrls; private readonly authenticationOptions; private readonly configService; readonly httpClient: AxiosInstance; protected readonly logger: Logger; protected readonly isProduction: boolean; /** * Constructs a base HTTP service instance configured for a downstream microservice. * * @param {string} serviceName - Name of the downstream service (used for contextualized logs). * @param {InternalExternalUrl} baseUrls - Tuple `[internalUrl, externalUrl]`. In production, internal URLs are preferred for security and speed. * @param {AuthenticationModuleOptions} authenticationOptions - Configuration containing Kubernetes token. * @param {ConfigService} configService - NestJS configuration provider. */ constructor(serviceName: string, baseUrls: InternalExternalUrl, authenticationOptions: AuthenticationModuleOptions, configService: ConfigService); /** * Converts an Axios error into a structured and consistent error format. * * This allows downstream callers to rely on predictable error shapes, * regardless of how the target service responds. * * @param {any} error - Raw Axios error object from a failed request. * @returns {{ status: number; data: string | Record; message: string }} */ protected parseError(error: any): { status: number; data: string | Record; message: string; }; /** * Retrieves a single resource from the downstream service. * * Why this abstraction matters: * - Prevents unhandled promise rejections by returning `undefined` on failure. * - Allows optional per-request configuration without mutating the shared Axios instance. * * @template T * @param {string} endpoint - Relative path (e.g., `"/users/42"`). * @param {AxiosRequestConfig} [config={}] - Optional request overrides (headers, params, etc.). * @returns {Promise} - The fetched resource, or `undefined` if the request fails. */ protected fetchResource(endpoint: string, config?: AxiosRequestConfig): Promise; /** * Retrieves a collection of resources from the downstream service. * * Handles cases where large sets of IDs need to be split into chunks * to avoid exceeding query parameter length limits. * * @template T * @param {string} endpoint - Relative API path. * @param {AxiosRequestConfig} [config={}] - Optional request customization. * @returns {Promise} - Array of resources, or an empty array if all attempts fail. */ protected fetchResourceCollection(endpoint: string, config?: AxiosRequestConfig): Promise; /** * Helper for fetching a batch (or full collection) of resources in a single request. * * Encapsulated separately to isolate retry/fallback behavior and keep * the main collection method simpler. * * @template T * @param {string} endpoint - Target API path. * @param {AxiosRequestConfig} [config={}] - Optional request customization. * @returns {Promise} - List of retrieved resources or an empty array if failed. */ private fetchResourceCollectionBatch; }