HttpHelper Utility Specification: Standardized Response Formatting & Header Normalization
Standardized Response Formatting & Header Normalization with HttpHelper
Section titled “Standardized Response Formatting & Header Normalization with HttpHelper”The HttpHelper utility namespace provides standardized builders for API
response envelopes and header normalization utilities. Frozen at initialization
(Object.freeze), it ensures that both successful outcomes and error responses
emitted across presentation layers strictly adhere to Xeno’s unified
ResponseDto<T> structural contracts.
Technical Overview & Design Characteristics
Section titled “Technical Overview & Design Characteristics”Located under shared/utils/http.utils.ts, HttpHelper normalizes
multi-transport HTTP interfaces, bridging the gap between raw web framework
headers and functional monad serialization.
Key Features
Section titled “Key Features”-
Uniform Response Envelopes: Formats data payloads into consistent JSON structures (
ResponseDto<T>), encapsulating status codes, boolean status flags (ok), and tracing headers. -
Header Normalization: Converts mixed string/array header representations into predictable string maps.
-
Automatic Tracing Propagation: Automatically populates
X-Correlation-Id,X-Request-Id, andX-Span-Idheaders on error responses. -
Cache Control Invalidation for Errors: Injects strict no-cache headers (
no-store, no-cache, must-revalidate) on error envelopes to prevent proxy edge caching of failures.
API Reference Specification
Section titled “API Reference Specification”1. Header Normalization
Section titled “1. Header Normalization”HttpHelper.normalizeHeaders(headers: unknown): HttpHeaders
Section titled “HttpHelper.normalizeHeaders(headers: unknown): HttpHeaders”Converts arbitrary or vendor-specific header objects into a normalized
HttpHeaders dictionary (Record<string, string>).
-
Behavior: Skips keys with
undefinedornullvalues. If a header key contains an array of string values (e.g., multi-value headers),normalizeHeadersjoins the elements into a single comma-separated string ('val1,val2'). -
Returns: A clean
HttpHeadersdictionary. Returns{}ifheadersis invalid or undefined. -
Example:
const rawHeaders = { 'accept': ['text/html', 'application/json'], 'x-retry': 3 }const normalized = HttpHelper.normalizeHeaders(rawHeaders)// Output: { 'accept': 'text/html,application/json', 'x-retry': '3' }2. Success Response Formatting
Section titled “2. Success Response Formatting”HttpHelper.success<T>(data: T | IPaginatedResult<T>, status = 200, meta: Dictionary = {}, customHeaders: HttpHeaders = {}): ResponseDto<T>
Section titled “HttpHelper.success<T>(data: T | IPaginatedResult<T>, status = 200, meta: Dictionary = {}, customHeaders: HttpHeaders = {}): ResponseDto<T>”Constructs a standardized success response envelope wrapping the expected payload or paginated dataset.
-
Parameters:
-
data: The primary return payload orIPaginatedResult<T>instance. -
status: HTTP status code (defaults to200OK). -
meta: Optional key-value metadata map (defaults to{}). -
customHeaders: Optional HTTP response headers map. -
Returns: A
ResponseDto<T>object whereok = trueanddatacontains aSuccessResponseDto<T>({ success: true, data, meta }).
3. Error Response Formatting
Section titled “3. Error Response Formatting”HttpHelper.error<T>(dto: ErrorResponseDto, status = 500, customHeaders?: HttpHeaders): ResponseDto<T>
Section titled “HttpHelper.error<T>(dto: ErrorResponseDto, status = 500, customHeaders?: HttpHeaders): ResponseDto<T>”Constructs a standardized error response envelope containing detailed failure diagnostics and tracing metadata.
-
Parameters:
-
dto: AnErrorResponseDtocontaining error details (code,message,details,path) alongside tracing GUIDs (correlationId,requestId,spanId). -
status: HTTP status code (defaults to500Internal Server Error). -
customHeaders: Optional response headers to merge with default error headers. -
Headers Injected:
-
Content-Type:['application/json'] -
X-Correlation-Id:[dto.correlationId] -
X-Request-Id:[dto.requestId] -
X-Span-Id:[dto.spanId] -
Cache-Control:['no-store, no-cache, must-revalidate, proxy-revalidate'] -
Returns: A
ResponseDto<T>object whereok = falseanddatacontains the structuredErrorResponseDto.
Quick Method Summary Table
Section titled “Quick Method Summary Table”| Method | Return Type | Description |
|---|---|---|
normalizeHeaders(headers) |
HttpHeaders |
Normalizes header maps by joining arrays into comma-separated string values. |
success<T>(data, status, meta, headers) |
ResponseDto<T> |
Formats a successful payload envelope with ok: true and 2xx status code. |
error<T>(dto, status, customHeaders) |
ResponseDto<T> |
Formats an error envelope with ok: false, status code, and tracing response headers. |
Practical Implementation Example
Section titled “Practical Implementation Example”The snippet below demonstrates how HttpHelper is used inside custom
controllers or middleware to render responses:
import { HttpHelper, STATUS_CODES, GuidHelper } from '@xeno-js/core'
export class CustomResponseController { public renderUserFound(user: { id: string; name: string }) { // 1. Return a standardized 200 OK success response return HttpHelper.success(user, STATUS_CODES.OK, { executionTimeMs: 12 }) }
public renderNotFound(path: string, correlationId: string) { // 2. Return a standardized 404 Not Found error response with tracing return HttpHelper.error( { success: false, error: { code: 'USER_NOT_FOUND', message: 'The requested user aggregate does not exist.', details: undefined, path, }, correlationId: correlationId as any, requestId: GuidHelper.generate(), spanId: GuidHelper.generate(), timestamp: new Date().toISOString(), }, STATUS_CODES.NOT_FOUND, ) }}Support Us
Section titled “Support Us”Xeno is an MIT-licensed open source project. It can grow thanks to the support of these awesome people. If you’d like to join them, please read more at support section