Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions app/src/commons/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,28 @@ class Helpers {
static msg(text, type = "") {
let log = "";
let msg = "";
const lon = (text.length < 90) ? (100 - text.length) : 0;
let auxText = typeof text === "object" ? JSON.stringify(text) : text;
const lon = (auxText.length < 90) ? (100 - auxText.length) : 0;
switch (type) {
case "e":
log = Colors.xterm(15).bgXterm(124).bold;
msg = " 🚨 ERROR ";
msg = " 🚨 ERROR ";
break;
case "s":
log = Colors.xterm(15).bgXterm(34).bold;
msg = " 🙌 SUCCESS ";
msg = " 🙌 SUCCESS ";
break;
case "w":
log = Colors.xterm(232).bgXterm(214).bold;
msg = " ⚠️ WARNING ";
msg = " ⚠️ WARNING ";
break;
default:
log = Colors.xterm(15).bgXterm(12).bold;
msg = " INFO ";
msg = " ℹ️ INFO ";
break;
}
for (let i = 0; i < lon; i += 1) { text += " "; }
console.log(log(` [${msg}] => ${text}`)); // eslint-disable-line
for (let i = 0; i < lon; i += 1) { auxText += " "; }
console.log(log(` [${msg}] => ${auxText}`)); // eslint-disable-line
}
}

Expand Down
10 changes: 7 additions & 3 deletions app/src/commons/MongoApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import PokemonModel from "Pokemon/Schema";
/* eslint-enable */

class MongoApi {
constructor({ args, domain, method, response, msg }) {
constructor({ args, domain, method, response, helpers }) {
this.domain = domain;
this.method = method;
this.params = args;
this.msg = msg;
this.msg = helpers.msg;
this.getTimeToLive = helpers.getTimeToLive;
this.response = response;
this.find = this.find.bind(this);
return (method !== "") ? this.action : this;
Expand Down Expand Up @@ -97,15 +98,18 @@ class MongoApi {
* @return {Void}.
*/
sendResult(error, data) {
const { response } = this;
const { getTimeToLive, method, response } = this;
const STATUS = error ? 404 : 200;
const SEND = error || data;
const ACTION = (typeof SEND === "object") ? "json" : "send";
let type = method;
if (STATUS === 200) {
this.msg("OK !", "s");
} else {
type = "error";
this.msg(SEND, "e");
}
getTimeToLive(response, 10800, `DB_${type}`);
response.status(STATUS);
response[ACTION](SEND);
}
Expand Down
29 changes: 5 additions & 24 deletions app/src/commons/Routers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,20 @@ const ServerRouter = (router, helpers) => {
// Consultando los metodos del Api(MongoDB)
Router.use("/:method?/:args?", (request, response) => {
const { params: { args = {}, method = "find" }, URL } = request;
helpers.getTimeToLive(response, 10800, `DB_${method}`);
const { msg, getTimeToLive } = helpers;
helpers.msg(`Lanzando el metodo ${method}`, "i");
const PARAMS = {
args,
domain: URL,
method,
response,
msg: helpers.msg,
helpers: {
getTimeToLive,
msg,
},
};
const API = new MongoApi(PARAMS);
});

// Pagina principal
Router.use((request, response) => {
const { db, originalUrl } = request;
let status = 404;
let type = "e";
let tag = "API_ERROR";
let data = { success: false, msg: "🚨 Not found 404" };

if (originalUrl === "/") {
status = 200;
type = "s";
tag = "API_ALL";
data = db;
}

helpers.getTimeToLive(response, 10800, tag);
helpers.msg("Respuesta recibida", type);
response.status(status);
response.json(data);
});

return Router;
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@
"test:app": "mocha",
"test:coverage": "nyc --reporter=html mocha"
},
"version": "1.6.0"
"version": "1.6.1"
}
6 changes: 3 additions & 3 deletions test/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ describe("📋 Testeando el servidor", () => {
it("1 .- Se tienen que listar todos los pokemons", (done) => {
Chai
.request(Server)
.get("/")
.get("/api")
.end((error, response) => {
response.should.have.status(200);
response.body.should.be.a("Array");
expect(response).to.have.header("Edge-Cache-Tag", "POKEMON_API_ALL");
expect(response).to.have.header("Edge-Cache-Tag", "POKEMON_API_FIND");
done();
});
});
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("📋 Testeando el servidor", () => {
response.should.have.status(404);
expect(response).to.be.json;
expect(body).to.have.nested.property("success");
expect(response).to.have.header("Edge-Cache-Tag", "POKEMON_API_ERROR");
expect(response).to.have.header("Edge-Cache-Tag", "POKEMON_DB_ERROR");
done();
});
});
Expand Down
Loading