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

Function fetchPost

examples/solid/solid-start-streaming/src/utils/api.ts:10–40  ·  view source on GitHub ↗
({
  postId,
  simulateError,
  sleep,
}: {
  postId: number
  simulateError?: boolean
  sleep?: number
})

Source from the content-addressed store, hash-verified

8const doSleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
9
10export const fetchPost = async ({
11 postId,
12 simulateError,
13 sleep,
14}: {
15 postId: number
16 simulateError?: boolean
17 sleep?: number
18}) => {
19 console.info('[api] fetchPost.start', { postId, sleep, simulateError })
20
21 let response
22 if (!simulateError) {
23 response = await fetch(
24 `https://jsonplaceholder.typicode.com/posts/${postId}`,
25 ).then((res) => res.json())
26 }
27
28 // simulate extra latency to make things like streaming behavior more clear
29 if (sleep) {
30 await doSleep(sleep)
31 }
32
33 console.info('[api] fetchPost.done', { postId, sleep, simulateError })
34
35 if (simulateError) {
36 throw new Error('API request to get post was not OK')
37 }
38
39 return [response] as PostData[]
40}
41
42export const fetchUser = async ({
43 sleep,

Callers 1

PostViewerFunction · 0.90

Calls 1

doSleepFunction · 0.85

Tested by

no test coverage detected