UNPKG

1.43 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.SwaggerTransformer = void 0;
4const lodash_1 = require("lodash");
5class SwaggerTransformer {
6 normalizePaths(denormalizedDoc) {
7 const roots = (0, lodash_1.filter)(denormalizedDoc, (r) => r.root);
8 const groupedByPath = (0, lodash_1.groupBy)(roots, ({ root }) => root.path);
9 const paths = (0, lodash_1.mapValues)(groupedByPath, (routes) => {
10 const keyByMethod = (0, lodash_1.keyBy)(routes, ({ root }) => root.method);
11 return (0, lodash_1.mapValues)(keyByMethod, (route) => {
12 return Object.assign(Object.assign({}, (0, lodash_1.omit)(route.root, ['method', 'path'])), (0, lodash_1.omit)(route, 'root'));
13 });
14 });
15 return {
16 paths
17 };
18 }
19 unescapeColonsInPath(app, moduleRoutes) {
20 const httpAdapter = app.getHttpAdapter();
21 const usingFastify = httpAdapter && httpAdapter.getType() === 'fastify';
22 const unescapeColon = usingFastify
23 ? (path) => path.replace(/:\{([^}]+)\}/g, ':$1')
24 : (path) => path.replace(/\[:\]/g, ':');
25 return moduleRoutes.map((moduleRoute) => (Object.assign(Object.assign({}, moduleRoute), { root: Object.assign(Object.assign({}, moduleRoute.root), { path: unescapeColon(moduleRoute.root.path) }) })));
26 }
27}
28exports.SwaggerTransformer = SwaggerTransformer;