This repository was archived by the owner on Apr 8, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 378
Expand file tree
/
Copy pathnote.ts
More file actions
86 lines (78 loc) · 1.74 KB
/
note.ts
File metadata and controls
86 lines (78 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { NoteModel } from 'libs/shared/note'
import { encode } from 'qss'
import { useCallback } from 'react'
import noteCache from '../cache/note'
import useFetcher from './fetcher'
export default function useNoteAPI() {
const { loading, request, abort, error } = useFetcher()
const find = useCallback(
async (id: string, params?: { sv: string }) => {
let qs = ''
if (params) {
qs = '?' + encode(params)
}
return request<null, NoteModel>({
method: 'GET',
url: `/api/notes/${id}` + qs,
})
},
[request]
)
const create = useCallback(
async (body: Partial<NoteModel>) => {
return request<Partial<NoteModel>, NoteModel>(
{
method: 'POST',
url: `/api/notes`,
},
body
)
},
[request]
)
const mutate = useCallback(
async (id: string, body: Partial<NoteModel>) => {
const data = body.updates
? await request<Partial<NoteModel>, NoteModel>(
{
method: 'POST',
url: `/api/notes/${id}`,
},
body
)
: await request<Partial<NoteModel>, NoteModel>(
{
method: 'POST',
url: `/api/notes/${id}/meta`,
},
body
)
return data
},
[request]
)
// fetch note from cache or api
const fetch = useCallback(
async (id: string) => {
const cache = await noteCache.getItem(id)
if (cache) {
return cache
}
const note = await find(id)
if (note) {
noteCache.setItem(id, note)
}
return note
},
[find]
)
return {
loading,
error,
abort,
find,
create,
mutate,
fetch,
}
}