Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/net/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@


Features:
* `fetch` from [undici](https://github.com/nodejs/undici) with caching enabled via `cacheable`
* Drop-in `fetch` with native semantics (built on the runtime's global `fetch`) — resolves with a `Response` on any status (check `response.ok`, no throwing on `4xx`/`5xx`) and preserves `response.url`, `redirected`, and `type`
* Optional response caching via `cacheable` — pass a cache instance (or use `CacheableNet`) to enable it
* HTTP method helpers: `get`, `post`, `put`, `patch`, `delete`, and `head` for easier development
* [RFC 7234](http://httpwg.org/specs/rfc7234.html) compliant HTTP caching with `http-cache-semantics`
* Smart caching with automatic cache key generation
Expand Down Expand Up @@ -119,6 +120,29 @@ const result2 = await net.post('https://api.example.com/data', { value: 1 }, {
});
```

## Error Handling

`@cacheable/net` follows native `fetch` semantics. It **resolves** with a `Response` for
every completed HTTP exchange — including `4xx` and `5xx` — and only rejects when the request
itself fails (DNS failure, connection refused, abort, etc.). Use `response.ok` (or
`response.status`) to detect HTTP errors instead of a `try/catch`:

```javascript
const net = new CacheableNet();

const { response, data } = await net.get('https://api.example.com/thing');
if (!response.ok) {
// 404, 500, etc. — `data` holds any error body the server returned
throw new Error(`Request failed with status ${response.status}`);
}
```

Only successful responses are cached. Under the default HTTP cache mode, `2xx` responses are
cached per RFC 7234 (honoring `Cache-Control`, `ETag`, `Expires`, etc.); in simple mode
(`httpCachePolicy: false`) every `2xx` response is cached. Error responses (`4xx`/`5xx`) are
always returned to the caller but **never** cached, so a transient failure is never replayed
from a cache hit.

## API Reference

### CacheableNet Class
Expand Down
1 change: 1 addition & 0 deletions packages/net/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"typescript": "^5.9.3"
},
"dependencies": {
"@cacheable/utils": "workspace:^",
"cacheable": "workspace:^",
"hookified": "^1.15.1",
"http-cache-semantics": "^4.2.0",
Expand Down
Loading
Loading