HttxClient
The main client class for making HTTP requests.
Constructor
typescript
constructor(config?: Partial<HttxConfig>)Creates a new HttxClient instance with optional configuration.
Parameters
config(optional): Partial configuration objectverbose: boolean | string[] - Enable debug loggingdefaultHeaders: Record<string, string> - Default headers for all requestsbaseUrl: string - Base URL for all requeststimeout: number - Default timeout in milliseconds
Example
typescript
const client = new HttxClient({
verbose: true,
defaultHeaders: {
Authorization: 'Bearer token123'
},
baseUrl: 'https://api.example.com',
timeout: 5000
})Methods
request
typescript
async request<T = unknown>(
url: string,
options: RequestOptions
): Promise<Result<HttxResponse<T>, Error>>Makes an HTTP request and returns a Result type containing either the response or an error.
Parameters
url: string - The URL to requestoptions: RequestOptions - Request configurationmethod: HttpMethod - HTTP method to useheaders?: Record<string, string> - Request headersbody?: BodyInit | Record<string, string> - Request bodyquery?: Record<string, string> - Query parameterstimeout?: number - Request timeout in millisecondsjson?: boolean - Whether to send/receive JSONform?: boolean - Whether to send form datamultipart?: boolean - Whether to send multipart form dataverbose?: boolean - Enable debug logging for this request
Returns
Promise<Result<HttxResponse<T>, Error>>- A Result type containing either:HttxResponse<T>: The successful responseError: The error that occurred
Example
typescript
const result = await client.request<User>('/api/users/1', {
method: 'GET',
json: true
})
if (result.isOk()) {
const user = result.value.data
console.log(user.name)
}
else {
console.error(result.error.message)
}Protected Methods
buildUrl
typescript
protected buildUrl(url: string, query?: Record<string, string>): stringBuilds the final URL by combining the base URL, path, and query parameters.
buildHeaders
typescript
protected buildHeaders(options: RequestOptions): HeadersBuilds the request headers by merging default headers, content-type headers, and request-specific headers.
buildBody
typescript
protected async buildBody(options: RequestOptions): Promise<BodyInit | undefined>Builds the request body based on the content type and body data.
parseResponse
typescript
protected async parseResponse<T>(response: Response): Promise<T>Parses the response based on its content type.
debugLog
typescript
protected debugLog(category: string, message: string, verbose: boolean | string[]): voidLogs debug information if verbose mode is enabled.