Code Read the original on Infoq 2 min read 0

Ky 2.0 launches with revamped hooks and native fetch integration

Sindre Sorhus has released Ky 2.0, the first major update to the lightweight JavaScript HTTP client in several years. The new version introduces unified hook states, smarter timeout management, and built-in response validation using Standard Schema. By prioritizing a native Fetch API architecture over legacy patterns, Ky positions itself as a high-performance alternative for developers seeking more robust functionality than standard fetch without the overhead of larger libraries.

Група людей працює над архітектурними планами на великому робочому столі, обговорюючи деталі проекту поруч із ноутбуком.
Група людей працює над архітектурними планами на великому робочому столі, обговорюючи деталі проекту поруч із ноутбуком. · Image source: Infoq

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 totalTimeout option caps the entire operation across all retries and delays.
  • The existing per-attempt timeout remains available for individual requests.
  • A dedicated NetworkError class 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.

FAQ

What platforms does Ky 2.0 support?
Ky 2.0 maintains a small footprint of just a few kilobytes and remains compatible across browsers, Node.js, Bun, and Deno.
How does Ky 2.0 handle URL configuration?
The library split its configuration into two distinct options to resolve confusion: prefix which allows leading slashes and baseUrl which follows standard URL resolution.
What is the difference between totalTimeout and per-attempt timeout?
The new totalTimeout option caps the entire operation across all retries and delays, while the existing per-attempt timeout remains available for individual requests.
Telegram

Fresh news on our Telegram

Get instant alerts for new posts in «Code»

@procodeandevenmore