Skip to content

Commit b100390

Browse files
committed
Added swagger-jsdoc, modify code
No need to run from cli swagger-jsdoc, that is retarded, do it automatically.
1 parent af64a52 commit b100390

File tree

6 files changed

+113
-83
lines changed

6 files changed

+113
-83
lines changed

package-lock.json

+12-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"sequelize": "5.21.13"
3434
},
3535
"devDependencies": {
36-
"@types/sequelize": "4.28.9",
36+
"@types/swagger-jsdoc": "3.0.2",
3737
"@types/bcrypt": "3.0.0",
3838
"@types/compression": "1.7.0",
3939
"@types/cookie-parser": "1.4.2",
@@ -42,6 +42,7 @@
4242
"@types/express": "4.17.6",
4343
"@types/helmet": "0.0.47",
4444
"@types/jsonwebtoken": "8.5.0",
45+
"@types/sequelize": "4.28.9",
4546
"@types/swagger-ui-express": "4.1.2",
4647
"swagger-jsdoc": "4.0.0",
4748
"swagger-ui-express": "4.1.4"

src/routes/AuthenticationRouter.ts

+75
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,82 @@ import {Router} from 'express';
77
const router: Router = Router();
88

99

10+
/**
11+
* POST method route
12+
* @example http://localhost:PORT/auth/create-account
13+
*
14+
* @swagger
15+
* /auth/create-account:
16+
* post:
17+
* description: Create new User
18+
* tags: ["users"]
19+
* security:
20+
* - bearerAuth: []
21+
* requestBody:
22+
* description: user creation request body
23+
* required: true
24+
* content:
25+
* application/json:
26+
* schema:
27+
* $ref: ''
28+
* example:
29+
* name: Test User
30+
31+
* password: toohardpasswd
32+
* responses:
33+
* 201:
34+
* description: return created user
35+
* content:
36+
* application/json:
37+
* schema:
38+
* oneOf:
39+
* - $ref: ''
40+
* default:
41+
* description: unexpected error
42+
* content:
43+
* application/json:
44+
* schema:
45+
* $ref: ''
46+
*/
1047
router.post('/create-account', AuthenticationComponent.createAccount);
48+
49+
50+
/**
51+
* POST method route
52+
* @example http://localhost:PORT/auth/login
53+
*
54+
* @swagger
55+
* /auth/login:
56+
* post:
57+
* description: Login with existing account
58+
* tags: ["users"]
59+
* security:
60+
* - bearerAuth: []
61+
* requestBody:
62+
* description: user login request body
63+
* required: true
64+
* content:
65+
* application/json:
66+
* schema:
67+
* $ref: ''
68+
* example:
69+
70+
* password: toohardpasswd
71+
* responses:
72+
* 201:
73+
* description: return login user and token
74+
* content:
75+
* application/json:
76+
* schema:
77+
* oneOf:
78+
* - $ref: ''
79+
* default:
80+
* description: unexpected error
81+
* content:
82+
* application/json:
83+
* schema:
84+
* $ref: ''
85+
*/
1186
router.post('/login', AuthenticationComponent.login);
1287

1388
/**

src/routes/index.ts

+11-20
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import * as express from 'express';
22
import * as http from 'http';
3+
import * as swaggerJsdoc from 'swagger-jsdoc'
34
import * as swaggerUi from 'swagger-ui-express';
45
import * as jwtConfig from '../server/jwtAuth';
56

67
import AuthenticationRouter from '../routes/AuthenticationRouter'
78
import ExampleRouter from '../routes/ExampleRouter'
89

9-
let swaggerDoc: Object;
1010

11-
try {
12-
swaggerDoc = require('../../swagger.json');
13-
} catch (error) {
14-
console.log('***************************************************');
15-
console.log(' Seems like you doesn\`t have swagger.json file');
16-
console.log(' Please, run: ');
17-
console.log(' $ swagger-jsdoc -d swagger.js -o swagger.json');
18-
console.log('***************************************************');
19-
}
11+
const swaggerOptions = require('../../swagger');
12+
13+
14+
const swaggerSpecs = swaggerJsdoc(swaggerOptions);
15+
16+
17+
18+
2019

2120
/**
2221
* @export
@@ -42,16 +41,8 @@ export function init(app: express.Application): void {
4241
* else send commands, how to get swagger.json file
4342
* @constructs
4443
*/
45-
if (swaggerDoc) {
46-
app.use('/docs', swaggerUi.serve);
47-
app.get('/docs', swaggerUi.setup(swaggerDoc));
48-
} else {
49-
app.get('/docs', (req, res) => {
50-
res.send('<p>Seems like you don\'t have <code>swagger.json</code> file.</p>' +
51-
'<p>For generate doc file use: <code>swagger-jsdoc -d swagger.js -o swagger.json</code> in terminal</p>' +
52-
'<p>Then, restart your application</p>');
53-
});
54-
}
44+
app.use('/docs', swaggerUi.serve);
45+
app.get('/docs', swaggerUi.setup(swaggerSpecs));
5546

5647
/**
5748
* @description No results returned mean the object is not found

swagger.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
const path = require('path');
2+
const dotEnv = require('dotenv');
3+
4+
dotEnv.config();
25

36
module.exports = {
4-
openapi: '3.0.0',
5-
info: {
6-
title: 'Node-Typescript API',
7-
version: '1.0.0',
8-
description: 'Node Express Orm Typescript template api',
7+
swaggerDefinition: {
8+
components: {},
9+
openapi: '3.0.0',
10+
info: {
11+
title: 'Node-Typescript API',
12+
version: '1.0.0',
13+
description: 'Node Express Orm Typescript Template API',
14+
},
15+
"host": 'http://localhost:' + process.env.PORT,
916
},
10-
servers: [
11-
{ url: 'http://localhost:3000' }
12-
],
1317
apis: [path.join(__dirname, './src/**/**/*.ts')]
1418
};
19+

swagger.json

-46
This file was deleted.

0 commit comments

Comments
 (0)