nextjs-routes-docs
Advanced tools
Comparing version
128
index.js
@@ -11,2 +11,31 @@ #!/usr/bin/env node | ||
function extractMethodFromIfStmt(ifStmt, reqVariableName) { | ||
if (ifStmt.type === "BinaryExpression") { | ||
let literal, variable; | ||
if (ifStmt.left.type === "Literal") { | ||
literal = ifStmt.left; | ||
variable = ifStmt.right; | ||
} else { | ||
literal = ifStmt.right; | ||
variable = ifStmt.left; | ||
} | ||
if ( | ||
variable.object.name === reqVariableName && | ||
variable.property.name === "method" | ||
) | ||
return literal.value; | ||
} else { | ||
if (ifStmt.left) { | ||
const left = extractMethodFromIfStmt(ifStmt.left, reqVariableName); | ||
if (left) return left; | ||
} | ||
if (ifStmt.right) { | ||
const right = extractMethodFromIfStmt(ifStmt.right, reqVariableName); | ||
if (right) return right; | ||
} | ||
} | ||
return null; | ||
} | ||
function generateRoutes(dir, options) { | ||
@@ -31,4 +60,5 @@ const content = []; | ||
files.forEach((file) => { | ||
//todo: handle path that has \\ in folder/file name | ||
const path = | ||
"/" + file.split("\\pages\\")[1].split(".")[0].replaceAll("\\", "/"); | ||
"/" + file.replaceAll("\\", "/").split("/pages/")[1].split(".")[0]; | ||
if (text || outputBoth) { | ||
@@ -45,64 +75,62 @@ content.push(file); | ||
}); | ||
let reqName; | ||
let requestArgumentVariableName; | ||
recast.visit(ast, { | ||
visitExportDefaultDeclaration: function (mainFunction) { | ||
reqName = mainFunction.value.declaration.params[0].name; | ||
requestArgumentVariableName = | ||
mainFunction.value.declaration.params[0].name; | ||
recast.visit(mainFunction, { | ||
visitIfStatement: function (ifStmtPath) { | ||
// console.log(path.value.type); | ||
if (ifStmtPath.value.test.type === "BinaryExpression") { | ||
const left = ifStmtPath.value.test.left; | ||
if ( | ||
left.object.name === reqName && | ||
left.property.name === "method" | ||
) { | ||
const method = ifStmtPath.value.test.right.value; | ||
if (swagger || outputBoth) { | ||
spec.paths[path][method.toLowerCase()] = {}; | ||
} | ||
const method = extractMethodFromIfStmt( | ||
ifStmtPath.value.test, | ||
requestArgumentVariableName | ||
); | ||
if (method) { | ||
if (swagger || outputBoth) { | ||
spec.paths[path][method.toLowerCase()] = {}; | ||
} | ||
let routeDeclarationText = `${method.toUpperCase()} \t ${path}`; | ||
if (params) { | ||
const varDeclarations = ifStmtPath.value.consequent.body; | ||
varDeclarations.forEach((varDeclaration) => { | ||
if (varDeclaration.declarations) { | ||
varDeclaration.declarations.forEach((declaration) => { | ||
if (declaration.init.type === "MemberExpression") { | ||
if (declaration.init.object.name === reqName) { | ||
const propLocation = | ||
declaration.init.property.name; | ||
const propList = declaration.id.properties | ||
.map((prop) => prop.key.name) | ||
.join(", "); | ||
const propDisplay = | ||
propLocation === "body" | ||
? `{body: {${propList}}}` | ||
: `?${propList}`; | ||
let routeDeclarationText = `${method.toUpperCase()} \t ${path}`; | ||
if (params) { | ||
const varDeclarations = ifStmtPath.value.consequent.body; | ||
varDeclarations.forEach((varDeclaration) => { | ||
if (varDeclaration.declarations) { | ||
varDeclaration.declarations.forEach((declaration) => { | ||
if (declaration.init.type === "MemberExpression") { | ||
if ( | ||
declaration.init.object.name === | ||
requestArgumentVariableName | ||
) { | ||
const propLocation = | ||
declaration.init.property.name; | ||
const propList = declaration.id.properties | ||
.map((prop) => prop.key.name) | ||
.join(", "); | ||
const propDisplay = | ||
propLocation === "body" | ||
? `{body: {${propList}}}` | ||
: `?${propList}`; | ||
routeDeclarationText += ` \t ${propDisplay}`; | ||
routeDeclarationText += ` \t ${propDisplay}`; | ||
if (swagger || outputBoth) { | ||
if (swagger || outputBoth) { | ||
spec.paths[path][method.toLowerCase()][ | ||
"parameters" | ||
] = []; | ||
declaration.id.properties.forEach((prop) => { | ||
spec.paths[path][method.toLowerCase()][ | ||
"parameters" | ||
] = []; | ||
declaration.id.properties.forEach((prop) => { | ||
spec.paths[path][method.toLowerCase()][ | ||
"parameters" | ||
].push({ | ||
in: propLocation.toLowerCase(), | ||
name: prop.key.name, | ||
}); | ||
].push({ | ||
in: propLocation.toLowerCase(), | ||
name: prop.key.name, | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
content.push(routeDeclarationText); | ||
} else { | ||
this.traverse(path); | ||
} | ||
content.push(routeDeclarationText); | ||
} | ||
@@ -109,0 +137,0 @@ this.traverse(ifStmtPath); |
{ | ||
"name": "nextjs-routes-docs", | ||
"version": "0.0.42", | ||
"version": "0.0.50", | ||
"description": "Generate docs for nextjs routes", | ||
@@ -5,0 +5,0 @@ "bin": { |
11938
22.52%5
66.67%183
16.56%