According to Infoq, the JavaScript HTTP client Ky has undergone a significant overhaul with its 2.0 release. Developed by Sindre Sorhus alongside Seth Holladay and Szymon Marczak, the library aims to provide a more elegant and consistent experience for developers who require advanced features beyond the native Fetch API.
Unified hooks and improved timeout logic
One of the most substantial architectural changes in Ky 2.0 is the unification of its hook system. Previously, various hooks received different sets of parameters; now, the beforeRequest, afterResponse, beforeRetry, and beforeError hooks all receive a single state object containing the request, options, and retry count. This change ensures API consistency and simplifies future extensibility for the maintainers.
The update also introduces more granular control over request lifecycles through new timeout mechanisms:
- A new
totalTimeoutoption caps the entire operation across all retries and delays. - The existing per-attempt
timeoutremains available for individual requests. - A dedicated
NetworkErrorclass now distinguishes genuine network failures, such as DNS errors, from application-level bugs.
URL handling and Schema validation
To resolve long-standing confusion regarding base URLs, the library has split its configuration into two distinct options: prefix (which allows leading slashes) and baseUrl (which follows standard URL resolution). Furthermore, Ky 2.0 integrates Standard Schema validation directly into the .json() method. This allows developers to validate and type responses using popular libraries like Zod or Valibot with built-in type inference.
When discussing the library's positioning against competitors, maintainer Seth Holladay noted on Hacker News that Ky was designed for Fetch from its inception, whereas other libraries often struggle to adapt legacy patterns to modern standards. "Ky was written to use fetch from the beginning, whereas Axios tries to adapt itself to fetch, which doesn't always work well," Holladay stated. The release also includes a comprehensive migration guide to help users transition from older versions or alternative clients.
By maintaining a small footprint of just a few kilobytes, Ky 2.0 remains compatible across browsers, Node.js, Bun, and Deno, offering a streamlined path for modern web development.