import { queryOptions } from '@tanstack/react-query' import { notFound } from '@tanstack/react-router' import { createServerFn } from '@tanstack/react-start' export type PostType = { id: number title: string body: string } export const fetchPost = createServerFn({ method: 'POST' }) .inputValidator((d: string) => d) .handler(async ({ data }) => { console.info(`Fetching post with id ${data}...`) const res = await fetch( `https://jsonplaceholder.typicode.com/posts/${data}`, ) if (!res.ok) { if (res.status === 404) { throw notFound() } throw new Error('Failed to fetch post') } const post = await res.json() return post as PostType }) export const fetchPosts = createServerFn().handler(async () => { console.info('Fetching posts...') const res = await fetch('https://jsonplaceholder.typicode.com/posts') if (!res.ok) { throw new Error('Failed to fetch posts') } const posts = await res.json() return (posts as Array).slice(0, 10) }) export const postsQueryOptions = () => queryOptions({ queryKey: ['posts'], queryFn: () => fetchPosts(), }) export const postQueryOptions = (postId: string) => queryOptions({ queryKey: ['post', postId], queryFn: () => fetchPost({ data: postId }), })