Benefits of using ky over plain fetch
mainKy provides several improvements over the standard Fetch API:
- Simpler API: Reduced boilerplate for common tasks.
- Method shortcuts: Direct access to HTTP methods like
ky.post(),ky.get(), etc. - Automatic Error Handling: Treats non-2xx status codes as errors (after redirects).
- Retries: Automatically retries failed requests.
- JSON support: A dedicated
jsonoption for request bodies and improved.json()response parsing. - Timeout support: Built-in support for request timeouts.
- Progress tracking: Support for upload and download progress.
- Base URL: Option to set a base URL for all requests in an instance.
- Custom Instances: Create instances with custom default options.
- Hooks: Lifecycle hooks for intercepting requests and responses.
- Response Validation: Integration with Standard Schema (e.g., Zod, Valibot).
- TypeScript improvements:
.json()supports generics and defaults tounknowninstead ofany.