Skip to content

Commit 5ba20ac

Browse files
authored
Merge pull request #8 from mugarate12/feature/swagger
Feature/swagger
2 parents 1af2ff5 + 0fb849b commit 5ba20ac

8 files changed

Lines changed: 144 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.2.7] - 2022-10-26
10+
### Added
11+
- Add swagger to documentation application
12+
913
## [1.2.6] - 2022-10-26
1014
### Added
1115
- Add pino and pino-pretty to log to console

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ project
5252
│ nginx
5353
| │ default.conf
5454
| │ Dockerfile
55+
│ docs
56+
| │ docs.yaml
5557
│ __tests__
5658
│ └───unit
5759
| │ | example.test.ts

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "basic-node-ts-config",
3-
"version": "1.2.6",
3+
"version": "1.2.7",
44
"description": "Configuração básica para um ambiente de desenvolvimento com NodeJs + TypeScript",
55
"main": "./lib/copyFile.js",
66
"preferGlobal": true,

project/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ project
123123
│ nginx
124124
| │ default.conf
125125
| │ Dockerfile
126+
│ docs
127+
| │ docs.yaml
126128
└───src
127129
| | app.ts
128130
| | server.ts

project/docs/docs.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: 3.0.0
2+
info:
3+
description: "This is a example of documentation to your API"
4+
version: 1.2.7
5+
title: Basic Code Ts Config
6+
termsOfService: http://swagger.io/terms/
7+
contact:
8+
name: "Mateus Cardoso dos Santos"
9+
email: serjumano17@gmail.com
10+
tags:
11+
- name: example
12+
description: Something
13+
externalDocs:
14+
description: Find out more
15+
url: http://swagger.io
16+
17+
paths:
18+
/:
19+
get:
20+
tags:
21+
- example
22+
summary: "Example to route docs"
23+
description: "name value can be use your name to return message"
24+
operationId: baseRoute
25+
parameters:
26+
- name: name
27+
in: query
28+
required: false
29+
schema:
30+
type: string
31+
example: "Mateus"
32+
responses:
33+
"200":
34+
description: "Successful request"
35+
content:
36+
application/json:
37+
schema:
38+
type: object
39+
properties:
40+
message:
41+
type: string
42+
example: "API está funcionando!"
43+
44+
45+
externalDocs:
46+
description: Find out more about Swagger
47+
url: http://swagger.io
48+

project/package-lock.json

Lines changed: 75 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

project/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
"pino": "^8.7.0",
3939
"pino-pretty": "^9.1.1",
4040
"redis": "^4.3.1",
41-
"run-script-os": "^1.1.6"
41+
"run-script-os": "^1.1.6",
42+
"swagger-ui-express": "^4.5.0",
43+
"yamljs": "^0.3.0"
4244
},
4345
"devDependencies": {
4446
"@babel/preset-env": "^7.19.1",
@@ -50,6 +52,7 @@
5052
"@types/jsonwebtoken": "^8.5.9",
5153
"@types/node": "^18.7.14",
5254
"@types/supertest": "^2.0.12",
55+
"@types/yamljs": "^0.2.31",
5356
"jest": "^29.0.3",
5457
"nodemon": "^2.0.19",
5558
"supertest": "^6.2.4",

project/src/app.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ import cors from 'cors';
33
import dotenv from 'dotenv';
44
import http from 'http';
55
import { errors } from 'celebrate';
6+
import swaggerUi from 'swagger-ui-express';
7+
import Yaml from 'yamljs';
68
import path from 'path';
79

810
import routes from './routes';
911

1012
dotenv.config();
1113
const applicationType = String(process.env.NODE_ENV);
14+
const swaggerDocument = applicationType === 'development' || applicationType === 'test' || applicationType === 'undefined' ?
15+
Yaml.load(path.resolve(__dirname, '..', 'docs', 'docs.yaml')) :
16+
Yaml.load(path.resolve(__dirname, '..', '..', 'docs', 'docs.yaml'));
1217

1318
const app = express();
1419
const server = http.createServer(app);
@@ -27,6 +32,9 @@ app.use(express.json());
2732
// use all routes settings
2833
app.use(routes);
2934

35+
// use swagger documentation
36+
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
37+
3038
// celebrate errors
3139
app.use(errors());
3240

0 commit comments

Comments
 (0)