Registry / devops / itty-fetcher

itty-fetcher

JSON →
library1.0.10jsnpmunverified

itty-fetcher is an ultra-compact (~650 bytes) typed wrapper around the native Fetch API, designed to eliminate boilerplate such as manually setting Content-Type headers, JSON encoding/decoding, and error handling. Version 1.0.10 is the latest stable release, maintained by the author of itty-router. It shifts the final response parsing, method/body handling, and base URL into a small, chainable, proxy-based interface. Differentiators include TypeScript generics for request/response types, composable response transforms via after hooks, and a runtime-minifiable snippet that fits in a tweet. The library is ESM-only and ships with TypeScript definitions.

npm install itty-fetcher
INSTALL
IMPORT
SIG · ITTY-FETCHER
I
itty-fetcher
devopsjavascriptv1.0.10
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fetcher
import { fetcher } from 'itty-fetcher'
import fetcher from 'itty-fetcher'
ESM-only; named export. Cannot be imported as default or via require().
Fetcher
import type { Fetcher } from 'itty-fetcher'
import { fetcher as Fetcher } from 'itty-fetcher'
Type import for TypeScript; Fetcher is the type of the returned proxy object.
fetcher (snippet copy)
Copy the minified snippet from README directly
Attempting to import { fetcher } from 'itty-fetcher' in a non-module script
The inline snippet is for environments without module support (e.g., plain <script> tags).

Creates a reusable fetcher with base URL, headers, and response hook, then demonstrates typed GET and POST calls.

import { fetcher } from 'itty-fetcher' const api = fetcher('https://api.example.com', { headers: { 'Authorization': `Bearer ${process.env.API_KEY ?? ''}` }, after: [ (response) => { console.log('Request to', response.url, 'status:', response.status) return response } ] }) // GET request with typed response interface User { id: number name: string } async function fetchUser() { const user = await api.get<User>('/users/1') console.log(user.name) return user } // POST request with typed request and response interface CreateUserInput { name: string } async function createUser() { const newUser = await api.post<CreateUserInput, User>('/users', { name: 'Alice' }) console.log('Created user:', newUser.id) return newUser }
Debug
Known issues
gotchaThe fetcher() function with no arguments returns a new proxy. Subsequent calls to .get() etc. on the same proxy always use the same options provided at creation.
fix
If you need different base URLs or headers, create separate fetcher instances.
affects: >=0.0.0
gotchaThe after hooks run on every response, including error responses. If you throw inside an after hook, it will override the original error.
fix
Only use after hooks for logging or transformation; avoid throwing in them.
affects: >=0.0.0
gotchaBy default, the library automatically parses the response as JSON. If the response is not JSON, it will throw a parse error.
fix
Set parse option to 'text' or 'blob' for non-JSON responses: api.get('/data.txt', { parse: 'text' })
affects: >=0.0.0
Errors
Common errors & fixes
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Response is not JSON but default parse mode is JSON (parse: 'json').
fix
Specify the correct parse type: await api.get('/text', { parse: 'text' })
TypeError: Cannot use 'in' operator to search for 'headers' in undefined
Using fetcher() with no arguments but without default parameter handling.
fix
Ensure the snippet is correctly copied (the minified snippet handles this). If using import, use fetcher({}) or fetcher(base, options).
Upgrade
Version history
1.0.10latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
itty-fetcher — npm install itty-fetcher · libregistry