Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | 16x 16x 15x 15x 15x 12x 15x 15x 16x 15x 15x 15x 13x 2x 2x 13x 2x 2x 15x 2x 15x 15x 1x 14x 15x 15x 15x 15x 15x 15x 5x 15x 1x 1x 2x 15x 6x 15x 1x 15x 15x 15x 19x 19x 15x 15x 15x 15x 11x 11x 2x 2x 1x 1x 1x 1x 15x 15x 15x 15x 15x 2x 2x 2x 2x 4x 2x 4x 4x 4x 4x 4x 4x 4x 4x 2x 4x 4x 4x 4x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 1x 4x 4x 4x 2x 2x 4x | import type { RouteForDocs, OpenApiOptions, RouteMetadata } from './types.js';
import { extractMetadataFromFile } from './jsdoc.js';
export function buildPaths(routes: RouteForDocs[], options: OpenApiOptions): Record<string, unknown> {
const paths: Record<string, unknown> = {};
for (const route of routes) {
const openApiPath = convertPatternToOpenApi(route.pattern);
const metadata = extractMetadataFromFile(route.file);
if (!paths[openApiPath]) {
paths[openApiPath] = {};
}
const pathItem = paths[openApiPath] as Record<string, unknown>;
pathItem[route.method] = generateOperationObject(route, metadata, options);
}
return paths;
}
export function buildSchemas(routes: RouteForDocs[]): Record<string, object> {
const schemas: Record<string, object> = {};
const typeNames = new Set<string>();
for (const route of routes) {
if (route.bodyType) {
const refs = extractTypeReferences(route.bodyType);
refs.forEach(ref => typeNames.add(ref));
}
if (route.queryType) {
const refs = extractTypeReferences(route.queryType);
refs.forEach(ref => typeNames.add(ref));
}
}
for (const typeName of typeNames) {
schemas[typeName] = {
type: 'object',
description: `${typeName} schema`,
};
}
return schemas;
}
function convertPatternToOpenApi(pattern: string): string {
if (!pattern || pattern === '') {
return '/';
}
return '/' + pattern
.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, '{$1}')
.replace(/\*([a-zA-Z_][a-zA-Z0-9_]*)/g, '{$1}');
}
function generateOperationObject(
route: RouteForDocs,
metadata: RouteMetadata,
options: OpenApiOptions
): object {
const operation: Record<string, unknown> = {
operationId: generateOperationId(route),
summary: metadata.summary || `${route.method.toUpperCase()} ${route.pattern}`,
};
Iif (metadata.description) {
operation.description = metadata.description;
}
Iif (metadata.tags && metadata.tags.length > 0) {
operation.tags = metadata.tags;
}
Iif (metadata.deprecated) {
operation.deprecated = true;
}
const parameters: unknown[] = [];
for (const param of route.params) {
parameters.push({
name: param,
in: 'path',
required: true,
schema: { type: 'string' },
description: metadata.paramDescriptions?.[param] || undefined,
});
}
if (route.queryType) {
const queryFields = extractTypeFields(route.queryType);
for (const field of queryFields) {
parameters.push({
name: field.name,
in: 'query',
required: field.required,
schema: field.schema,
description: field.description,
});
}
}
if (parameters.length > 0) {
operation.parameters = parameters;
}
if (route.bodyType && ['post', 'put', 'patch'].includes(route.method)) {
operation.requestBody = {
description: metadata.bodyDescription || 'Request body',
required: true,
content: {
'application/json': {
schema: typeToSchema(route.bodyType),
},
},
};
}
operation.responses = generateResponses(route, metadata);
return operation;
}
function generateOperationId(route: RouteForDocs): string {
const patternParts = route.pattern
.split('/')
.filter(Boolean)
.map(part => {
const clean = part.replace(/^[:*]/, '');
return clean.charAt(0).toUpperCase() + clean.slice(1);
});
return route.method + patternParts.join('');
}
function generateResponses(route: RouteForDocs, metadata: RouteMetadata): object {
const responses: Record<string, object> = {};
Iif (metadata.responses && Object.keys(metadata.responses).length > 0) {
for (const [code, responseMeta] of Object.entries(metadata.responses)) {
responses[code] = {
description: responseMeta.description,
content: responseMeta.type ? {
'application/json': {
schema: typeStringToJsonSchema(responseMeta.type),
example: responseMeta.example,
},
} : undefined,
};
}
} else {
switch (route.method) {
case 'get':
responses['200'] = {
description: 'Successful response',
content: {
'application/json': {
schema: { type: 'object' },
},
},
};
break;
case 'post':
responses['201'] = {
description: 'Created successfully',
content: {
'application/json': {
schema: { type: 'object' },
},
},
};
break;
case 'put':
case 'patch':
responses['200'] = {
description: 'Updated successfully',
content: {
'application/json': {
schema: { type: 'object' },
},
},
};
break;
case 'delete':
responses['204'] = {
description: 'Deleted successfully',
};
break;
}
}
responses['400'] = { description: 'Bad request' };
responses['401'] = { description: 'Unauthorized' };
responses['404'] = { description: 'Not found' };
responses['500'] = { description: 'Internal server error' };
return responses;
}
function extractTypeFields(typeStr: string): Array<{
name: string;
required: boolean;
schema: object;
description?: string;
}> {
const fields: Array<{ name: string; required: boolean; schema: object; description?: string }> = [];
const cleanType = typeStr.replace(/^[\s\n]+/, '').replace(/[\s\n]+$/, '');
Iif (!cleanType.startsWith('{')) {
return fields;
}
const inner = cleanType.slice(1, -1).trim();
const propLines = inner.split(';').filter(s => s.trim());
for (const line of propLines) {
const trimmed = line.trim();
Iif (!trimmed) continue;
const match = trimmed.match(/^(\w+)(\?)?:\s*(.+)$/);
Eif (match) {
const name = match[1];
const optional = !!match[2];
const type = match[3].trim();
fields.push({
name,
required: !optional,
schema: typeStringToJsonSchema(type),
});
}
}
return fields;
}
function typeStringToJsonSchema(type: string): object {
const cleanType = type.trim();
Iif (cleanType.endsWith('[]')) {
const itemType = cleanType.slice(0, -2);
return {
type: 'array',
items: typeStringToJsonSchema(itemType),
};
}
Iif (cleanType.includes('|')) {
const types = cleanType.split('|').map(t => t.trim());
const schemas = types.map(t => typeStringToJsonSchema(t));
return { anyOf: schemas };
}
switch (cleanType.toLowerCase()) {
case 'string':
return { type: 'string' };
case 'number':
case 'integer':
return { type: 'number' };
case 'boolean':
return { type: 'boolean' };
case 'date':
return { type: 'string', format: 'date-time' };
case 'object':
return { type: 'object' };
case 'any':
return {};
default:
if (cleanType.match(/^[A-Z]\w*$/)) {
return { $ref: `#/components/schemas/${cleanType}` };
}
return { type: 'object' };
}
}
function typeToSchema(typeStr: string): object {
Eif (typeStr.trim().startsWith('{')) {
const fields = extractTypeFields(typeStr);
const properties: Record<string, object> = {};
const required: string[] = [];
for (const field of fields) {
properties[field.name] = field.schema;
Eif (field.required) {
required.push(field.name);
}
}
return {
type: 'object',
properties,
required: required.length > 0 ? required : undefined,
};
}
return typeStringToJsonSchema(typeStr);
}
function extractTypeReferences(typeStr: string): string[] {
const refs: string[] = [];
const matches = typeStr.matchAll(/\b([A-Z][a-zA-Z0-9_]*)\b/g);
for (const match of matches) {
Eif (!['Object', 'Array', 'Date', 'String', 'Number', 'Boolean'].includes(match[1])) {
refs.push(match[1]);
}
}
return [...new Set(refs)];
}
|