-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswagger.js
More file actions
151 lines (141 loc) · 4.42 KB
/
Copy pathswagger.js
File metadata and controls
151 lines (141 loc) · 4.42 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// swagger.js
const swaggerJsDoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const path = require("path");
const { cropList } = require("./enums/cropEnum"); // 👉 your enum list
const options = {
definition: {
openapi: "3.0.0",
info: {
title: "Crop Dashboard API",
version: "1.0.0",
description: "API for crop dashboard with farmer and buyer responses",
},
servers: [
{
url: "http://localhost:3001",
description: "Development server",
},
],
components: {
securitySchemes: {
ApiKeyAuth: {
type: "apiKey",
in: "header",
name: "x-api-key",
},
},
schemas: {
Crop: {
type: "object",
properties: {
name: { type: "string" }, // enum inject later
createdAt: { type: "string", format: "date-time" },
},
required: ["name"],
},
FarmerResponse: {
type: "object",
properties: {
userId: { type: "string", example: "user123" },
cropName: { type: "string" }, // enum inject later
isReadyToHarvest: { type: "boolean", example: true },
quantity: { type: "string", example: "500kg" },
variety: { type: "string", example: "Basmati" },
nextHarvestDate: {
type: "string",
format: "date",
example: "2025-10-01",
},
expectedPrice: { type: "number", example: 2500 },
createdAt: { type: "string", format: "date-time" },
updatedAt: { type: "string", format: "date-time" },
},
required: [
"userId",
"cropName",
"isReadyToHarvest",
"quantity",
"variety",
"nextHarvestDate",
"expectedPrice",
],
},
BuyerResponse: {
type: "object",
properties: {
userId: { type: "string", example: "buyer456" },
buyingFrom: {
type: "string",
enum: ["farmer", "aggregator"],
example: "farmer",
},
regions: {
type: "array",
items: { type: "string", example: "Punjab" },
},
cropName: { type: "string" }, // enum inject later
quantityCanDeal: { type: "string", example: "1000kg" },
expectedPrice: { type: "number", example: 2200 },
createdAt: { type: "string", format: "date-time" },
updatedAt: { type: "string", format: "date-time" },
},
required: [
"userId",
"buyingFrom",
"regions",
"cropName",
"quantityCanDeal",
"expectedPrice",
],
},
},
},
security: [
{
ApiKeyAuth: [],
},
],
},
apis: [path.join(__dirname, "./routes/*.js")],
};
const specs = swaggerJsDoc(options);
// 🔥 Centralized ENUM injection
function injectCropEnum(specs) {
// Questions route param
const questionsParams = specs.paths?.["/questions"]?.get?.parameters;
if (questionsParams) {
const cropParam = questionsParams.find((p) => p.name === "cropName");
if (cropParam) {
cropParam.schema.enum = cropList;
}
}
// Schemas -> Crop
if (specs.components.schemas.Crop?.properties?.name) {
specs.components.schemas.Crop.properties.name.enum = cropList;
}
// 🔹 Schemas -> FarmerResponse
if (specs.components.schemas.FarmerResponse?.properties?.cropName) {
specs.components.schemas.FarmerResponse.properties.cropName.enum = cropList;
}
// 🔹 Schemas -> BuyerResponse
if (specs.components.schemas.BuyerResponse?.properties?.cropName) {
specs.components.schemas.BuyerResponse.properties.cropName.enum = cropList;
}
// 🔹 Parameters (query/path me jo cropName hai unme inject karna)
for (const pathKey of Object.keys(specs.paths)) {
const pathObj = specs.paths[pathKey];
for (const methodKey of Object.keys(pathObj)) {
const methodObj = pathObj[methodKey];
if (methodObj.parameters) {
methodObj.parameters.forEach((param) => {
if (param.name === "cropName" && param.schema?.type === "string") {
param.schema.enum = cropList; // 🚀 inject dropdown
}
});
}
}
}
}
injectCropEnum(specs);
module.exports = { swaggerUi, specs };