@@ -29,22 +29,50 @@ let swaggerDefinition = {
29
29
*/
30
30
let app ;
31
31
32
+ /**
33
+ * Asynchronously loads modules from a specified base directory.
34
+ * @param {string } baseDir - The base directory path to search for modules.
35
+ * @returns {Promise<Array<Object>> } An array of objects, each containing the path and imported module.
36
+ */
32
37
const loadModules = async ( baseDir ) => {
33
38
const modulePaths = [ ] ;
34
39
35
40
if ( existsSync ( baseDir ) ) {
41
+ /**
42
+ * Recursively searches for 'module.js' files within a given base directory and its subdirectories.
43
+ * @param {string } baseDir - The base directory to start the search from.
44
+ * @returns {string[] } An array of file paths to the found 'module.js' files.
45
+ */
36
46
readdirSync ( baseDir ) . forEach ( ( moduleDirectory ) => {
37
47
const versionDirectories = readdirSync ( join ( baseDir , moduleDirectory ) )
48
+ /**
49
+ * Filters version directories based on whether they are actual directories
50
+ * @param {string } versionDirectory - The name of a potential version directory
51
+ * @returns {boolean } True if the version directory is an actual directory, false otherwise
52
+ */
38
53
. filter ( ( versionDirectory ) =>
39
54
statSync (
40
55
join ( baseDir , moduleDirectory , versionDirectory )
41
56
) . isDirectory ( )
42
57
)
43
58
. sort ( ) ;
44
59
60
+ /**
61
+ * Searches for 'module.js' files within version directories and adds their paths to the modulePaths array.
62
+ * @param {string[] } versionDirectories - An array of version directory names to search through.
63
+ * @param {string } baseDir - The base directory path.
64
+ * @param {string } moduleDirectory - The name of the module directory.
65
+ * @param {string[] } modulePaths - An array to store the paths of found 'module.js' files.
66
+ * @returns {void } This function does not return a value, but modifies the modulePaths array.
67
+ */
45
68
versionDirectories . forEach ( ( versionDir ) => {
46
69
const module = readdirSync (
47
70
join ( baseDir , moduleDirectory , versionDir )
71
+ /**
72
+ * Finds and returns the 'module.js' file from an array of file names
73
+ * @param {string[] } fileName - An array of file names to search through
74
+ * @returns {string|undefined } The 'module.js' file name if found, otherwise undefined
75
+ */
48
76
) . find ( ( fileName ) => fileName === 'module.js' ) ;
49
77
50
78
if ( module ) {
@@ -54,6 +82,11 @@ const loadModules = async (baseDir) => {
54
82
} ) ;
55
83
}
56
84
85
+ /**
86
+ * Asynchronously imports modules from specified paths and returns an array of objects containing the module path and imported module.
87
+ * @param {string[] } modulePaths - An array of module paths to import.
88
+ * @returns {Promise<Array<{path: string, module: any}>> } A promise that resolves to an array of objects, each containing the module path and the imported module.
89
+ */
57
90
const modules = modulePaths . map ( async ( modulePath ) => {
58
91
return {
59
92
path : modulePath ,
@@ -70,6 +103,12 @@ const loadModules = async (baseDir) => {
70
103
*/
71
104
const getDocumentParameters = ( type , schema ) => {
72
105
const { swagger } = joiToSwagger ( schema ) ;
106
+ /**
107
+ * Transforms Swagger properties into an array of parameter objects
108
+ * @param {Object } swagger - The Swagger object containing properties and required fields
109
+ * @param {string } type - The type of parameter (e.g., 'query', 'body', 'path')
110
+ * @returns {Array } An array of parameter objects formatted for OpenAPI specification
111
+ */
73
112
const params = Object . keys ( swagger . properties ) . map ( ( key ) => {
74
113
const { required } = swagger ;
75
114
const { description, ...schema } = swagger . properties [ key ] ;
@@ -132,11 +171,25 @@ const getDocRequestBody = (schema, config) => {
132
171
return { content } ;
133
172
} ;
134
173
174
+ /**
175
+ * Creates middleware functions for schema validation of request query and body.
176
+ * @param {Object } validationSchema - An object containing schema definitions for query and body.
177
+ * @param {Object } [validationSchema.query] - Schema for validating request query parameters.
178
+ * @param {Object } [validationSchema.body] - Schema for validating request body.
179
+ * @returns {Array<Function> } An array of middleware functions for schema validation.
180
+ */
135
181
const schemaValidationMiddlewares = ( validationSchema ) => {
136
182
const { query, body } = validationSchema ?? { query : null , body : null } ;
137
183
const middlewares = [ ] ;
138
184
139
185
if ( query ) {
186
+ /**
187
+ * Middleware function to validate query parameters against a schema
188
+ * @param {Object } req - Express request object
189
+ * @param {Object } res - Express response object
190
+ * @param {Function } next - Express next middleware function
191
+ * @returns {void } Calls next() if validation passes, otherwise sends a 400 status with errors
192
+ */
140
193
middlewares . push ( ( req , res , next ) => {
141
194
if ( req . query ) {
142
195
const { errors, value } = validateSchema ( req . query , query , res ) ;
@@ -153,6 +206,13 @@ const schemaValidationMiddlewares = (validationSchema) => {
153
206
}
154
207
155
208
if ( body ) {
209
+ /**
210
+ * Middleware function to validate request body against a schema
211
+ * @param {Object } req - Express request object
212
+ * @param {Object } res - Express response object
213
+ * @param {Function } next - Express next middleware function
214
+ * @returns {void } Calls next middleware if validation passes, otherwise sends 400 status with errors
215
+ */
156
216
middlewares . push ( ( req , res , next ) => {
157
217
if ( req . body ) {
158
218
const { errors } = validateSchema ( req . body , body , res ) ;
@@ -172,12 +232,24 @@ const schemaValidationMiddlewares = (validationSchema) => {
172
232
return middlewares ;
173
233
} ;
174
234
235
+ /**
236
+ * Middleware function to set a default logger for incoming requests
237
+ * @param {Object } req - Express request object
238
+ * @param {Object } res - Express response object
239
+ * @param {Function } next - Express next middleware function
240
+ * @returns {void } This function doesn't return anything
241
+ */
175
242
const setDefaultLogger = ( req , res , next ) => {
176
243
req . log . debug ( `Request ${ req . method . toUpperCase ( ) } ${ req . path } ` ) ;
177
244
178
245
next ( ) ;
179
246
} ;
180
247
248
+ /**
249
+ * Initializes routes for the application
250
+ * @param {Array } modules - An array of module objects containing route information
251
+ * @returns {Promise<void> } No return value
252
+ */
181
253
const initRoutes = async ( modules ) => {
182
254
swaggerDefinition . paths = { } ;
183
255
@@ -236,6 +308,11 @@ const initRoutes = async (modules) => {
236
308
...swaggerDefinition . paths [ path ] ,
237
309
...{
238
310
[ method ] : {
311
+ /**
312
+ * Filters out the 'api' tag from the given array of tags
313
+ * @param {string[] } tags - An array of tag strings
314
+ * @returns {string[] } A new array of tags with 'api' removed
315
+ */
239
316
tags : tags . filter ( ( tag ) => tag !== 'api' ) ,
240
317
summary,
241
318
description,
0 commit comments