Skip to content

feat: Add file rename functionality via PATCH /vault/{filepath} #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

duquesnay
Copy link

@duquesnay duquesnay commented Jul 8, 2025

Summary

adds file rename functionality to the Obsidian Local REST API

API Usage

Rename a file using PATCH with these headers:

curl -X PATCH https://localhost:27124/vault/path/to/file.md \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Operation: replace" \
  -H "Target-Type: file" \
  -H "Target: name" \
  -d "new-filename.md"

Implementation Details

  • API Endpoint: PATCH /vault/{filepath}
  • Headers Required:
    • Operation: replace (legacy compatibility)
    • Target-Type: file
    • Target: name
  • Request Body: New filename
  • Link Preservation: Uses app.fileManager.renameFile() to update internal vault references
  • Error Handling: Returns 404 for missing files, 409 for conflicts, 400 for validation errors

Commit Structure

This PR contains 5 commits extracted from git history:

  1. 3221a95 - fix: Add null check for Target header in PATCH requests
  2. 7bd91d8 - fix: improve error messages for PATCH operations
  3. 77eac7a - feat: Add file rename endpoint with PATCH /vault/{filepath}
  4. 4636d36 - test: Add tests for file rename operation
  5. b7702b3 - docs: Add OpenAPI documentation for file rename operation

Testing

Includes tests for:

  • Successful rename operation
  • Error handling for non-existent files
  • Conflict detection when destination exists
  • FileManager API integration via mocks

Files Modified

  • src/requestHandler.ts - Core implementation and fixes
  • src/requestHandler.test.ts - Test coverage
  • src/constants.ts - Error message improvements
  • docs/openapi.yaml - API documentation
  • docs/src/lib/patch.jsonnet - Documentation templates

duquesnay added 5 commits July 8, 2025 15:31
Prevents potential error when Target header is missing by providing empty string default
- MissingTargetTypeHeader: List all valid Target-Type options with descriptions
- InvalidTargetTypeHeader: Show available options when invalid type provided
- MissingTargetHeader: Explain expected Target values based on Target-Type
- MissingOperation: List all operations with context about when to use each
- InvalidOperation: Show valid operations with their specific use cases

These improved error messages guide users to correct API usage by providing
helpful context and listing all available options directly in the error response.
- Implemented rename operation using PATCH with Target-Type: file, Target: name
- Uses app.fileManager.renameFile() to preserve internal links
- Added comprehensive error handling for missing files and existing destinations
- Test successful rename with Target-Type: file, Target: name
- Test rename fails with non-existent file
- Test rename fails when destination already exists
- Mock fileManager.renameFile() to verify proper API usage
- Add rename operation to PATCH endpoint enum
- Add file Target-Type to support file operations
- Include example and description for file rename
- Documents Operation: rename, Target-Type: file usage
@duquesnay
Copy link
Author

Context: commits extracted from my personal fork
Thank you for any feedback as I'm trying to break down into granular commits and PRs a series of features originally done in a not-so-clean series of commits (file crud, directory crud, tags crud, links, advanced search).
I've been using these in an MCP client

@duquesnay duquesnay marked this pull request as ready for review July 8, 2025 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant