Utilities
Fetch
Make requests with timeouts, cancellation, and consistent HTTP errors.
Fetch
runtimeFetch()
runtimeFetch() uses the runtime's global fetch and normalizes timeouts and cancellation.
const response = await runtimeFetch('https://api.example.com/data', {
method: 'GET',
timeout: 5_000
})
It accepts every standard RequestInit option plus timeout?: number. Without timeout, no additional deadline is applied.
HTTP status codes
Like native Fetch, runtimeFetch() resolves 404 and 500 responses. Use fetchResponse() to turn any non-2xx response into HTTPResponseError:
const response = await fetchResponse('/api/user')
Read JSON or text
const user = await fetchJSON<{ id: number; name: string }>('/api/user')
const body = await fetchText('/health')
The TypeScript generic describes the expected result but does not validate JSON at runtime. Validate untrusted data with a schema library.
Cancellation
const controller = new AbortController()
const request = runtimeFetch('/api/report', { signal: controller.signal })
controller.abort('No longer needed')
await request // throws OperationAbortedError
An already-aborted signal prevents the request from starting.
Errors and availability
- An expired deadline throws
TimeoutError. - External cancellation throws
OperationAbortedError. - Other network failures become
RuntimeOperationError; the native cause is preserved. - A missing global Fetch API throws
UnsupportedCapabilityError.