Loading...
Automatically generates or updates API documentation when endpoint files are modified
{
"hookConfig": {
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/api-endpoint-documentation-generator.sh",
"matchers": [
"write",
"edit"
]
}
}
},
"scriptContent": "#!/usr/bin/env bash\n\n# Read the tool input from stdin\nINPUT=$(cat)\nTOOL_NAME=$(echo \"$INPUT\" | jq -r '.tool_name')\nFILE_PATH=$(echo \"$INPUT\" | jq -r '.tool_input.file_path // .tool_input.path // \"\"')\n\nif [ -z \"$FILE_PATH\" ]; then\n exit 0\nfi\n\n# Check if it's an API-related file\nif [[ \"$FILE_PATH\" == *routes/* ]] || [[ \"$FILE_PATH\" == *controllers/* ]] || [[ \"$FILE_PATH\" == *api/* ]]; then\n echo \"📚 Generating API documentation for $FILE_PATH...\"\n \n # Get file extension\n EXT=\"${FILE_PATH##*.}\"\n \n case \"$EXT\" in\n js|jsx|ts|tsx)\n # JavaScript/TypeScript API files\n if command -v swagger-jsdoc &> /dev/null && [ -f \"swaggerDef.js\" ]; then\n echo \"Generating Swagger documentation...\"\n npx swagger-jsdoc -d swaggerDef.js -o ./docs/api.json \"$FILE_PATH\" 2>/dev/null\n echo \"✅ Swagger documentation updated\" >&2\n elif command -v npx &> /dev/null; then\n echo \"Generating JSDoc documentation...\"\n npx jsdoc \"$FILE_PATH\" -d ./docs/api/ 2>/dev/null\n echo \"✅ JSDoc documentation updated\" >&2\n fi\n ;;\n py)\n # Python API files\n if command -v pydoc &> /dev/null; then\n echo \"Generating Python API documentation...\"\n python -m pydoc -w \"$FILE_PATH\" 2>/dev/null\n echo \"✅ Python documentation updated\" >&2\n fi\n ;;\n esac\nelse\n echo \"File not in API directory, skipping documentation generation\" >&2\nfi\n\nexit 0"
}.claude/hooks/~/.claude/hooks/{
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/api-endpoint-documentation-generator.sh",
"matchers": [
"write",
"edit"
]
}
}
}#!/usr/bin/env bash
# Read the tool input from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [ -z "$FILE_PATH" ]; then
exit 0
fi
# Check if it's an API-related file
if [[ "$FILE_PATH" == *routes/* ]] || [[ "$FILE_PATH" == *controllers/* ]] || [[ "$FILE_PATH" == *api/* ]]; then
echo "📚 Generating API documentation for $FILE_PATH..."
# Get file extension
EXT="${FILE_PATH##*.}"
case "$EXT" in
js|jsx|ts|tsx)
# JavaScript/TypeScript API files
if command -v swagger-jsdoc &> /dev/null && [ -f "swaggerDef.js" ]; then
echo "Generating Swagger documentation..."
npx swagger-jsdoc -d swaggerDef.js -o ./docs/api.json "$FILE_PATH" 2>/dev/null
echo "✅ Swagger documentation updated" >&2
elif command -v npx &> /dev/null; then
echo "Generating JSDoc documentation..."
npx jsdoc "$FILE_PATH" -d ./docs/api/ 2>/dev/null
echo "✅ JSDoc documentation updated" >&2
fi
;;
py)
# Python API files
if command -v pydoc &> /dev/null; then
echo "Generating Python API documentation..."
python -m pydoc -w "$FILE_PATH" 2>/dev/null
echo "✅ Python documentation updated" >&2
fi
;;
esac
else
echo "File not in API directory, skipping documentation generation" >&2
fi
exit 0Hook triggers but file path pattern match fails
Debug path detection: echo "$FILE_PATH" | grep -E 'routes|controllers|api'. Verify directory structure matches expected patterns. Add custom paths to case statement.
swagger-jsdoc command not found during execution
Install dependencies: npm i -D swagger-jsdoc. Verify swaggerDef.js exists in project root. Check node_modules/.bin is in PATH. Use npx for local binaries.
Documentation generates but writes to wrong location
Create docs/api directory: mkdir -p ./docs/api. Verify write permissions on output paths. Check current working directory in hook context: pwd >&2.
PostToolUse fires on all edits not just API files
Enhance path filtering in script. Use stricter regex: [[ "$FILE_PATH" =~ (routes|api)/.*\.(ts|js)$ ]]. Add early exit for non-matching paths.
Hook processes file but npx commands timeout silently
Remove 2>/dev/null to see actual errors. Add timeout wrapper: timeout 30s npx command. Check for hanging processes: ps aux | grep jsdoc. Verify npm registry access.
Loading reviews...
Join our community of Claude power users. No spam, unsubscribe anytime.
Automated accessibility testing and compliance checking for web applications following WCAG guidelines
Automatically formats code files after Claude writes or edits them using Prettier, Black, or other formatters
Automatically creates timestamped backups of files before modification to prevent data loss