From d3ce936626751f24f20081403271d77e2c346e03 Mon Sep 17 00:00:00 2001 From: Karl-Philipp Wulfert Date: Tue, 26 Feb 2019 16:16:26 +0100 Subject: [PATCH] feat: adjust generation of route documentation Fixes #12 --- src/common.ts | 2 +- src/routes.ts | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/common.ts b/src/common.ts index 36db9daa..aa3fd42c 100644 --- a/src/common.ts +++ b/src/common.ts @@ -55,7 +55,7 @@ export interface RouteWithMetaInformation { * Instance of the route */ route: { - errorNames: string[]; + errorNames: Error[]; method: string; obligatoryParameters: { [k: string]: string; diff --git a/src/routes.ts b/src/routes.ts index 871317cd..ad2c4259 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -13,10 +13,8 @@ * this program. If not, see . */ import {asyncPool} from 'async-pool-native/dist/async-pool'; -import humanizeString = require('humanize-string'); import {ProjectReflection} from 'typedoc'; -import {logger, NodesWithMetaInformation, NodeWithMetaInformation} from './common'; -import {RouteWithMetaInformation} from './common'; +import {logger, NodesWithMetaInformation, NodeWithMetaInformation, RouteWithMetaInformation} from './common'; /** * Gather relevant information of routes @@ -33,20 +31,25 @@ export async function gatherRouteInformation(reflection: ProjectReflection): Pro throw new Error('Project reflection doesn\'t contain any modules.'); } - await asyncPool(2, reflection.children, async (module: any) => { + await asyncPool(2, reflection.children, async (module) => { if (Array.isArray(module.children) && module.children.length > 0) { - await asyncPool(2, module.children, (async (node: any) => { + await asyncPool(2, module.children, (async (node) => { if (Array.isArray(node.extendedTypes) && node.extendedTypes.length > 0) { - if (node.extendedTypes.some((extendedType: any) => { - return extendedType.name === 'SCAbstractRoute'; + if (node.extendedTypes.some((extendedType) => { + return (extendedType as any).name === 'SCAbstractRoute'; })) { logger.info(`Found ${node.name} in ${module.originalName}.`); + if (module.originalName.match(/\.d\.ts$/)) { + module.originalName = module.originalName.substr(0, module.originalName.length - 5); + logger.info(`Using compiled version of module in ${module.originalName}.`); + } + const importedModule = await import(module.originalName); const route = new importedModule[node.name](); - routes.push({description: node.comment, name: node.name, route}); + routes.push({description: node.comment!, name: node.name, route}); } } })); @@ -68,6 +71,8 @@ export async function gatherRouteInformation(reflection: ProjectReflection): Pro * @param humanize Whether to humanize the name or not */ export function getLinkedNameForNode(name: string, node: NodeWithMetaInformation, humanize: boolean = false): string { + const humanizeString = require('humanize-string'); + let printableName = name; if (humanize) { @@ -137,8 +142,8 @@ export function generateDocumentationForRoute(routeWithInfo: RouteWithMetaInform | request | ${getLinkedNameForNode(route.requestBodyName, nodes[route.requestBodyName])} | | response | ${getLinkedNameForNode(route.responseBodyName, nodes[route.responseBodyName])} | | success code | ${route.statusCodeSuccess} | -| errors | ${route.errorNames.map((errorName) => { - return getLinkedNameForNode(errorName, nodes[errorName]); +| errors | ${route.errorNames.map((error) => { + return getLinkedNameForNode(error.name, nodes[error.name]); }).join('
')} | `; if (typeof route.obligatoryParameters === 'object' && Object.keys(route.obligatoryParameters).length > 0) {