obsidian-mcp-server 1.5.8 → 2.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 (221) hide show
  1. package/README.md +248 -104
  2. package/dist/config/index.d.ts +41 -0
  3. package/dist/config/index.js +191 -0
  4. package/dist/index.d.ts +1 -5
  5. package/dist/index.js +296 -18
  6. package/dist/mcp-server/server.d.ts +33 -0
  7. package/dist/mcp-server/server.js +211 -0
  8. package/dist/mcp-server/tools/obsidianDeleteFileTool/index.d.ts +12 -0
  9. package/dist/mcp-server/tools/obsidianDeleteFileTool/index.js +12 -0
  10. package/dist/mcp-server/tools/obsidianDeleteFileTool/logic.d.ts +51 -0
  11. package/dist/mcp-server/tools/obsidianDeleteFileTool/logic.js +168 -0
  12. package/dist/mcp-server/tools/obsidianDeleteFileTool/registration.d.ts +19 -0
  13. package/dist/mcp-server/tools/obsidianDeleteFileTool/registration.js +91 -0
  14. package/dist/mcp-server/tools/obsidianGlobalSearchTool/index.d.ts +12 -0
  15. package/dist/mcp-server/tools/obsidianGlobalSearchTool/index.js +12 -0
  16. package/dist/mcp-server/tools/obsidianGlobalSearchTool/logic.d.ts +77 -0
  17. package/dist/mcp-server/tools/obsidianGlobalSearchTool/logic.js +341 -0
  18. package/dist/mcp-server/tools/obsidianGlobalSearchTool/registration.d.ts +18 -0
  19. package/dist/mcp-server/tools/obsidianGlobalSearchTool/registration.js +69 -0
  20. package/dist/mcp-server/tools/obsidianListFilesTool/index.d.ts +12 -0
  21. package/dist/mcp-server/tools/obsidianListFilesTool/index.js +12 -0
  22. package/dist/mcp-server/tools/obsidianListFilesTool/logic.d.ts +64 -0
  23. package/dist/mcp-server/tools/obsidianListFilesTool/logic.js +179 -0
  24. package/dist/mcp-server/tools/obsidianListFilesTool/registration.d.ts +19 -0
  25. package/dist/mcp-server/tools/obsidianListFilesTool/registration.js +96 -0
  26. package/dist/mcp-server/tools/obsidianManageFrontmatterTool/index.d.ts +3 -0
  27. package/dist/mcp-server/tools/obsidianManageFrontmatterTool/index.js +2 -0
  28. package/dist/mcp-server/tools/obsidianManageFrontmatterTool/logic.d.ts +42 -0
  29. package/dist/mcp-server/tools/obsidianManageFrontmatterTool/logic.js +152 -0
  30. package/dist/mcp-server/tools/obsidianManageFrontmatterTool/registration.d.ts +3 -0
  31. package/dist/mcp-server/tools/obsidianManageFrontmatterTool/registration.js +52 -0
  32. package/dist/mcp-server/tools/obsidianManageTagsTool/index.d.ts +3 -0
  33. package/dist/mcp-server/tools/obsidianManageTagsTool/index.js +2 -0
  34. package/dist/mcp-server/tools/obsidianManageTagsTool/logic.d.ts +28 -0
  35. package/dist/mcp-server/tools/obsidianManageTagsTool/logic.js +161 -0
  36. package/dist/mcp-server/tools/obsidianManageTagsTool/registration.d.ts +3 -0
  37. package/dist/mcp-server/tools/obsidianManageTagsTool/registration.js +52 -0
  38. package/dist/mcp-server/tools/obsidianReadFileTool/index.d.ts +12 -0
  39. package/dist/mcp-server/tools/obsidianReadFileTool/index.js +12 -0
  40. package/dist/mcp-server/tools/obsidianReadFileTool/logic.d.ts +87 -0
  41. package/dist/mcp-server/tools/obsidianReadFileTool/logic.js +216 -0
  42. package/dist/mcp-server/tools/obsidianReadFileTool/registration.d.ts +20 -0
  43. package/dist/mcp-server/tools/obsidianReadFileTool/registration.js +101 -0
  44. package/dist/mcp-server/tools/obsidianSearchReplaceTool/index.d.ts +12 -0
  45. package/dist/mcp-server/tools/obsidianSearchReplaceTool/index.js +12 -0
  46. package/dist/mcp-server/tools/obsidianSearchReplaceTool/logic.d.ts +255 -0
  47. package/dist/mcp-server/tools/obsidianSearchReplaceTool/logic.js +583 -0
  48. package/dist/mcp-server/tools/obsidianSearchReplaceTool/registration.d.ts +22 -0
  49. package/dist/mcp-server/tools/obsidianSearchReplaceTool/registration.js +111 -0
  50. package/dist/mcp-server/tools/obsidianUpdateFileTool/index.d.ts +12 -0
  51. package/dist/mcp-server/tools/obsidianUpdateFileTool/index.js +12 -0
  52. package/dist/mcp-server/tools/obsidianUpdateFileTool/logic.d.ts +183 -0
  53. package/dist/mcp-server/tools/obsidianUpdateFileTool/logic.js +490 -0
  54. package/dist/mcp-server/tools/obsidianUpdateFileTool/registration.d.ts +21 -0
  55. package/dist/mcp-server/tools/obsidianUpdateFileTool/registration.js +108 -0
  56. package/dist/mcp-server/transports/authentication/authContext.d.ts +33 -0
  57. package/dist/mcp-server/transports/authentication/authContext.js +24 -0
  58. package/dist/mcp-server/transports/authentication/authMiddleware.d.ts +30 -0
  59. package/dist/mcp-server/transports/authentication/authMiddleware.js +145 -0
  60. package/dist/mcp-server/transports/authentication/authUtils.d.ts +18 -0
  61. package/dist/mcp-server/transports/authentication/authUtils.js +45 -0
  62. package/dist/mcp-server/transports/authentication/oauthMiddleware.d.ts +24 -0
  63. package/dist/mcp-server/transports/authentication/oauthMiddleware.js +109 -0
  64. package/dist/mcp-server/transports/authentication/types.d.ts +17 -0
  65. package/dist/mcp-server/transports/authentication/types.js +5 -0
  66. package/dist/mcp-server/transports/httpTransport.d.ts +24 -0
  67. package/dist/mcp-server/transports/httpTransport.js +496 -0
  68. package/dist/mcp-server/transports/stdioTransport.d.ts +42 -0
  69. package/dist/mcp-server/transports/stdioTransport.js +63 -0
  70. package/dist/services/obsidianRestAPI/index.d.ts +15 -0
  71. package/dist/services/obsidianRestAPI/index.js +17 -0
  72. package/dist/services/obsidianRestAPI/methods/activeFileMethods.d.ts +38 -0
  73. package/dist/services/obsidianRestAPI/methods/activeFileMethods.js +62 -0
  74. package/dist/services/obsidianRestAPI/methods/commandMethods.d.ts +22 -0
  75. package/dist/services/obsidianRestAPI/methods/commandMethods.js +31 -0
  76. package/dist/services/obsidianRestAPI/methods/openMethods.d.ts +16 -0
  77. package/dist/services/obsidianRestAPI/methods/openMethods.js +21 -0
  78. package/dist/services/obsidianRestAPI/methods/patchMethods.d.ts +37 -0
  79. package/dist/services/obsidianRestAPI/methods/patchMethods.js +94 -0
  80. package/dist/services/obsidianRestAPI/methods/periodicNoteMethods.d.ts +42 -0
  81. package/dist/services/obsidianRestAPI/methods/periodicNoteMethods.js +66 -0
  82. package/dist/services/obsidianRestAPI/methods/searchMethods.d.ts +25 -0
  83. package/dist/services/obsidianRestAPI/methods/searchMethods.js +36 -0
  84. package/dist/services/obsidianRestAPI/methods/vaultMethods.d.ts +58 -0
  85. package/dist/services/obsidianRestAPI/methods/vaultMethods.js +144 -0
  86. package/dist/services/obsidianRestAPI/service.d.ts +195 -0
  87. package/dist/services/obsidianRestAPI/service.js +379 -0
  88. package/dist/services/obsidianRestAPI/types.d.ts +127 -0
  89. package/dist/services/obsidianRestAPI/types.js +7 -0
  90. package/dist/services/obsidianRestAPI/vaultCache/index.d.ts +4 -0
  91. package/dist/services/obsidianRestAPI/vaultCache/index.js +4 -0
  92. package/dist/services/obsidianRestAPI/vaultCache/service.d.ts +88 -0
  93. package/dist/services/obsidianRestAPI/vaultCache/service.js +299 -0
  94. package/dist/types-global/errors.d.ts +73 -0
  95. package/dist/types-global/errors.js +71 -0
  96. package/dist/utils/index.d.ts +5 -8
  97. package/dist/utils/index.js +13 -9
  98. package/dist/utils/internal/asyncUtils.d.ts +54 -0
  99. package/dist/utils/internal/asyncUtils.js +101 -0
  100. package/dist/utils/internal/errorHandler.d.ts +176 -0
  101. package/dist/utils/internal/errorHandler.js +351 -0
  102. package/dist/utils/internal/index.d.ts +4 -0
  103. package/dist/utils/internal/index.js +4 -0
  104. package/dist/utils/internal/logger.d.ts +141 -0
  105. package/dist/utils/internal/logger.js +406 -0
  106. package/dist/utils/internal/requestContext.d.ts +83 -0
  107. package/dist/utils/internal/requestContext.js +72 -0
  108. package/dist/utils/metrics/index.d.ts +1 -0
  109. package/dist/utils/metrics/index.js +1 -0
  110. package/dist/utils/metrics/tokenCounter.d.ts +27 -0
  111. package/dist/utils/metrics/tokenCounter.js +128 -0
  112. package/dist/utils/obsidian/index.d.ts +5 -0
  113. package/dist/utils/obsidian/index.js +5 -0
  114. package/dist/utils/obsidian/obsidianApiUtils.d.ts +14 -0
  115. package/dist/utils/obsidian/obsidianApiUtils.js +29 -0
  116. package/dist/utils/obsidian/obsidianStatUtils.d.ts +68 -0
  117. package/dist/utils/obsidian/obsidianStatUtils.js +143 -0
  118. package/dist/utils/parsing/dateParser.d.ts +56 -0
  119. package/dist/utils/parsing/dateParser.js +104 -0
  120. package/dist/utils/parsing/index.d.ts +2 -0
  121. package/dist/utils/parsing/index.js +3 -0
  122. package/dist/utils/parsing/jsonParser.d.ts +80 -0
  123. package/dist/utils/parsing/jsonParser.js +133 -0
  124. package/dist/utils/security/idGenerator.d.ts +140 -0
  125. package/dist/utils/security/idGenerator.js +194 -0
  126. package/dist/utils/security/index.d.ts +3 -0
  127. package/dist/utils/security/index.js +3 -0
  128. package/dist/utils/security/rateLimiter.d.ts +156 -0
  129. package/dist/utils/security/rateLimiter.js +235 -0
  130. package/dist/utils/security/sanitization.d.ts +244 -0
  131. package/dist/utils/security/sanitization.js +599 -0
  132. package/package.json +58 -37
  133. package/dist/index.js.map +0 -1
  134. package/dist/mcp/handlers.d.ts +0 -29
  135. package/dist/mcp/handlers.js +0 -305
  136. package/dist/mcp/handlers.js.map +0 -1
  137. package/dist/mcp/index.d.ts +0 -6
  138. package/dist/mcp/index.js +0 -7
  139. package/dist/mcp/index.js.map +0 -1
  140. package/dist/mcp/server.d.ts +0 -18
  141. package/dist/mcp/server.js +0 -240
  142. package/dist/mcp/server.js.map +0 -1
  143. package/dist/mcp/types.d.ts +0 -70
  144. package/dist/mcp/types.js +0 -49
  145. package/dist/mcp/types.js.map +0 -1
  146. package/dist/obsidian/client.d.ts +0 -109
  147. package/dist/obsidian/client.js +0 -403
  148. package/dist/obsidian/client.js.map +0 -1
  149. package/dist/obsidian/errors.d.ts +0 -28
  150. package/dist/obsidian/errors.js +0 -75
  151. package/dist/obsidian/errors.js.map +0 -1
  152. package/dist/obsidian/index.d.ts +0 -6
  153. package/dist/obsidian/index.js +0 -7
  154. package/dist/obsidian/index.js.map +0 -1
  155. package/dist/obsidian/types.d.ts +0 -107
  156. package/dist/obsidian/types.js +0 -12
  157. package/dist/obsidian/types.js.map +0 -1
  158. package/dist/resources/index.d.ts +0 -13
  159. package/dist/resources/index.js +0 -15
  160. package/dist/resources/index.js.map +0 -1
  161. package/dist/resources/tags.d.ts +0 -39
  162. package/dist/resources/tags.js +0 -257
  163. package/dist/resources/tags.js.map +0 -1
  164. package/dist/resources/types.d.ts +0 -27
  165. package/dist/resources/types.js +0 -5
  166. package/dist/resources/types.js.map +0 -1
  167. package/dist/tools/base.d.ts +0 -46
  168. package/dist/tools/base.js +0 -88
  169. package/dist/tools/base.js.map +0 -1
  170. package/dist/tools/files/content.d.ts +0 -58
  171. package/dist/tools/files/content.js +0 -171
  172. package/dist/tools/files/content.js.map +0 -1
  173. package/dist/tools/files/index.d.ts +0 -14
  174. package/dist/tools/files/index.js +0 -22
  175. package/dist/tools/files/index.js.map +0 -1
  176. package/dist/tools/files/list.d.ts +0 -35
  177. package/dist/tools/files/list.js +0 -133
  178. package/dist/tools/files/list.js.map +0 -1
  179. package/dist/tools/index.d.ts +0 -21
  180. package/dist/tools/index.js +0 -31
  181. package/dist/tools/index.js.map +0 -1
  182. package/dist/tools/properties/index.d.ts +0 -14
  183. package/dist/tools/properties/index.js +0 -19
  184. package/dist/tools/properties/index.js.map +0 -1
  185. package/dist/tools/properties/manager.d.ts +0 -62
  186. package/dist/tools/properties/manager.js +0 -302
  187. package/dist/tools/properties/manager.js.map +0 -1
  188. package/dist/tools/properties/tools.d.ts +0 -47
  189. package/dist/tools/properties/tools.js +0 -239
  190. package/dist/tools/properties/tools.js.map +0 -1
  191. package/dist/tools/properties/types.d.ts +0 -141
  192. package/dist/tools/properties/types.js +0 -70
  193. package/dist/tools/properties/types.js.map +0 -1
  194. package/dist/tools/search/complex.d.ts +0 -38
  195. package/dist/tools/search/complex.js +0 -270
  196. package/dist/tools/search/complex.js.map +0 -1
  197. package/dist/tools/search/index.d.ts +0 -14
  198. package/dist/tools/search/index.js +0 -20
  199. package/dist/tools/search/index.js.map +0 -1
  200. package/dist/tools/search/simple.d.ts +0 -25
  201. package/dist/tools/search/simple.js +0 -127
  202. package/dist/tools/search/simple.js.map +0 -1
  203. package/dist/utils/errors.d.ts +0 -24
  204. package/dist/utils/errors.js +0 -59
  205. package/dist/utils/errors.js.map +0 -1
  206. package/dist/utils/idGenerator.d.ts +0 -15
  207. package/dist/utils/idGenerator.js +0 -21
  208. package/dist/utils/idGenerator.js.map +0 -1
  209. package/dist/utils/index.js.map +0 -1
  210. package/dist/utils/logging.d.ts +0 -245
  211. package/dist/utils/logging.js +0 -417
  212. package/dist/utils/logging.js.map +0 -1
  213. package/dist/utils/rate-limiting.d.ts +0 -50
  214. package/dist/utils/rate-limiting.js +0 -94
  215. package/dist/utils/rate-limiting.js.map +0 -1
  216. package/dist/utils/tokenization.d.ts +0 -28
  217. package/dist/utils/tokenization.js +0 -75
  218. package/dist/utils/tokenization.js.map +0 -1
  219. package/dist/utils/validation.d.ts +0 -22
  220. package/dist/utils/validation.js +0 -92
  221. package/dist/utils/validation.js.map +0 -1
