👉 Create Instance
Create a Z-fetch instance with custom options or configuration.
Z-Fetch allows you to create custom instances with their own configuration, which is useful for different API endpoints or services in your application.
Signature
Explanation
- You can create an instance with custom options or configuration, these will be applied to all requests made through that instance.
- You can also create multiple instances with different configurations, allowing you to handle different API endpoints or services in your application.
- See Instance Options for a list of available options.
- Each request can also take in z-fetch specific options but also all native fetch options are supported.
- Each request returns a result object containing the response data, error, and the native response object if you need more control over the response. And other stuff too for extra functionality such as refetching etc.
- You can also setup some interceptors to modify requests or responses before they are sent or received using hooks, see using hooks for more details.
Dev Note
Z-Fetch is 100% type safe, so expect some cool auto-completions from your IDE.
Example Of Basic Instance Creation And Usage
Instance Methods
Each instance provides the following methods when created:
get(url, options?)
- Make a GET requestpost(url, options?)
- Make a POST requestput(url, options?)
- Make a PUT requestdelete(url, options?)
- Make a DELETE requestpatch(url, options?)
- Make a PATCH requestoptions(url, options?)
- Make an OPTIONS requesttrace(url, options?)
- Make a TRACE requesthead(url, options?)
- Make a HEAD requestcustom(url, method, options?)
- Make a request with a custom method
Instance Helpers
Each instance also provides helper methods: