Open History to see the last commit
1export type Todo = {
2 id: number
3 title: string
4}
5β
6export type TodosResponse = {
7 items: Todo[]
8 requestCount: number
9 source: 'server' | 'cache'
10}
11β
12let todos: Todo[] = [
13 { id: 1, title: 'Ship lit-query alpha' },
14 { id: 2, title: 'Write integration checks' },
15]
16β
17let requestCount = 0
18let nextTodoId = 3
19let failNextFetch = false
20let failNextMutation = false
21β
22const delay = (ms: number): Promise<void> =>
23 new Promise((resolve) => {
24 setTimeout(resolve, ms)
25 })
26β
27export async function fetchTodosFromServer(): Promise<TodosResponse> {
28 await delay(90)
29 if (failNextFetch) {
30 failNextFetch = false
31 throw new Error('Forced fetch failure (test)')
32 }
33 requestCount += 1
34β
35 return {
36 items: todos.map((todo) => ({ ...todo })),
37 requestCount,
38 source: 'server',
39 }
40}
41β
42export async function addTodoOnServer(title: string): Promise<Todo> {
43 await delay(70)
44 if (failNextMutation) {
45 failNextMutation = false
46 throw new Error('Forced mutation failure (test)')
47 }
48β
49 const nextTodo: Todo = {
50 id: nextTodoId,
51 title,
52 }
53β
54 nextTodoId += 1
55 todos = [...todos, nextTodo]
56β
57 return { ...nextTodo }
58}
59β
60export function resetTodoApi(): void {
61 todos = [
62 { id: 1, title: 'Ship lit-query alpha' },
63 { id: 2, title: 'Write integration checks' },
64 ]
65 requestCount = 0
66 nextTodoId = 3
67 failNextFetch = false
68 failNextMutation = false
69}
70β
71export function failNextFetchRequest(): void {
72 failNextFetch = true
73}
74β
75export function failNextMutationRequest(): void {
76 failNextMutation = true
77}