@@ -0,0 +1,62 @@
1
+ /**
2
+ * @module ActiveFileMethods
3
+ * @description
4
+ * Methods for interacting with the currently active file in Obsidian via the REST API.
5
+ */
6
+ /**
7
+ * Gets the content of the currently active file in Obsidian.
8
+ * @param _request - The internal request function from the service instance.
9
+ * @param format - 'markdown' or 'json' (for NoteJson).
10
+ * @param context - Request context.
11
+ * @returns The file content (string) or NoteJson object.
12
+ */
13
+ export async function getActiveFile(_request, format = "markdown", context) {
14
+ const acceptHeader = format === "json" ? "application/vnd.olrapi.note+json" : "text/markdown";
15
+ return _request({
16
+ method: "GET",
17
+ url: `/active/`,
18
+ headers: { Accept: acceptHeader },
19
+ }, context, "getActiveFile");
20
+ }
21
+ /**
22
+ * Updates (overwrites) the content of the currently active file.
23
+ * @param _request - The internal request function from the service instance.
24
+ * @param content - The new content.
25
+ * @param context - Request context.
26
+ * @returns {Promise<void>} Resolves on success (204 No Content).
27
+ */
28
+ export async function updateActiveFile(_request, content, context) {
29
+ await _request({
30
+ method: "PUT",
31
+ url: `/active/`,
32
+ headers: { "Content-Type": "text/markdown" },
33
+ data: content,
34
+ }, context, "updateActiveFile");
35
+ }
36
+ /**
37
+ * Appends content to the end of the currently active file.
38
+ * @param _request - The internal request function from the service instance.
39
+ * @param content - The content to append.
40
+ * @param context - Request context.
41
+ * @returns {Promise<void>} Resolves on success (204 No Content).
42
+ */
43
+ export async function appendActiveFile(_request, content, context) {
44
+ await _request({
45
+ method: "POST",
46
+ url: `/active/`,
47
+ headers: { "Content-Type": "text/markdown" },
48
+ data: content,
49
+ }, context, "appendActiveFile");
50
+ }
51
+ /**
52
+ * Deletes the currently active file.
53
+ * @param _request - The internal request function from the service instance.
54
+ * @param context - Request context.
55
+ * @returns {Promise<void>} Resolves on success (204 No Content).
56
+ */
57
+ export async function deleteActiveFile(_request, context) {
58
+ await _request({
59
+ method: "DELETE",
60
+ url: `/active/`,
61
+ }, context, "deleteActiveFile");
62
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @module CommandMethods
3
+ * @description
4
+ * Methods for interacting with Obsidian commands via the REST API.
5
+ */
6
+ import { RequestContext } from "../../../utils/index.js";
7
+ import { ObsidianCommand, RequestFunction } from "../types.js";
8
+ /**
9
+ * Executes a registered Obsidian command by its ID.
10
+ * @param _request - The internal request function from the service instance.
11
+ * @param commandId - The ID of the command (e.g., "app:go-back").
12
+ * @param context - Request context.
13
+ * @returns {Promise<void>} Resolves on success (204 No Content).
14
+ */
15
+ export declare function executeCommand(_request: RequestFunction, commandId: string, context: RequestContext): Promise<void>;
16
+ /**
17
+ * Lists all available Obsidian commands.
18
+ * @param _request - The internal request function from the service instance.
19
+ * @param context - Request context.
20
+ * @returns A list of available commands.
21
+ */
22
+ export declare function listCommands(_request: RequestFunction, context: RequestContext): Promise<ObsidianCommand[]>;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * @module CommandMethods
3
+ * @description
4
+ * Methods for interacting with Obsidian commands via the REST API.
5
+ */
6
+ /**
7
+ * Executes a registered Obsidian command by its ID.
8
+ * @param _request - The internal request function from the service instance.
9
+ * @param commandId - The ID of the command (e.g., "app:go-back").
10
+ * @param context - Request context.
11
+ * @returns {Promise<void>} Resolves on success (204 No Content).
12
+ */
13
+ export async function executeCommand(_request, commandId, context) {
14
+ await _request({
15
+ method: "POST",
16
+ url: `/commands/${encodeURIComponent(commandId)}/`,
17
+ }, context, "executeCommand");
18
+ }
19
+ /**
20
+ * Lists all available Obsidian commands.
21
+ * @param _request - The internal request function from the service instance.
22
+ * @param context - Request context.
23
+ * @returns A list of available commands.
24
+ */
25
+ export async function listCommands(_request, context) {
26
+ const response = await _request({
27
+ method: "GET",
28
+ url: "/commands/",
29
+ }, context, "listCommands");
30
+ return response.commands;
31
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @module OpenMethods
3
+ * @description
4
+ * Methods for opening files in Obsidian via the REST API.
5
+ */
6
+ import { RequestContext } from "../../../utils/index.js";
7
+ import { RequestFunction } from "../types.js";
8
+ /**
9
+ * Opens a specific file in Obsidian. Creates the file if it doesn't exist.
10
+ * @param _request - The internal request function from the service instance.
11
+ * @param filePath - Vault-relative path to the file.
12
+ * @param newLeaf - Whether to open the file in a new editor tab (leaf).
13
+ * @param context - Request context.
14
+ * @returns {Promise<void>} Resolves on success (200 OK, but no body expected).
15
+ */
16
+ export declare function openFile(_request: RequestFunction, filePath: string, newLeaf: boolean | undefined, context: RequestContext): Promise<void>;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @module OpenMethods
3
+ * @description
4
+ * Methods for opening files in Obsidian via the REST API.
5
+ */
6
+ /**
7
+ * Opens a specific file in Obsidian. Creates the file if it doesn't exist.
8
+ * @param _request - The internal request function from the service instance.
9
+ * @param filePath - Vault-relative path to the file.
10
+ * @param newLeaf - Whether to open the file in a new editor tab (leaf).
11
+ * @param context - Request context.
12
+ * @returns {Promise<void>} Resolves on success (200 OK, but no body expected).
13
+ */
14
+ export async function openFile(_request, filePath, newLeaf = false, context) {
15
+ // This endpoint returns 200 OK, not 204
16
+ await _request({
17
+ method: "POST",
18
+ url: `/open/${encodeURIComponent(filePath)}`,
19
+ params: { newLeaf },
20
+ }, context, "openFile");
21
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * @module PatchMethods
3
+ * @description
4
+ * Methods for performing granular PATCH operations within notes via the Obsidian REST API.
5
+ */
6
+ import { RequestContext } from "../../../utils/index.js";
7
+ import { PatchOptions, Period, RequestFunction } from "../types.js";
8
+ /**
9
+ * Patches a specific file in the vault.
10
+ * @param _request - The internal request function from the service instance.
11
+ * @param filePath - Vault-relative path to the file.
12
+ * @param content - The content to insert/replace (string or JSON for tables/frontmatter).
13
+ * @param options - Patch operation details (operation, targetType, target, etc.).
14
+ * @param context - Request context.
15
+ * @returns {Promise<void>} Resolves on success (200 OK).
16
+ */
17
+ export declare function patchFile(_request: RequestFunction, filePath: string, content: string | object, // Allow object for JSON content type
18
+ options: PatchOptions, context: RequestContext): Promise<void>;
19
+ /**
20
+ * Patches the currently active file in Obsidian.
21
+ * @param _request - The internal request function from the service instance.
22
+ * @param content - The content to insert/replace.
23
+ * @param options - Patch operation details.
24
+ * @param context - Request context.
25
+ * @returns {Promise<void>} Resolves on success (200 OK).
26
+ */
27
+ export declare function patchActiveFile(_request: RequestFunction, content: string | object, options: PatchOptions, context: RequestContext): Promise<void>;
28
+ /**
29
+ * Patches a periodic note.
30
+ * @param _request - The internal request function from the service instance.
31
+ * @param period - The period type ('daily', 'weekly', etc.).
32
+ * @param content - The content to insert/replace.
33
+ * @param options - Patch operation details.
34
+ * @param context - Request context.
35
+ * @returns {Promise<void>} Resolves on success (200 OK).
36
+ */
37
+ export declare function patchPeriodicNote(_request: RequestFunction, period: Period, content: string | object, options: PatchOptions, context: RequestContext): Promise<void>;
@@ -0,0 +1,94 @@
1
+ /**
2
+ * @module PatchMethods
3
+ * @description
4
+ * Methods for performing granular PATCH operations within notes via the Obsidian REST API.
5
+ */
6
+ import { encodeVaultPath } from "../../../utils/obsidian/obsidianApiUtils.js";
7
+ /**
8
+ * Helper to construct headers for PATCH requests.
9
+ */
10
+ function buildPatchHeaders(options) {
11
+ const headers = {
12
+ Operation: options.operation,
13
+ "Target-Type": options.targetType,
14
+ // Spec requires URL encoding for non-ASCII characters in Target header
15
+ Target: encodeURIComponent(options.target),
16
+ };
17
+ if (options.targetDelimiter) {
18
+ headers["Target-Delimiter"] = options.targetDelimiter;
19
+ }
20
+ if (options.trimTargetWhitespace !== undefined) {
21
+ headers["Trim-Target-Whitespace"] = String(options.trimTargetWhitespace);
22
+ }
23
+ // Add Create-Target-If-Missing header if provided in options
24
+ if (options.createTargetIfMissing !== undefined) {
25
+ headers["Create-Target-If-Missing"] = String(options.createTargetIfMissing);
26
+ }
27
+ if (options.contentType) {
28
+ headers["Content-Type"] = options.contentType;
29
+ }
30
+ else {
31
+ // Default to markdown if not specified, especially for non-JSON content
32
+ headers["Content-Type"] = "text/markdown";
33
+ }
34
+ return headers;
35
+ }
36
+ /**
37
+ * Patches a specific file in the vault.
38
+ * @param _request - The internal request function from the service instance.
39
+ * @param filePath - Vault-relative path to the file.
40
+ * @param content - The content to insert/replace (string or JSON for tables/frontmatter).
41
+ * @param options - Patch operation details (operation, targetType, target, etc.).
42
+ * @param context - Request context.
43
+ * @returns {Promise<void>} Resolves on success (200 OK).
44
+ */
45
+ export async function patchFile(_request, filePath, content, // Allow object for JSON content type
46
+ options, context) {
47
+ const headers = buildPatchHeaders(options);
48
+ const requestData = typeof content === "object" ? JSON.stringify(content) : content;
49
+ const encodedPath = encodeVaultPath(filePath);
50
+ // PATCH returns 200 OK according to spec
51
+ await _request({
52
+ method: "PATCH",
53
+ url: `/vault${encodedPath}`, // Use the encoded path
54
+ headers: headers,
55
+ data: requestData,
56
+ }, context, "patchFile");
57
+ }
58
+ /**
59
+ * Patches the currently active file in Obsidian.
60
+ * @param _request - The internal request function from the service instance.
61
+ * @param content - The content to insert/replace.
62
+ * @param options - Patch operation details.
63
+ * @param context - Request context.
64
+ * @returns {Promise<void>} Resolves on success (200 OK).
65
+ */
66
+ export async function patchActiveFile(_request, content, options, context) {
67
+ const headers = buildPatchHeaders(options);
68
+ const requestData = typeof content === "object" ? JSON.stringify(content) : content;
69
+ await _request({
70
+ method: "PATCH",
71
+ url: `/active/`,
72
+ headers: headers,
73
+ data: requestData,
74
+ }, context, "patchActiveFile");
75
+ }
76
+ /**
77
+ * Patches a periodic note.
78
+ * @param _request - The internal request function from the service instance.
79
+ * @param period - The period type ('daily', 'weekly', etc.).
80
+ * @param content - The content to insert/replace.
81
+ * @param options - Patch operation details.
82
+ * @param context - Request context.
83
+ * @returns {Promise<void>} Resolves on success (200 OK).
84
+ */
85
+ export async function patchPeriodicNote(_request, period, content, options, context) {
86
+ const headers = buildPatchHeaders(options);
87
+ const requestData = typeof content === "object" ? JSON.stringify(content) : content;
88
+ await _request({
89
+ method: "PATCH",
90
+ url: `/periodic/${period}/`,
91
+ headers: headers,
92
+ data: requestData,
93
+ }, context, "patchPeriodicNote");
94
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @module PeriodicNoteMethods
3
+ * @description
4
+ * Methods for interacting with periodic notes (daily, weekly, etc.) via the Obsidian REST API.
5
+ */
6
+ import { RequestContext } from "../../../utils/index.js";
7
+ import { NoteJson, Period, RequestFunction } from "../types.js";
8
+ /**
9
+ * Gets the content of a periodic note (daily, weekly, etc.).
10
+ * @param _request - The internal request function from the service instance.
11
+ * @param period - The period type ('daily', 'weekly', 'monthly', 'quarterly', 'yearly').
12
+ * @param format - 'markdown' or 'json'.
13
+ * @param context - Request context.
14
+ * @returns The note content or NoteJson.
15
+ */
16
+ export declare function getPeriodicNote(_request: RequestFunction, period: Period, format: "markdown" | "json" | undefined, context: RequestContext): Promise<string | NoteJson>;
17
+ /**
18
+ * Updates (overwrites) the content of a periodic note. Creates if needed.
19
+ * @param _request - The internal request function from the service instance.
20
+ * @param period - The period type.
21
+ * @param content - The new content.
22
+ * @param context - Request context.
23
+ * @returns {Promise<void>} Resolves on success (204 No Content).
24
+ */
25
+ export declare function updatePeriodicNote(_request: RequestFunction, period: Period, content: string, context: RequestContext): Promise<void>;
26
+ /**
27
+ * Appends content to a periodic note. Creates if needed.
28
+ * @param _request - The internal request function from the service instance.
29
+ * @param period - The period type.
30
+ * @param content - The content to append.
31
+ * @param context - Request context.
32
+ * @returns {Promise<void>} Resolves on success (204 No Content).
33
+ */
34
+ export declare function appendPeriodicNote(_request: RequestFunction, period: Period, content: string, context: RequestContext): Promise<void>;
35
+ /**
36
+ * Deletes a periodic note.
37
+ * @param _request - The internal request function from the service instance.
38
+ * @param period - The period type.
39
+ * @param context - Request context.
40
+ * @returns {Promise<void>} Resolves on success (204 No Content).
41
+ */
42
+ export declare function deletePeriodicNote(_request: RequestFunction, period: Period, context: RequestContext): Promise<void>;
@@ -0,0 +1,66 @@
1
+ /**
2
+ * @module PeriodicNoteMethods
3
+ * @description
4
+ * Methods for interacting with periodic notes (daily, weekly, etc.) via the Obsidian REST API.
5
+ */
6
+ /**
7
+ * Gets the content of a periodic note (daily, weekly, etc.).
8
+ * @param _request - The internal request function from the service instance.
9
+ * @param period - The period type ('daily', 'weekly', 'monthly', 'quarterly', 'yearly').
10
+ * @param format - 'markdown' or 'json'.
11
+ * @param context - Request context.
12
+ * @returns The note content or NoteJson.
13
+ */
14
+ export async function getPeriodicNote(_request, period, format = "markdown", context) {
15
+ const acceptHeader = format === "json" ? "application/vnd.olrapi.note+json" : "text/markdown";
16
+ return _request({
17
+ method: "GET",
18
+ url: `/periodic/${period}/`,
19
+ headers: { Accept: acceptHeader },
20
+ }, context, "getPeriodicNote");
21
+ }
22
+ /**
23
+ * Updates (overwrites) the content of a periodic note. Creates if needed.
24
+ * @param _request - The internal request function from the service instance.
25
+ * @param period - The period type.
26
+ * @param content - The new content.
27
+ * @param context - Request context.
28
+ * @returns {Promise<void>} Resolves on success (204 No Content).
29
+ */
30
+ export async function updatePeriodicNote(_request, period, content, context) {
31
+ await _request({
32
+ method: "PUT",
33
+ url: `/periodic/${period}/`,
34
+ headers: { "Content-Type": "text/markdown" },
35
+ data: content,
36
+ }, context, "updatePeriodicNote");
37
+ }
38
+ /**
39
+ * Appends content to a periodic note. Creates if needed.
40
+ * @param _request - The internal request function from the service instance.
41
+ * @param period - The period type.
42
+ * @param content - The content to append.
43
+ * @param context - Request context.
44
+ * @returns {Promise<void>} Resolves on success (204 No Content).
45
+ */
46
+ export async function appendPeriodicNote(_request, period, content, context) {
47
+ await _request({
48
+ method: "POST",
49
+ url: `/periodic/${period}/`,
50
+ headers: { "Content-Type": "text/markdown" },
51
+ data: content,
52
+ }, context, "appendPeriodicNote");
53
+ }
54
+ /**
55
+ * Deletes a periodic note.
56
+ * @param _request - The internal request function from the service instance.
57
+ * @param period - The period type.
58
+ * @param context - Request context.
59
+ * @returns {Promise<void>} Resolves on success (204 No Content).
60
+ */
61
+ export async function deletePeriodicNote(_request, period, context) {
62
+ await _request({
63
+ method: "DELETE",
64
+ url: `/periodic/${period}/`,
65
+ }, context, "deletePeriodicNote");
66
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @module SearchMethods
3
+ * @description
4
+ * Methods for performing searches via the Obsidian REST API.
5
+ */
6
+ import { RequestContext } from "../../../utils/index.js";
7
+ import { SimpleSearchResult, ComplexSearchResult, RequestFunction } from "../types.js";
8
+ /**
9
+ * Performs a simple text search across the vault.
10
+ * @param _request - The internal request function from the service instance.
11
+ * @param query - The text query string.
12
+ * @param contextLength - Number of characters surrounding each match (default 100).
13
+ * @param context - Request context.
14
+ * @returns An array of search results.
15
+ */
16
+ export declare function searchSimple(_request: RequestFunction, query: string, contextLength: number | undefined, context: RequestContext): Promise<SimpleSearchResult[]>;
17
+ /**
18
+ * Performs a complex search using Dataview DQL or JsonLogic.
19
+ * @param _request - The internal request function from the service instance.
20
+ * @param query - The query string (DQL) or JSON object (JsonLogic).
21
+ * @param contentType - The content type header indicating the query format.
22
+ * @param context - Request context.
23
+ * @returns An array of search results.
24
+ */
25
+ export declare function searchComplex(_request: RequestFunction, query: string | object, contentType: "application/vnd.olrapi.dataview.dql+txt" | "application/vnd.olrapi.jsonlogic+json", context: RequestContext): Promise<ComplexSearchResult[]>;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @module SearchMethods
3
+ * @description
4
+ * Methods for performing searches via the Obsidian REST API.
5
+ */
6
+ /**
7
+ * Performs a simple text search across the vault.
8
+ * @param _request - The internal request function from the service instance.
9
+ * @param query - The text query string.
10
+ * @param contextLength - Number of characters surrounding each match (default 100).
11
+ * @param context - Request context.
12
+ * @returns An array of search results.
13
+ */
14
+ export async function searchSimple(_request, query, contextLength = 100, context) {
15
+ return _request({
16
+ method: "POST",
17
+ url: "/search/simple/",
18
+ params: { query, contextLength }, // Send as query parameters
19
+ }, context, "searchSimple");
20
+ }
21
+ /**
22
+ * Performs a complex search using Dataview DQL or JsonLogic.
23
+ * @param _request - The internal request function from the service instance.
24
+ * @param query - The query string (DQL) or JSON object (JsonLogic).
25
+ * @param contentType - The content type header indicating the query format.
26
+ * @param context - Request context.
27
+ * @returns An array of search results.
28
+ */
29
+ export async function searchComplex(_request, query, contentType, context) {
30
+ return _request({
31
+ method: "POST",
32
+ url: "/search/",
33
+ headers: { "Content-Type": contentType },
34
+ data: query,
35
+ }, context, "searchComplex");
36
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @module VaultMethods
3
+ * @description
4
+ * Methods for interacting with vault files and directories via the Obsidian REST API.
5
+ */
6
+ import { RequestContext } from "../../../utils/index.js";
7
+ import { NoteJson, NoteStat, RequestFunction } from "../types.js";
8
+ /**
9
+ * Gets the content of a specific file in the vault.
10
+ * @param _request - The internal request function from the service instance.
11
+ * @param filePath - Vault-relative path to the file.
12
+ * @param format - 'markdown' or 'json' (for NoteJson).
13
+ * @param context - Request context.
14
+ * @returns The file content (string) or NoteJson object.
15
+ */
16
+ export declare function getFileContent(_request: RequestFunction, filePath: string, format: "markdown" | "json" | undefined, context: RequestContext): Promise<string | NoteJson>;
17
+ /**
18
+ * Updates (overwrites) the content of a file or creates it if it doesn't exist.
19
+ * @param _request - The internal request function from the service instance.
20
+ * @param filePath - Vault-relative path to the file.
21
+ * @param content - The new content for the file.
22
+ * @param context - Request context.
23
+ * @returns {Promise<void>} Resolves on success (204 No Content).
24
+ */
25
+ export declare function updateFileContent(_request: RequestFunction, filePath: string, content: string, context: RequestContext): Promise<void>;
26
+ /**
27
+ * Appends content to the end of a file. Creates the file if it doesn't exist.
28
+ * @param _request - The internal request function from the service instance.
29
+ * @param filePath - Vault-relative path to the file.
30
+ * @param content - The content to append.
31
+ * @param context - Request context.
32
+ * @returns {Promise<void>} Resolves on success (204 No Content).
33
+ */
34
+ export declare function appendFileContent(_request: RequestFunction, filePath: string, content: string, context: RequestContext): Promise<void>;
35
+ /**
36
+ * Deletes a specific file in the vault.
37
+ * @param _request - The internal request function from the service instance.
38
+ * @param filePath - Vault-relative path to the file.
39
+ * @param context - Request context.
40
+ * @returns {Promise<void>} Resolves on success (204 No Content).
41
+ */
42
+ export declare function deleteFile(_request: RequestFunction, filePath: string, context: RequestContext): Promise<void>;
43
+ /**
44
+ * Lists files within a specified directory in the vault.
45
+ * @param _request - The internal request function from the service instance.
46
+ * @param dirPath - Vault-relative path to the directory. Use empty string "" or "/" for the root.
47
+ * @param context - Request context.
48
+ * @returns A list of file and directory names.
49
+ */
50
+ export declare function listFiles(_request: RequestFunction, dirPath: string, context: RequestContext): Promise<string[]>;
51
+ /**
52
+ * Gets the metadata (stat) of a specific file using a lightweight HEAD request.
53
+ * @param _request - The internal request function from the service instance.
54
+ * @param filePath - Vault-relative path to the file.
55
+ * @param context - Request context.
56
+ * @returns The file's metadata.
57
+ */
58
+ export declare function getFileMetadata(_request: RequestFunction, filePath: string, context: RequestContext): Promise<NoteStat | null>;
@@ -0,0 +1,144 @@
1
+ /**
2
+ * @module VaultMethods
3
+ * @description
4
+ * Methods for interacting with vault files and directories via the Obsidian REST API.
5
+ */
6
+ import { encodeVaultPath } from "../../../utils/obsidian/obsidianApiUtils.js";
7
+ /**
8
+ * Gets the content of a specific file in the vault.
9
+ * @param _request - The internal request function from the service instance.
10
+ * @param filePath - Vault-relative path to the file.
11
+ * @param format - 'markdown' or 'json' (for NoteJson).
12
+ * @param context - Request context.
13
+ * @returns The file content (string) or NoteJson object.
14
+ */
15
+ export async function getFileContent(_request, filePath, format = "markdown", context) {
16
+ const acceptHeader = format === "json" ? "application/vnd.olrapi.note+json" : "text/markdown";
17
+ const encodedPath = encodeVaultPath(filePath); // Use the new encoding function
18
+ return _request({
19
+ method: "GET",
20
+ url: `/vault${encodedPath}`,
21
+ headers: { Accept: acceptHeader },
22
+ }, context, "getFileContent");
23
+ }
24
+ /**
25
+ * Updates (overwrites) the content of a file or creates it if it doesn't exist.
26
+ * @param _request - The internal request function from the service instance.
27
+ * @param filePath - Vault-relative path to the file.
28
+ * @param content - The new content for the file.
29
+ * @param context - Request context.
30
+ * @returns {Promise<void>} Resolves on success (204 No Content).
31
+ */
32
+ export async function updateFileContent(_request, filePath, content, context) {
33
+ const encodedPath = encodeVaultPath(filePath); // Use the new encoding function
34
+ // PUT returns 204 No Content, so the expected type is void
35
+ await _request({
36
+ method: "PUT",
37
+ url: `/vault${encodedPath}`, // Construct URL correctly
38
+ headers: { "Content-Type": "text/markdown" },
39
+ data: content,
40
+ }, context, "updateFileContent");
41
+ }
42
+ /**
43
+ * Appends content to the end of a file. Creates the file if it doesn't exist.
44
+ * @param _request - The internal request function from the service instance.
45
+ * @param filePath - Vault-relative path to the file.
46
+ * @param content - The content to append.
47
+ * @param context - Request context.
48
+ * @returns {Promise<void>} Resolves on success (204 No Content).
49
+ */
50
+ export async function appendFileContent(_request, filePath, content, context) {
51
+ const encodedPath = encodeVaultPath(filePath); // Use the new encoding function
52
+ await _request({
53
+ method: "POST",
54
+ url: `/vault${encodedPath}`, // Construct URL correctly
55
+ headers: { "Content-Type": "text/markdown" },
56
+ data: content,
57
+ }, context, "appendFileContent");
58
+ }
59
+ /**
60
+ * Deletes a specific file in the vault.
61
+ * @param _request - The internal request function from the service instance.
62
+ * @param filePath - Vault-relative path to the file.
63
+ * @param context - Request context.
64
+ * @returns {Promise<void>} Resolves on success (204 No Content).
65
+ */
66
+ export async function deleteFile(_request, filePath, context) {
67
+ const encodedPath = encodeVaultPath(filePath); // Use the new encoding function
68
+ await _request({
69
+ method: "DELETE",
70
+ url: `/vault${encodedPath}`, // Construct URL correctly
71
+ }, context, "deleteFile");
72
+ }
73
+ /**
74
+ * Lists files within a specified directory in the vault.
75
+ * @param _request - The internal request function from the service instance.
76
+ * @param dirPath - Vault-relative path to the directory. Use empty string "" or "/" for the root.
77
+ * @param context - Request context.
78
+ * @returns A list of file and directory names.
79
+ */
80
+ export async function listFiles(_request, dirPath, context) {
81
+ // Normalize path: remove leading/trailing slashes for consistency, except for root
82
+ let pathSegment = dirPath.trim();
83
+ // Explicitly handle root path variations ('', '/') by setting pathSegment to empty.
84
+ // This ensures that the final URL constructed later will be '/vault/', which the API
85
+ // uses to list the root directory contents.
86
+ if (pathSegment === "" || pathSegment === "/") {
87
+ pathSegment = ""; // Use empty string to signify root for URL construction
88
+ }
89
+ else {
90
+ // For non-root paths:
91
+ // 1. Remove any leading/trailing slashes to prevent issues like '/vault//path/' or '/vault/path//'.
92
+ // 2. URI-encode *each component* of the remaining path segment to handle special characters safely.
93
+ pathSegment = pathSegment
94
+ .replace(/^\/+|\/+$/g, "")
95
+ .split("/")
96
+ .map(encodeURIComponent)
97
+ .join("/");
98
+ }
99
+ // Construct the final URL for the API request:
100
+ // - If pathSegment is not empty (i.e., it's a specific directory), format as '/vault/{encoded_path}/'.
101
+ // - If pathSegment IS empty (signifying the root), format as '/vault/'.
102
+ // The trailing slash is important for directory listing endpoints in this API.
103
+ const url = pathSegment ? `/vault/${pathSegment}/` : "/vault/";
104
+ const response = await _request({
105
+ method: "GET",
106
+ url: url, // Use the correctly constructed URL
107
+ }, context, "listFiles");
108
+ return response.files;
109
+ }
110
+ /**
111
+ * Gets the metadata (stat) of a specific file using a lightweight HEAD request.
112
+ * @param _request - The internal request function from the service instance.
113
+ * @param filePath - Vault-relative path to the file.
114
+ * @param context - Request context.
115
+ * @returns The file's metadata.
116
+ */
117
+ export async function getFileMetadata(_request, filePath, context) {
118
+ const encodedPath = encodeVaultPath(filePath);
119
+ try {
120
+ const response = await _request({
121
+ method: "HEAD",
122
+ url: `/vault${encodedPath}`,
123
+ }, context, "getFileMetadata");
124
+ if (response && response.headers) {
125
+ const headers = response.headers;
126
+ return {
127
+ mtime: headers["x-obsidian-mtime"]
128
+ ? parseFloat(headers["x-obsidian-mtime"]) * 1000
129
+ : 0,
130
+ ctime: headers["x-obsidian-ctime"]
131
+ ? parseFloat(headers["x-obsidian-ctime"]) * 1000
132
+ : 0,
133
+ size: headers["content-length"]
134
+ ? parseInt(headers["content-length"], 10)
135
+ : 0,
136
+ };
137
+ }
138
+ return null;
139
+ }
140
+ catch (error) {
141
+ // Errors are already logged by the _request function, so we can just return null
142
+ return null;
143
+ }
144
+ }