Fetch
Small conveniences around the platform fetch — JSON GET and POST, plain text, a timeout wrapper and error handling. It stays a thin layer rather than an HTTP client: no retries, interceptors, base URLs or response caching, because those are application decisions (or a job for ky). fetchWithTimeout is built on AbortController with an 8 second default, so it composes with the abortController module.
Example
import { fetchWithTimeout, getJson, postJson } from '@rtorcato/js-common/fetch'
const user = await getJson<User>('/api/users/1')
const created = await postJson<NewUser, User>('/api/users', { name: 'Ada' })
// Same request, abandoned after 2s instead of the 8s default.
const report = await fetchWithTimeout('/api/report', {}, 2_000)
None of these check response.ok — a 500 with a JSON body resolves. Use fetch directly when
the status code matters.
Import
import { fetchText, fetchWithTimeout, getJson } from '@rtorcato/js-common/fetch'
Exports
| Name | Summary |
|---|---|
fetchText | Fetches a resource and returns the response as text. |
fetchWithTimeout | Fetches a resource with a timeout. |
getJson | Sends a GET request and returns JSON. |
handleApiError | Handles fetch errors and returns a fallback value or throws. |
postJson | Sends a POST request with a JSON body. |
See also
- promises — delay, timeout and settle helpers
- abortController — cancel in-flight work with an
AbortSignal - json — safe parse/stringify and JSON deep clone
- url — parse, validate and edit URLs and query params