Skip to content

Commit fa521e3

Browse files
Add swagger auto
1 parent f9d8dfb commit fa521e3

5 files changed

Lines changed: 753 additions & 7 deletions

File tree

backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"body-parser": "^1.20.1",
1313
"dotenv": "^16.0.3",
1414
"express": "^4.18.2",
15+
"express-oas-generator": "^1.0.46",
1516
"jest": "^29.3.1",
1617
"mongoose": "^6.8.4",
1718
"ts-jest": "^29.0.5",

backend/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ import express from "express";
22
import bodyParser from "body-parser";
33
import userRouter from "./users/views";
44
import customerRouter from "./customers/views";
5+
import expressOasGenerator from "express-oas-generator";
56
import { dbConnect } from "./database";
67

78
const app = express();
9+
expressOasGenerator.handleResponses(app, {
10+
specOutputPath: "swagger.json",
11+
specOutputFileBehavior: "merge",
12+
swaggerDocumentOptions: undefined,
13+
});
814

915
// Middleware to parse json request bodies
1016
app.use(bodyParser.json());
@@ -26,6 +32,8 @@ app.post("/", (req, res) => {
2632
res.send(req.body);
2733
});
2834

35+
expressOasGenerator.handleRequests();
36+
2937
app.listen(process.env.PORT || 3000, async () => {
3038
console.log("✅ Server is up and running");
3139
await dbConnect();

backend/swagger.json

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
{
2+
"definitions": {},
3+
"info": {
4+
"title": "backend",
5+
"version": "1.0.0",
6+
"license": {
7+
"name": "MIT"
8+
},
9+
"description": "Specification JSONs: [v2](/api-spec/v2), [v3](/api-spec/v3)."
10+
},
11+
"paths": {
12+
"/users": {
13+
"get": {
14+
"summary": "/users",
15+
"consumes": [
16+
"application/json"
17+
],
18+
"parameters": [],
19+
"responses": {},
20+
"tags": []
21+
},
22+
"post": {
23+
"summary": "/users",
24+
"consumes": [
25+
"application/json"
26+
],
27+
"parameters": [],
28+
"responses": {},
29+
"tags": []
30+
}
31+
},
32+
"/customers": {
33+
"get": {
34+
"summary": "/customers",
35+
"consumes": [
36+
"application/json"
37+
],
38+
"parameters": [],
39+
"responses": {},
40+
"tags": []
41+
},
42+
"post": {
43+
"summary": "/customers",
44+
"consumes": [
45+
"application/json"
46+
],
47+
"parameters": [],
48+
"responses": {},
49+
"tags": []
50+
}
51+
},
52+
"/customers/{id}": {
53+
"get": {
54+
"summary": "/customers/{id}",
55+
"consumes": [
56+
"application/json"
57+
],
58+
"parameters": [
59+
{
60+
"name": "id",
61+
"in": "path",
62+
"required": true
63+
},
64+
{
65+
"name": "name",
66+
"in": "query",
67+
"required": true
68+
}
69+
],
70+
"responses": {},
71+
"tags": []
72+
}
73+
},
74+
"/customers/updateName/{id}": {
75+
"post": {
76+
"summary": "/customers/updateName/{id}",
77+
"consumes": [
78+
"application/json"
79+
],
80+
"parameters": [
81+
{
82+
"name": "id",
83+
"in": "path",
84+
"required": true
85+
}
86+
],
87+
"responses": {
88+
"200": {
89+
"description": "OK",
90+
"schema": {
91+
"type": "object",
92+
"properties": {
93+
"success": {
94+
"type": "boolean",
95+
"example": true
96+
},
97+
"data": {
98+
"type": "object",
99+
"properties": {
100+
"_id": {
101+
"type": "string",
102+
"example": "63c6f577241114bcda3dce60"
103+
},
104+
"name": {
105+
"type": "string",
106+
"example": "Archit Mehta"
107+
},
108+
"age": {
109+
"type": "number",
110+
"example": 19
111+
},
112+
"job": {
113+
"type": "object",
114+
"properties": {
115+
"title": {
116+
"type": "string",
117+
"example": "SWE"
118+
},
119+
"company": {
120+
"type": "string",
121+
"example": "Google"
122+
},
123+
"_id": {
124+
"type": "string",
125+
"example": "63c6f577241114bcda3dce61"
126+
}
127+
}
128+
},
129+
"__v": {
130+
"type": "number",
131+
"example": 0
132+
}
133+
}
134+
}
135+
}
136+
}
137+
}
138+
},
139+
"tags": [],
140+
"produces": [
141+
"application/json"
142+
]
143+
}
144+
},
145+
"/customers/resetAges": {
146+
"post": {
147+
"summary": "/customers/resetAges",
148+
"consumes": [
149+
"application/json"
150+
],
151+
"parameters": [],
152+
"responses": {},
153+
"tags": []
154+
}
155+
},
156+
"/": {
157+
"get": {
158+
"summary": "/",
159+
"consumes": [
160+
"application/json"
161+
],
162+
"parameters": [],
163+
"responses": {},
164+
"tags": []
165+
},
166+
"post": {
167+
"summary": "/",
168+
"consumes": [
169+
"application/json"
170+
],
171+
"parameters": [],
172+
"responses": {},
173+
"tags": []
174+
}
175+
}
176+
},
177+
"swagger": "2.0",
178+
"tags": []
179+
}

backend/swagger_v3.json

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "backend",
5+
"version": "1.0.0",
6+
"license": {
7+
"name": "MIT"
8+
},
9+
"description": "Specification JSONs: [v2](/api-spec/v2), [v3](/api-spec/v3)."
10+
},
11+
"paths": {
12+
"/users": {
13+
"get": {
14+
"summary": "/users",
15+
"responses": {},
16+
"tags": []
17+
},
18+
"post": {
19+
"summary": "/users",
20+
"responses": {},
21+
"tags": []
22+
}
23+
},
24+
"/customers": {
25+
"get": {
26+
"summary": "/customers",
27+
"responses": {},
28+
"tags": []
29+
},
30+
"post": {
31+
"summary": "/customers",
32+
"responses": {},
33+
"tags": []
34+
}
35+
},
36+
"/customers/{id}": {
37+
"get": {
38+
"summary": "/customers/{id}",
39+
"parameters": [
40+
{
41+
"name": "id",
42+
"in": "path",
43+
"required": true,
44+
"schema": {
45+
"type": "string"
46+
}
47+
}
48+
],
49+
"responses": {},
50+
"tags": []
51+
}
52+
},
53+
"/customers/updateName/{id}": {
54+
"post": {
55+
"summary": "/customers/updateName/{id}",
56+
"parameters": [
57+
{
58+
"name": "id",
59+
"in": "path",
60+
"required": true,
61+
"schema": {
62+
"type": "string"
63+
}
64+
}
65+
],
66+
"responses": {
67+
"200": {
68+
"description": "OK",
69+
"content": {
70+
"application/json": {
71+
"schema": {
72+
"type": "object",
73+
"properties": {
74+
"success": {
75+
"type": "boolean",
76+
"example": true
77+
},
78+
"data": {
79+
"type": "object",
80+
"properties": {
81+
"_id": {
82+
"type": "string",
83+
"example": "63c6f577241114bcda3dce60"
84+
},
85+
"name": {
86+
"type": "string",
87+
"example": "Archit Mehta"
88+
},
89+
"age": {
90+
"type": "number",
91+
"example": 19
92+
},
93+
"job": {
94+
"type": "object",
95+
"properties": {
96+
"title": {
97+
"type": "string",
98+
"example": "SWE"
99+
},
100+
"company": {
101+
"type": "string",
102+
"example": "Google"
103+
},
104+
"_id": {
105+
"type": "string",
106+
"example": "63c6f577241114bcda3dce61"
107+
}
108+
}
109+
},
110+
"__v": {
111+
"type": "number",
112+
"example": 0
113+
}
114+
}
115+
}
116+
}
117+
}
118+
}
119+
}
120+
}
121+
},
122+
"tags": []
123+
}
124+
},
125+
"/customers/resetAges": {
126+
"post": {
127+
"summary": "/customers/resetAges",
128+
"responses": {},
129+
"tags": []
130+
}
131+
},
132+
"/": {
133+
"get": {
134+
"summary": "/",
135+
"responses": {},
136+
"tags": []
137+
},
138+
"post": {
139+
"summary": "/",
140+
"responses": {},
141+
"tags": []
142+
}
143+
}
144+
},
145+
"tags": []
146+
}

0 commit comments

Comments
 (0)