Skip to content

Commit

Permalink
Merge pull request #51 from Psychopoulet/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Psychopoulet committed May 18, 2021
2 parents 4006ef0 + 200dff4 commit 24b5816
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js
node_js:
- "10"
- "12"
- "14"
install:
- "npm install -g typescript"
- "npm install"
script: "npm run-script ci"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2020, Sébastien Vidal
Copyright (c) 2021, Sébastien Vidal

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand Down
40 changes: 22 additions & 18 deletions lib/components/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ module.exports = class Server extends MediatorUser {
}).then((api) => {

this._log("info", "<= [" + req.validatedIp + "] " + JSON.stringify(api));
send(req, res, api, 200, this._cors);
send(req, res, 200, api, this._Descriptor.info.version, this._cors);

}).catch((err) => {

Expand All @@ -139,7 +139,7 @@ module.exports = class Server extends MediatorUser {
};

this._log("error", "<= " + JSON.stringify(result));
send(req, res, result, 500, this._cors);
send(req, res, 500, result, this._Descriptor.info.version, this._cors);

});

Expand All @@ -152,7 +152,7 @@ module.exports = class Server extends MediatorUser {
const status = initialized ? "INITIALIZED" : "ENABLED";

this._log("info", "<= [" + req.validatedIp + "] " + status);
send(req, res, status, 200, this._cors);
send(req, res, 200, status, this._Descriptor.info.version, this._cors);

}

Expand All @@ -165,7 +165,7 @@ module.exports = class Server extends MediatorUser {
};

this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
send(req, res, result, 501, this._cors);
send(req, res, 501, result, this._Descriptor.info.version, this._cors);

}

Expand All @@ -178,7 +178,7 @@ module.exports = class Server extends MediatorUser {
};

this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
send(req, res, result, 501, this._cors);
send(req, res, 501, result, this._Descriptor.info.version, this._cors);

}

Expand All @@ -193,7 +193,7 @@ module.exports = class Server extends MediatorUser {
};

this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
send(req, res, result, 411, this._cors);
send(req, res, 411, result, this._Descriptor.info.version, this._cors);

}

