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)
Creates a configured instance of the HTTP client with custom behavior.
Parameter | Type | Description |
---|---|---|
fetch | typeof globalThis.fetch | The fetch implementation to use (browser's fetch or a polyfill) |
config | VocaConfig | Optional 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)
Makes a GET request to the specified URL.
Parameter | Type | Description |
---|---|---|
url | string | The URL to request |
headers | HeadersInit | Optional HTTP headers for the request |
voca.post(url, data, headers)
Makes a POST request to the specified URL with the provided data.
Parameter | Type | Description |
---|---|---|
url | string | The URL to request |
data | any | Optional data to send in the request body |
headers | HeadersInit | Optional HTTP headers for the request |
voca.put(url, data, headers)
Makes a PUT request to the specified URL with the provided data.
Parameter | Type | Description |
---|---|---|
url | string | The URL to request |
data | any | Optional data to send in the request body |
headers | HeadersInit | Optional HTTP headers for the request |
voca.patch(url, data, headers)
Makes a PATCH request to the specified URL with the provided data.
Parameter | Type | Description |
---|---|---|
url | string | The URL to request |
data | any | Optional data to send in the request body |
headers | HeadersInit | Optional HTTP headers for the request |
voca.delete(url, data, headers)
Makes a DELETE request to the specified URL.
Parameter | Type | Description |
---|---|---|
url | string | The URL to request |
data | any | Optional data to send in the request body |
headers | HeadersInit | Optional HTTP headers for the request |
File Handling
voca.uploadFile(url, file, headers, onProgress)
Uploads a file to the specified URL with optional progress tracking.
Parameter | Type | Description |
---|---|---|
url | string | The URL to upload the file to |
file | File | The file object to upload |
headers | HeadersInit | Optional HTTP headers for the request |
onProgress | (percent: number) => void | Optional callback function to track upload progress |
voca.downloadFile(url, headers, onProgress)
Downloads a file from the specified URL with optional progress tracking.
Parameter | Type | Description |
---|---|---|
url | string | The URL to download the file from |
headers | HeadersInit | Optional HTTP headers for the request |
onProgress | (percent: number) => void | Optional callback function to track download progress |
voca.trackProgress(progressEvent, onProgress)
Utility function to track progress from an upload or download event.
Parameter | Type | Description |
---|---|---|
progressEvent | ProgressEvent | The progress event object |
onProgress | (percent: number) => void | Callback function to receive progress updates |
Configuration
voca.config.addRequestInterceptor(interceptor)
Adds a request interceptor to modify requests before they are sent.
Parameter | Type | Description |
---|---|---|
interceptor | RequestInterceptor | Function that receives and optionally modifies request options |
voca.config.addResponseInterceptor(interceptor)
Adds a response interceptor to modify responses before they are returned.
Parameter | Type | Description |
---|---|---|
interceptor | ResponseInterceptor | Function that receives and optionally modifies response objects |
voca.config.setTimeout(timeout)
Sets the default timeout for requests in milliseconds.
Parameter | Type | Description |
---|---|---|
timeout | number | Timeout in milliseconds |
voca.config.setRetryCount(count)
Sets the default number of retry attempts for failed requests.
Parameter | Type | Description |
---|---|---|
count | number | Number of retry attempts |
Interfaces
VocaConfig
Configuration options for creating a voca-http instance.
RequestOptions
Options for an HTTP request, extending the standard RequestInit interface.