mfdoc
Version:
Auto generate JS SDK and HTTP API documentation
50 lines • 2.25 kB
JavaScript
import fse from 'fs-extra';
import { forEach } from 'lodash-es';
import { posix } from 'path';
import { getEndpointsFromSrcPath } from './getEndpointsFromSrcPath.js';
import { kMfdocHttpHeaderItems } from './headers.js';
import { mfdocConstruct, } from './mfdoc.js';
import { kMfdocHttpResponseItems } from './response.js';
import { filterEndpointsByTags } from './utils.js';
function generateEndpointInfoFromEndpoints(params) {
const { endpoints, tags } = params;
const infoMap = new Map();
const pickedEndpoints = filterEndpointsByTags(endpoints, tags);
forEach(pickedEndpoints, endpoint => {
const inf = mfdocConstruct.constructHttpEndpointDefinition({
...endpoint,
errorResponseHeaders: endpoint.errorResponseHeaders ??
kMfdocHttpHeaderItems.responseHeaders_JsonContentType,
errorResponseBody: endpoint.errorResponseBody ?? kMfdocHttpResponseItems.errorResponseBody,
});
infoMap.set(endpoint, JSON.stringify(inf, /** replacer */ undefined, /** spaces */ 4));
});
return infoMap;
}
async function writeEndpointInfoToFile(endpointPath, info) {
await fse.ensureFile(endpointPath);
return fse.writeFile(endpointPath, info, { encoding: 'utf-8' });
}
export async function genHttpApiEndpointsInfo(params) {
const { endpoints, tags, outputPath } = params;
const infoMap = generateEndpointInfoFromEndpoints({ endpoints, tags });
const promises = [];
await fse.remove(outputPath);
infoMap.forEach((info, endpoint) => {
const pathname = endpoint.name
.split('/')
.filter(p => !p.startsWith(':'))
.join('/');
const method = endpoint.method;
const filename = `${pathname}__${method}.json`;
const endpointPath = posix.normalize(outputPath + '/' + filename);
promises.push(writeEndpointInfoToFile(endpointPath, info));
});
return Promise.all(promises);
}
export async function genHttpApiEndpointsInfoCmd(params) {
const { srcPath, tags, outputPath } = params;
const endpoints = await getEndpointsFromSrcPath({ srcPath });
await genHttpApiEndpointsInfo({ endpoints, tags, outputPath });
}
//# sourceMappingURL=genHttpApiEndpointsInfo.js.map