API Reference

This page provides a detailed reference for all the methods and interfaces in the voca-http library.

Core Methods

voca.create(fetch, config)

create(fetch: typeof globalThis.fetch, config?: VocaConfig): VocaInstance

Creates a configured instance of the HTTP client with custom behavior.

ParameterTypeDescription
fetchtypeof globalThis.fetchThe fetch implementation to use (browser's fetch or a polyfill)
configVocaConfigOptional configuration object with settings for the client

Returns a function that can be used to make HTTP requests with the configured settings.

voca.get(url, headers)

get(url: string, headers?: HeadersInit): Promise<any>

Makes a GET request to the specified URL.

ParameterTypeDescription
urlstringThe URL to request
headersHeadersInitOptional HTTP headers for the request

voca.post(url, data, headers)

post(url: string, data?: any, headers?: HeadersInit): Promise<any>

Makes a POST request to the specified URL with the provided data.

ParameterTypeDescription
urlstringThe URL to request
dataanyOptional data to send in the request body
headersHeadersInitOptional HTTP headers for the request

voca.put(url, data, headers)

put(url: string, data?: any, headers?: HeadersInit): Promise<any>

Makes a PUT request to the specified URL with the provided data.

ParameterTypeDescription
urlstringThe URL to request
dataanyOptional data to send in the request body
headersHeadersInitOptional HTTP headers for the request

voca.patch(url, data, headers)

patch(url: string, data?: any, headers?: HeadersInit): Promise<any>

Makes a PATCH request to the specified URL with the provided data.

ParameterTypeDescription
urlstringThe URL to request
dataanyOptional data to send in the request body
headersHeadersInitOptional HTTP headers for the request

voca.delete(url, data, headers)

delete(url: string, data?: any, headers?: HeadersInit): Promise<any>

Makes a DELETE request to the specified URL.

ParameterTypeDescription
urlstringThe URL to request
dataanyOptional data to send in the request body
headersHeadersInitOptional HTTP headers for the request

File Handling

voca.uploadFile(url, file, headers, onProgress)

uploadFile(url: string, file: File, headers?: HeadersInit, onProgress?: (percent: number) => void): Promise<any>

Uploads a file to the specified URL with optional progress tracking.

ParameterTypeDescription
urlstringThe URL to upload the file to
fileFileThe file object to upload
headersHeadersInitOptional HTTP headers for the request
onProgress(percent: number) => voidOptional callback function to track upload progress

voca.downloadFile(url, headers, onProgress)

downloadFile(url: string, headers?: HeadersInit, onProgress?: (percent: number) => void): Promise<string>

Downloads a file from the specified URL with optional progress tracking.

ParameterTypeDescription
urlstringThe URL to download the file from
headersHeadersInitOptional HTTP headers for the request
onProgress(percent: number) => voidOptional callback function to track download progress

voca.trackProgress(progressEvent, onProgress)

trackProgress(progressEvent: ProgressEvent, onProgress: (percent: number) => void): void

Utility function to track progress from an upload or download event.

ParameterTypeDescription
progressEventProgressEventThe progress event object
onProgress(percent: number) => voidCallback function to receive progress updates

Configuration

voca.config.addRequestInterceptor(interceptor)

addRequestInterceptor(interceptor: RequestInterceptor): void

Adds a request interceptor to modify requests before they are sent.

ParameterTypeDescription
interceptorRequestInterceptorFunction that receives and optionally modifies request options

voca.config.addResponseInterceptor(interceptor)

addResponseInterceptor(interceptor: ResponseInterceptor): void

Adds a response interceptor to modify responses before they are returned.

ParameterTypeDescription
interceptorResponseInterceptorFunction that receives and optionally modifies response objects

voca.config.setTimeout(timeout)

setTimeout(timeout: number): void

Sets the default timeout for requests in milliseconds.

ParameterTypeDescription
timeoutnumberTimeout in milliseconds

voca.config.setRetryCount(count)

setRetryCount(count: number): void

Sets the default number of retry attempts for failed requests.

ParameterTypeDescription
countnumberNumber of retry attempts

Interfaces

VocaConfig

interface VocaConfig { baseUrl?: string; onError?: (reason: any) => Promise<any>; onRequest?: (method: string, url: string, data?: any, headers?: HeadersInit) => RequestOptions; onRequestError?: (reason: any) => Promise<any>; onResponse?: (response: Response) => Promise<any>; onResponseError?: (error: Error) => Promise<any>; interceptors?: { request: RequestInterceptor[]; response: ResponseInterceptor[]; }; retryCount?: number; timeout?: number; }

Configuration options for creating a voca-http instance.

RequestOptions

interface RequestOptions extends RequestInit { url: string; method: HttpMethod; body?: BodyInit | null; headers?: HeadersInit; signal?: AbortSignal; }

Options for an HTTP request, extending the standard RequestInit interface.