Skip to content

Commit 6add2f0

Browse files
feat: swagger docs & improvement tests (#13)
1 parent 98dd068 commit 6add2f0

31 files changed

+1142
-253
lines changed

.eslintrc.json

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
{
6969
"devDependencies": true
7070
}
71+
],
72+
"jsdoc/check-tag-names": [
73+
"warn",
74+
{
75+
"definedTags": ["swagger"]
76+
}
7177
]
7278
},
7379
"settings": {

.github/workflows/sonar-scan.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
on:
2+
# Trigger analysis when pushing to your main branches, and when creating a pull request.
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
types: [opened, synchronize, reopened]
8+
9+
name: Sonnar Scan
10+
jobs:
11+
sonarqube:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
with:
17+
fetch-depth: 0
18+
19+
- name: Setup Node.js
20+
uses: actions/setup-node@v3
21+
with:
22+
node-version: '21.7.1' # Adjust to your desired Node.js version
23+
24+
- name: Install dependencies
25+
run: npm install
26+
27+
- name: Run tests
28+
run: npm test
29+
30+
- name: SonarQube Scan
31+
uses: sonarsource/sonarqube-scan-action@master
32+
env:
33+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
34+
SONAR_HOST_URL: ${{ secrets.SONAR_HOST }}

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ dist/
44
.vscode/
55
.env
66
out/
7-
docs/
7+
docs/
8+
report.json
9+
test-report.xml

app/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import App from './server';
2-
32
/**
43
* The port on which the server will listen.
54
* @type {number}

app/internal/adapters/routes/auth.routes.js

+18
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,23 @@ import { AuthController } from '../../core/controllers';
1111
export const AuthRoute = (app) => {
1212
const router = express.Router();
1313
app.use('/auth', router);
14+
15+
/**
16+
* @swagger
17+
* /auth/token:
18+
* get:
19+
* description: Returns token
20+
* tags:
21+
* - Auth
22+
* responses:
23+
* 200:
24+
* description: A token
25+
* content:
26+
* application/json:
27+
* schema:
28+
* $ref: '#/components/schemas/AuthTokenResponse'
29+
* 500:
30+
* $ref: '#/components/responses/InternalServerError'
31+
*/
1432
router.get('/token', AuthController.getToken);
1533
};

app/internal/adapters/routes/main.routes.js

+17
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,22 @@ import { MainController } from '../../core/controllers';
1111
export const MainRoute = (app) => {
1212
const router = express.Router();
1313
app.use('/', router);
14+
/**
15+
* @swagger
16+
* /health:
17+
* get:
18+
* description: Returns server health
19+
* tags:
20+
* - Main
21+
* responses:
22+
* 201:
23+
* description: A User object
24+
* content:
25+
* application/json:
26+
* schema:
27+
* $ref: '#/components/schemas/DefaultResponse'
28+
* 500:
29+
* $ref: '#/components/responses/InternalServerError'
30+
*/
1431
router.get('/health', MainController.healthCheck);
1532
};

app/internal/adapters/routes/users.routes.js

+113-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express from 'express';
22
import { UsersController } from '../../core/controllers';
33

