import axios from 'redaxios' import { queryOptions } from '@tanstack/solid-query' export class NotFoundError extends Error {} type PostType = { id: string title: string body: string } const fetchPosts = async () => { console.info('Fetching posts...') await new Promise((r) => setTimeout(r, 500)) return axios .get>('https://jsonplaceholder.typicode.com/posts') .then((r) => r.data.slice(0, 10)) } const fetchPost = async (postId: string) => { console.info(`Fetching post with id ${postId}...`) await new Promise((r) => setTimeout(r, 500)) const post = await axios .get(`https://jsonplaceholder.typicode.com/posts/${postId}`) .then((r) => r.data) // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (!post) { throw new NotFoundError(`Post with id "${postId}" not found!`) } return post } export const postQueryOptions = (postId: string) => queryOptions({ queryKey: ['posts', { postId }], queryFn: () => fetchPost(postId), }) export const postsQueryOptions = queryOptions({ queryKey: ['posts'], queryFn: () => fetchPosts(), })