Skip to content

fetchRP

reference
1 min readUpdated

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

ts
async function fetchRP(fetchRes: Response | Promise<Response>): Promise<any>

Parameters

NameType
fetchRes`Response

Returns: Promise<any>

Diagram

mermaid
graph LR
  A[fetch request] --> B[Response]
  B --> C[fetchRP]
  C --> D{response.ok}
  D -->|true| E[Parsed result]
  D -->|false| F[DetailedError]

Usage

ts
import { 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 Response to fetchRP before reading its body with json(), text(), or similar methods.
  • Await fetchRP, since parsing the response body is asynchronous.
  • Handle errors around fetchRP; non-ok responses throw DetailedError.
  • Keep request code focused on making the fetch call and let fetchRP decide 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)

  • mergePathsrc/client/utils.ts:11

Was this page helpful?

Download as PDF
fetchRP — Hono (narrator proof)