next-routes-readme
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -15,3 +15,5 @@ #!/usr/bin/env node | ||
const folderPath = process.argv[2]; | ||
fs_1.default.writeFileSync(constants_1.constants.markdownFilename, ''); | ||
async function main() { | ||
let currentFileNumber = 1; | ||
await (0, traverseDictory_1.traverseDirectory)(folderPath, async (file) => { | ||
@@ -21,6 +23,5 @@ try { | ||
file.endsWith(`${constants_1.constants.routeFilename}.js`)) { | ||
const handlers = (0, parser_1.parseRouteHandlers)(file); | ||
fs_1.default.writeFileSync(constants_1.constants.markdownFilename, ''); | ||
for (const handler of handlers) { | ||
return await (0, generateMarkdown_1.generateMarkdownOutput)([handler], file); | ||
const currentHandler = (0, parser_1.parseRouteHandlers)(file); | ||
if (currentHandler) { | ||
return await (0, generateMarkdown_1.generateMarkdownOutput)(currentHandler, file, currentFileNumber++); | ||
} | ||
@@ -27,0 +28,0 @@ } |
@@ -37,3 +37,2 @@ "use strict"; | ||
const content = fs_1.default.readFileSync(routeFile, 'utf-8'); | ||
const handlers = []; | ||
const dependencies = []; | ||
@@ -47,6 +46,11 @@ let currentHandler = null; | ||
enter(path) { | ||
var _a, _b; | ||
var _a, _b, _c; | ||
if (t.isImportDeclaration(path.node)) { | ||
dependencies.push(path.toString()); | ||
} | ||
// Extract dynamic route parameter from the file name | ||
const fileNameParts = routeFile.split('/').filter(Boolean); | ||
const fileName = fileNameParts[fileNameParts.length - 2]; | ||
const routeParamMatch = fileName.match(/^\[([^]+)\]$/); | ||
const routeParam = routeParamMatch ? routeParamMatch[1] : ''; | ||
if (t.isFunctionDeclaration(path.node) && path.node.async) { | ||
@@ -63,3 +67,3 @@ const { name } = path.node.id; | ||
name, | ||
method: 'GET', | ||
method: ((_a = path.node.id) === null || _a === void 0 ? void 0 : _a.name) || '', | ||
doc: { | ||
@@ -70,2 +74,4 @@ variables: [], | ||
comments: [], | ||
queryParams: [], | ||
routeParams: routeParam ? [routeParam] : [], | ||
}, | ||
@@ -77,3 +83,6 @@ dependencies, | ||
const value = content.substring(comment.start, comment.end); | ||
currentHandler.doc.comments.push({ value, line: comment.loc.start.line }); | ||
currentHandler.doc.comments.push({ | ||
value, | ||
line: comment.loc.start.line, | ||
}); | ||
}); | ||
@@ -83,3 +92,13 @@ } | ||
if (currentHandler) { | ||
const currentLineNumber = ((_a = path.node.loc) === null || _a === void 0 ? void 0 : _a.start.line) || 0; | ||
const currentLineNumber = ((_b = path.node.loc) === null || _b === void 0 ? void 0 : _b.start.line) || 0; | ||
if (t.isCallExpression(path.node) && | ||
t.isMemberExpression(path.node.callee) && | ||
t.isIdentifier(path.node.callee.property) && | ||
path.node.callee.property.name === 'get' && | ||
t.isStringLiteral(path.node.arguments[0])) { | ||
const [queryParam] = path.node.arguments; | ||
if (queryParam) { | ||
currentHandler.doc.queryParams.push(queryParam.value); | ||
} | ||
} | ||
if (t.isVariableDeclaration(path.node)) { | ||
@@ -101,3 +120,3 @@ const value = content.substring(path.node.start, path.node.end); | ||
const value = content.substring(path.node.start, path.node.end); | ||
const currentLineNumber = ((_b = path.node.loc) === null || _b === void 0 ? void 0 : _b.start.line) || 0; | ||
const currentLineNumber = ((_c = path.node.loc) === null || _c === void 0 ? void 0 : _c.start.line) || 0; | ||
currentHandler.doc.errors.push({ value, line: currentLineNumber }); | ||
@@ -117,3 +136,6 @@ } | ||
const value = content.substring(comment.start, comment.end); | ||
currentHandler.doc.comments.push({ value, line: comment.loc.start.line }); | ||
currentHandler.doc.comments.push({ | ||
value, | ||
line: comment.loc.start.line, | ||
}); | ||
}); | ||
@@ -151,8 +173,5 @@ } | ||
}); | ||
if (currentHandler) { | ||
handlers.push(currentHandler); | ||
} | ||
return handlers; | ||
return currentHandler; | ||
} | ||
exports.parseRouteHandlers = parseRouteHandlers; | ||
//# sourceMappingURL=parser.js.map |
@@ -8,15 +8,13 @@ "use strict"; | ||
const fs_1 = __importDefault(require("fs")); | ||
async function generateMarkdownOutput(handlers, file) { | ||
const markdownOutput = handlers | ||
.map(handler => { | ||
return ` | ||
async function generateMarkdownOutput(currentHandler, file, index) { | ||
const markdownOutput = ` | ||
--- | ||
# Route: [${file}](${file}) | ||
# Route ${index}: [${file}](${file}) | ||
**Implementation**: \`${handler.implementation}\` | ||
**HTTP Method**: ${handler.method} | ||
**Implementation**: \`${currentHandler.implementation}\` | ||
**HTTP Method**: ${currentHandler.method} | ||
**Documentation**: | ||
\`\`\`json | ||
${JSON.stringify(handler.doc, null, 2)} | ||
${JSON.stringify(currentHandler.doc, null, 2)} | ||
\`\`\` | ||
@@ -26,8 +24,6 @@ | ||
\`\`\`json | ||
${JSON.stringify(handler.dependencies, null, 2)} | ||
${JSON.stringify(currentHandler.dependencies, null, 2)} | ||
\`\`\` | ||
`; | ||
}) | ||
.join(''); | ||
fs_1.default.appendFileSync('ROUTES.md', markdownOutput); | ||
@@ -34,0 +30,0 @@ } |
@@ -12,2 +12,5 @@ "use strict"; | ||
const files = await promises_1.default.readdir(directory); | ||
if (files.length === 0) { | ||
return Promise.reject(Error(`There are no Next.js Route files for path ${directory}.`)); | ||
} | ||
for (const file of files) { | ||
@@ -20,6 +23,5 @@ const filePath = path_1.default.join(directory, file); | ||
else { | ||
return fileCallback(filePath, parentFolderName); | ||
await fileCallback(filePath, parentFolderName); | ||
} | ||
} | ||
return Promise.reject(Error(`There are no Next.js Route files for path ${directory}.`)); | ||
} | ||
@@ -26,0 +28,0 @@ catch (error) { |
{ | ||
"name": "next-routes-readme", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "Creates README badges from istanbul coverage report", | ||
@@ -5,0 +5,0 @@ "type": "commonjs", |
# Next Routes Readme | ||
> Creates README documentation for Next API Routes (Next 13+) | ||
Next Routes Readme is a cool utility that helps generate short and brief markdown documentation for Next App Routes (Next 13+). It automates the process of extracting information from your App Route files and creates well-structured markdown documentation. | ||
## Features | ||
- Automatically extracts information from API route files. | ||
- Generates markdown documentation with details about each API route. | ||
- Provides information on HTTP methods, route implementations, dependencies, and more. | ||
## Installation | ||
```bash | ||
npm install next-routes-readme | ||
``` | ||
## Usage | ||
```bash | ||
npx next-routes-readme <path-to-your-app-folder> | ||
``` | ||
> https://nextjs.org/docs/app | ||
Replace `<path-to-api-folder>` with the path to the folder containing your API routes. | ||
## Example Output | ||
Here is an example of the generated documentation for an API route: | ||
```markdown | ||
--- | ||
# Route: [examples/app/route.ts](examples/app/route.ts) | ||
**Implementation**: `async function POST(request: NextRequest)` | ||
**HTTP Method**: GET | ||
**Documentation**: | ||
```json | ||
{ | ||
"variables": [ | ||
{ | ||
"value": "const {\n severity,\n message,\n functionName,\n gcpProject,\n serviceAccountEmail,\n serviceAccountKey,\n env,\n } = await request.json();", | ||
"line": 5 | ||
} | ||
], | ||
"conditionals": [ | ||
{ | ||
"value": "if (\n !severity ||\n !message ||\n !functionName ||\n !gcpProject ||\n !serviceAccountEmail ||\n !serviceAccountKey ||\n !env\n ) {\n return NextResponse.json({ error: 'Invalid Request' }, { status: 500 });\n }", | ||
"line": 15 | ||
} | ||
], | ||
"errors": [ | ||
{ | ||
"value": "NextResponse.json({ error: 'Invalid Request' }, { status: 500 })", | ||
"line": 24 | ||
}, | ||
{ | ||
"value": "catch (error) {\n console.error('Error logging to GCP:', error);\n return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });\n }", | ||
"line": 31 | ||
}, | ||
{ | ||
"value": "NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })", | ||
"line": 33 | ||
} | ||
], | ||
"comments": [ | ||
{ | ||
"value": "// eslint-disable-next-line @typescript-eslint/no-explicit-any", | ||
"line": 27 | ||
} | ||
] | ||
} | ||
``` | ||
## Contributing | ||
If you'd like to contribute to this project, please follow these steps: | ||
1. Fork the repository. | ||
2. Create a new branch for your feature or bug fix. | ||
3. Make your changes and commit them. | ||
4. Push your changes to your fork. | ||
5. Submit a pull request. | ||
## License | ||
This project is licensed under the [MIT License](LICENSE). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
29456
293
89