MCPcopy Create free account
hub / github.com/TanStack/query / readJson

Function readJson

examples/lit/ssr/src/api.ts:21–49  ·  view source on GitHub ↗
(response: Response)

Source from the content-addressed store, hash-verified

19}
20
21async function readJson<TResponse>(response: Response): Promise<TResponse> {
22 let payload: TResponse | { error?: string } | null = null
23
24 try {
25 payload = (await response.json()) as TResponse | { error?: string }
26 } catch {
27 if (response.ok) {
28 throw new Error(
29 `Failed to parse JSON response with status ${response.status}.`,
30 )
31 }
32 }
33
34 if (!response.ok) {
35 const errorDetail =
36 payload &&
37 typeof payload === 'object' &&
38 'error' in payload &&
39 typeof payload.error === 'string'
40 ? `: ${payload.error}`
41 : ''
42
43 throw new Error(
44 `Request failed with status ${response.status}${errorDetail}`,
45 )
46 }
47
48 return payload as TResponse
49}
50
51export function createDataQueryOptions(apiBaseUrl = '') {
52 return {

Callers 1

createDataQueryOptionsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected