Skip to content

Commit cc1c1f1

Browse files
Merge pull request #18 from renanlopescoder/swagger-api-doc
Feature(docs): add swagger documentation
2 parents 6cf409d + 7a60fa3 commit cc1c1f1

File tree

8 files changed

+166
-67
lines changed

8 files changed

+166
-67
lines changed

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node_api",
3-
"version": "3.1.5",
3+
"version": "3.2.0",
44
"description": "Node API is production ready and open source project in Node, Express, MongoDB",
55
"scripts": {
66
"start": "node server.js"
@@ -13,9 +13,11 @@
1313
"consign": "^0.1.6",
1414
"cors": "^2.8.3",
1515
"express": "^4.14.0",
16-
"jsonwebtoken": "^7.2.1",
17-
"mongoose": "5.9.10",
18-
"nodemailer": "6.4.6"
16+
"jsonwebtoken": "^8.5.1",
17+
"mongoose": "5.9.11",
18+
"nodemailer": "6.4.6",
19+
"swagger-jsdoc": "^4.0.0",
20+
"swagger-ui-express": "^4.1.4"
1921
},
2022
"engines": {
2123
"node": "14.0.0"

src/app/Controllers/AuthController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AuthController {
1414
if (!match) {
1515
res.status(401).send({ error: "error", message: "Password mismatch" });
1616
token = jwt.sign({ user_id: user._id }, secret, {
17-
expiresIn: "3h"
17+
expiresIn: "3h",
1818
});
1919
}
2020

@@ -24,7 +24,7 @@ class AuthController {
2424
username: user.username,
2525
photo: user.photo,
2626
email: user.email,
27-
token: token
27+
token: token,
2828
});
2929
} catch (error) {
3030
res

src/app/Models/Project.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
var mongoose = require('mongoose');
1+
const mongoose = require("mongoose");
22

3-
var schema = mongoose.Schema({
4-
project: {
5-
type: String,
6-
required: false
7-
},
8-
technologies: {
9-
type: String,
10-
required: false
11-
},
12-
description: {
13-
type: String,
14-
required: false
15-
},
16-
demoLink: {
17-
type: String,
18-
required: false
19-
},
20-
githubLink: {
21-
type: String,
22-
required: false
23-
},
24-
author: {
25-
type: String,
26-
required: false
27-
},
28-
authorLink: {
29-
type: String,
30-
required: false
31-
},
32-
status: {
33-
type: String,
34-
required: false
35-
}
3+
const schema = mongoose.Schema({
4+
project: {
5+
type: String,
6+
required: false,
7+
},
8+
technologies: {
9+
type: String,
10+
required: false,
11+
},
12+
description: {
13+
type: String,
14+
required: false,
15+
},
16+
demoLink: {
17+
type: String,
18+
required: false,
19+
},
20+
githubLink: {
21+
type: String,
22+
required: false,
23+
},
24+
author: {
25+
type: String,
26+
required: false,
27+
},
28+
authorLink: {
29+
type: String,
30+
required: false,
31+
},
32+
status: {
33+
type: String,
34+
required: false,
35+
},
3636
});
3737

38-
mongoose.model('Project', schema);
38+
mongoose.model("Project", schema);

src/app/Models/User.js

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,58 @@
1-
var mongoose = require('mongoose');
1+
/**
2+
* @swagger
3+
* components:
4+
* schemas:
5+
* User:
6+
* type: object
7+
* required:
8+
* - username
9+
* - email
10+
* - password
11+
* properties:
12+
* username:
13+
* type: string
14+
* email:
15+
* type: string
16+
* format: email
17+
* description: Email for the user, needs to be unique.
18+
* password:
19+
* type: string
20+
* photo:
21+
* type: string
22+
* description: Image URL string
23+
* nickname:
24+
* type: string
25+
* example:
26+
* username: Renan
27+
* email: fake@email.com
28+
* password: 123456aa
29+
* nickname: Dexter
30+
* photo: https://photourl.com/image.png
31+
*/
232

3-
var schema = mongoose.Schema({
4-
username: {
5-
type: String,
6-
required: true
7-
},
8-
password : {
9-
type: String,
10-
required: true
11-
},
12-
email : {
13-
type: String,
14-
required: true
15-
},
16-
photo : {
17-
type: String,
18-
required: false
19-
},
20-
nickname: {
21-
type: String,
22-
required: false
23-
}
33+
const mongoose = require("mongoose");
34+
35+
const schema = mongoose.Schema({
36+
username: {
37+
type: String,
38+
required: true,
39+
},
40+
password: {
41+
type: String,
42+
required: true,
43+
},
44+
email: {
45+
type: String,
46+
required: true,
47+
},
48+
photo: {
49+
type: String,
50+
required: false,
51+
},
52+
nickname: {
53+
type: String,
54+
required: false,
55+
},
2456
});
2557

26-
mongoose.model('User', schema);
58+
mongoose.model("User", schema);

src/config/express.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ const express = require("express");
22
const bodyParser = require("body-parser");
33
const cors = require("cors");
44
const consign = require("consign");
5+
const swaggerUi = require("swagger-ui-express");
6+
const swaggerJsdoc = require("swagger-jsdoc");
7+
8+
const swaggerOptions = require("../../swagger");
9+
510
const app = express();
611

712
app.use(cors({ origin: "*" }));
813
app.use(bodyParser.json());
914
app.set("secret", "opensecret");
1015

16+
const specs = swaggerJsdoc(swaggerOptions);
17+
app.use("/docs", swaggerUi.serve);
18+
app.get("/docs", swaggerUi.setup(specs, { explorer: true }));
19+
1120
consign({ cwd: process.cwd() + "/src" })
1221
.include("app/Models")
1322
.then("app/Controllers")

src/routes/project.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
module.exports = src => {
1+
module.exports = (src) => {
22
const { ProjectController } = src.app.Controllers;
3-
43
src.get("/projects", ProjectController.getAllProjects);
54
src.post("/projects/create", ProjectController.createProject);
65
src.put("/projects/update/:id", ProjectController.updateProject);

src/routes/user.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
1-
module.exports = src => {
1+
/**
2+
* @swagger
3+
* tags:
4+
* name: Users
5+
* description: User management
6+
*/
7+
8+
/**
9+
* @swagger
10+
* path:
11+
* /users/create:
12+
* post:
13+
* summary: Create a new user
14+
* tags: [Users]
15+
* requestBody:
16+
* required: true
17+
* content:
18+
* application/json:
19+
* schema:
20+
* $ref: '#/components/schemas/User'
21+
* responses:
22+
* "200":
23+
* description: A user schema
24+
* content:
25+
* application/json:
26+
* schema:
27+
* $ref: '#/components/schemas/User'
28+
*/
29+
30+
module.exports = (src) => {
231
const { UserController } = src.app.Controllers;
332

433
src.get("/users", UserController.getAllUsers);

swagger.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"swaggerDefinition": {
3+
"openapi": "3.0.0",
4+
"info": {
5+
"title": "Node Rest API",
6+
"version": "1.0.0",
7+
"description": "Node API is production ready and open source project in Node, Express, MongoDB",
8+
"license": {
9+
"name": "MIT",
10+
"url": "https://choosealicense.com/licenses/mit/"
11+
},
12+
"contact": {
13+
"name": "Renan Lopes",
14+
"url": "https://renanlopes.com",
15+
"email": "renanlopescoder@gmail.com"
16+
}
17+
},
18+
"servers": [
19+
{
20+
"url": "http://localhost:8080"
21+
},
22+
{
23+
"url": "https://rest-api-node.herokuapp.com"
24+
}
25+
]
26+
},
27+
"apis": ["./src/app/Models/User.js", "./src/routes/user.js"]
28+
}

0 commit comments

Comments
 (0)