Kind: Function
Source: src/client/fetch-result-please.ts
Part of: Client
Smartly parses and return the consumable result from a fetch Response.
Throwing a structured error if the response is not ok. (DetailedError)
fetchRP accepts a Fetch API Response and parses it into a value callers can consume. If the response is not ok, it throws a DetailedError so request code can handle failures through a single error path.
Signature
tsasync function fetchRP(fetchRes: Response | Promise<Response>): Promise<any>
Parameters
| Name | Type |
|---|---|
fetchRes | `Response |
Returns: Promise<any>
Diagram
mermaidgraph LR A[fetch request] --> B[Response] B --> C[fetchRP] C --> D{response.ok} D -->|true| E[Parsed result] D -->|false| F[DetailedError]
Usage
tsimport { fetchRP } from './client/fetch-result-please';
async function loadProfile() {
const response = await fetch('/api/profile');
try {
const profile = await fetchRP(response);
return profile;
} catch (error) {
console.error('Profile request failed:', error);
throw error;
}
}
AI Coding Instructions
- Pass the original
ResponsetofetchRPbefore reading its body withjson(),text(), or similar methods. - Await
fetchRP, since parsing the response body is asynchronous. - Handle errors around
fetchRP; non-okresponses throwDetailedError. - Keep request code focused on making the fetch call and let
fetchRPdecide how to parse the response.
Used by
1 reference from 1 file. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.
Imported by (1)
mergePath—src/client/utils.ts:11
Was this page helpful?