Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queries.ts

37 lines1.1 KB

1import { mutationOptions, queryOptions } from '@tanstack/lit-query'
2import { projectsAPI } from './api'
3import type { CreateProjectInput, ToggleProjectFavoriteInput } from './types'
4​
5export const projectsQueries = {
6 all: () => ['projects'] as const,
7 list: (page: number, delayMs: number, forceError: boolean) =>
8 queryOptions({
9 queryKey: [
10 ...projectsQueries.all(),
11 'list',
12 page,
13 delayMs,
14 forceError,
15 ] as const,
16 queryFn: () => projectsAPI.getProjects(page, delayMs, forceError),
17 }),
19​
20export const projectsMutations = {
21 create: () =>
22 mutationOptions({
23 mutationKey: [...projectsQueries.all(), 'create'] as const,
24 mutationFn: async (input: CreateProjectInput) => {
25 const response = await projectsAPI.createProject(input)
26 return response.project
27 },
28 }),
29 updateFavorite: () =>
30 mutationOptions({
31 mutationKey: [...projectsQueries.all(), 'update-favorite'] as const,
32 mutationFn: async (input: ToggleProjectFavoriteInput) => {
33 const response = await projectsAPI.updateProjectFavorite(input)
34 return response.project
35 },
36 }),