fastify-openapi-glue
Version:
generate a fastify configuration from an openapi specification
28 lines (24 loc) • 703 B
JavaScript
import { dump } from "js-yaml";
export function comments(route, indent = 0) {
const prefix = `${"\t".repeat(indent)}//`;
const items = {
"req.headers": route.schema.headers,
"req.params": route.schema.params,
"req.query": route.schema.querystring,
"req.body": route.schema.body,
"valid responses": route.openapiSource.responses,
};
const commentize = (label) => {
const data = items[label];
if (!data) {
return "";
}
const dataStrings = dump(data).split("\n");
return `${prefix} ${label}
${dataStrings
.map((item) => (item.length > 0 ? `${prefix} ${item}` : prefix))
.join("\n")}
`;
};
return Object.keys(items).reduce((acc, label) => acc + commentize(label), "");
}