44
/**
5+
* {
56
* Set up routes related to Users CRUD operations
67
* @namespace Users
78
* @function UsersRoute
@@ -12,12 +13,120 @@ export const UsersRoute = (app) => {
1213
const router = express.Router();
1314
app.use('/users', router);
1415

15-
// List by id
16+
/**
17+
* @swagger
18+
* /users/{id}:
19+
* get:
20+
* description: Returns a user by id
21+
* parameters:
22+
* - in: path
23+
* name: id
24+
* schema:
25+
* type: string
26+
* required: true
27+
* description: ID of the user to get
28+
* security:
29+
* - bearerAuth: []
30+
* tags:
31+
* - Users
32+
* responses:
33+
* 200:
34+
* description: User info
35+
* content:
36+
* application/json:
37+
* schema:
38+
* $ref: '#/components/schemas/DefaultResponse'
39+
* 401:
40+
* $ref: '#/components/responses/UnauthorizedError'
41+
* 500:
42+
* $ref: '#/components/responses/InternalServerError'
43+
*/
1644
router.get('/:id', UsersController.getById);
17-
// Create a new user
45+
46+
/**
47+
* @swagger
48+
* /users:
49+
* post:
50+
* description: Create new user
51+
* requestBody:
52+
* description: User request body
53+
* required: true
54+
* content:
55+
* application/json:
56+
* schema:
57+
* $ref: '#/definitions/User'
58+
* tags:
59+
* - Users
60+
* responses:
61+
* 200:
62+
* description: A User response
63+
* content:
64+
* application/json:
65+
* schema:
66+
* $ref: '#/components/schemas/UserResponse'
67+
* 401:
68+
* $ref: '#/components/responses/UnauthorizedError'
69+
* 500:
70+
* $ref: '#/components/responses/InternalServerError'
71+
*/
1872
router.post('/', UsersController.create);
19-
// Edit User
73+
74+
/**
75+
* @swagger
76+
* /users/{id}:
77+
* put:
78+
* description: Update the user info by id
79+
* parameters:
80+
* - in: path
81+
* name: id
82+
* schema:
83+
* type: string
84+
* required: true
85+
* description: ID of the user to update
86+
* requestBody:
87+
* description: User request body
88+
* required: true
89+
* content:
90+
* application/json:
91+
* schema:
92+
* $ref: '#/definitions/User'
93+
* tags:
94+
* - Users
95+
* responses:
96+
* 201:
97+
* description: User info
98+
* content:
99+
* application/json:
100+
* schema:
101+
* $ref: '#/components/schemas/DefaultResponse'
102+
* 401:
103+
* $ref: '#/components/responses/UnauthorizedError'
104+
* 500:
105+
* $ref: '#/components/responses/InternalServerError'
106+
*/
20107
router.put('/:id', UsersController.update);
21-
// Delete user
108+
109+
/**
110+
* @swagger
111+
* /users/{id}:
112+
* delete:
113+
* description: Delete an user by id
114+
* parameters:
115+
* - in: path
116+
* name: id
117+
* schema:
118+
* type: string
119+
* required: true
120+
* description: ID of the user to delete
121+
* tags:
122+
* - Users
123+
* responses:
124+
* 200:
125+
* $ref: '#/components/responses/Accepted'
126+
* 401:
127+
* $ref: '#/components/responses/UnauthorizedError'
128+
* 500:
129+
* $ref: '#/components/responses/InternalServerError'
130+
*/
22131
router.delete('/:id', UsersController.delete);
23132
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
/** @module Controllers */
1+
/** @module Controllers/Auth */
22
import { constants } from 'http2';
33
import { AuthServices } from '../services';
44

5+
/** @typedef {import('express').Request} Request */
6+
/** @typedef {import('express').Response} Response */
7+
/** @typedef {import('express').NextFunction} NextFunction */
8+
59
/**
610
* Callback function getting a token.
711
* @function
8-
* @param {import('express').Request} req - Express request object.
9-
* @param {import('express').Response} res - Express response object.
10-
* @param {import('express').NextFunction} next - Express next middleware function.
12+
* @param {Request} req - Express request object.
13+
* @param {Response} res - Express response object.
14+
* @param {NextFunction} next - Express next middleware function.
1115
*/
1216
const getToken = (req, res, next) => {
1317
try {
@@ -22,11 +26,6 @@ const getToken = (req, res, next) => {
2226
}
2327
};
2428

25-
/**
26-
* @namespace AuthController
27-
* @memberof module:Controllers
28-
* @constant
29-
*/
3029
export const AuthController = {
3130
getToken,
3231
};
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
/** @module Controllers */
1+
/** @module Controllers/Main */
22

33
import { constants } from 'http2';
44
import { MainServices } from '../services';
55

6+
/** @typedef {import('express').Request} Request */
7+
/** @typedef {import('express').Response} Response */
8+
/** @typedef {import('express').NextFunction} NextFunction */
9+
610
/**
711
* Callback function the health check.
812
* @function
9-
* @param {import('express').Request} req - Express request object.
10-
* @param {import('express').Response} res - Express response object.
11-
* @param {import('express').NextFunction} next - Express next middleware function.
13+
* @param {Request} req - Express request object.
14+
* @param {Response} res - Express response object.
15+
* @param {NextFunction} next - Express next middleware function.
1216
*/
1317
const healthCheck = (req, res, next) => {
1418
try {
@@ -19,10 +23,6 @@ const healthCheck = (req, res, next) => {
1923
}
2024
};
2125

22-
/**
23-
* @namespace MainController
24-
* @constant
25-
*/
2626
export const MainController = {
2727
healthCheck,
2828
};

0 commit comments

Comments
 (0)