Files
api/app/core/utils/misc/get-endpoints.ts
2020-11-11 16:13:30 +10:30

35 lines
953 B
TypeScript

/*!
* Copyright 2019-2020 Lime Technology Inc. All rights reserved.
* Written by: Alexis Tyler
*/
import { Express } from 'express';
import expressListEndpoints from 'express-list-endpoints';
import flatten from 'flatten';
import { removeDuplicatesFromArray } from './remove-duplicates-from-array';
/**
* Get array of endpoints with associated methods from express router.
*/
export const getEndpoints = (app: Express) => {
const endpoints = expressListEndpoints(app);
const deDeupedEndpoints = removeDuplicatesFromArray(endpoints, 'path');
return deDeupedEndpoints.map(endpoint => {
const flattenedMethods: string[] = flatten([
// @ts-ignore
endpoint.methods,
// @ts-ignore
endpoints.filter(({ path }) => path === endpoint.path).map(({ methods }) => methods)
]);
const methods = flattenedMethods.filter((item, pos, self) => self.indexOf(item) === pos);
return {
// @ts-ignore
...endpoint,
methods
};
});
};