Skip to content

Commit 614ee29

Browse files
Merge pull request #10 from Archit404Error/archit/swagger-autogen
Swagger Autogen
2 parents fa521e3 + 6dd9541 commit 614ee29

8 files changed

Lines changed: 254 additions & 746 deletions

File tree

backend/api-spec.json

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

backend/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
"@types/express": "^4.17.15",
1010
"@types/jest": "^29.2.5",
1111
"@types/node": "^18.11.18",
12+
"@types/swagger-ui-express": "^4.1.3",
1213
"body-parser": "^1.20.1",
1314
"dotenv": "^16.0.3",
1415
"express": "^4.18.2",
15-
"express-oas-generator": "^1.0.46",
1616
"jest": "^29.3.1",
1717
"mongoose": "^6.8.4",
18+
"swagger-autogen": "^2.22.0",
19+
"swagger-ui-express": "^4.6.0",
1820
"ts-jest": "^29.0.5",
1921
"ts-node": "^10.9.1",
2022
"typescript": "^4.9.4"
@@ -23,6 +25,7 @@
2325
"start": "ts-node -r dotenv/config src/index.ts",
2426
"dev": "nodemon -r dotenv/config src/index.ts",
2527
"build": "tsc",
26-
"test": "jest"
28+
"test": "jest",
29+
"swagger": "ts-node -r dotenv/config src/swagger.ts"
2730
}
2831
}

backend/src/index.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +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";
5+
import swaggerUI from "swagger-ui-express";
6+
import spec from "../api-spec.json";
67
import { dbConnect } from "./database";
78

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

1511
// Middleware to parse json request bodies
1612
app.use(bodyParser.json());
13+
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(spec));
1714

1815
/**
1916
* Sub-routers for our main router, we should have one sub-router per "entity" in the application
@@ -32,8 +29,6 @@ app.post("/", (req, res) => {
3229
res.send(req.body);
3330
});
3431

35-
expressOasGenerator.handleRequests();
36-
3732
app.listen(process.env.PORT || 3000, async () => {
3833
console.log("✅ Server is up and running");
3934
await dbConnect();

backend/src/swagger.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import swaggerAutogen from "swagger-autogen";
2+
3+
const doc = {
4+
info: {
5+
version: "1.0.0",
6+
title: "My API",
7+
description:
8+
"Documentation automatically generated by the <b>swagger-autogen</b> module.",
9+
},
10+
host: "localhost:3000",
11+
basePath: "/",
12+
schemes: ["http", "https"],
13+
consumes: ["application/json"],
14+
produces: ["application/json"],
15+
tags: [
16+
{
17+
name: "User",
18+
description: "Endpoints",
19+
},
20+
],
21+
securityDefinitions: {
22+
apiKeyAuth: {
23+
type: "apiKey",
24+
in: "header", // can be "header", "query" or "cookie"
25+
name: "X-API-KEY", // name of the header, query parameter or cookie
26+
description: "any description...",
27+
},
28+
},
29+
};
30+
31+
const outputFile = "../api-spec.json";
32+
const endpointsFiles = ["./index.ts"];
33+
34+
swaggerAutogen()(outputFile, endpointsFiles, doc);

0 commit comments

Comments
 (0)