@@ -10,43 +10,43 @@ type DataProps = {
10
10
todos : TodoItem [ ] ;
11
11
} ;
12
12
13
- const storage : DataProps = {
14
- todos : JSON . parse ( window . localStorage ?. getItem ( "todos" ) || "[]" ) ,
15
- } ;
16
-
17
- export const data : Data < DataProps > = {
13
+ export const data : Data < DataProps , DataProps > = {
14
+ cacheTtl : 0 ,
15
+ any : ( _req , ctx ) => {
16
+ ctx . todos = JSON . parse ( window . localStorage ?. getItem ( "todos" ) || "[]" ) ;
17
+ } ,
18
18
get : ( _req , ctx ) => {
19
- return ctx . json ( storage ) ;
19
+ return ctx . json ( { todos : ctx . todos } ) ;
20
20
} ,
21
21
put : async ( req , ctx ) => {
22
22
const { message } = await req . json ( ) ;
23
23
if ( typeof message === "string" ) {
24
- storage . todos . push ( { id : Date . now ( ) , message, completed : false } ) ;
25
- window . localStorage ?. setItem ( "todos" , JSON . stringify ( storage . todos ) ) ;
24
+ ctx . todos . push ( { id : Date . now ( ) , message, completed : false } ) ;
25
+ window . localStorage ?. setItem ( "todos" , JSON . stringify ( ctx . todos ) ) ;
26
26
}
27
- return ctx . json ( storage ) ;
27
+ return ctx . json ( { todos : ctx . todos } ) ;
28
28
} ,
29
29
patch : async ( req , ctx ) => {
30
30
const { id, message, completed } = await req . json ( ) ;
31
- const todo = storage . todos . find ( ( todo ) => todo . id === id ) ;
31
+ const todo = ctx . todos . find ( ( todo ) => todo . id === id ) ;
32
32
if ( todo ) {
33
33
if ( typeof message === "string" ) {
34
34
todo . message = message ;
35
35
}
36
36
if ( typeof completed === "boolean" ) {
37
37
todo . completed = completed ;
38
38
}
39
- window . localStorage ?. setItem ( "todos" , JSON . stringify ( storage . todos ) ) ;
39
+ window . localStorage ?. setItem ( "todos" , JSON . stringify ( ctx . todos ) ) ;
40
40
}
41
- return ctx . json ( storage ) ;
41
+ return ctx . json ( { todos : ctx . todos } ) ;
42
42
} ,
43
43
delete : async ( req , ctx ) => {
44
44
const { id } = await req . json ( ) ;
45
45
if ( id ) {
46
- storage . todos = storage . todos . filter ( ( todo ) => todo . id !== id ) ;
47
- window . localStorage ?. setItem ( "todos" , JSON . stringify ( storage . todos ) ) ;
46
+ ctx . todos = ctx . todos . filter ( ( todo ) => todo . id !== id ) ;
47
+ window . localStorage ?. setItem ( "todos" , JSON . stringify ( ctx . todos ) ) ;
48
48
}
49
- return ctx . json ( storage ) ;
49
+ return ctx . json ( { todos : ctx . todos } ) ;
50
50
} ,
51
51
} ;
52
52
0 commit comments