Expand Down Expand Up @@ -363,23 +363,27 @@ module.exports = class Server extends MediatorUser {
// created
if ("put" === req.method) {

this._log("success", "<= [" + req.validatedIp + "] " + (
"undefined" === typeof content || null === content ? content : "no content"
));
if ("undefined" === typeof content || null === content) {
this._log("success", "<= [" + req.validatedIp + "] no content");
send(req, res, 201, undefined, this._Descriptor.info.version, this._cors);
}

send(req, res, content, 201, this._cors);
else {
this._log("success", "<= [" + req.validatedIp + "] " + JSON.stringify(content));
send(req, res, 201, content, this._Descriptor.info.version, this._cors);
}

}

// no content
else if ("undefined" === typeof content || null === content) {
this._log("warning", "<= [" + req.validatedIp + "] no content");
send(req, res, undefined, 204, this._cors);
send(req, res, 204, undefined, this._Descriptor.info.version, this._cors);
}

else {
this._log("success", "<= [" + req.validatedIp + "] " + JSON.stringify(content));
send(req, res, content, 200, this._cors);
send(req, res, 200, content, this._Descriptor.info.version, this._cors);
}

}).catch((err) => {
Expand All @@ -392,7 +396,7 @@ module.exports = class Server extends MediatorUser {
};

this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
send(req, res, result, 400, this._cors);
send(req, res, 400, result, this._Descriptor.info.version, this._cors);

}
else if (err instanceof TypeError) {
Expand All @@ -403,7 +407,7 @@ module.exports = class Server extends MediatorUser {
};

this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
send(req, res, result, 400, this._cors);
send(req, res, 400, result, this._Descriptor.info.version, this._cors);

}
else if (err instanceof RangeError) {
Expand All @@ -414,7 +418,7 @@ module.exports = class Server extends MediatorUser {
};

this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
send(req, res, result, 400, this._cors);
send(req, res, 400, result, this._Descriptor.info.version, this._cors);

}
else if (err instanceof SyntaxError) {
Expand All @@ -425,7 +429,7 @@ module.exports = class Server extends MediatorUser {
};

this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
send(req, res, result, 400, this._cors);
send(req, res, 400, result, this._Descriptor.info.version, this._cors);

}
else {
Expand All @@ -436,7 +440,7 @@ module.exports = class Server extends MediatorUser {
};

this._log("error", "<= [" + req.validatedIp + "] " + JSON.stringify(result));
send(req, res, result, 500, this._cors);
send(req, res, 500, result, this._Descriptor.info.version, this._cors);

}

Expand All @@ -456,7 +460,7 @@ module.exports = class Server extends MediatorUser {
};

this._log("error", "<= " + JSON.stringify(result));
send(req, res, result, 500, this._cors);
send(req, res, 500, result, this._Descriptor.info.version, this._cors);

});

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/file/readJSONFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function readJSONFile (file) {

return new Promise((resolve, reject) => {

readFile(file, (err, content) => {
readFile(file, "utf8", (err, content) => {
return err ? reject(err) : resolve(content);
});

Expand Down
5 changes: 3 additions & 2 deletions lib/utils/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

// module

module.exports = function send (req, res, content, code, cors = false) {
module.exports = function send (req, res, code, content, apiVersion, cors) {

const result = "undefined" !== typeof content ? JSON.stringify(content) : "";

res.writeHead(code,
Object.assign({
"Content-Type": "application/json; charset=utf-8",
"Content-Length": Buffer.byteLength(result),
"Status-Code-Url-Cat": "https://http.cat/" + code
"Status-Code-Url-Cat": "https://http.cat/" + code,
"API-Version": apiVersion
}, cors ? {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
Expand Down
24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-pluginsmanager-plugin",
"version": "4.3.1",
"version": "4.4.0",
"description": "An abstract parent plugin for node-pluginsmanager",
"main": "lib/main.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -36,25 +36,27 @@
},
"dependencies": {
"@apidevtools/swagger-parser": "10.0.2",
"express-openapi-validate": "0.5.1"
"express-openapi-validate": "0.6.1"
},
"devDependencies": {
"@types/node": "14.14.6",
"@types/ws": "7.2.9",
"@types/socket.io": "2.1.11",
"@types/node": "15.3.0",
"@types/socket.io": "3.0.2",
"@types/ws": "7.4.4",
"check-version-modules": "1.3.0",
"coveralls": "3.1.0",
"colors": "1.4.0",
"eslint": "7.12.1",
"eslint": "7.26.0",
"express": "4.17.1",
"husky": "4.3.0",
"mocha": "8.2.0",
"husky": "6.0.0",
"mocha": "8.4.0",
"nyc": "15.1.0",
"socket.io": "2.3.0",
"ws": "7.3.1"
"socket.io": "4.1.2",
"socket.io-client": "4.1.2",
"typescript": "4.2.4",
"ws": "7.4.5"
},
"homepage": "https://github.com/Psychopoulet/node-pluginsmanager-plugin#readme",
"engines": {
"node": ">=6.0.0"
"node": ">=10.0.0"
}
}
2 changes: 1 addition & 1 deletion test/0_compilation_typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("compilation typescript", () => {

it("should compile typescript file", (done) => {

exec("tsc " + join(__dirname, "typescript", "compilation.ts") + " --target ES2015", {
exec("npx tsc " + join(__dirname, "typescript", "compilation.ts"), {
"cwd": join(__dirname, ".."),
"windowsHide": true
}, (err) => {
Expand Down
2 changes: 1 addition & 1 deletion test/utils/DescriptorUser/Descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openapi": "3.0.2",
"info": {
"title": "node-pluginsmanager-plugin",
"version": "4.3.1",
"version": "4.4.0",
"description": "An abstract parent plugin for node-pluginsmanager",
"contact": {
"name": "Sébastien VIDAL",
Expand Down
4 changes: 2 additions & 2 deletions test/utils/Server/checkBodyBasics.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function checkBodyBasics (URL_API) {

return httpRequestTest(URL_API + "/create?url-param=ok", "put", null, 400, "Bad Request", {
"code": "MISSING_PARAMETER",
"message": "Error while validating request: request.body should have required property 'body-param'"
"message": "Error while validating request: request/body must have required property 'body-param'"
});

});
Expand All @@ -31,7 +31,7 @@ module.exports = function checkBodyBasics (URL_API) {
"body-param": false
}, 400, "Bad Request", {
"code": "WRONG_TYPE_PARAMETER",
"message": "Error while validating request: request.body['body-param'] should be string"
"message": "Error while validating request: request/body/body-param must be string"
});

});
Expand Down
Loading

0 comments on commit 24b5816

Please sign in to comment.