Skip to content

Commit 43218ee

Browse files
author
patched.codes[bot]
committed
Patched /tmp/tmpptoa8sbv/src/start.js
1 parent 626c1d8 commit 43218ee

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/start.js

+77
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,50 @@ let swaggerDefinition = {
2929
*/
3030
let app;
3131

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+
*/
3237
const loadModules = async (baseDir) => {
3338
const modulePaths = [];
3439

3540
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+
*/
3646
readdirSync(baseDir).forEach((moduleDirectory) => {
3747
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+
*/
3853
.filter((versionDirectory) =>
3954
statSync(
4055
join(baseDir, moduleDirectory, versionDirectory)
4156
).isDirectory()
4257
)
4358
.sort();
4459

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+
*/
4568
versionDirectories.forEach((versionDir) => {
4669
const module = readdirSync(
4770
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+
*/
4876
).find((fileName) => fileName === 'module.js');
4977

5078
if (module) {
@@ -54,6 +82,11 @@ const loadModules = async (baseDir) => {
5482
});
5583
}
5684

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+
*/
5790
const modules = modulePaths.map(async (modulePath) => {
5891
return {
5992
path: modulePath,
@@ -70,6 +103,12 @@ const loadModules = async (baseDir) => {
70103
*/
71104
const getDocumentParameters = (type, schema) => {
72105
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+
*/
73112
const params = Object.keys(swagger.properties).map((key) => {
74113
const { required } = swagger;
75114
const { description, ...schema } = swagger.properties[key];
@@ -132,11 +171,25 @@ const getDocRequestBody = (schema, config) => {
132171
return { content };
133172
};
134173

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+
*/
135181
const schemaValidationMiddlewares = (validationSchema) => {
136182
const { query, body } = validationSchema ?? { query: null, body: null };
137183
const middlewares = [];
138184

139185
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+
*/
140193
middlewares.push((req, res, next) => {
141194
if (req.query) {
142195
const { errors, value } = validateSchema(req.query, query, res);
@@ -153,6 +206,13 @@ const schemaValidationMiddlewares = (validationSchema) => {
153206
}
154207

155208
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+
*/
156216
middlewares.push((req, res, next) => {
157217
if (req.body) {
158218
const { errors } = validateSchema(req.body, body, res);
@@ -172,12 +232,24 @@ const schemaValidationMiddlewares = (validationSchema) => {
172232
return middlewares;
173233
};
174234

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+
*/
175242
const setDefaultLogger = (req, res, next) => {
176243
req.log.debug(`Request ${req.method.toUpperCase()} ${req.path}`);
177244

178245
next();
179246
};
180247

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+
*/
181253
const initRoutes = async (modules) => {
182254
swaggerDefinition.paths = {};
183255

@@ -236,6 +308,11 @@ const initRoutes = async (modules) => {
236308
...swaggerDefinition.paths[path],
237309
...{
238310
[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+
*/
239316
tags: tags.filter((tag) => tag !== 'api'),
240317
summary,
241318
description,

0 commit comments

Comments
 (0)