/** * Response Normalizer * * Normalizes diverse API responses into consistent formats. * Handles different API conventions, pagination styles, and error formats. * * @module api/advanced/response-normalizer */ /** * Normalized response structure. */ export interface NormalizedResponse { /** Response data */ data: T; /** Pagination information */ pagination?: NormalizedPagination; /** Response metadata */ meta?: Record; /** Links for HATEOAS */ links?: NormalizedLinks; /** Included/embedded resources */ included?: Record; } /** * Normalized pagination structure. */ export interface NormalizedPagination { /** Current page number (1-indexed) */ currentPage: number; /** Total number of pages */ totalPages: number; /** Items per page */ pageSize: number; /** Total number of items */ totalItems: number; /** Whether there's a next page */ hasNextPage: boolean; /** Whether there's a previous page */ hasPrevPage: boolean; /** Cursor for cursor-based pagination */ cursor?: string; /** Next cursor */ nextCursor?: string; /** Previous cursor */ prevCursor?: string; } /** * Normalized links structure. */ export interface NormalizedLinks { /** Link to self */ self?: string; /** Link to first page */ first?: string; /** Link to last page */ last?: string; /** Link to next page */ next?: string; /** Link to previous page */ prev?: string; /** Additional links */ [key: string]: string | undefined; } /** * Normalized error structure. */ export interface NormalizedError { /** Error code */ code: string; /** Error message */ message: string; /** Field that caused the error */ field?: string; /** Detailed error description */ details?: string; /** HTTP status code */ status?: number; /** Additional error context */ context?: Record; } /** * Response format configuration. */ export interface ResponseFormatConfig { /** Path to data in response */ dataPath?: string | string[]; /** Path to pagination info */ paginationPath?: string; /** Path to meta info */ metaPath?: string; /** Path to links */ linksPath?: string; /** Path to included resources */ includedPath?: string; /** Pagination field mapping */ paginationMapping?: { currentPage?: string; totalPages?: string; pageSize?: string; totalItems?: string; hasNext?: string; hasPrev?: string; cursor?: string; nextCursor?: string; prevCursor?: string; }; /** Error field mapping */ errorMapping?: { code?: string; message?: string; field?: string; details?: string; }; } /** * Predefined API response formats. */ export type ResponseFormat = 'standard' | 'json-api' | 'hal' | 'spring' | 'laravel' | 'django' | 'graphql' | 'custom'; /** * Response Normalizer for consistent API responses. * * @example * ```typescript * const normalizer = new ResponseNormalizer('json-api'); * * // Normalize a JSON:API response * const normalized = normalizer.normalize(response); * console.log(normalized.data); * console.log(normalized.pagination); * * // Normalize errors * const errors = normalizer.normalizeErrors(errorResponse); * ``` */ export declare class ResponseNormalizer { private config; /** * Create a new response normalizer. * * @param format - Response format or custom config */ constructor(format?: ResponseFormat | ResponseFormatConfig); /** * Normalize a response to standard format. * * @param response - Raw API response * @returns Normalized response */ normalize(response: unknown): NormalizedResponse; /** * Normalize error response. * * @param response - Error response * @returns Normalized errors */ normalizeErrors(response: unknown): NormalizedError[]; /** * Update configuration. */ setConfig(config: Partial): void; /** * Get current configuration. */ getConfig(): ResponseFormatConfig; /** * Normalize a single error. */ private normalizeError; /** * Extract data from response. */ private extractData; /** * Extract pagination from response. */ private extractPagination; /** * Extract meta information from response. */ private extractMeta; /** * Extract links from response. */ private extractLinks; /** * Extract included/embedded resources. */ private extractIncluded; /** * Extract additional context from error. */ private extractErrorContext; /** * Get nested value from object using dot notation. */ private getNestedValue; /** * Check if value is a plain object. */ private isObject; /** * Check if value is an error object. */ private isErrorObject; } /** * Create a new response normalizer. * * @param format - Response format or custom config * @returns ResponseNormalizer instance */ export declare function createResponseNormalizer(format?: ResponseFormat | ResponseFormatConfig): ResponseNormalizer; /** * Quick normalize function for one-off use. */ export declare function normalizeResponse(response: unknown, format?: ResponseFormat): NormalizedResponse; /** * Quick normalize errors function. */ export declare function normalizeErrors(response: unknown, format?: ResponseFormat): NormalizedError[];