-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathobsidian.ts
More file actions
62 lines (53 loc) · 2.3 KB
/
obsidian.ts
File metadata and controls
62 lines (53 loc) · 2.3 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
/**
* Copyright (c) 2023 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { fetchDelete, fetchGet, fetchPutText } from "./http"
export const DEFAULT_ENDPOINT = "http://127.0.0.1:27123"
export const DEFAULT_VAULT = "vault"
export const INVALID_AUTH_CODE = 40101
export const NOT_FOUND_CODE = 40400
export type ObsidianResult<T> = {
message?: string
errorCode?: number
} & T
export type ObsidianRequestContext = {
endpoint?: string
vault?: string
auth: string
}
const authHeaders = (auth: string): Record<string, string> => ({
"Authorization": `Bearer ${auth}`
})
export async function listAllFiles(context: ObsidianRequestContext, dirPath: string): Promise<ObsidianResult<{ files: string[] }>> {
const { endpoint, auth, vault } = context || {}
const url = `${endpoint || DEFAULT_ENDPOINT}/${vault || DEFAULT_VAULT}/${dirPath || ''}`
const response = await fetchGet(url, { headers: authHeaders(auth) })
return await response?.json()
}
export async function updateFile(context: ObsidianRequestContext, filePath: string, content: string): Promise<void> {
const { endpoint, auth, vault } = context || {}
const url = `${endpoint || DEFAULT_ENDPOINT}/${vault || DEFAULT_VAULT}/${filePath}`
const headers = authHeaders(auth)
headers["Content-Type"] = "text/markdown"
await fetchPutText(url, content, { headers })
}
export async function getFileContent(context: ObsidianRequestContext, filePath: string): Promise<string | null> {
const { endpoint, auth, vault } = context || {}
const url = `${endpoint || DEFAULT_ENDPOINT}/${vault || DEFAULT_VAULT}/${filePath}`
const headers = authHeaders(auth)
const response = await fetchGet(url, { headers })
const { status } = response
return status >= 200 && status < 300 ? await response.text() : null
}
export async function deleteFile(context: ObsidianRequestContext, filePath: string): Promise<void> {
const { endpoint, auth, vault } = context || {}
const url = `${endpoint || DEFAULT_ENDPOINT}/${vault || DEFAULT_VAULT}/${filePath}`
const headers = authHeaders(auth)
const response = await fetchDelete(url, { headers })
if (response.status !== 200) {
console.log(`Failed to delete file of Obsidian. filePath=${filePath}`)
}
}