@prairielearn/express-list-endpoints 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.cjs +3 -0
- package/.turbo/turbo-build.log +0 -0
- package/LICENSE +22 -0
- package/README.md +9 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +160 -0
- package/dist/index.js.map +1 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +491 -0
- package/dist/index.test.js.map +1 -0
- package/package.json +38 -0
- package/src/index.test.ts +638 -0
- package/src/index.ts +219 -0
- package/tsconfig.json +8 -0
package/.mocharc.cjs
ADDED
|
File without changes
|
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Portions copyright (c) 2015 Alberto Fernandez Medina
|
|
4
|
+
Portions copyright (c) 2024 PrairieLearn, Inc.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# `@prairielearn/express-list-endpoints`
|
|
2
|
+
|
|
3
|
+
A fork of https://github.com/AlbertoFdzM/express-list-endpoints with support for the latest versions of Express.
|
|
4
|
+
|
|
5
|
+
This fork currently only has tested support for `express@^4.21.0`. It may function with older versions, but they are untested and unsupported.
|
|
6
|
+
|
|
7
|
+
## License
|
|
8
|
+
|
|
9
|
+
The code in this directory is licensed under the [MIT License](./LICENSE).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Express, Router } from 'express';
|
|
2
|
+
export interface Endpoint {
|
|
3
|
+
path: string;
|
|
4
|
+
methods: string[];
|
|
5
|
+
middlewares: string[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Returns an array of strings with all the detected endpoints
|
|
9
|
+
* @param app The express/router instance to get the endpoints from
|
|
10
|
+
* @returns The array of endpoints
|
|
11
|
+
*/
|
|
12
|
+
declare function expressListEndpoints(app: Express | Router | any): Endpoint[];
|
|
13
|
+
export default expressListEndpoints;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
const regExpToParseExpressPathRegExp = /^\/\^\\?\/?(?:(:?[\w\\.-]*(?:\\\/:?[\w\\.-]*)*)|(\(\?:\\?\/?\([^)]+\)\)))\\\/.*/;
|
|
2
|
+
const regExpToReplaceExpressPathRegExpParams = /\(\?:\\?\/?\([^)]+\)\)/;
|
|
3
|
+
const regexpExpressParamRegexp = /\(\?:\\?\\?\/?\([^)]+\)\)/g;
|
|
4
|
+
const regexpExpressPathParamRegexp = /(:[^)]+)\([^)]+\)/g;
|
|
5
|
+
const EXPRESS_ROOT_PATH_REGEXP_VALUE = '/^\\/?(?=\\/|$)/i';
|
|
6
|
+
const STACK_ITEM_VALID_NAMES = ['router', 'bound dispatch', 'mounted_app'];
|
|
7
|
+
/**
|
|
8
|
+
* Returns all the verbs detected for the passed route
|
|
9
|
+
*/
|
|
10
|
+
function getRouteMethods(route) {
|
|
11
|
+
let methods = Object.keys(route.methods);
|
|
12
|
+
methods = methods.filter((method) => method !== '_all');
|
|
13
|
+
methods = methods.map((method) => method.toUpperCase());
|
|
14
|
+
return methods;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Returns the names (or anonymous) of all the middlewares attached to the
|
|
18
|
+
* passed route.
|
|
19
|
+
*/
|
|
20
|
+
function getRouteMiddlewares(route) {
|
|
21
|
+
return route.stack.map((item) => {
|
|
22
|
+
return item.handle.name || 'anonymous';
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns true if found regexp related with express params.
|
|
27
|
+
*/
|
|
28
|
+
function hasParams(expressPathRegExp) {
|
|
29
|
+
return regexpExpressParamRegexp.test(expressPathRegExp);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* @param route Express route object to be parsed
|
|
33
|
+
* @param basePath The basePath the route is on
|
|
34
|
+
* @return Endpoints info
|
|
35
|
+
*/
|
|
36
|
+
function parseExpressRoute(route, basePath) {
|
|
37
|
+
const paths = [];
|
|
38
|
+
if (Array.isArray(route.path)) {
|
|
39
|
+
paths.push(...route.path);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
paths.push(route.path);
|
|
43
|
+
}
|
|
44
|
+
const endpoints = paths.map((path) => {
|
|
45
|
+
const completePath = basePath && path === '/' ? basePath : `${basePath}${path}`;
|
|
46
|
+
const endpoint = {
|
|
47
|
+
path: completePath.replace(regexpExpressPathParamRegexp, '$1'),
|
|
48
|
+
methods: getRouteMethods(route),
|
|
49
|
+
middlewares: getRouteMiddlewares(route),
|
|
50
|
+
};
|
|
51
|
+
return endpoint;
|
|
52
|
+
});
|
|
53
|
+
return endpoints;
|
|
54
|
+
}
|
|
55
|
+
function parseExpressPath(expressPathRegExp, params) {
|
|
56
|
+
let parsedRegExp = expressPathRegExp.toString();
|
|
57
|
+
let expressPathRegExpExec = regExpToParseExpressPathRegExp.exec(parsedRegExp);
|
|
58
|
+
let paramIndex = 0;
|
|
59
|
+
while (hasParams(parsedRegExp)) {
|
|
60
|
+
const paramName = params[paramIndex].name;
|
|
61
|
+
const paramId = `:${paramName}`;
|
|
62
|
+
parsedRegExp = parsedRegExp.replace(regExpToReplaceExpressPathRegExpParams, (str) => {
|
|
63
|
+
// Express >= 4.20.0 uses a different RegExp for parameters: it
|
|
64
|
+
// captures the slash as part of the parameter. We need to check
|
|
65
|
+
// for this case and add the slash to the value that will replace
|
|
66
|
+
// the parameter in the path.
|
|
67
|
+
if (str.startsWith('(?:\\/')) {
|
|
68
|
+
return `\\/${paramId}`;
|
|
69
|
+
}
|
|
70
|
+
return paramId;
|
|
71
|
+
});
|
|
72
|
+
paramIndex++;
|
|
73
|
+
}
|
|
74
|
+
if (parsedRegExp !== expressPathRegExp.toString()) {
|
|
75
|
+
expressPathRegExpExec = regExpToParseExpressPathRegExp.exec(parsedRegExp);
|
|
76
|
+
}
|
|
77
|
+
// TODO: can we do better than this?
|
|
78
|
+
if (!expressPathRegExpExec) {
|
|
79
|
+
throw new Error('Error parsing express path');
|
|
80
|
+
}
|
|
81
|
+
const parsedPath = expressPathRegExpExec[1].replace(/\\\//g, '/');
|
|
82
|
+
return parsedPath;
|
|
83
|
+
}
|
|
84
|
+
function parseEndpoints(app, basePath, endpoints) {
|
|
85
|
+
const stack = app.stack || (app._router && app._router.stack);
|
|
86
|
+
endpoints = endpoints || [];
|
|
87
|
+
basePath = basePath || '';
|
|
88
|
+
if (!stack) {
|
|
89
|
+
if (endpoints.length) {
|
|
90
|
+
endpoints = addEndpoints(endpoints, [
|
|
91
|
+
{
|
|
92
|
+
path: basePath,
|
|
93
|
+
methods: [],
|
|
94
|
+
middlewares: [],
|
|
95
|
+
},
|
|
96
|
+
]);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
endpoints = parseStack(stack, basePath, endpoints);
|
|
101
|
+
}
|
|
102
|
+
return endpoints;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Ensures the path of the new endpoints isn't yet in the array.
|
|
106
|
+
* If the path is already in the array merges the endpoints with the existing
|
|
107
|
+
* one, if not, it adds them to the array.
|
|
108
|
+
*
|
|
109
|
+
* @param currentEndpoints Array of current endpoints
|
|
110
|
+
* @param endpointsToAdd New endpoints to be added to the array
|
|
111
|
+
* @returns Updated endpoints array
|
|
112
|
+
*/
|
|
113
|
+
function addEndpoints(currentEndpoints, endpointsToAdd) {
|
|
114
|
+
endpointsToAdd.forEach((newEndpoint) => {
|
|
115
|
+
const existingEndpoint = currentEndpoints.find((endpoint) => endpoint.path === newEndpoint.path);
|
|
116
|
+
if (existingEndpoint !== undefined) {
|
|
117
|
+
const newMethods = newEndpoint.methods.filter((method) => !existingEndpoint.methods.includes(method));
|
|
118
|
+
existingEndpoint.methods = existingEndpoint.methods.concat(newMethods);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
currentEndpoints.push(newEndpoint);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
return currentEndpoints;
|
|
125
|
+
}
|
|
126
|
+
function parseStack(stack, basePath, endpoints) {
|
|
127
|
+
stack.forEach((stackItem) => {
|
|
128
|
+
if (stackItem.route) {
|
|
129
|
+
const newEndpoints = parseExpressRoute(stackItem.route, basePath);
|
|
130
|
+
endpoints = addEndpoints(endpoints, newEndpoints);
|
|
131
|
+
}
|
|
132
|
+
else if (STACK_ITEM_VALID_NAMES.includes(stackItem.name)) {
|
|
133
|
+
const isExpressPathRegexp = regExpToParseExpressPathRegExp.test(stackItem.regexp);
|
|
134
|
+
let newBasePath = basePath;
|
|
135
|
+
if (isExpressPathRegexp) {
|
|
136
|
+
const parsedPath = parseExpressPath(stackItem.regexp, stackItem.keys);
|
|
137
|
+
newBasePath += `/${parsedPath}`;
|
|
138
|
+
}
|
|
139
|
+
else if (!stackItem.path &&
|
|
140
|
+
stackItem.regexp &&
|
|
141
|
+
stackItem.regexp.toString() !== EXPRESS_ROOT_PATH_REGEXP_VALUE) {
|
|
142
|
+
const regExpPath = ` RegExp(${stackItem.regexp}) `;
|
|
143
|
+
newBasePath += `/${regExpPath}`;
|
|
144
|
+
}
|
|
145
|
+
endpoints = parseEndpoints(stackItem.handle, newBasePath, endpoints);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
return endpoints;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Returns an array of strings with all the detected endpoints
|
|
152
|
+
* @param app The express/router instance to get the endpoints from
|
|
153
|
+
* @returns The array of endpoints
|
|
154
|
+
*/
|
|
155
|
+
function expressListEndpoints(app) {
|
|
156
|
+
const endpoints = parseEndpoints(app);
|
|
157
|
+
return endpoints;
|
|
158
|
+
}
|
|
159
|
+
export default expressListEndpoints;
|
|
160
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,MAAM,8BAA8B,GAClC,iFAAiF,CAAC;AACpF,MAAM,sCAAsC,GAAG,wBAAwB,CAAC;AACxE,MAAM,wBAAwB,GAAG,4BAA4B,CAAC;AAC9D,MAAM,4BAA4B,GAAG,oBAAoB,CAAC;AAE1D,MAAM,8BAA8B,GAAG,mBAAmB,CAAC;AAC3D,MAAM,sBAAsB,GAAG,CAAC,QAAQ,EAAE,gBAAgB,EAAE,aAAa,CAAC,CAAC;AAE3E;;GAEG;AACH,SAAS,eAAe,CAAC,KAAY;IACnC,IAAI,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAEzC,OAAO,GAAG,OAAO,CAAC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ACH,SAAS,oBAAoB,CAAC,GAA2B;IACvD,MAAM,SAAS,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC;IAEtC,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,eAAe,oBAAoB,CAAC","sourcesContent":["import type { Express, Router } from 'express';\n\ninterface Route {\n methods: object;\n path: string | string[];\n stack: any[];\n}\n\nexport interface Endpoint {\n path: string;\n methods: string[];\n middlewares: string[];\n}\n\nconst regExpToParseExpressPathRegExp =\n /^\\/\\^\\\\?\\/?(?:(:?[\\w\\\\.-]*(?:\\\\\\/:?[\\w\\\\.-]*)*)|(\\(\\?:\\\\?\\/?\\([^)]+\\)\\)))\\\\\\/.*/;\nconst regExpToReplaceExpressPathRegExpParams = /\\(\\?:\\\\?\\/?\\([^)]+\\)\\)/;\nconst regexpExpressParamRegexp = /\\(\\?:\\\\?\\\\?\\/?\\([^)]+\\)\\)/g;\nconst regexpExpressPathParamRegexp = /(:[^)]+)\\([^)]+\\)/g;\n\nconst EXPRESS_ROOT_PATH_REGEXP_VALUE = '/^\\\\/?(?=\\\\/|$)/i';\nconst STACK_ITEM_VALID_NAMES = ['router', 'bound dispatch', 'mounted_app'];\n\n/**\n * Returns all the verbs detected for the passed route\n */\nfunction getRouteMethods(route: Route) {\n let methods = Object.keys(route.methods);\n\n methods = methods.filter((method) => method !== '_all');\n methods = methods.map((method) => method.toUpperCase());\n\n return methods;\n}\n\n/**\n * Returns the names (or anonymous) of all the middlewares attached to the\n * passed route.\n */\nfunction getRouteMiddlewares(route: Route): string[] {\n return route.stack.map((item) => {\n return item.handle.name || 'anonymous';\n });\n}\n\n/**\n * Returns true if found regexp related with express params.\n */\nfunction hasParams(expressPathRegExp: string): boolean {\n return regexpExpressParamRegexp.test(expressPathRegExp);\n}\n\n/**\n * @param route Express route object to be parsed\n * @param basePath The basePath the route is on\n * @return Endpoints info\n */\nfunction parseExpressRoute(route: Route, basePath: string): Endpoint[] {\n const paths = [];\n\n if (Array.isArray(route.path)) {\n paths.push(...route.path);\n } else {\n paths.push(route.path);\n }\n\n const endpoints: Endpoint[] = paths.map((path) => {\n const completePath = basePath && path === '/' ? basePath : `${basePath}${path}`;\n\n const endpoint: Endpoint = {\n path: completePath.replace(regexpExpressPathParamRegexp, '$1'),\n methods: getRouteMethods(route),\n middlewares: getRouteMiddlewares(route),\n };\n\n return endpoint;\n });\n\n return endpoints;\n}\n\nfunction parseExpressPath(expressPathRegExp: RegExp, params: any[]): string {\n let parsedRegExp = expressPathRegExp.toString();\n let expressPathRegExpExec = regExpToParseExpressPathRegExp.exec(parsedRegExp);\n let paramIndex = 0;\n\n while (hasParams(parsedRegExp)) {\n const paramName = params[paramIndex].name;\n const paramId = `:${paramName}`;\n\n parsedRegExp = parsedRegExp.replace(regExpToReplaceExpressPathRegExpParams, (str) => {\n // Express >= 4.20.0 uses a different RegExp for parameters: it\n // captures the slash as part of the parameter. We need to check\n // for this case and add the slash to the value that will replace\n // the parameter in the path.\n if (str.startsWith('(?:\\\\/')) {\n return `\\\\/${paramId}`;\n }\n\n return paramId;\n });\n\n paramIndex++;\n }\n\n if (parsedRegExp !== expressPathRegExp.toString()) {\n expressPathRegExpExec = regExpToParseExpressPathRegExp.exec(parsedRegExp);\n }\n\n // TODO: can we do better than this?\n if (!expressPathRegExpExec) {\n throw new Error('Error parsing express path');\n }\n\n const parsedPath = expressPathRegExpExec[1].replace(/\\\\\\//g, '/');\n\n return parsedPath;\n}\n\nfunction parseEndpoints(\n app: Express | Router | any,\n basePath?: string,\n endpoints?: Endpoint[],\n): Endpoint[] {\n const stack = app.stack || (app._router && app._router.stack);\n\n endpoints = endpoints || [];\n basePath = basePath || '';\n\n if (!stack) {\n if (endpoints.length) {\n endpoints = addEndpoints(endpoints, [\n {\n path: basePath,\n methods: [],\n middlewares: [],\n },\n ]);\n }\n } else {\n endpoints = parseStack(stack, basePath, endpoints);\n }\n\n return endpoints;\n}\n\n/**\n * Ensures the path of the new endpoints isn't yet in the array.\n * If the path is already in the array merges the endpoints with the existing\n * one, if not, it adds them to the array.\n *\n * @param currentEndpoints Array of current endpoints\n * @param endpointsToAdd New endpoints to be added to the array\n * @returns Updated endpoints array\n */\nfunction addEndpoints(currentEndpoints: Endpoint[], endpointsToAdd: Endpoint[]): Endpoint[] {\n endpointsToAdd.forEach((newEndpoint) => {\n const existingEndpoint = currentEndpoints.find(\n (endpoint) => endpoint.path === newEndpoint.path,\n );\n\n if (existingEndpoint !== undefined) {\n const newMethods = newEndpoint.methods.filter(\n (method) => !existingEndpoint.methods.includes(method),\n );\n\n existingEndpoint.methods = existingEndpoint.methods.concat(newMethods);\n } else {\n currentEndpoints.push(newEndpoint);\n }\n });\n\n return currentEndpoints;\n}\n\nfunction parseStack(stack: any[], basePath: string, endpoints: Endpoint[]): Endpoint[] {\n stack.forEach((stackItem) => {\n if (stackItem.route) {\n const newEndpoints = parseExpressRoute(stackItem.route, basePath);\n\n endpoints = addEndpoints(endpoints, newEndpoints);\n } else if (STACK_ITEM_VALID_NAMES.includes(stackItem.name)) {\n const isExpressPathRegexp = regExpToParseExpressPathRegExp.test(stackItem.regexp);\n\n let newBasePath = basePath;\n\n if (isExpressPathRegexp) {\n const parsedPath = parseExpressPath(stackItem.regexp, stackItem.keys);\n\n newBasePath += `/${parsedPath}`;\n } else if (\n !stackItem.path &&\n stackItem.regexp &&\n stackItem.regexp.toString() !== EXPRESS_ROOT_PATH_REGEXP_VALUE\n ) {\n const regExpPath = ` RegExp(${stackItem.regexp}) `;\n\n newBasePath += `/${regExpPath}`;\n }\n\n endpoints = parseEndpoints(stackItem.handle, newBasePath, endpoints);\n }\n });\n\n return endpoints;\n}\n\n/**\n * Returns an array of strings with all the detected endpoints\n * @param app The express/router instance to get the endpoints from\n * @returns The array of endpoints\n */\nfunction expressListEndpoints(app: Express | Router | any): Endpoint[] {\n const endpoints = parseEndpoints(app);\n\n return endpoints;\n}\n\nexport default expressListEndpoints;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|