@wetoria/siyuan-sdk 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/README.md +44 -0
  2. package/dist/api/archive/index.d.ts +12 -0
  3. package/dist/api/archive/index.d.ts.map +1 -0
  4. package/dist/api/archive/index.js +8 -0
  5. package/dist/api/archive/index.js.map +1 -0
  6. package/dist/api/asset/index.d.ts +12 -0
  7. package/dist/api/asset/index.d.ts.map +1 -0
  8. package/dist/api/asset/index.js +10 -0
  9. package/dist/api/asset/index.js.map +1 -0
  10. package/dist/api/attr/index.d.ts +39 -0
  11. package/dist/api/attr/index.d.ts.map +1 -0
  12. package/dist/api/attr/index.js +17 -0
  13. package/dist/api/attr/index.js.map +1 -0
  14. package/dist/api/block/index.d.ts +91 -0
  15. package/dist/api/block/index.d.ts.map +1 -0
  16. package/dist/api/block/index.js +87 -0
  17. package/dist/api/block/index.js.map +1 -0
  18. package/dist/api/convert/index.d.ts +12 -0
  19. package/dist/api/convert/index.d.ts.map +1 -0
  20. package/dist/api/convert/index.js +5 -0
  21. package/dist/api/convert/index.js.map +1 -0
  22. package/dist/api/export/index.d.ts +18 -0
  23. package/dist/api/export/index.d.ts.map +1 -0
  24. package/dist/api/export/index.js +12 -0
  25. package/dist/api/export/index.js.map +1 -0
  26. package/dist/api/file/index.d.ts +27 -0
  27. package/dist/api/file/index.d.ts.map +1 -0
  28. package/dist/api/file/index.js +27 -0
  29. package/dist/api/file/index.js.map +1 -0
  30. package/dist/api/filetree/index.d.ts +50 -0
  31. package/dist/api/filetree/index.d.ts.map +1 -0
  32. package/dist/api/filetree/index.js +47 -0
  33. package/dist/api/filetree/index.js.map +1 -0
  34. package/dist/api/index.d.ts +38 -0
  35. package/dist/api/index.d.ts.map +1 -0
  36. package/dist/api/index.js +17 -0
  37. package/dist/api/index.js.map +1 -0
  38. package/dist/api/network/index.d.ts +12 -0
  39. package/dist/api/network/index.d.ts.map +1 -0
  40. package/dist/api/network/index.js +12 -0
  41. package/dist/api/network/index.js.map +1 -0
  42. package/dist/api/notebook/index.d.ts +48 -0
  43. package/dist/api/notebook/index.d.ts.map +1 -0
  44. package/dist/api/notebook/index.js +32 -0
  45. package/dist/api/notebook/index.js.map +1 -0
  46. package/dist/api/notification/index.d.ts +17 -0
  47. package/dist/api/notification/index.d.ts.map +1 -0
  48. package/dist/api/notification/index.js +14 -0
  49. package/dist/api/notification/index.js.map +1 -0
  50. package/dist/api/sql/index.d.ts +29 -0
  51. package/dist/api/sql/index.d.ts.map +1 -0
  52. package/dist/api/sql/index.js +18 -0
  53. package/dist/api/sql/index.js.map +1 -0
  54. package/dist/api/sqlite/index.d.ts +12 -0
  55. package/dist/api/sqlite/index.d.ts.map +1 -0
  56. package/dist/api/sqlite/index.js +5 -0
  57. package/dist/api/sqlite/index.js.map +1 -0
  58. package/dist/api/system/index.d.ts +27 -0
  59. package/dist/api/system/index.d.ts.map +1 -0
  60. package/dist/api/system/index.js +14 -0
  61. package/dist/api/system/index.js.map +1 -0
  62. package/dist/api/template/index.d.ts +18 -0
  63. package/dist/api/template/index.d.ts.map +1 -0
  64. package/dist/api/template/index.js +11 -0
  65. package/dist/api/template/index.js.map +1 -0
  66. package/dist/core/index.d.ts +103 -0
  67. package/dist/core/index.d.ts.map +1 -0
  68. package/dist/core/index.js +152 -0
  69. package/dist/core/index.js.map +1 -0
  70. package/dist/index.d.ts +8 -0
  71. package/dist/index.d.ts.map +1 -0
  72. package/dist/index.js +7 -0
  73. package/dist/index.js.map +1 -0
  74. package/dist/types/index.d.ts +88 -0
  75. package/dist/types/index.d.ts.map +1 -0
  76. package/dist/types/index.js +2 -0
  77. package/dist/types/index.js.map +1 -0
  78. package/dist/utils/index.d.ts +2 -0
  79. package/dist/utils/index.d.ts.map +1 -0
  80. package/dist/utils/index.js +4 -0
  81. package/dist/utils/index.js.map +1 -0
  82. package/package.json +42 -0
  83. package/src/api/archive/index.ts +26 -0
  84. package/src/api/asset/index.ts +26 -0
  85. package/src/api/attr/index.ts +59 -0
  86. package/src/api/block/index.ts +226 -0
  87. package/src/api/convert/index.ts +18 -0
  88. package/src/api/export/index.ts +36 -0
  89. package/src/api/file/index.ts +59 -0
  90. package/src/api/filetree/index.ts +129 -0
  91. package/src/api/index.ts +56 -0
  92. package/src/api/network/index.ts +32 -0
  93. package/src/api/notebook/index.ts +93 -0
  94. package/src/api/notification/index.ts +33 -0
  95. package/src/api/sql/index.ts +65 -0
  96. package/src/api/sqlite/index.ts +20 -0
  97. package/src/api/system/index.ts +46 -0
  98. package/src/api/template/index.ts +32 -0
  99. package/src/core/index.ts +237 -0
  100. package/src/index.ts +9 -0
  101. package/src/types/index.ts +140 -0
  102. package/src/utils/index.ts +3 -0
