From a62a3b7b7c059be3c32018c39bf7e5b61a60eab4 Mon Sep 17 00:00:00 2001 From: Sergio Date: Sat, 24 Dec 2022 19:41:57 +0100 Subject: [PATCH] fix(pathparameters): path parameters to use md class instead of direct markup --- src/transformers/path.ts | 7 +++-- src/transformers/pathParameters.ts | 42 +++++++++++++++++------------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/transformers/path.ts b/src/transformers/path.ts index 64d6c97..19cf601 100644 --- a/src/transformers/path.ts +++ b/src/transformers/path.ts @@ -38,7 +38,7 @@ export const transformPath = ( md.line(''); // Set method as a subheader md.line(md.string(method.toUpperCase()).h4()); - const pathInfo = data[method]; + const pathInfo: OpenAPIV2.OperationObject = data[method]; // Set summary if ('summary' in pathInfo) { @@ -58,7 +58,10 @@ export const transformPath = ( // Build parameters if ('parameters' in pathInfo || pathParameters) { - const builtParameters = md.string(transformParameters(pathInfo.parameters, pathParameters)); + const builtParameters = md.string(transformParameters( + pathInfo.parameters, + pathParameters, + )); if (builtParameters.length) { md.line(builtParameters).line(); } diff --git a/src/transformers/pathParameters.ts b/src/transformers/pathParameters.ts index 7f7ba37..48f90f7 100644 --- a/src/transformers/pathParameters.ts +++ b/src/transformers/pathParameters.ts @@ -1,26 +1,33 @@ +import { OpenAPIV2 } from 'openapi-types'; import { dataTypeResolver } from './dataTypes'; import { Schema } from '../models/schema'; -import { textEscape } from '../lib/textEscape'; +import { Markdown } from '../lib/markdown'; -export const transformParameters = (parameters, pathParameters?: any) => { - const res = []; - res.push('##### Parameters\n'); - res.push('| Name | Located in | Description | Required | Schema |'); - res.push('| ---- | ---------- | ----------- | -------- | ---- |'); - [].concat(pathParameters, parameters).forEach((keys) => { +export const transformParameters = ( + parameters: OpenAPIV2.Parameters, + pathParameters?: OpenAPIV2.Parameters, +) => { + const md = Markdown.md(); + + md.line(md.string('Parameters').h5()).line(); + const table = md.table(); + table.th('Name').th('Located in').th('Description').th('Required') + .th('Schema'); + + [].concat(pathParameters, parameters).forEach((keys: OpenAPIV2.Parameter) => { if (keys) { - const line = []; + const tr = table.tr(); // Name first - line.push(keys.name || ''); + tr.td(keys.name || ''); // Scope (in) - line.push(keys.in || ''); + tr.td(keys.in || ''); // description if ('description' in keys) { - line.push(textEscape(keys.description.replace(/[\r\n]/g, ' '))); + tr.td(md.string(keys.description.replace(/[\r\n]/g, ' ')).escape()); } else { - line.push(''); + tr.td(''); } - line.push(keys.required ? 'Yes' : 'No'); + tr.td(keys.required ? 'Yes' : 'No'); // Prepare schema to be transformed let schema = null; @@ -34,11 +41,10 @@ export const transformParameters = (parameters, pathParameters?: any) => { schema.setItems('items' in keys ? keys.items : null); } - line.push(dataTypeResolver(schema)); - // Add spaces and glue using pipeline - const glued = line.map((el) => ` ${el} `).join('|'); - res.push(`|${glued}|`); + tr.td(dataTypeResolver(schema)); } }); - return res.join('\n'); + + md.line(table); + return md.export(); };