import { queryOptions } from '@tanstack/react-query' import { notFound } from '@tanstack/react-router' import { createServerFn, Fetcher } from '@tanstack/start' import axios from 'redaxios' import { serverFnQuery } from './serverFnQuery' export type PostType = { id: string title: string body: string } export const fetchPosts = createServerFn({ method: 'GET' }).handler( async () => { console.info('Fetching posts...') return axios .get>('https://jsonplaceholder.typicode.com/posts') .then((r) => r.data.slice(0, 10)) }, ) export const postsFnQuery = serverFnQuery(fetchPosts) export const fetchPost = createServerFn({ method: 'GET' }) .validator((d: string) => d) .handler(async ({ data }) => { console.info(`Fetching post with id ${data}...`) const post = await axios .get(`https://jsonplaceholder.typicode.com/posts/${data}`) .then((r) => r.data) .catch((err) => { console.error(err) if (err.status === 404) { throw notFound() } throw err }) return post }) export const postFnQuery = serverFnQuery(fetchPost)