@@ -0,0 +1,237 @@
1
+ /**
2
+ * SiYuan SDK - API Client
3
+ *
4
+ * Supports maintaining baseURL and token, binding custom fetch, and dynamically adding methods
5
+ *
6
+ * 支持维护 baseURL 和 token,绑定自定义 fetch,动态添加方法
7
+ *
8
+ */
9
+
10
+ import { IWebSocketData } from 'siyuan'
11
+
12
+
13
+
14
+ /**
15
+ * 标准 API 响应格式
16
+ */
17
+ export interface SyApiResponse<T = any> {
18
+ code: number
19
+ msg: string
20
+ data: T
21
+ }
22
+
23
+ export type SyApiMethodResponse<T = any> = Promise<SyApiResponse<T> | IWebSocketData>
24
+
25
+
26
+ /**
27
+ * 请求选项
28
+ */
29
+ export interface RequestOptions {
30
+ /** 请求方法 */
31
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
32
+ /** 请求头 */
33
+ headers?: Record<string, string>
34
+ /** 超时时间(毫秒) */
35
+ timeout?: number
36
+ }
37
+
38
+
39
+
40
+
41
+ /**
42
+ * 基础 Fetch 函数类型(统一的结构)
43
+ */
44
+ export type SyBaseApiMethod<T = any> = (
45
+ url: string,
46
+ data?: any,
47
+ options?: RequestOptions
48
+ ) => SyApiMethodResponse<T>
49
+
50
+ /**
51
+ * 自定义 Fetch 函数类型(别名)
52
+ */
53
+ export type CustomFetch<T = any> = SyBaseApiMethod<T>
54
+
55
+
56
+
57
+ /**
58
+ * API 配置(两种方式互斥)
59
+ */
60
+ export type SiYuanAPIConfig =
61
+ | { baseURL: string, token: string, customFetch?: never }
62
+ | { baseURL?: never, token?: never, customFetch: SyBaseApiMethod }
63
+
64
+
65
+ /**
66
+ * SiYuan API 客户端
67
+ */
68
+ export class SiYuanAPI {
69
+ /** 基础 URL */
70
+ private baseURL: string
71
+ /** 认证 Token(与 customFetch 互斥) */
72
+ private token?: string
73
+ /** 自定义 fetch 函数(与 token 互斥) */
74
+ private customFetch?: CustomFetch
75
+
76
+
77
+ constructor(config: SiYuanAPIConfig) {
78
+ this.setConfig(config)
79
+ }
80
+
81
+ setConfig(config: SiYuanAPIConfig): void {
82
+ // 根据配置类型设置属性
83
+ if ('customFetch' in config) {
84
+ // 使用 customFetch
85
+ this.baseURL = '' // customFetch 模式下 baseURL 可能不需要
86
+ this.customFetch = config.customFetch
87
+ this.token = undefined
88
+ } else {
89
+ // 使用 baseURL + token
90
+ this.baseURL = config.baseURL
91
+ this.token = config.token
92
+ this.customFetch = undefined
93
+ }
94
+ }
95
+
96
+ /**
97
+ * 获取当前配置
98
+ */
99
+ getConfig(): {
100
+ baseURL: string
101
+ token?: string
102
+ customFetch?: CustomFetch
103
+ } {
104
+ return {
105
+ baseURL: this.baseURL,
106
+ token: this.token,
107
+ customFetch: this.customFetch,
108
+ }
109
+ }
110
+
111
+ /**
112
+ * 默认的 baseFetch 实现(使用标准 fetch)
113
+ */
114
+ private async _defaultBaseFetch(
115
+ url: string,
116
+ data?: any,
117
+ options?: RequestOptions,
118
+ ): Promise<any> {
119
+ const baseURL = this.baseURL || ''
120
+ const fullURL = url.startsWith('http')
121
+ ? url
122
+ : `${baseURL.replace(/\/$/, '')}${url}`
123
+
124
+ const method = options?.method || 'POST'
125
+ const headers: Record<string, string> = {
126
+ 'Content-Type': 'application/json',
127
+ ...options?.headers,
128
+ }
129
+
130
+ // 如果有 token,添加到请求头
131
+ if (this.token) {
132
+ headers.Authorization = `Token ${this.token}`
133
+ }
134
+
135
+ const requestInit: RequestInit = {
136
+ method,
137
+ headers,
138
+ }
139
+
140
+ // 处理请求体
141
+ if (data !== undefined) {
142
+ if (data instanceof FormData) {
143
+ delete headers['Content-Type']
144
+ requestInit.body = data
145
+ } else {
146
+ requestInit.body = JSON.stringify(data)
147
+ }
148
+ }
149
+
150
+ // 处理超时
151
+ const timeout = options?.timeout || 30000
152
+ const controller = new AbortController()
153
+ const timeoutId = setTimeout(() => controller.abort(), timeout)
154
+
155
+ try {
156
+ const response = await fetch(fullURL, {
157
+ ...requestInit,
158
+ signal: controller.signal,
159
+ })
160
+
161
+ clearTimeout(timeoutId)
162
+
163
+ if (!response.ok) {
164
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`)
165
+ }
166
+
167
+ const contentType = response.headers.get('content-type')
168
+
169
+ if (contentType && contentType.includes('application/json')) {
170
+ return await response.json()
171
+ } else {
172
+ return await response.text()
173
+ }
174
+ } catch (error: any) {
175
+ clearTimeout(timeoutId)
176
+ if (error.name === 'AbortError') {
177
+ throw new Error(`Request timeout after ${timeout}ms`)
178
+ }
179
+ throw error
180
+ }
181
+ }
182
+
183
+ /**
184
+ * 内部请求方法
185
+ * 如果存在自定义 fetch,使用自定义 fetch;否则使用默认的 baseFetch(会自动设置 baseURL 和 token)
186
+ * 返回思源标准格式 SyApiResponse<T>
187
+ * 此方法为内部方法,禁止外部修改或覆盖
188
+ * 允许在注入的 API 方法中使用
189
+ *
190
+ * @param url - API 端点路径
191
+ * @param data - 请求数据
192
+ * @param options - 请求选项
193
+ * @returns SyApiResponse<T>
194
+ *
195
+ * @internal
196
+ */
197
+ async request<T = any>(
198
+ url: string,
199
+ data?: any,
200
+ options?: RequestOptions,
201
+ ): SyApiMethodResponse<T> {
202
+ // 如果提供了自定义 fetch,使用自定义 fetch
203
+ if (this.customFetch) {
204
+ return await this.customFetch(url, data, options) as SyApiResponse<T>
205
+ }
206
+
207
+ // 否则使用默认的 baseFetch
208
+ return await this._defaultBaseFetch(url, data, options) as SyApiResponse<T>
209
+ }
210
+
211
+
212
+ async post<T = any>(url: string, data?: any, options?: RequestOptions): SyApiMethodResponse<T> {
213
+ return await this.request(url, data, {
214
+ ...options,
215
+ method: 'POST',
216
+ }) as SyApiResponse<T>
217
+ }
218
+
219
+
220
+ /**
221
+ * 提取响应数据(用于插件模式,直接返回 data)
222
+ * 如果 code === 0,返回 data;否则返回 null
223
+ *
224
+ * @param response - API 响应
225
+ * @returns 提取的数据或 null
226
+ */
227
+ extractData<T>(response: SyApiResponse<T>): T | null {
228
+ return response.code === 0 ? response.data : null
229
+ }
230
+ }
231
+
232
+ /**
233
+ * 创建 API 实例的便捷函数
234
+ */
235
+ export function createAPI(config: SiYuanAPIConfig): SiYuanAPI {
236
+ return new SiYuanAPI(config)
237
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * SiYuan SDK
3
+ */
4
+
5
+ export * from './api/index.js'
6
+ export type * from './core/index.js'
7
+ export * from './core/index.js'
8
+ export * from './types/index.js'
9
+
@@ -0,0 +1,140 @@
1
+ /**
2
+ * Frequently used data structures in SiYuan
3
+ */
4
+ export type DocumentId = string
5
+ export type BlockId = string
6
+ export type NotebookId = string
7
+ export type PreviousID = BlockId
8
+ export type ParentID = BlockId | DocumentId
9
+
10
+ export interface Notebook {
11
+ id: NotebookId
12
+ name: string
13
+ icon: string
14
+ sort: number
15
+ closed: boolean
16
+ }
17
+
18
+ export interface NotebookConf {
19
+ name: string
20
+ closed: boolean
21
+ refCreateSavePath: string
22
+ createDocNameTemplate: string
23
+ dailyNoteSavePath: string
24
+ dailyNoteTemplatePath: string
25
+ }
26
+
27
+ export type BlockType =
28
+ | "d"
29
+ | "s"
30
+ | "h"
31
+ | "t"
32
+ | "i"
33
+ | "p"
34
+ | "f"
35
+ | "audio"
36
+ | "video"
37
+ | "other"
38
+
39
+ export type BlockSubType =
40
+ | "d1"
41
+ | "d2"
42
+ | "s1"
43
+ | "s2"
44
+ | "s3"
45
+ | "t1"
46
+ | "t2"
47
+ | "h1"
48
+ | "h2"
49
+ | "h3"
50
+ | "h4"
51
+ | "h5"
52
+ | "h6"
53
+ | "table"
54
+ | "task"
55
+ | "toggle"
56
+ | "latex"
57
+ | "quote"
58
+ | "html"
59
+ | "code"
60
+ | "footnote"
61
+ | "cite"
62
+ | "collection"
63
+ | "bookmark"
64
+ | "attachment"
65
+ | "comment"
66
+ | "mindmap"
67
+ | "spreadsheet"
68
+ | "calendar"
69
+ | "image"
70
+ | "audio"
71
+ | "video"
72
+ | "other"
73
+
74
+ export interface Block {
75
+ id: BlockId
76
+ parent_id?: BlockId
77
+ root_id: DocumentId
78
+ hash: string
79
+ box: string
80
+ path: string
81
+ hpath: string
82
+ name: string
83
+ alias: string
84
+ memo: string
85
+ tag: string
86
+ content: string
87
+ fcontent?: string
88
+ markdown: string
89
+ length: number
90
+ type: BlockType
91
+ subtype: BlockSubType
92
+ /**
93
+ * string of { [key: string]: string }
94
+ * For instance: "{: custom-type=\"query-code\" id=\"20230613234017-zkw3pr0\" updated=\"20230613234509\"}"
95
+ */
96
+ ial?: string
97
+ sort: number
98
+ created: string
99
+ updated: string
100
+ }
101
+
102
+ export interface doOperation {
103
+ action: string
104
+ data: string
105
+ id: BlockId
106
+ parentID: BlockId | DocumentId
107
+ previousID: BlockId
108
+ retData: null
109
+ }
110
+
111
+ declare global {
112
+ interface Window {
113
+ siyuan: {
114
+ notebooks: any
115
+ menus: any
116
+ dialogs: any
117
+ blockPanels: any
118
+ storage: any
119
+ user: any
120
+ ws: any
121
+ languages: any
122
+ config: import('siyuan').Config.IConf
123
+ }
124
+ }
125
+ }
126
+
127
+ export interface IBreadcrumb {
128
+ id: string
129
+ name: string
130
+ type: string
131
+ subType: string
132
+ children: any
133
+ }
134
+
135
+ export interface backlinkData {
136
+ dom: string
137
+ blockPaths: IBreadcrumb[]
138
+ expand: boolean
139
+ }
140
+
@@ -0,0 +1,3 @@
1
+ export function trimSqlBlank(sql: string) {
2
+ return sql.replace(/\s+/g, ' ').trim()
3
+ }