-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswagger.js
133 lines (126 loc) · 3.8 KB
/
swagger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const swaggerAutogen = require("swagger-autogen")();
const outputFile = "./swagger-output.json";
const routes = ["./app.js"];
const doc = {
info: {
title: "CareLinc Backend API",
description: "Outlines the different endpoints contained in the CareLinc Back-End API. These API Endpoints handle the management of accounts, appointment bookings, inventory management and other operations that are crucial to the functioning of the CareLinc platform.",
},
host: "localhost:3000",
securityDefinitions: {
BearerAuth: {
type: "apiKey",
name: "Authorization",
in: "header",
description: "Enter your bearer token in the format **Bearer <token>**",
},
},
tags: [
{
name: "Authentication",
description: "Endpoints related to authentication and authorisation operations (login, create account, etc)",
},
{
name: "Admin",
description: "Endpoints related to admin operations",
},
{
name: "Patient",
description: "Endpoints related to patient operations",
},
{
name: "Company",
description: "Endpoints related to company operations",
},
{
name: "Doctor",
description: "Endpoints related to doctor operations",
},
{
name: "Pay-it-Forward",
description: "Endpoints related to the Pay-it-Forward program, allowing patients, doctors and companies to manage requests for help",
},
{
name: "Inventory",
description: "Endpoints related to drug inventory management",
},
{
name: "Appointments",
description: "Endpoints related to appointment scheduling and availability",
},
{
name: "Notifications",
description: "Endpoints related to notifications",
},
{
name: "Chatbot",
description: "Endpoints related to chatbot operations, allowing patients to interact with the chatbot",
},
{
name: "Mail",
description: "Endpoints related to mail operations which sends patients a payment confirmation email",
}
],
paths: {}
};
// Sort each API Endpoint into it's respective tags
const tagsWithCategory = {
"Authentication": [
"generateQRCode", "getAuth", "verify2FA", "auth"
],
"Company": [
"company", "drugRequests", "drugRequest", "drugContributionOrders"
],
"Patient": [
"patient", "patients", "paymentMethod", "paymentMethods", "questionnaire"
],
"Doctor": [
"doctors",
],
"Admin": [
"staff", "patients/unapproved", "drugTopup", "patient/admin/", "drugTopup", "patient/reject/", "patient/approve/", "admin"
],
"Pay-it-Forward": [
"helpRequests", "paymentRequests", "paymentRequest"
],
"Inventory": [
"drugInventory", "companyDrugInventory", "drugInventoryRecord", "inventoryRecord"
],
"Appointments": [
"appointment", "appointments", "availableSlot", "availableSlots"
],
"Notifications": [
"notification", "notifications"
],
"Chatbot": [
"chatbot"
],
"Mail": [
"mail"
],
"2FA": [
"verify2FA", "generateQRCode", "getAuth"
]
};
const addTagsToPaths = (paths, tagsConfig) => {
Object.keys(paths).forEach((path) => {
Object.entries(tagsConfig).forEach(([tag, keywords]) => {
if (keywords.some((keyword) => path.startsWith(`/api/${keyword}`))) {
Object.keys(paths[path]).forEach((method) => {
paths[path][method].tags = [tag];
});
}
});
});
return paths;
};
// Generate the swagger output
swaggerAutogen(outputFile, routes, doc).then(() => {
// Read the generated file
const fs = require('fs');
const generatedDoc = require(outputFile);
// Add the Company tag to specific paths
generatedDoc.paths = addTagsToPaths(generatedDoc.paths, tagsWithCategory);
// Write the modified document back to the file
fs.writeFileSync(outputFile, JSON.stringify(generatedDoc, null, 2));
});