Configuration
Configuring Dvori is straightforward yet powerful, allowing you to tailor the HTTP client to meet your specific requirements. This guide covers the essential configuration options available, providing the foundation you need to start making requests.
Basic Configuration
Start by importing and configuring your Dvori client. Here’s a simple example:
Options
baseURL
: Sets a prefix URL for all requests made using this client.headers
: Default headers to be applied to every request.timeout
: Specifies a timeout period for the request in milliseconds.params
: Default query parameters to be included in every request.signal
: AnAbortSignal
object that allows you to abort requests.
Customizing Fetch Options
Dvori passes additional fetch options directly to the underlying Fetch API, allowing you to utilize its full power:
Using Composables
Composables offer a way to encapsulate and reuse custom logic across requests. Define global composables in the client configuration:
Overriding Configuration per Request
You can override the global configuration for individual requests:
To extend the headers or modify them per request, take a look at the composable beforeRequest
lifecycle hook.
URL Resolution
Dvori simplifies working with APIs by resolving URLs intelligently, combining the baseURL
and endpoint paths to eliminate redundancy and reduce the potential for errors.
Base URL and Endpoint Concatenation
When you specify a baseURL
in your client configuration, Dvori automatically prepends it to any relative URL passed to a request method, ensuring your requests are always directed to the correct base address.
Handling Absolute URLs
If an absolute URL is provided in a request, Dvori recognizes it and bypasses the baseURL, using the provided URL as is.
Parameter Serialization
Dvori automatically serializes query parameters, converting an object of key-value pairs into a URL-encoded string. This feature simplifies the inclusion of query parameters in your requests.
Body Serialization
Dvori intelligently handles JSON bodies, automatically serializing JavaScript objects when the Content-Type
header is set to application/json
. This removes the need to manually stringify JSON payloads.
Setting the Correct Headers
When you provide a JavaScript object as the body and set the Content-Type
to application/json
, Dvori not only serializes the body but also ensures the appropriate headers are set, streamlining the process of sending JSON requests.
Special Behavior and Defaults
Dvori’s design philosophy emphasizes ease of use and sensible defaults, differentiating it from the standard Fetch API:
- Automatic URL resolution simplifies working with base paths and endpoints.
- Query parameter serialization removes the boilerplate code associated with preparing URLs.
- Intelligent body serialization with
application/json
content type automates what is typically a manual process.
Configuring Dvori is designed to be both simple for basic use cases and robust enough to handle complex scenarios. By leveraging these configuration options, you can create a highly customizable HTTP client that fits seamlessly into your application’s architecture.
For detailed information on each configuration option, refer to the API reference.