Skip to content

Commit

Permalink
feat: swagger docs & improvement tests (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosemaPereira authored Mar 28, 2024
1 parent 98dd068 commit 6add2f0
Show file tree
Hide file tree
Showing 31 changed files with 1,142 additions and 253 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
{
"devDependencies": true
}
],
"jsdoc/check-tag-names": [
"warn",
{
"definedTags": ["swagger"]
}
]
},
"settings": {
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/sonar-scan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
on:
# Trigger analysis when pushing to your main branches, and when creating a pull request.
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]

name: Sonnar Scan
jobs:
sonarqube:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '21.7.1' # Adjust to your desired Node.js version

- name: Install dependencies
run: npm install

- name: Run tests
run: npm test

- name: SonarQube Scan
uses: sonarsource/sonarqube-scan-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST }}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ dist/
.vscode/
.env
out/
docs/
docs/
report.json
test-report.xml
1 change: 0 additions & 1 deletion app/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import App from './server';

/**
* The port on which the server will listen.
* @type {number}
Expand Down
18 changes: 18 additions & 0 deletions app/internal/adapters/routes/auth.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,23 @@ import { AuthController } from '../../core/controllers';
export const AuthRoute = (app) => {
const router = express.Router();
app.use('/auth', router);

/**
* @swagger
* /auth/token:
* get:
* description: Returns token
* tags:
* - Auth
* responses:
* 200:
* description: A token
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/AuthTokenResponse'
* 500:
* $ref: '#/components/responses/InternalServerError'
*/
router.get('/token', AuthController.getToken);
};
17 changes: 17 additions & 0 deletions app/internal/adapters/routes/main.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,22 @@ import { MainController } from '../../core/controllers';
export const MainRoute = (app) => {
const router = express.Router();
app.use('/', router);
/**
* @swagger
* /health:
* get:
* description: Returns server health
* tags:
* - Main
* responses:
* 201:
* description: A User object
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/DefaultResponse'
* 500:
* $ref: '#/components/responses/InternalServerError'
*/
router.get('/health', MainController.healthCheck);
};
117 changes: 113 additions & 4 deletions app/internal/adapters/routes/users.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from 'express';
import { UsersController } from '../../core/controllers';

/**
* {
* Set up routes related to Users CRUD operations
* @namespace Users
* @function UsersRoute
Expand All @@ -12,12 +13,120 @@ export const UsersRoute = (app) => {
const router = express.Router();
app.use('/users', router);

// List by id
/**
* @swagger
* /users/{id}:
* get:
* description: Returns a user by id
* parameters:
* - in: path
* name: id
* schema:
* type: string
* required: true
* description: ID of the user to get
* security:
* - bearerAuth: []
* tags:
* - Users
* responses:
* 200:
* description: User info
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/DefaultResponse'
* 401:
* $ref: '#/components/responses/UnauthorizedError'
* 500:
* $ref: '#/components/responses/InternalServerError'
*/
router.get('/:id', UsersController.getById);
// Create a new user

/**
* @swagger
* /users:
* post:
* description: Create new user
* requestBody:
* description: User request body
* required: true
* content:
* application/json:
* schema:
* $ref: '#/definitions/User'
* tags:
* - Users
* responses:
* 200:
* description: A User response
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/UserResponse'
* 401:
* $ref: '#/components/responses/UnauthorizedError'
* 500:
* $ref: '#/components/responses/InternalServerError'
*/
router.post('/', UsersController.create);
// Edit User

/**
* @swagger
* /users/{id}:
* put:
* description: Update the user info by id
* parameters:
* - in: path
* name: id
* schema:
* type: string
* required: true
* description: ID of the user to update
* requestBody:
* description: User request body
* required: true
* content:
* application/json:
* schema:
* $ref: '#/definitions/User'
* tags:
* - Users
* responses:
* 201:
* description: User info
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/DefaultResponse'
* 401:
* $ref: '#/components/responses/UnauthorizedError'
* 500:
* $ref: '#/components/responses/InternalServerError'
*/
router.put('/:id', UsersController.update);
// Delete user

/**
* @swagger
* /users/{id}:
* delete:
* description: Delete an user by id
* parameters:
* - in: path
* name: id
* schema:
* type: string
* required: true
* description: ID of the user to delete
* tags:
* - Users
* responses:
* 200:
* $ref: '#/components/responses/Accepted'
* 401:
* $ref: '#/components/responses/UnauthorizedError'
* 500:
* $ref: '#/components/responses/InternalServerError'
*/
router.delete('/:id', UsersController.delete);
};
17 changes: 8 additions & 9 deletions app/internal/core/controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/** @module Controllers */
/** @module Controllers/Auth */
import { constants } from 'http2';
import { AuthServices } from '../services';

/** @typedef {import('express').Request} Request */
/** @typedef {import('express').Response} Response */
/** @typedef {import('express').NextFunction} NextFunction */

/**
* Callback function getting a token.
* @function
* @param {import('express').Request} req - Express request object.
* @param {import('express').Response} res - Express response object.
* @param {import('express').NextFunction} next - Express next middleware function.
* @param {Request} req - Express request object.
* @param {Response} res - Express response object.
* @param {NextFunction} next - Express next middleware function.
*/
const getToken = (req, res, next) => {
try {
Expand All @@ -22,11 +26,6 @@ const getToken = (req, res, next) => {
}
};

/**
* @namespace AuthController
* @memberof module:Controllers
* @constant
*/
export const AuthController = {
getToken,
};
16 changes: 8 additions & 8 deletions app/internal/core/controllers/main.controller.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/** @module Controllers */
/** @module Controllers/Main */

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

/** @typedef {import('express').Request} Request */
/** @typedef {import('express').Response} Response */
/** @typedef {import('express').NextFunction} NextFunction */

/**
* Callback function the health check.
* @function
* @param {import('express').Request} req - Express request object.
* @param {import('express').Response} res - Express response object.
* @param {import('express').NextFunction} next - Express next middleware function.
* @param {Request} req - Express request object.
* @param {Response} res - Express response object.
* @param {NextFunction} next - Express next middleware function.
*/
const healthCheck = (req, res, next) => {
try {
Expand All @@ -19,10 +23,6 @@ const healthCheck = (req, res, next) => {
}
};

/**
* @namespace MainController
* @constant
*/
export const MainController = {
healthCheck,
};
Loading

0 comments on commit 6add2f0

Please sign in to comment.