Ky 2.0 Fetch API Wrapper with Revamped Hooks, Smarter Timeouts, and Built-In Schema Validation

· Source: InfoQ · Field: Technology & Digital — Software Development & Engineering · Depth: Intermediate, quick

Summary

Ky 2.0, a tiny JavaScript HTTP client built on the Fetch API, has been released, marking its first major update in years. This version introduces revamped hooks, unifying "beforeRequest", "afterResponse", "beforeRetry", and "beforeError" into a single state object, and adds a new "init" hook. Timeouts are smarter with a "totalTimeout" option capping entire operations and "NetworkError" for genuine network failures. URL handling is clearer, splitting into "prefix" (renamed from "prefixUrl") and a new "baseUrl" option. Crucially, Ky 2.0 integrates Standard Schema validation for ".json()" responses, supporting validators like Zod and Valibot, and exposes pre-parsed error bodies via "HTTPError.data". Community feedback highlights its fetch-native approach and superior TypeScript support compared to Axios. The release requires Node.js 22 and includes a migration guide for breaking changes.

Key takeaway

For JavaScript developers building web applications or Node.js services that rely on HTTP requests, upgrading to Ky 2.0 offers significant improvements in reliability and maintainability. You should review the migration guide, especially for changes to hook callbacks and error body access, to leverage its unified API, smarter timeouts, and built-in schema validation. This update provides a more robust, fetch-native client experience, potentially reducing boilerplate and improving type safety in your projects.

Key insights

Ky 2.0 enhances Fetch API with unified hooks, smarter timeouts, and built-in schema validation for robust HTTP client operations.

Principles

In practice

Topics

Code references

Best for: Machine Learning Engineer, Software Engineer

Related on AIssential

Open in AIssential →

Editorial summary, takeaway, and curation by AIssential. Original article published by InfoQ.