Commit 13977aea authored by Alex Quispe's avatar Alex Quispe
Browse files

Corrección de errores y mensajes por consola optimizados.

parent 3b1d6433
......@@ -72,7 +72,7 @@ class ApidocGenerator {
for (let i in filesName) {
const fileName = `${filesName[i]}.js`
const filePath = path.resolve(ApidocGenerator.SRC_PATH, fileName)
console.log(`\n Procesando archivo "${filePath.replace(process.cwd(), '')}" ...\n`)
console.log(`\n Procesando archivo "${filePath.replace(process.cwd(), '')}" ...`)
const dirname = path.dirname(filePath)
const outputDirPath = path.resolve(ApidocGenerator.BUILD_PATH, `routes/${fileName}`)
if(util.isDir(outputDirPath)) util.rmdir(outputDirPath)
......@@ -82,8 +82,9 @@ class ApidocGenerator {
try {
await require(filePath)()
} catch (e) {
console.log(` \x1b[31mError:\x1b[0m Archivo "${filePath.replace(process.cwd(), '')}"\n`)
console.log(e)
archivosConError.push(filePath)
console.log(` \x1b[31mError:\x1b[0m ${e.message}`)
}
}
......@@ -98,9 +99,9 @@ class ApidocGenerator {
if (archivosConError.length > 0) {
console.log('\n \x1b[32minfo:\x1b[0m Apidoc generado <<< SIN DATOS >>> :)\n')
console.log('Los siguientes archivos, no pudieron ser procesados:\n')
console.log(' Los siguientes archivos, no pudieron ser procesados:\n')
console.log(archivosConError)
console.log('\n Verifique que el servicio a documentar se encuentre activo.')
console.log('\n Verifique que el servicio a documentar se encuentre activo.\n')
return
}
......@@ -170,7 +171,7 @@ class ApidocGenerator {
this.route.name = this.route.name || `[${this.route.method}] ${_toWords(this.route.path)}${this.route.key ? ` (${this.route.key})` : ''}`
this.route.description = this.route.description || this.route.name
this.route.input = toFieldGroup(this.route.inputData || {})
this.route.outputData = (this.route.request) ? await _request(this.route.method, this.route.requestPath, this.route.inputData.body, this.route.inputData.headers) : {}
this.route.outputData = (this.route.request) ? await _request(this.route) : {}
this.route.output = toFieldGroup(this.route.outputData || {})
let apidocContent = this._getApidocContent()
let swaggerContent = this._getSwaggerContent()
......@@ -183,7 +184,7 @@ class ApidocGenerator {
console.log(` \x1b[33m${_.padEnd(`[${route.method}]`, 9)} ${route.path} - ${route.group} (${route.name}) \u2715 Ya ha sido documentado\x1b[0m`)
} else {
apidocContent += newBlock
console.log(` ${_.padEnd(`[${this.route.method}]`, 9)} ${this.route.path} - ${this.route.group} (${this.route.name}) \u2713`)
// console.log(` ${_.padEnd(`[${this.route.method}]`, 9)} ${this.route.path} - ${this.route.group} (${this.route.name}) \u2713`)
}
// SWAGER
......@@ -254,7 +255,7 @@ class ApidocGenerator {
this.route.name = this.route.name || `[${this.route.method}] ${_toWords(this.route.path)}${this.route.key ? ` (${this.route.key})` : ''}`
this.route.description = this.route.description || this.route.name
this.route.input = toFieldGroup(this.route.inputData || {})
this.route.outputData = await _request(this.route.method, this.route.requestPath, this.route.inputData.body, this.route.inputData.headers)
this.route.outputData = await _request(this.route)
this.route.output = toFieldGroup(this.route.outputData || {})
return this.route.outputData
}
......@@ -353,10 +354,10 @@ function getField (OBJ) {
return OBJ
}
function _request (method, uri, body, headers) {
const options = { method: method.toUpperCase(), uri: `${ApidocGenerator.API_URL}${uri}` }
if (body) { options.json = body }
if (headers) { options.headers = headers }
function _request (route) {
const options = { method: route.method.toUpperCase(), uri: `${ApidocGenerator.API_URL}${route.requestPath}` }
if (route.inputData.body) { options.json = route.inputData.body }
if (route.inputData.headers) { options.headers = route.inputData.headers }
return new Promise((resolve, reject) => {
return request(options, (error, response, body) => {
if (error) {
......@@ -365,10 +366,21 @@ function _request (method, uri, body, headers) {
}
return reject(error)
}
const CODE = response.statusCode
const CODE = response.statusCode
const STATUS = response.statusMessage
try { if (typeof body === 'string') body = JSON.parse(body) } catch (e) {}
if (CODE >= 200 && CODE < 300) { return resolve(body) }
return reject(new Error(`Hubo un error al procesar la petición [${method}] ${uri}\n\n${body}`))
console.log(`\n \x1b[31mError:\x1b[0m [${route.method}] ${route.path}\n`)
console.log('DATA:')
console.log(route.inputData)
console.log()
console.log('REQUEST:')
console.log(options)
console.log()
console.log(`RESPONSE (${CODE} ${STATUS}):`)
console.log(body)
console.log()
return reject(new Error(`La petición ha finalizado con un error.`))
})
})
}
......@@ -427,7 +439,7 @@ async function _build () {
}
async function _buildApidocJS () {
console.log(' Compilando ficheros ...\n');
console.log(' Publicando ficheros ...\n');
const templatePath = path.resolve(__dirname, '../template/apidocjs')
const templatePathSwagger = path.resolve(__dirname, '../template/swagger')
const tmpPath = path.resolve(__dirname, '../.temp')
......
......@@ -7,7 +7,8 @@
"start": "node index",
"doc:scaffold": "node documentation/scaffold",
"doc:generate": "node documentation/generate",
"doc:server": "node documentation/server"
"doc:server": "node documentation/server",
"doc:start": "npm run doc:scaffold && npm run doc:generate && npm run doc:server"
},
"author": "",
"license": "ISC"
......
const ApiGen = require('apidoc-generator')
const ApiGen = require('./../../')
module.exports = async () => {
// <!-- [ROUTE DEFINITION] --!> //
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment