Files
api/app/core/utils/misc/get-endpoints.ts
Alexis Tyler 4e1b0bd72c chore: lint
2021-01-28 15:45:14 +10:30

35 lines
971 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-expect-error
endpoint.methods,
// @ts-expect-error
endpoints.filter(({ path }) => path === endpoint.path).map(({ methods }) => methods)
]);
const methods = flattenedMethods.filter((item, pos, self) => self.indexOf(item) === pos);
return {
// @ts-expect-error
...endpoint,
methods
};
});
};