Skip to content

Commit

Permalink
automate the boring stuff with swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
samayun committed Dec 20, 2021
1 parent 5e5bfc1 commit 1478d76
Show file tree
Hide file tree
Showing 27 changed files with 6,478 additions and 233 deletions.
3 changes: 3 additions & 0 deletions config/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.API_VERSION = process.env.API_VERSION || 'v1';

exports.API_ROUTE_PREFIX = process.env.API_ROUTE_PREFIX || '/api';
5 changes: 5 additions & 0 deletions config/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require('dotenv').config();
module.exports = {
host: process.env.HOST || '127.0.0.1',
port: process.env.PORT || 5000
};
16 changes: 16 additions & 0 deletions modules/__template__/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = '/v1/tests';
const router = require('express').Router();

module.exports = () => {
router.get('/', (req, res, next) => {
/* #swagger.tags = ['__template'] */
res.status(200).json({
message: 'TEmp Swagger Docs'
});
});

return {
path,
router
};
};
35 changes: 24 additions & 11 deletions modules/authentication/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,36 @@ const path = '/v1/auth';
const router = require('express').Router();

module.exports = () => {
// POST /api/auth/login
router.get('/test', (req, res, next) => {
/* #swagger.tags = ['Authentication']
#swagger.basePath = '/v1/auth'
#swagger.description = 'XXXXXXXXXXXX'
*/
res.status(200).json({
message: 'TEST Swagger Docs'
});
});
router.get('/hello2', (req, res, next) => {
/* #swagger.tags = ['Authentication']
#swagger.basePath = '/v1/auth'
#swagger.description = 'XXXXXXXXXXXX'
*/
res.status(200).json({
message: 'TEST Swagger Docs'
});
});
router.post('/login', loginValidator, async (req, res, next) => {
try {
/* #swagger.tags = ['User']
/* #swagger.basePath = 'auth'
#swagger.description = 'Endpoint to sign in a specific user' */
/* #swagger.tags = ['Authentication']
#swagger.basePath = '/v1/auth'
#swagger.description = 'Sign in a specific user'
/* #swagger.parameters['obj'] = {
#swagger.parameters['obj'] = {
in: 'body',
description: 'User information.',
required: true,
schema: { $ref: "#/definitions/AddUser" }
} */

/* #swagger.security = [{
"apiKeyAuth": []
}] */
try {
const user = await authService.login({
email: req.body.email,
password: req.body.password
Expand All @@ -53,7 +66,7 @@ module.exports = () => {
* { name : "Salman Akash", email: "[email protected], password: "123456"}
* */
router.post('/register', registerValidator, async (req, res, next) => {
/* #swagger.tags = ['User']
/* #swagger.tags = ['Authentication']
#swagger.description = 'Endpoint to sign up a specific user' */
try {
const user = await authService.register(req.body);
Expand Down
19 changes: 19 additions & 0 deletions modules/categories/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const path = '/v1/categories';
const router = require('express').Router();

module.exports = () => {
router.get('/hello', (req, res, next) => {
/* #swagger.tags = ['User']
#swagger.basePath = '/v1/auth'
#swagger.description = 'XXXXXXXXXXXX'
*/
res.status(200).json({
message: 'TEmp Swagger Docs'
});
});

return {
path,
router
};
};
8 changes: 8 additions & 0 deletions modules/categories/services/news.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const CrudService = require("../../../app/services/crud.service");

class NewsService extends CrudService {
constructor(model) {
super(model);
}
}
module.exports = (model) => new NewsService(model);
13 changes: 13 additions & 0 deletions modules/categories/validator/news.validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.createNewsValidator = async (req, res, next) => {
const { title, published, draft } = req.body;

if (!title) return next(new Error("Title is required 😢"));

if (published === undefined)
return next(new Error("Published is required 😢"));

if (draft === undefined) {
return next(new Error("Draft is required 😢"));
}
next();
};
12 changes: 7 additions & 5 deletions modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ const fs = require('fs');
const { resolve, join } = require('path');

module.exports = () => {
// let routes = [];
let routes = [];
const modulePath = resolve(`${__dirname}`);
const modules = fs.readdirSync(modulePath);
const routers = modules
.map(mod => {
const routePath = join(modulePath, mod);

// If only modules have routes_directory/index.js means it's RESTful route
// otherwise It may be utilities module or GraphQL based modules

if (fs.existsSync(routePath + '/routes/index.js')) {
routes.push(`${routePath}/routes/index.js`);
return require(routePath + '/routes/index')();
// routes.push(`${routePath}/routes/index.js`);
}
})
.filter(mod => !!mod);
return routers;

return {
routes,
routers
};
};
8 changes: 7 additions & 1 deletion modules/media/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ function sizeTheFile(fileSize) {
const routePath = '/v1/media';
const router = require('express').Router();

module.exports = routes => {
module.exports = () => {
router.get('/', async (req, res, next) => {
/* #swagger.tags = ['media'] */
try {
return res.json({
success: true,
Expand Down Expand Up @@ -61,6 +62,7 @@ module.exports = routes => {
// },
// }),
async (req, res, next) => {
/* #swagger.tags = ['media'] */
try {
console.log(req.file);
return res.json({
Expand All @@ -75,6 +77,7 @@ module.exports = routes => {
);

router.get('/files/:file', (req, res, next) => {
/* #swagger.tags = ['media'] */
try {
// do a bunch of if statements to make sure the user is
// authorized to view this image, then
Expand All @@ -88,6 +91,7 @@ module.exports = routes => {
let uploads = {};

router.post('/upload', (req, res, next) => {
/* #swagger.tags = ['media'] */
let fileId = req.headers['x-file-id'];
let startByte = parseInt(req.headers['x-start-byte'], 10);
let name = req.headers['name'];
Expand Down Expand Up @@ -176,6 +180,7 @@ module.exports = routes => {
}

router.post('/convert', async (req, res, next) => {
/* #swagger.tags = ['media'] */
if (!req.files) {
return res.status(500).send({ msg: 'file is not found' });
}
Expand All @@ -189,6 +194,7 @@ module.exports = routes => {
});

router.get('/status', (req, res) => {
/* #swagger.tags = ['media'] */
let fileId = req.headers['x-file-id'];
let name = req.headers['name'];
let fileSize = parseInt(req.headers['size'], 10);
Expand Down
18 changes: 8 additions & 10 deletions modules/news/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ const path = '/v1/news';
const router = require('express').Router();

module.exports = () => {
/**
* @swagger
* /api/news:
* get:
* summary: News Route
* responses:
* 200:
*/
router.get('/', async (req, res, next) => {
/* #swagger.tags = ['news'] */
try {
return res.json({
success: true,
Expand All @@ -26,8 +19,9 @@ module.exports = () => {
next(error);
}
});
// POST /api/news

router.post('/', createNewsValidator, async (req, res, next) => {
/* #swagger.tags = ['news'] */
try {
return res.json({
success: true,
Expand All @@ -38,8 +32,9 @@ module.exports = () => {
next(error);
}
});
// GET /api/news/update/:id

router.put('/update/:id', async (req, res, next) => {
/* #swagger.tags = ['news'] */
try {
return res.json({
success: true,
Expand All @@ -52,6 +47,7 @@ module.exports = () => {
});
// GET /api/news/show?_id=aeb54 | [email protected] | slug=test
router.get('/show', async (req, res, next) => {
/* #swagger.tags = ['news'] */
try {
if (req.query.email || req.query._id || req.query.slug) {
return res.json({
Expand All @@ -67,6 +63,7 @@ module.exports = () => {
});
// PUT /api/news/update/?_id=aeb54 | [email protected] | slug=test
router.put('/update', async (req, res, next) => {
/* #swagger.tags = ['news'] */
try {
if (req.query.email || req.query._id || req.query.slug) {
return res.json({
Expand All @@ -83,6 +80,7 @@ module.exports = () => {

// DELETE /api/news/update/?_id=aeb54 | [email protected] | slug=test
router.delete('/delete', async (req, res, next) => {
/* #swagger.tags = ['news'] */
try {
if (req.query.email || req.query._id || req.query.slug) {
return res.json({
Expand Down
Loading

0 comments on commit 1478d76

Please sign in to comment.